/// <summary>
        /// Make a donation approval request to the parent
        /// </summary>
        /// <param name="donation">The donation</param>
        /// <returns>The donation</returns>
        public Donation Donate(Donation donation)
        {
            var canAllowTransaction = _earningsService.CanTransact(EarningsBucketType.Share, donation.Amount);

            if (donation.Amount == 0 || !canAllowTransaction)
            {
                throw new InvalidOperationException($"Insufficient balance in share bucket!");
            }

            donation.FamilyMemberID = _currentUserService.MemberID;
            donation.Date           = DateTime.UtcNow;
            Repository.Insert(donation);

            // Updates child earnings
            var childEarnings = _earningsService.GetByMemberId(_currentUserService.MemberID);

            childEarnings.Share -= donation.Amount;
            Repository.Update(childEarnings);

            var child   = _familyService.GetMemberById(donation.FamilyMemberID);
            var admin   = _familyService.GetAdmin();
            var charity = Repository.Table <Charity>().SingleOrDefault(p => p.Id == donation.CharityID);

            var message = $"{child.Firstname.FirstCharToUpper()} has decided to donate ${donation.Amount:N2} to {charity.Name}."
                          + $" Are you OK with transfering ${donation.Amount:N2} back into your account so you can make the donation? Reply YES or NO.";

            _smsApprovalHistory.Add(admin.Id, ApprovalType.CharityDonation, message, donation.Id);

            if (admin != null && !string.IsNullOrEmpty(admin.PhoneNumber))
            {
                _textMessageService.Send(admin.PhoneNumber, message);
            }
            return(donation);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Purchase gift card
        /// </summary>
        /// <param name="giftCard">Gift card</param>
        /// <returns>Purchased gift card</returns>
        public PurchasedGiftCard PurchaseGiftCardRequest(int giftCardId)
        {
            var giftCard = Repository.Table <GiftCard>().FirstOrDefault(m => m.CardId == giftCardId);

            if (giftCard == null)
            {
                throw new InvalidParameterException("Gift card not found!");
            }

            var canTransact = _earningsService.CanTransact(EarningsBucketType.Spend, giftCard.Amount);

            if (giftCard.Amount == 0 || !canTransact)
            {
                throw new InvalidParameterException("Insufficient balance in spend bucket!");
            }

            var familyMember = _familyService.GetMember();
            var adminMember  = _familyService.GetAdmin();

            var purchasedGiftCard = new PurchasedGiftCard
            {
                FamilyMemberID = familyMember.Id,
                CardId         = giftCard.CardId,
                Name           = giftCard.GiftCardName ?? giftCard.MerchantName,
                Amount         = giftCard.Amount,
                CoverImageUrl  = giftCard.MerchantIconImageUrl,
                PurchasedOn    = DateTime.UtcNow,
                Status         = ApprovalStatus.PendingApproval
            };

            AddPurchasedGiftCard(purchasedGiftCard);

            // deduct spend amount of corresponding child from child earnings
            var childEarnings = _earningsService.GetByMemberId(familyMember.Id);

            childEarnings.Spend -= giftCard.Amount;
            _earningsService.Update(childEarnings);

            // Send spent info to parent through SMS
            var message = $"{familyMember.Firstname.FirstCharToUpper()} wants to purchase a ${purchasedGiftCard.Amount:N2} {purchasedGiftCard.Name} gift card. Do you approve? Reply YES or NO.";

            _smsApprovalHistory.Add(adminMember.Id, ApprovalType.GiftPurchase, message, purchasedGiftCard.Id);

            if (adminMember != null && !string.IsNullOrEmpty(adminMember.PhoneNumber))
            {
                _textMessageService.Send(adminMember.PhoneNumber, message);
            }
            return(purchasedGiftCard);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Delete family member by identifier
        /// </summary>
        /// <param name="memberId">Member identifier</param>
        public void DeleteMember(int memberId)
        {
            var familyMember = GetMemberById(memberId);

            if (familyMember.MemberType == MemberType.Child)
            {
                var childEarnings = _earningsService.GetByMemberId(memberId);
                var totalAmount   = childEarnings.Save + childEarnings.Share + childEarnings.Spend;
                if (totalAmount > 0)
                {
                    if (!_bankService.IsBankLinked(_currentUserService.MemberID))
                    {
                        throw new InvalidOperationException($"Not connected to bank. Please link your bank account to transafer the {familyMember.Firstname}'s money back.");
                    }

                    // Tranfer amount to customer account
                    var transactionResult = _transactionService.Transfer(_currentUserService.MemberID, totalAmount, PaymentType.ChildRemoved, TransferType.InternalToExternalAccount);

                    // If transaction failure
                    if (!transactionResult.HasValue)
                    {
                        throw new InvalidOperationException("Unable to process the transaction. Please contact bank or mail to us!");
                    }

                    childEarnings.Save  = 0;
                    childEarnings.Share = 0;
                    childEarnings.Spend = 0;
                    _earningsService.Update(childEarnings);
                }
            }

            familyMember.IsDeleted = true;
            Repository.Update(familyMember);
        }
        /// <summary>
        /// Get child balance message
        /// </summary>
        /// <param name="familyId">Family identifier</param>
        /// <param name="childName">Child name</param>
        /// <returns>Result message</returns>
        private string GetKidBalance(int familyId, string childName)
        {
            var childMember = _familyService.GetMember(familyId, childName, MemberType.Child);

            if (childMember == null)
            {
                return("Child not found");
            }

            var earnings = _earningsService.GetByMemberId(childMember.Id);

            if (earnings == null)
            {
                return("No earnings");
            }

            return(string.Format("{0}- Spend: $ {1}, Save: $ {2}, Share: $ {3}", childMember.Firstname.FirstCharToUpper(), earnings.Spend, earnings.Save, earnings.Share));
        }
Exemplo n.º 5
0
        public HttpResponseMessage GetByMemberId(int memberId)
        {
            if (!_accountService.BelongsToThisFamily(memberId))
            {
                throw new UnauthorizedAccessException();
            }

            return(Request.CreateResponse(HttpStatusCode.OK, _earningsService.GetByMemberId(memberId)));
        }
Exemplo n.º 6
0
        /// <summary>
        /// initiates the stock purchase.
        /// </summary>
        /// <param name="stockPurchaseRequest">The stock purchase request.</param>
        /// <returns>The stock purchase request.</returns>
        public StockPurchaseRequest InitiateStockPurchase(StockPurchaseRequest stockPurchaseRequest)
        {
            stockPurchaseRequest.Fee = StockFee;
            var canAllowTransaction = _earningsService.CanTransact(EarningsBucketType.Save, (stockPurchaseRequest.Amount + stockPurchaseRequest.Fee));

            if (stockPurchaseRequest.Amount == 0 || !canAllowTransaction)
            {
                throw new InvalidOperationException("Insufficient balance in save bucket!");
            }

            stockPurchaseRequest.LineItemID    = Guid.NewGuid();
            stockPurchaseRequest.TransactionID = Guid.NewGuid();
            stockPurchaseRequest.DateCreated   = DateTime.UtcNow;
            stockPurchaseRequest.ChildID       = _currentUserService.MemberID;

            Repository.Insert(stockPurchaseRequest);

            // deduct save amount of corresponding child from child earnings
            var childEarnings = _earningsService.GetByMemberId(_currentUserService.MemberID);

            childEarnings.Save -= (stockPurchaseRequest.Amount + stockPurchaseRequest.Fee); // Deducting stock amount including Fee
            Repository.Update(childEarnings);

            var admin     = _familyService.GetAdmin();
            var child     = _familyService.GetMember();
            var stock     = GetById(stockPurchaseRequest.StockItemID);
            var stockName = string.IsNullOrEmpty(stock.BrandName) ? stock.CompanyPopularName : stock.BrandName;

            var message = $"{child.Firstname.FirstCharToUpper()} would like to buy ${stockPurchaseRequest.Amount:N2} of {stockName} stock. Are you OK with this? Reply YES or NO.";

            _smsApprovalHistory.Add(admin.Id, ApprovalType.StockPurchase, message, stockPurchaseRequest.Id);

            if (admin != null && !string.IsNullOrEmpty(admin.PhoneNumber))
            {
                _textMessageService.Send(admin.PhoneNumber, message);
            }

            return(stockPurchaseRequest);
        }