Exemplo n.º 1
0
        public async Task <GenericServiceResponse> ServiceAppointmentUpdate(PayloadParent ResponseObject, LogCommand logCommand)
        {
            const string method           = "ServiceAppointmentUpdate";
            var          updateEventState = string.Empty;

            try
            {
                logCommand.LogMessage = $"{Service}.{method} Starting input parameter ResponseObject = {ResponseObject}";
                _logHandler.HandleLog(logCommand);

                var serviceAppointmentPayload = new ServiceAppointmentPayload(ResponseObject);

                logCommand.LogMessage = $"{Service}.{method} Starting input parameter serviceAppointmentPayload = {serviceAppointmentPayload}";
                _logHandler.HandleLog(logCommand);
                var recordsAffected = 0;

                Guid programGuid;
                Guid subprogramGuid;
                Guid WorkOrderScoreWeightingGuid;
                Guid StatusTypeGuid;
                Guid UsernameMappingGuid;

                //ProgramGuid
                updateEventState = "Attempting To Get Program Guid" + Environment.NewLine;
                if (!string.IsNullOrEmpty(serviceAppointmentPayload._PayloadHeader.SalesforceProgramIDC))
                {
                    var program = await _programRepository.GetProgramFromSFID(serviceAppointmentPayload._PayloadHeader.SalesforceProgramIDC, logCommand);

                    programGuid = program.ProgramGuid;
                }
                else
                {
                    //SubProgramGuid	ProgramGuid
                    // programGuid = Guid.Parse("8CC20C6D-13C2-424B-9EB3-194F653CC778");
                    throw new Exception($"Error: {Service}.{method} -- Missing SalesforceProgramID ");
                }

                //SubprogramGuid
                updateEventState = "Attempting To Get SubProgram Guid" + Environment.NewLine;
                if (!string.IsNullOrEmpty(serviceAppointmentPayload._PayloadHeader.SalesforceSubProgramIDC))
                {
                    var subprogram = await _programRepository.GetSubProgramSFID(serviceAppointmentPayload._PayloadHeader.SalesforceSubProgramIDC, logCommand);

                    subprogramGuid = subprogram.SubProgramGuid;
                }
                else
                {
                    subprogramGuid = Guid.Parse("FB02657F-3985-4F06-84C3-1669BE2F2991");
                }

                // Mapping SFUserid To AD Username
                updateEventState = "Attempting To Get Map Status GUid" + Environment.NewLine;
                if (!string.IsNullOrEmpty(serviceAppointmentPayload._PayloadBody.ServiceAppointment.Status))
                {
                    var lkpSchedulingType = await _schedulingRepository.GetLkpSchedulingTypeEnum(logCommand);

                    var statusTypeMapping = lkpSchedulingType.FirstOrDefault(a => string.Equals(a.SchedulingTypeName, serviceAppointmentPayload._PayloadBody.ServiceAppointment.Status, StringComparison.CurrentCultureIgnoreCase));
                    if (statusTypeMapping != null)
                    {
                        StatusTypeGuid = statusTypeMapping.LkpSchedulingTypeGuid;
                    }
                    else
                    {
                        throw new Exception($"Error: {Service}.{method} -- Cannot Status GUid ");
                    }
                }
                else
                {
                    throw new Exception($"Error: {Service}.{method} -- Cannot Status GUid ");
                }

                // LKPWorkOrderScoreMapping Workorder WorkTypeId
                updateEventState = "Attempting To Get Map Work Order Type Guid" + Environment.NewLine;
                if (!string.IsNullOrEmpty(serviceAppointmentPayload._PayloadBody.WorkOrder.WorkTypeId))
                {
                    var lkpAuditType = await _schedulingRepository.GetWorkOrderScoreWeightingEnum(logCommand);

                    var workOrderScoreMapping = lkpAuditType.FirstOrDefault(a => a.SalesforceRecordID == serviceAppointmentPayload._PayloadBody.WorkOrder.WorkTypeId);
                    if (workOrderScoreMapping != null)
                    {
                        WorkOrderScoreWeightingGuid = workOrderScoreMapping.WorkOrderScoreWeightingGuid;
                    }
                    else
                    {
                        throw new Exception($"Error: {Service}.{method} -- Cannot Map Work order Type ");
                    }
                }
                else
                {
                    throw new Exception($"Error: {Service}.{method} -- Cannot Map Work order Type ");
                }

                // LKPWorkOrderScoreMapping Workorder WorkTypeId
                updateEventState = "Attempting To Get Username Guid" + Environment.NewLine;
                if (!string.IsNullOrEmpty(serviceAppointmentPayload._PayloadBody.ServiceAppointment.LastModifiedById))
                {
                    var usernameMappingList = await _schedulingRepository.GetUsernameMappingSFtoADEnum(logCommand);

                    var usernameMapping = usernameMappingList.FirstOrDefault(a => a.SalesForceUserRecordID == serviceAppointmentPayload._PayloadBody.ServiceAppointment.LastModifiedById);
                    if (usernameMapping != null)
                    {
                        UsernameMappingGuid = usernameMapping.UsernameMappingSFtoADGuid;
                    }
                    else
                    {
                        throw new Exception($"Error: {Service}.{method} -- Cannot Map Username ");
                    }
                }
                else
                {
                    throw new Exception($"Error: {Service}.{method} -- Cannot Map Username");
                }
                updateEventState = "Attempting Execute" + Environment.NewLine;


                var schedulingDataObject = new Scheduling()
                {
                    SubprogramGuid = subprogramGuid,
                    ProgramGuid    = programGuid,
                    WorkOrderScoreWeightingGuid = WorkOrderScoreWeightingGuid,
                    UsernameMappingSFtoADGuid   = UsernameMappingGuid,
                    CreatedDateTime             = DateTime.Now,
                    LkpSchedulingTypeGuid       = StatusTypeGuid,
                    SalesforceRecordID          = serviceAppointmentPayload._PayloadHeader.RecordIdC
                };

                recordsAffected = await _schedulingRepository.CreateNewSchedulingRecord(schedulingDataObject, logCommand);

                ServiceResponse = await InsertAuditRecordAsync(logCommand, ResponseObject,
                                                               schedulingDataObject.GetType().FullName,
                                                               serviceAppointmentPayload._PayloadBody.ServiceAppointment.LastModifiedById);

                if (!ServiceResponse.Success)
                {
                    return(ServiceResponse);
                }

                updateEventState = "Executions Complete" + Environment.NewLine;


                ServiceResponse = new GenericServiceResponse
                {
                    Entity             = $"Update Scheduling Related Tables Successful, rows added/updated = {recordsAffected}",
                    Success            = true,
                    RestResponseStatus = GenericServiceResponse.RestStatus.Success
                };

                logCommand.LogMessage = string.Format($"{Service}.{method} completed");
                _logHandler.HandleLog(logCommand);

                return(ServiceResponse);
            }
            catch (Exception ex)
            {
                ex.Data.Add("Point of Failure", updateEventState);
                AppLogger.LogException(_loggingInstance, ex.Message, ex);
                return(ServiceHelper.SetErrorGenericServiceResponse(ex));
            }
        }