Esempio n. 1
0
        public async Task <MemberSubscription> PurchaseSubscription(Guid memberId, Guid chapterSubscriptionId, string cardToken)
        {
            ChapterSubscription chapterSubscription = await _chapterRepository.GetChapterSubscription(chapterSubscriptionId);

            if (chapterSubscription == null)
            {
                throw new OdkServiceException("Subscription not found");
            }

            Member member = await GetMember(memberId);

            await _authorizationService.AssertMemberIsChapterMember(member, chapterSubscription.ChapterId);

            await _paymentService.MakePayment(member, chapterSubscription.Amount, cardToken, chapterSubscription.Title);

            MemberSubscription memberSubscription = await _memberRepository.GetMemberSubscription(member.Id);

            DateTime expiryDate = (memberSubscription?.ExpiryDate ?? DateTime.UtcNow).AddMonths(chapterSubscription.Months);

            memberSubscription = new MemberSubscription(member.Id, chapterSubscription.Type, expiryDate);

            await _memberRepository.UpdateMemberSubscription(memberSubscription);

            MemberSubscriptionRecord record = new MemberSubscriptionRecord(memberId, chapterSubscription.Type, DateTime.UtcNow,
                                                                           chapterSubscription.Amount, chapterSubscription.Months);
            await _memberRepository.AddMemberSubscriptionRecord(record);

            _cacheService.UpdatedVersionedItem(memberSubscription, memberId);
            _cacheService.RemoveVersionedCollection <Member>(member.ChapterId);

            return(memberSubscription);
        }
Esempio n. 2
0
        private async Task ValidateChapterSubscription(ChapterSubscription subscription)
        {
            if (!Enum.IsDefined(typeof(SubscriptionType), subscription.Type) || subscription.Type == SubscriptionType.None)
            {
                throw new OdkServiceException("Invalid type");
            }

            if (string.IsNullOrWhiteSpace(subscription.Description) ||
                string.IsNullOrWhiteSpace(subscription.Name) ||
                string.IsNullOrWhiteSpace(subscription.Title))
            {
                throw new OdkServiceException("Some required fields are missing");
            }

            if (subscription.Amount < 0)
            {
                throw new OdkServiceException("Amount cannot be less than 0");
            }

            if (subscription.Months < 1)
            {
                throw new OdkServiceException("Subscription must be for at least 1 month");
            }

            IReadOnlyCollection <ChapterSubscription> existing = await _chapterRepository.GetChapterSubscriptions(subscription.ChapterId);

            if (existing.Any(x => x.Id != subscription.Id && x.Name.Equals(subscription.Name)))
            {
                throw new OdkServiceException("A subscription with that name already exists");
            }
        }
Esempio n. 3
0
        public async Task CreateChapterSubscription(Guid currentMemberId, Guid chapterId, CreateChapterSubscription subscription)
        {
            await AssertMemberIsChapterAdmin(currentMemberId, chapterId);

            ChapterSubscription create = new ChapterSubscription(Guid.Empty, chapterId, subscription.Type, subscription.Name,
                                                                 subscription.Title, subscription.Description, subscription.Amount, subscription.Months);

            await ValidateChapterSubscription(create);

            await _chapterRepository.CreateChapterSubscription(create);
        }
Esempio n. 4
0
        public async Task <ChapterSubscription> GetChapterSubscription(Guid currentMemberId, Guid id)
        {
            ChapterSubscription subscription = await _chapterRepository.GetChapterSubscription(id);

            if (subscription == null)
            {
                throw new OdkNotFoundException();
            }

            await AssertMemberIsChapterAdmin(currentMemberId, subscription.ChapterId);

            return(subscription);
        }
Esempio n. 5
0
 public async Task UpdateChapterSubscription(ChapterSubscription subscription)
 {
     await Context
     .Update <ChapterSubscription>()
     .Set(x => x.Amount, subscription.Amount)
     .Set(x => x.Description, subscription.Description)
     .Set(x => x.Months, subscription.Months)
     .Set(x => x.Name, subscription.Name)
     .Set(x => x.Title, subscription.Title)
     .Set(x => x.Type, subscription.Type)
     .Where(x => x.Id).EqualTo(subscription.Id)
     .ExecuteAsync();
 }
Esempio n. 6
0
        public async Task UpdateChapterSubscription(Guid currentMemberId, Guid id, CreateChapterSubscription subscription)
        {
            ChapterSubscription existing = await _chapterRepository.GetChapterSubscription(id);

            if (existing == null)
            {
                throw new OdkNotFoundException();
            }

            await AssertMemberIsChapterAdmin(currentMemberId, existing.ChapterId);

            existing.Amount      = subscription.Amount;
            existing.Description = subscription.Description;
            existing.Months      = subscription.Months;
            existing.Name        = subscription.Name;
            existing.Title       = subscription.Title;
            existing.Type        = subscription.Type;

            await ValidateChapterSubscription(existing);

            await _chapterRepository.UpdateChapterSubscription(existing);
        }
Esempio n. 7
0
        public async Task <ChapterSubscriptionApiResponse> GetSubscription(Guid id)
        {
            ChapterSubscription subscription = await _chapterAdminService.GetChapterSubscription(GetMemberId(), id);

            return(_mapper.Map <ChapterSubscriptionApiResponse>(subscription));
        }
Esempio n. 8
0
        public async Task DeleteChapterSubscription(Guid currentMemberId, Guid id)
        {
            ChapterSubscription subscription = await GetChapterSubscription(currentMemberId, id);

            await _chapterRepository.DeleteChapterSubscription(subscription.Id);
        }
Esempio n. 9
0
 public async Task CreateChapterSubscription(ChapterSubscription subscription)
 {
     await Context
     .Insert(subscription)
     .ExecuteAsync();
 }