예제 #1
0
        public async Task <OpportunityResponseSaveResponse> Apply(OpportunityResponseApplyRequest model, IUser user)
        {
            var existing = await _opportunityResponseService.GetById(model.Id);

            if (existing == null)
            {
                throw new NotFoundException();
            }
            if (existing.UserId != user.Id)
            {
                throw new UnauthorizedAccessException();
            }
            if (existing.Opportunity.OpportunityUser.Any(ou => ou.UserId == user.Id))
            {
                throw new ValidationErrorException("You cannot apply for your own opportunity");
            }
            var toSave = _mapper.Map(model, existing);
            var saved  = await _opportunityResponseService.Update(toSave, user);

            if (!user.EmailVerified)
            {
                throw new ValidationErrorException("Email verification required. You can verify your email in your profile.");
            }
            toSave.SubmittedAt = DateTime.UtcNow;
            saved = await _opportunityResponseService.Update(toSave, user);

            var result = _mapper.Map <OpportunityResponseSaveResponse>(saved);
            var agency = _lookupService.Get("agency", existing.Opportunity.Agency);
            await _notifyService.SuccessfullyApplied(existing.Opportunity, agency, user);

            foreach (var ou in existing.Opportunity.OpportunityUser)
            {
                await _notifyService.ApplicationReceived(existing.Opportunity, agency, ou.User);
            }


            return(result);
        }