/// <summary>
        /// Close the signal, and create a new incident by copying the data over.
        /// </summary>
        /// <param name="hostId"></param>
        /// <returns></returns>
        public async Task <int> CloseCreateIncident(string reason, int hostId)
        {
            // Create
            var signal = this.ctx.Signals
                         .Include(a => a.Notes)
                         .Include(a => a.Stakeholders)
                         .First(a => a.Id == hostId);


            var signalProds = this.ctx.SignalProducts.AsNoTracking()
                              .Include(a => a.ProductDates)
                              .Include(a => a.RelatedFBOs)
                              .Include(a => a.ProductType)
                              .Include(a => a.PackSizes)
                              .Where(o => o.HostId == hostId).ToList();

            if (signal.SignalStatusId < 50)
            {
                // Signals are text all the way
                // But the info should match with our stored lookups.
                var hazardGroup  = signal.HazardGroup ?? "";
                var prods        = mapper.Map <List <IncidentProductDb> >(signalProds);
                var stakeholders = this.mapper.Map <List <IncidentStakeholderDb> >(signal.Stakeholders);

                var signalNotes = this.mapper.Map <List <IncidentNoteDb> >(signal.Notes);
                // fetch the relevant lookupIds
                var incidentType = this.ctx.HazardGroups.FirstOrDefault(a => a.Title == hazardGroup);
                incidentType = (incidentType == null) ? this.ctx.HazardGroups.FirstOrDefault(a => a.Id == 36) : incidentType;

                var otherDatasource = this.ctx.DataSources.FirstOrDefault(a => a.Title == "FSA RAM Referral");
                var productType     = this.ctx.ProductTypes.First(a => a.Title == "Undefined");
                var incidentSource  = this.ctx.IncidentSources.First(a => a.Title == "Unknown");
                var newIncident     = new IncidentDb
                {
                    IncidentTitle    = signal.Title,
                    PriorityId       = (int)PrioritiesStatus.TBC,
                    ProductTypeId    = productType.Id,
                    IncidentStatusId = (int)IncidentStatusTypes.Unassigned,
                    DataSourceId     = otherDatasource.Id,
                    IncidentTypeId   = incidentType.Id,
                    SignalUrl        = signal.SourceLink,
                    LeadOfficer      = "",
                    OIMTGroups       = "",
                    ClassificationId = 1, //Routine
                    ContactMethodId  = 4, //No Preference
                    IncidentSourceId = incidentSource.Id,
                    IncidentCreated  = DateTime.Now,
                    ReceivedOn       = DateTime.Now,
                    Stakeholders     = stakeholders,
                    Products         = prods,
                    Notes            = new List <IncidentNoteDb> {
                        new IncidentNoteDb {
                            Note = reason
                        }
                    }.Concat(signalNotes.Reverse <IncidentNoteDb>()).ToList(),
                };

                var savedIncident = ctx.Incidents.Add(newIncident);
                signal.SignalStatusId = (int)SignalStatusTypes.Closed_Incident;

                // save changes, create incident.
                await ctx.SaveChangesAsync();

                // Update links and create closure note
                this.ctx.SignalIncidentLinks.Add(new Entities.Signals.SignalIncidentLinkDb
                {
                    SignalId   = signal.Id,
                    IncidentId = savedIncident.Entity.Id
                });

                var note = this.ClosureNote("Create a new incident", GeneralExtensions.GenerateIncidentId(savedIncident.Entity.Id), reason);
                note.HostId = signal.Id;
                signal.Notes.Add(note);

                await ctx.SaveChangesAsync();


                return(savedIncident.Entity.Id);
            }

            return(-1);
        }
Ejemplo n.º 2
0
        public async Task <int> CloseCreateIncident(int onlineFormId, string reason)
        {
            var dbEnt = this.ctx.OnlineForms
                        .Include(a => a.Notes)
                        .Include(a => a.Stakeholders)
                        .First(a => a.Id == onlineFormId);

            if (!dbEnt.IsClosed)
            {
                ctx.OnlineFormNotes.Add(new OnlineFormNoteDb
                {
                    HostId = onlineFormId,
                    Note   = reason
                });
                // tracked item automatically update on save
                dbEnt.IsClosed = true;

                // Now create an incident
                var onlineProds = this.ctx.OnlineFormProducts.AsNoTracking()
                                  .Include(a => a.ProductDates)
                                  .Include(a => a.RelatedFBOs)
                                  .Include(a => a.ProductType)
                                  .Include(a => a.PackSizes)
                                  .Where(o => o.HostId == onlineFormId).ToList();

                var prods        = mapper.Map <List <IncidentProductDb> >(onlineProds);
                var stakeholders = this.mapper.Map <List <IncidentStakeholderDb> >(dbEnt.Stakeholders);

                var notes          = this.mapper.Map <List <IncidentNoteDb> >(dbEnt.Notes);
                var productType    = this.ctx.ProductTypes.First(a => a.Title == "Undefined");
                var incidentSource = this.ctx.IncidentSources.First(a => a.Title == "Unknown");
                var newIncident    = new IncidentDb
                {
                    IncidentTitle    = dbEnt.Title,
                    PriorityId       = (int)PrioritiesStatus.TBC,
                    ProductTypeId    = productType.Id,
                    IncidentStatusId = (int)IncidentStatusTypes.Unassigned,
                    DataSourceId     = 46, // Other
                    IncidentTypeId   = 36,
                    NotifierId       = null,
                    SignalUrl        = "",
                    LeadOfficer      = "",
                    OIMTGroups       = "",
                    ClassificationId = 1, //Routine
                    ContactMethodId  = 4, //No Preference
                    IncidentSourceId = incidentSource.Id,
                    IncidentCreated  = DateTime.Now,
                    ReceivedOn       = DateTime.Now,
                    Stakeholders     = stakeholders,
                    Products         = prods,
                    OnlineFormId     = dbEnt.Id,
                    Notes            = notes.Reverse <IncidentNoteDb>().ToList()
                };
                var savedIncident = ctx.Incidents.Add(newIncident);
                await ctx.SaveChangesAsync();

                dbEnt.IncidentId = savedIncident.Entity.Id;
                await ctx.SaveChangesAsync();

                return(savedIncident.Entity.Id);
            }
            return(-1);
        }