public async Task <BLLAppDTO.GiftBLL> DeleteArchivedAsync(BLLAppDTO.ArchivedGiftBLL archivedGift, Guid userId) { var targetGiftId = archivedGift.GiftId; var giftReceiverId = userId; var giftGiverId = archivedGift.UserGiverId; // UserIds are mandatory for reactivating gift. Current user has to be receiver, not giver. if (userId == null || giftGiverId == null || giftGiverId == userId) { throw new ArgumentNullException(nameof(userId)); } // Check that target gift exists and status is archived var gift = Mapper.Map(await UOW.Gifts.FirstOrDefaultAsync(targetGiftId, giftReceiverId)); if (gift == null || !gift.StatusId.ToString().Equals(_archivedId)) { throw new NotSupportedException( $"Could not find pending Gift {targetGiftId.ToString()} to confirm archival"); } // Get corresponding ArchivedGift var archivedPendingGift = await UOW.ArchivedGifts.GetReceivedByGiftIdAsync(targetGiftId, giftReceiverId); if (archivedPendingGift == null || giftGiverId != archivedPendingGift.UserGiverId) { throw new NotSupportedException($"Could not find pending archived Gift {targetGiftId.ToString()} to confirm archival"); } // Remove ArchivedGift await UOW.ArchivedGifts.RemoveAsync(archivedPendingGift, giftReceiverId); // Remove Gift var activeGift = Mapper.Map(await UOW.Gifts.RemoveAsync(Mapper.Map(gift), giftReceiverId)); return(activeGift); }
/** Delete pending archived gift, change Gift status back to Active */ public async Task <BLLAppDTO.GiftBLL> DenyPendingArchivedAsync(BLLAppDTO.ArchivedGiftBLL archivedGift, Guid userId) { var targetGiftId = archivedGift.GiftId; var giftReceiverId = userId; var giftGiverId = archivedGift.UserGiverId; // Make Gift active again var gift = await UOW.Gifts.FirstOrDefaultAsync(targetGiftId, giftReceiverId); var reactivatedGift = await UpdateGiftStatusToActiveAsync(Mapper.Map(gift), giftReceiverId); if (reactivatedGift == null) { throw new NotSupportedException( $"Could not deny gifting and reactivate Gift {targetGiftId.ToString()}"); } // Get corresponding pending ArchivedGift and remove it var archivedPendingGift = await UOW.ArchivedGifts.GetPendingReceivedByGiftIdAsync(targetGiftId, userId); if (archivedPendingGift == null || giftGiverId != archivedPendingGift.UserGiverId) { throw new NotSupportedException($"Could not find pending received Gift {targetGiftId.ToString()} to deny"); } await UOW.ArchivedGifts.RemoveAsync(archivedPendingGift.Id, userId); return(reactivatedGift); }
/** Create a copy of existing Gift but make it Active, keep archived entries */ public async Task <BLLAppDTO.GiftBLL> ReactivateArchivedAsync(BLLAppDTO.ArchivedGiftBLL archivedGift, Guid userId) { var targetGiftId = archivedGift.GiftId; var giftReceiverId = userId; var giftGiverId = archivedGift.UserGiverId; // UserIds are mandatory for reactivating gift. Current user has to be receiver, not giver. if (userId == null || giftGiverId == null || giftGiverId == userId) { throw new ArgumentNullException(nameof(userId)); } // Check that target gift exists and status is archived var gift = Mapper.Map(await UOW.Gifts.FirstOrDefaultAsync(targetGiftId, giftReceiverId)); if (gift == null || !gift.StatusId.ToString().Equals(_archivedId)) { throw new NotSupportedException( $"Could not find archived Gift {targetGiftId.ToString()} to reactivate"); } // Get corresponding ArchivedGift var archivedReceivedGift = await UOW.ArchivedGifts.GetReceivedByGiftIdAsync(targetGiftId, giftReceiverId); if (archivedReceivedGift == null || giftGiverId != archivedReceivedGift.UserGiverId) { throw new NotSupportedException($"Could not find archived Gift {targetGiftId.ToString()} to reactivate"); } // Keep the archive entry but create a new copy of it in Active status var activeGift = await AddNewGiftBasedOnArchivedEntry(Mapper.MapArchivedGiftDALToResponse(archivedReceivedGift), giftReceiverId); if (activeGift == null) { throw new NotSupportedException($"Could not reactivate Gift {targetGiftId.ToString()} - data insertion fail"); } return(activeGift); // // Remove ArchivedGift // await UOW.ArchivedGifts.RemoveAsync(archivedPendingGift, giftReceiverId); // Update gift's status to Active // var activeGift = await UpdateGiftStatusToActive(gift, giftReceiverId); // return activeGift; }
/** Change ArchivedGift to isConfirmed */ public async Task <BLLAppDTO.GiftBLL> ConfirmPendingArchivedAsync(BLLAppDTO.ArchivedGiftBLL archivedGift, Guid userId) { var targetGiftId = archivedGift.GiftId; var giftReceiverId = userId; var giftGiverId = archivedGift.UserGiverId; // UserIds are mandatory for archiving pending gift. Current user has to be receiver, not giver. if (userId == null || giftGiverId == null || giftGiverId == userId) { throw new ArgumentNullException(nameof(userId)); } // Check that target gift exists and status is archived var gift = Mapper.Map(await UOW.Gifts.FirstOrDefaultAsync(targetGiftId, giftReceiverId)); if (gift == null || !gift.StatusId.ToString().Equals(_archivedId)) { throw new NotSupportedException( $"Could not find pending Gift {targetGiftId.ToString()} to confirm archival"); } // Get corresponding ArchivedGift var archivedPendingGift = await UOW.ArchivedGifts.GetPendingReceivedByGiftIdAsync(targetGiftId, userId); if (archivedPendingGift == null || giftGiverId != archivedPendingGift.UserGiverId) { throw new NotSupportedException($"Could not find pending archived Gift {targetGiftId.ToString()} to confirm archival"); } // UPDATE ArchivedGift to confirmed status archivedPendingGift.IsConfirmed = true; var confirmedArchivedGift = Mapper.MapArchivedGiftDALToResponse(await UOW.ArchivedGifts.UpdateAsync(archivedPendingGift, giftReceiverId)); if (confirmedArchivedGift == null || confirmedArchivedGift.IsConfirmed == false) { return(null !); } // Include new data regarding reservation in response gift.IsArchivalConfirmed = confirmedArchivedGift.IsConfirmed; // should be 'true' gift.ArchivedFrom = confirmedArchivedGift.DateArchived; gift.UserGiverId = confirmedArchivedGift.UserGiverId; gift.UserGiverName = archivedPendingGift.UserGiver?.FullName; return(gift); }