Beispiel #1
0
        public async Task <IHttpActionResult> Create([FromBody] CreatePersonnelDto personnel)
        {
            if (personnel == null)
            {
                return(BadRequest());
            }
            //custom validations
            var validator = new CreatePersonnelDtoValidator();
            var results   = validator.Validate(personnel);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    ModelState.AddModelError(failure.PropertyName, failure.ErrorMessage);
                }
            }
            if (!ModelState.IsValid)
            {
                string errorMessage = new ModelStateError(_logger).OutputMessage(ModelState);
                return(BadRequest(errorMessage));
            }
            try
            {
                var result = await _personnelService.Create(personnel);

                if (!result.IsValid)
                {
                    return(BadRequest(result.Message));
                }
            }
            catch (LogicalException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch
            {
                return(BadRequest(AppSettings.INTERNAL_SERVER_ERROR_MESSAGE));
            }
            return(Ok());
        }
        public async Task <CustomResult> Create(CreatePersonnelDto dto)
        {
            #region Register New Personnel
            if (PersonnelCodeExistsInDB(dto.Code))
            {
                return(new CustomResult
                {
                    IsValid = false,
                    Message = "person code is not unique"
                });
            }
            if (NationalCodeExistsInDB(dto.NationalCode))
            {
                return(new CustomResult
                {
                    IsValid = false,
                    Message = "national code is not unique"
                });
            }
            var personnel = new Personnel
            {
                Code                        = dto.Code,
                Name                        = dto.Name,
                LastName                    = dto.LastName,
                FathersName                 = dto.FathersName,
                NationalCode                = dto.NationalCode,
                BirthCertificateCode        = dto.BirthCertificateCode,
                PlaceOfBirth                = dto.PlaceOfBirth,
                State                       = dto.State,
                City                        = dto.City,
                PostalCode                  = dto.PostalCode,
                BirthDate                   = dto.BirthDate,
                Email                       = dto.Email,
                Mobile                      = dto.Mobile,
                Phone                       = dto.Phone,
                Address                     = dto.Address,
                Education                   = dto.Education,
                MilitaryServiceStatus       = dto.MilitaryServiceStatus,
                Gender                      = dto.Gender,
                MaritalStatus               = dto.MaritalStatus,
                GroupCategoryId             = dto.GroupCategoryId,
                EmployeementTypeId          = dto.EmployeementTypeId,
                PositionId                  = dto.PositionId,
                InsuranceRecordDuration     = dto.InsuranceRecordDuration,
                NoneInsuranceRecordDuration = dto.NoneInsuranceRecordDuration,
                BankAccountNumber           = dto.BankAccountNumber,
                DateOfEmployeement          = dto.DateOfEmployeement,
                FirstDateOfWork             = dto.FirstDateOfWork,
                LastDateOfWork              = dto.LastDateOfWork,
                LeavingWorkCause            = dto.LeavingWorkCause,
                ActiveState                 = dto.ActiveState,
                IsPresent                   = true
            };

            _personnelRepository.Insert(personnel);
            #endregion

            #region Create System User For The Personnel
            var identityResult = await _authService.RegisterUserAsync(new RegisterUserDto
            {
                UserName    = personnel.Code,
                Password    = personnel.NationalCode,
                IsPersonnel = true,
                UserInfo    = new RegisterUserInfoDto
                {
                    Name     = personnel.Name,
                    LastName = personnel.LastName
                }
            });

            if (!identityResult.Succeeded)
            {
                return(new CustomResult
                {
                    Message = "user creation for the person was not successful"
                });
            }
            #endregion

            #region Approvals
            _personnelApprovalProcService.CreateOrUpdate(new PersonnelApprovalProcDto
            {
                Id = personnel.Id,
                DismissalApprovalProcId = dto.DismissalApprovalProcId,
                DutyApprovalProcId      = dto.DutyApprovalProcId,
                ShiftReplacementProcId  = dto.ShiftReplacementProcId
            });

            _dismissalApprovalService.Create(dto.DismissalApprovals, personnel.Id);

            _dutyApprovalService.Create(dto.DutyApprovals, personnel.Id);
            #endregion

            return(new CustomResult
            {
                IsValid = true
            });
        }