/// <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); }
/// <summary> /// Update purchased gift card /// </summary> /// <param name="purchasedGiftCard">Purchased gift card</param> public void UpdatePurchasedGiftCard(PurchasedGiftCard purchasedGiftCard) { Repository.Update(purchasedGiftCard); }
/// <summary> /// Add purchased gift card /// </summary> /// <param name="purchasedGiftCard">Purchased gift card</param> public void AddPurchasedGiftCard(PurchasedGiftCard purchasedGiftCard) { Repository.Insert(purchasedGiftCard); }