Ejemplo n.º 1
0
        public async Task <GenericServiceResponse> SimpleServiceAppointmentHandler(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);

                Guid programGuid;

                //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");
                //}
                updateEventState = "Attempting to Check Dates" + Environment.NewLine;
                if (serviceAppointmentPayload._PayloadBody.ServiceAppointment.SchedStartTime == null)
                {
                    throw new Exception($"Error: {Service}.{method} -- Missing SchedStartTime ");
                }
                var scheduleStartTime = (DateTime)serviceAppointmentPayload._PayloadBody.ServiceAppointment.SchedStartTime;

                if (!DateTime.TryParse(serviceAppointmentPayload._PayloadBody.ServiceAppointment.LastModifiedDate,
                                       out var lastModifiedDate))
                {
                    lastModifiedDate = DateTime.Now;
                }

                updateEventState = "Attempting to Map dataObject" + Environment.NewLine;
                var schedulingDataObject = new SimpleScheduling
                {
                    SfRecordID           = serviceAppointmentPayload._PayloadHeader.RecordIdC,
                    ProgramGuid          = programGuid,
                    SalesforceUser       = serviceAppointmentPayload._PayloadBody.ServiceAppointment.LastModifiedById,
                    Technician           = serviceAppointmentPayload._PayloadBody.ServiceAppointment.TechnicianGuidC,
                    WorkOrderType        = serviceAppointmentPayload._PayloadBody.WorkOrder.WorkTypeId,
                    ScheduleBit          = true,
                    ScheduleDateTime     = scheduleStartTime,
                    LastModifiedDateTime = lastModifiedDate
                };

                if (string.IsNullOrEmpty(schedulingDataObject.SfRecordID) ||
                    string.IsNullOrEmpty(schedulingDataObject.WorkOrderType) ||
                    string.IsNullOrEmpty(schedulingDataObject.SalesforceUser))
                {
                    throw new Exception($"Error: {Service}.{method} -- Missing required fields");
                }

                updateEventState = "Attempting to Execute First" + Environment.NewLine;
                var recordsChanged = await _schedulingRepository.CreateSimpleSchedulingRecordFirst(schedulingDataObject, logCommand);

                if (recordsChanged > 0)
                {
                    ServiceResponse = new GenericServiceResponse
                    {
                        Entity             = $"Record Created in Simple Scheduling, rows affected = {recordsChanged}",
                        Success            = true,
                        RestResponseStatus = GenericServiceResponse.RestStatus.Success
                    };

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

                    return(ServiceResponse);
                }

                updateEventState = "Attempting to get List" + Environment.NewLine;
                var simpleSchedulingList =
                    (await _schedulingRepository.GetSimpleScheduling(schedulingDataObject.SfRecordID, logCommand)).ToList();
                updateEventState = "Attempting to start reschedule logic" + Environment.NewLine;
                var simpleSchedulingList0 = simpleSchedulingList.Where(a => a.ScheduleBit == false).ToList();
                if (simpleSchedulingList0.Any())
                {
                    updateEventState = "Attempting to Rescheduled [1] " + Environment.NewLine;
                    var firstOrDefault = simpleSchedulingList0.OrderByDescending(a => a.SSID).FirstOrDefault();
                    if (
                        firstOrDefault != null && (schedulingDataObject.SalesforceUser != firstOrDefault.SalesforceUser &&
                                                   schedulingDataObject.LastModifiedDateTime > firstOrDefault.LastModifiedDateTime.AddMinutes(+5)
                                                   &&
                                                   (schedulingDataObject.WorkOrderType != firstOrDefault.WorkOrderType ||
                                                    schedulingDataObject.ScheduleDateTime != firstOrDefault.ScheduleDateTime ||
                                                    schedulingDataObject.Technician != firstOrDefault.Technician))
                        )
                    {
                        recordsChanged = await _schedulingRepository.CreateSimpleSchedulingRecordReschedule(schedulingDataObject,
                                                                                                            logCommand);
                    }
                }
                else
                {
                    updateEventState = "Attempting to Rescheduled [2] " + Environment.NewLine;
                    var firstOrDefault = simpleSchedulingList.OrderByDescending(a => a.SSID)
                                         .FirstOrDefault(a => a.ScheduleBit);
                    if (firstOrDefault == null)
                    {
                        throw new Exception($"Error: {Service}.{method} -- Missing Created Date Record on Reschedule ");
                    }
                    if (schedulingDataObject.SalesforceUser != firstOrDefault.SalesforceUser &&
                        schedulingDataObject.LastModifiedDateTime > firstOrDefault.LastModifiedDateTime.AddMinutes(+30))
                    {
                        recordsChanged = await _schedulingRepository.CreateSimpleSchedulingRecordReschedule(schedulingDataObject,
                                                                                                            logCommand);
                    }
                }
                updateEventState = "Executions Complete" + Environment.NewLine;

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

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

                if (recordsChanged >= 1)
                {
                    ServiceResponse = new GenericServiceResponse
                    {
                        Entity             = $"Successful: Update Scheduling Related Table, rows added/updated = {recordsChanged}",
                        Success            = true,
                        RestResponseStatus = GenericServiceResponse.RestStatus.Success
                    };
                }
                else
                {
                    ServiceResponse = new GenericServiceResponse
                    {
                        Entity             = $"Failure: Update Scheduling Related Table, rows added/updated = {recordsChanged}",
                        Success            = true,
                        RestResponseStatus = GenericServiceResponse.RestStatus.Empty
                    };
                }

                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));
            }
        }