예제 #1
0
        public async Task <ActionResult <TestingPersonnelInvitationConfirmedShiftsDto> > ConfirmInvitationAsync([FromBody]
                                                                                                                TestingPersonnelInvitationConfirmDto confirmDto)
        {
            if (confirmDto == null)
            {
                return(BadRequest());
            }

            var shiftNumbers = await _testingPersonnelInvitationsService
                               .ConfirmInvitationAsync(confirmDto)
                               .ConfigureAwait(false);

            if (!_testingPersonnelInvitationsService.ValidationDictionary.IsValid())
            {
                return(BadRequest(new
                {
                    errors = _testingPersonnelInvitationsService.ValidationDictionary.GetErrorMessages()
                }));
            }

            return(Ok(shiftNumbers));
        }
        public async Task <TestingPersonnelInvitationConfirmedShiftsDto> ConfirmInvitationAsync(TestingPersonnelInvitationConfirmDto confirmDto)
        {
            if (confirmDto == null)
            {
                throw new ArgumentNullException(nameof(confirmDto));
            }

            var testingPersonnelAndInvitationData = await _invitationConfirmationTokensRepository
                                                    .GetTestingPersonnelAndInvitationByConfirmationTokenAsync(confirmDto.Token)
                                                    .ConfigureAwait(false);

            if (testingPersonnelAndInvitationData == null)
            {
                ValidationDictionary
                .AddModelError("The token is not valid", $"The provided token has been already used or doesn't exist.");

                return(null);
            }

            var doesConfirmationExist = await _testingPersonnelConfirmationsRepository
                                        .CheckIfConfirmationExistsForInvitationAndTestingPersonnelAsync(testingPersonnelAndInvitationData.InvitationId, testingPersonnelAndInvitationData.TestingPersonnelId)
                                        .ConfigureAwait(false);

            if (doesConfirmationExist)
            {
                ValidationDictionary
                .AddModelError("Confirmation already exists", $"Confirmation for this invitation already exists.");

                await _invitationConfirmationTokensRepository
                .DisposeInvitationConfirmationToken(confirmDto.Token)
                .ConfigureAwait(false);

                return(null);
            }

            var testingPersonnelConfirmSpecDto = new TestingPersonnelConfirmationSpecDto
            {
                InvitationId = testingPersonnelAndInvitationData.InvitationId,
                PersonnelId  = testingPersonnelAndInvitationData.TestingPersonnelId,
                ShiftNumbers = confirmDto.Shifts
            };

            var shiftsBooked = await _testingPersonnelConfirmationsRepository
                               .AddConfirmationOfInvitationAsync(testingPersonnelConfirmSpecDto)
                               .ConfigureAwait(false);

            await _invitationConfirmationTokensRepository
            .DisposeInvitationConfirmationToken(confirmDto.Token)
            .ConfigureAwait(false);

            if (shiftsBooked.ShiftsBooked.Any())
            {
                await _mailSender
                .SendConfirmationForPoolingAssignmentAsync(testingPersonnelAndInvitationData.Email,
                                                           testingPersonnelAndInvitationData.InvitationDate, shiftsBooked.ShiftsBooked)
                .ConfigureAwait(false);
            }

            return(shiftsBooked);
        }