Example #1
0
        public async Task <ActionResult <TestingPersonnelInvitationConfirmedShiftsDto> > CreateConfirmationAsync([FromBody]
                                                                                                                 TestingPersonnelManuallyAddedConfirmationDto confirmDto)
        {
            if (confirmDto == null)
            {
                return(BadRequest());
            }

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

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

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

            var invitationId = await _testingPersonnelInvitationsRepository.GetInvitationIdByDateAsync(confirmDto.Date.Date)
                               .ConfigureAwait(false);

            if (invitationId == Guid.Empty)
            {
                ValidationDictionary
                .AddModelError("Invitation for date was not found", $"{confirmDto.Date.ToString(dateFormat, CultureInfo.InvariantCulture)}");
                return(null);
            }

            var doesUserExist = await _testingPersonnelsRepository
                                .AnyAsync(tp => tp.Id == confirmDto.TestingPersonnelId)
                                .ConfigureAwait(false);

            if (!doesUserExist)
            {
                ValidationDictionary
                .AddModelError("Testing personnel was not found", $"Testing personnel was not found");
                return(null);
            }

            var doesConfirmationExist = await _testingPersonnelConfirmationsRepository
                                        .CheckIfConfirmationExistsForSelectedShiftsAsync(invitationId, confirmDto.TestingPersonnelId, confirmDto.Shifts)
                                        .ConfigureAwait(false);

            if (doesConfirmationExist)
            {
                ValidationDictionary
                .AddModelError("Confirmation already exists for selected testing personnel", $"Confirmation already exists for selected testing personnel");
                return(null);
            }

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

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

            if (!confirmedShifts.ShiftsBooked.Any())
            {
                ValidationDictionary
                .AddModelError("Shift capacity is already full", $"Shift capacity is already full");
                return(null);
            }

            return(confirmedShifts);
        }