コード例 #1
0
 public ExternalSystemSetup WithExternalEmployees(string organizationNumber, List <Employee> employees)
 {
     A.CallTo(() => _unimicroClient.GetEmployees(organizationNumber)).Returns(employees);
     return(this);
 }
        public async Task <IEnumerable <ExternalData> > GetExternalEmployeeData(int unitId)
        {
            var unit = await SignIn(unitId);

            var externalDataList = new List <ExternalData>();

            var employees = await _unimicroClient.GetEmployees(unit.BusinessOrganizationNumber);

            var employeeIds = employees.Select(e => e.Id).ToList();
            var employments = _unimicroClient.GetEmployments(employeeIds).Result
                              .Where(e => !e.Deleted && (!e.EndDate.HasValue || e.EndDate.Value > DateTime.Today));
            var workers = await _unimicroClient.GetWorkers(employeeIds);

            var workerIds     = workers.Select(w => w.Id).ToList();
            var workRelations = await _unimicroClient.GetWorkRelations(workerIds);

            var userIds = workers.Where(w => w.UserId.HasValue).Select(w => w.UserId.Value);
            var users   = await _unimicroClient.GetUsers(userIds);

            foreach (var employee in employees)
            {
                var employment   = employments?.FirstOrDefault(e => e.EmployeeId == employee.Id);
                var worker       = workers?.FirstOrDefault(w => w.EmployeeId == employee.Id);
                var workRelation = worker != null?workRelations.FirstOrDefault(wr => wr.WorkerID == worker.Id) : null;

                var user = worker != null?users?.FirstOrDefault(u => u.Id == worker.UserId) : null;

                var externalData = new ExternalData();
                CheckIfIsSupported(externalData, workRelation, employment, worker, employee);

                externalData.Identifiers.Add(new Identifier {
                    Entity = unimicro.IdentifierEntity.WorkRelation.ToString(), Property = unimicro.IdentifierProperty.Id.ToString(), Value = workRelation?.Id.ToString()
                });
                externalData.Identifiers.Add(new Identifier {
                    Entity = unimicro.IdentifierEntity.Worker.ToString(), Property = unimicro.IdentifierProperty.Id.ToString(), Value = worker?.Id.ToString()
                });
                externalData.Identifiers.Add(new Identifier {
                    Entity = unimicro.IdentifierEntity.User.ToString(), Property = unimicro.IdentifierProperty.Id.ToString(), Value = worker?.UserId.ToString()
                });
                externalData.Identifiers.Add(new Identifier {
                    Entity = unimicro.IdentifierEntity.Employee.ToString(), Property = unimicro.IdentifierProperty.Id.ToString(), Value = employee?.Id.ToString()
                });
                externalData.Identifiers.Add(new Identifier {
                    Entity = unimicro.IdentifierEntity.Employee.ToString(), Property = unimicro.IdentifierProperty.SocialSecurityNumber.ToString(), Value = employee?.SocialSecurityNumber
                });
                externalData.Identifiers.Add(new Identifier {
                    Entity = unimicro.IdentifierEntity.Employment.ToString(), Property = unimicro.IdentifierProperty.Id.ToString(), Value = employment?.Id.ToString()
                });


                externalData.DataSet.Add(new Data {
                    Code = PropertyName.Name.ToString(), Value = employee?.BusinessRelationInfo?.Name
                });
                externalData.DataSet.Add(new Data {
                    Code = PropertyName.Email.ToString(), Value = employee?.DefaultEmail?.EmailAddress
                });
                externalData.DataSet.Add(new Data {
                    Code = PropertyName.JobDescription.ToString(), Value = employment?.JobName
                });
                externalData.DataSet.Add(new Data {
                    Code = PropertyName.Name.ToString(), Value = user?.DisplayName
                });
                externalData.DataSet.Add(new Data {
                    Code = PropertyName.UserName.ToString(), Value = user?.UserName
                });
                externalData.DataSet.Add(new Data {
                    Code = PropertyName.Email.ToString(), Value = user?.Email
                });

                if (!externalData.ValidForUse)
                {
                    var infoText = "Employee is not valid for mapping. Reason: " + string.Join(",", externalData.NotValidReasons);
                    Logger.LogInformation(infoText);
                }
                externalDataList.Add(externalData);
            }

            RemoveNullData(externalDataList);
            return(externalDataList);
        }