Exemple #1
0
        public async Task <ITimeregValidationResult> ValidateExportAbsence(int unitId, Absence absence)
        {
            foreach (var absenceEntry in absence.AbsenceEntries)
            {
                int?workTypeId            = null;
                int?employmentLeaveTypeId = null;

                workTypeId = await _entityMapService.GetWorkTypeId(absenceEntry.LocalAbsenceCode, unitId);

                if (workTypeId.HasValue)
                {
                    var workRelationId = await _entityMapService.GetWorkRelationId(absence.EmployeeId, unitId);

                    if (!workRelationId.HasValue)
                    {
                        return(new FailedResult($"Missing mapping from Employee to WorkRelation {absence.EmployeeId}"));
                    }
                }
                else
                {
                    employmentLeaveTypeId = await _entityMapService.GetEmploymentLeaveId(absenceEntry.LocalAbsenceCode, unitId);

                    if (employmentLeaveTypeId.HasValue)
                    {
                        var employmentId = await _entityMapService.GetEmploymentId(absence.EmployeeId, unitId);

                        if (!employmentId.HasValue)
                        {
                            return(new FailedResult($"Missing mapping from Employee to Employment {absence.EmployeeId}"));
                        }
                    }
                }

                if (!workTypeId.HasValue && !employmentLeaveTypeId.HasValue)
                {
                    return(new FailedResult($"Missing mapping from AbsenceType to WorkType or EmploymentLeaveType {absenceEntry.LocalAbsenceCode}"));
                }
            }
            return(new OkResult("Absence validated OK for export"));
        }
        public async Task <Absence> ResendAbsence(int integrationUnitId, Absence absence)
        {
            await SignIn(integrationUnitId);

            foreach (var absenceEntry  in absence.AbsenceEntries)
            {
                var workTypeId = await _entityMapService.GetWorkTypeId(absenceEntry.LocalAbsenceCode, integrationUnitId);

                var isWorkItem = workTypeId != null;

                if (isWorkItem)
                {
                    var workRelationId = await _entityMapService.GetWorkRelationId(absence.EmployeeId, integrationUnitId);

                    absenceEntry.ExternalEntityId    = workRelationId.ToString();
                    absenceEntry.ExternalAbsenceCode = workTypeId.ToString();

                    var workItem = new WorkItem
                    {
                        // From AbsenceDay
                        StartTime  = absenceEntry.StartTime,
                        EndTime    = absenceEntry.EndTime,
                        WorkTypeId = workTypeId.Value,

                        // From Absence
                        UnitId         = absence.UnitId,
                        WorkRelationId = workRelationId.Value,
                        Description    = absence.Description,
                        LunchInMinutes = absence.LunchInMinutes
                    };
                    if (string.IsNullOrWhiteSpace(absenceEntry.ExternalId))
                    {
                        var externalId = await _unimicroClient.PostWorkItem(workItem);

                        absenceEntry.ExternalId = externalId.ToString();
                    }
                    else
                    {
                        workItem.Id = int.Parse(absenceEntry.ExternalId);
                        await _unimicroClient.PutWorkItem(workItem);
                    }
                }
                else
                {
                    var employementLeaveId = await _entityMapService.GetEmploymentLeaveId(absenceEntry.LocalAbsenceCode, absence.UnitId);

                    var isEmploymentLeave = employementLeaveId != null;

                    if (isEmploymentLeave)
                    {
                        var employmentId = await _entityMapService.GetEmploymentId(absence.EmployeeId, integrationUnitId);

                        absenceEntry.ExternalEntityId    = employmentId.ToString();
                        absenceEntry.ExternalAbsenceCode = employementLeaveId.ToString();

                        var employmentLeave = new EmploymentLeave()
                        {
                            EmploymentID = employmentId.Value,
                            LeaveType    = Enum.Parse <LeaveType>(employementLeaveId.Value.ToString()),
                            FromDate     = absenceEntry.StartTime.Date,
                            ToDate       = absenceEntry.StartTime.Date,
                            LeavePercent = 100
                        };

                        if (string.IsNullOrWhiteSpace(absenceEntry.ExternalId))
                        {
                            var externalId = await _unimicroClient.PostEmploymentLeave(employmentLeave);

                            absenceEntry.ExternalId = externalId.ToString();
                        }
                        else
                        {
                            employmentLeave.Id = int.Parse(absenceEntry.ExternalId);
                            await _unimicroClient.PutEmploymentLeave(employmentLeave);
                        }
                    }
                    else
                    {
                        throw new ArgumentException($"Mapping is not found for absencecode={absenceEntry.LocalAbsenceCode}. This error should have been taken care of in pre-validation");
                    }
                }
            }
            return(absence);
        }