2024-09-28 14:58:36 -04:00
|
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
|
using System.Data;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Data.SeaHavenIndustries
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
|
|
|
|
|
|
{
|
|
|
|
|
|
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnModelCreating(builder);
|
|
|
|
|
|
foreach (var foreignKey in builder.Model.GetEntityTypes().SelectMany
|
|
|
|
|
|
(e => e.GetForeignKeys()))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
|
|
|
|
|
|
}
|
2026-04-20 12:01:53 -04:00
|
|
|
|
|
|
|
|
|
|
builder.Entity<VendorAccessToken>()
|
|
|
|
|
|
.HasIndex(t => t.Token)
|
|
|
|
|
|
.IsUnique();
|
2026-04-20 13:25:56 -04:00
|
|
|
|
|
|
|
|
|
|
builder.Entity<DispatchUpliftRequest>()
|
|
|
|
|
|
.HasIndex(u => new { u.DispatchId, u.Status })
|
|
|
|
|
|
.IsUnique()
|
|
|
|
|
|
.HasFilter("[Status] = 'Pending'");
|
2026-06-29 17:22:31 -03:00
|
|
|
|
|
|
|
|
|
|
builder.Entity<WorkOrderFieldLock>()
|
|
|
|
|
|
.HasIndex(l => new { l.WorkOrderId, l.FieldName })
|
|
|
|
|
|
.IsUnique();
|
|
|
|
|
|
|
2026-07-09 14:08:48 -03:00
|
|
|
|
builder.Entity<WorkOrderWeekRolledLedger>()
|
|
|
|
|
|
.HasIndex(l => new { l.WorkOrderId, l.SourceWeekStart })
|
|
|
|
|
|
.IsUnique();
|
|
|
|
|
|
|
2026-07-13 13:12:07 -03:00
|
|
|
|
builder.Entity<CompletionDocTemplate>()
|
|
|
|
|
|
.HasIndex(t => new { t.ServiceKey, t.IsActive });
|
|
|
|
|
|
|
2026-07-24 14:20:16 -03:00
|
|
|
|
|
2026-06-29 17:22:31 -03:00
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.Property(w => w.RowVersion)
|
|
|
|
|
|
.IsRowVersion();
|
2026-07-24 22:13:25 -03:00
|
|
|
|
if (Database.IsSqlServer())
|
|
|
|
|
|
builder.HasSequence<long>("WorkOrderInternalNumberSequence");
|
2026-06-29 17:22:31 -03:00
|
|
|
|
|
2026-07-10 13:42:16 -03:00
|
|
|
|
// Bounded lengths required for SQL Server index keys (nvarchar(max) is not indexable).
|
|
|
|
|
|
// SiteCode: board create contract MaximumLength(32). InternalWONumber: normalized to 11 digits.
|
|
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.Property(w => w.SiteCode)
|
|
|
|
|
|
.HasMaxLength(32);
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.Property(w => w.InternalWONumber)
|
|
|
|
|
|
.HasMaxLength(11);
|
|
|
|
|
|
|
2026-07-24 14:20:16 -03:00
|
|
|
|
|
2026-07-10 11:45:24 -03:00
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.HasIndex(w => w.SiteCode)
|
|
|
|
|
|
.HasFilter("[istemplate] = 0");
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.HasIndex(w => w.InternalWONumber)
|
|
|
|
|
|
.HasFilter("[istemplate] = 0");
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.HasIndex(w => new { w.LifecycleStatus, w.ScheduledDate })
|
|
|
|
|
|
.HasFilter("[istemplate] = 0");
|
|
|
|
|
|
|
2026-07-24 21:03:50 -03:00
|
|
|
|
// These indexes already exist in the migration history. Keep them in
|
|
|
|
|
|
// the runtime model so future additive migrations do not drop them.
|
|
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.HasIndex(w => new { w.AssignTo, w.ScheduledDate });
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.HasIndex(w => w.ScheduledDate);
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.HasIndex(w => w.WorkOrderType);
|
|
|
|
|
|
|
2026-07-13 13:14:51 -03:00
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.Property(w => w.ExternalWorkOrderId)
|
|
|
|
|
|
.HasMaxLength(450);
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.HasIndex(w => w.ExternalWorkOrderId)
|
|
|
|
|
|
.IsUnique()
|
|
|
|
|
|
.HasFilter("[ExternalWorkOrderId] IS NOT NULL AND [ExternalWorkOrderId] <> ''");
|
|
|
|
|
|
|
2026-07-24 21:03:50 -03:00
|
|
|
|
builder.Entity<WorkOrderWebhookDelivery>()
|
|
|
|
|
|
.HasIndex(d => d.DeliveryId)
|
|
|
|
|
|
.IsUnique();
|
|
|
|
|
|
|
2026-07-24 22:13:25 -03:00
|
|
|
|
builder.Entity<Comments>()
|
|
|
|
|
|
.Property(c => c.ExternalSource)
|
|
|
|
|
|
.HasMaxLength(128);
|
|
|
|
|
|
builder.Entity<Comments>()
|
|
|
|
|
|
.Property(c => c.ExternalVersionHash)
|
|
|
|
|
|
.HasMaxLength(64);
|
|
|
|
|
|
builder.Entity<Comments>()
|
|
|
|
|
|
.Property(c => c.ExternalSourceEmailS3Key)
|
|
|
|
|
|
.HasMaxLength(2048);
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<Locations>()
|
|
|
|
|
|
.Property(l => l.ExternalSource)
|
|
|
|
|
|
.HasMaxLength(128);
|
|
|
|
|
|
builder.Entity<Locations>()
|
|
|
|
|
|
.Property(l => l.ExternalLocationId)
|
|
|
|
|
|
.HasMaxLength(450);
|
|
|
|
|
|
builder.Entity<WorkOrderExternalReceipt>()
|
|
|
|
|
|
.HasIndex(r => new { r.Source, r.Kind, r.ExternalId })
|
|
|
|
|
|
.IsUnique()
|
|
|
|
|
|
.HasDatabaseName("IX_WorkOrderExternalReceipts_Source_Kind_ExternalId");
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<WorkOrderReconciliationJob>()
|
|
|
|
|
|
.HasIndex(j => j.State);
|
|
|
|
|
|
builder.Entity<WorkOrderReconciliationJob>()
|
|
|
|
|
|
.Property(j => j.Id)
|
|
|
|
|
|
.ValueGeneratedNever();
|
2026-07-24 14:20:16 -03:00
|
|
|
|
|
2026-06-29 17:22:31 -03:00
|
|
|
|
builder.Entity<Dispatch>()
|
|
|
|
|
|
.Property(d => d.RowVersion)
|
|
|
|
|
|
.IsRowVersion();
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<WorkOrder>()
|
|
|
|
|
|
.HasOne(w => w.PrimaryDispatch)
|
|
|
|
|
|
.WithMany()
|
|
|
|
|
|
.HasForeignKey(w => w.PrimaryDispatchId)
|
|
|
|
|
|
.OnDelete(DeleteBehavior.Restrict);
|
2026-07-23 17:02:40 -03:00
|
|
|
|
|
|
|
|
|
|
builder.Entity<VendorCompany>()
|
|
|
|
|
|
.HasIndex(c => c.NormalizedName)
|
|
|
|
|
|
.IsUnique();
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<Vendor>()
|
|
|
|
|
|
.HasOne(v => v.Company)
|
|
|
|
|
|
.WithMany(c => c.Vendors)
|
|
|
|
|
|
.HasForeignKey(v => v.CompanyId)
|
|
|
|
|
|
.OnDelete(DeleteBehavior.Restrict);
|
2026-07-23 19:18:06 -03:00
|
|
|
|
|
|
|
|
|
|
builder.Entity<SitePreferredVendor>()
|
|
|
|
|
|
.HasIndex(p => new { p.LocationId, p.VendorId, p.Trade })
|
|
|
|
|
|
.IsUnique()
|
|
|
|
|
|
.HasFilter("[Trade] IS NOT NULL");
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<SitePreferredVendor>()
|
|
|
|
|
|
.HasIndex(p => new { p.LocationId, p.VendorId })
|
|
|
|
|
|
.IsUnique()
|
|
|
|
|
|
.HasFilter("[Trade] IS NULL");
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
public DbSet<Category> Categories { get; set; }
|
|
|
|
|
|
public DbSet<Locations> Locations { get; set; }
|
|
|
|
|
|
public DbSet<Contacts> Contacts { get; set; }
|
|
|
|
|
|
public DbSet<WorkOrder> workOrders { get; set; }
|
|
|
|
|
|
public DbSet<Template> Templates { get; set; }
|
2026-01-21 15:41:19 -06:00
|
|
|
|
public DbSet<Comments> Comments { get; set; }
|
|
|
|
|
|
public DbSet<Quotes> Quotes { get; set; }
|
|
|
|
|
|
public DbSet<QuotesLineItems> QuotesLines { get; set; }
|
|
|
|
|
|
public DbSet<ForgetPasswordCode> ForgetPasswordCodes { get; set; }
|
|
|
|
|
|
public DbSet<WorkOrderContacts> WorkOrderContacts { get; set; }
|
|
|
|
|
|
public DbSet<WorkOrderCategories> workOrderCategories { get; set; }
|
|
|
|
|
|
public DbSet<WorkOrderAttachments> workOrderAttachments { get; set; }
|
2026-07-13 13:12:07 -03:00
|
|
|
|
public DbSet<CompletionDocTemplate> CompletionDocTemplates { get; set; }
|
2026-04-16 18:09:57 -04:00
|
|
|
|
public DbSet<WorkOrderAuditLog> WorkOrderAuditLogs { get; set; }
|
2026-06-29 17:22:31 -03:00
|
|
|
|
public DbSet<WorkOrderFieldLock> WorkOrderFieldLocks { get; set; }
|
2026-07-09 14:08:48 -03:00
|
|
|
|
public DbSet<WorkOrderWeekRolledLedger> WorkOrderWeekRolledLedgers { get; set; }
|
2026-07-24 21:03:50 -03:00
|
|
|
|
public DbSet<WorkOrderWebhookDelivery> WorkOrderWebhookDeliveries { get; set; }
|
2026-07-24 22:13:25 -03:00
|
|
|
|
public DbSet<WorkOrderReconciliationJob> WorkOrderReconciliationJobs { get; set; }
|
|
|
|
|
|
public DbSet<WorkOrderExternalReceipt> WorkOrderExternalReceipts { get; set; }
|
Add configurable dropdowns, new WO fields, and seed data
- Create DropdownOption model with Category, Value, ParentValue for Trade/SubTrade/Problem
- Add DropdownOptionsController with CRUD + ByCategory endpoint with parent filtering
- Add Problem, Trade, SubTrade, VendorNTE, ScheduledDate, CompletedDate, Source to WorkOrder
- Update EditWorkorder_DTO and GetWorkorderById with new fields
- Audit log tracks changes to all new fields
- Seed default Trades (10), SubTrades (20), and Problems (11) on startup
2026-04-17 10:40:38 -04:00
|
|
|
|
public DbSet<DropdownOption> DropdownOptions { get; set; }
|
Add vendor dispatch workflow backend
- Create Vendor model with company info, trade specialties, active flag
- Create Dispatch model (doubles as Vendor PO) with PO number, NTE, status, reply-to address
- VendorController: CRUD, paginated list, dropdown endpoint with trade filtering
- DispatchToVendor endpoint: multi-vendor dispatch, auto-generated PO numbers (VPO-00001),
HTML email with full WO details via SendGrid, reply-to wo-{number}@int.seahaven.com
- GetDispatches endpoint for listing dispatches by WO
- Include dispatches in GetWorkorderById response
- SendMessage.SendDispatchEmail with reply-to support
- Audit log entry for each dispatch
2026-04-17 11:37:35 -04:00
|
|
|
|
public DbSet<Vendor> Vendors { get; set; }
|
2026-07-23 17:02:40 -03:00
|
|
|
|
public DbSet<VendorCompany> VendorCompanies { get; set; }
|
|
|
|
|
|
public DbSet<VendorAuditLog> VendorAuditLogs { get; set; }
|
2026-04-20 12:01:53 -04:00
|
|
|
|
public DbSet<VendorAccessToken> VendorAccessTokens { get; set; }
|
2026-07-23 19:18:06 -03:00
|
|
|
|
public DbSet<VendorCompletionDocument> VendorCompletionDocuments { get; set; }
|
|
|
|
|
|
public DbSet<SitePreferredVendor> SitePreferredVendors { get; set; }
|
Add vendor dispatch workflow backend
- Create Vendor model with company info, trade specialties, active flag
- Create Dispatch model (doubles as Vendor PO) with PO number, NTE, status, reply-to address
- VendorController: CRUD, paginated list, dropdown endpoint with trade filtering
- DispatchToVendor endpoint: multi-vendor dispatch, auto-generated PO numbers (VPO-00001),
HTML email with full WO details via SendGrid, reply-to wo-{number}@int.seahaven.com
- GetDispatches endpoint for listing dispatches by WO
- Include dispatches in GetWorkorderById response
- SendMessage.SendDispatchEmail with reply-to support
- Audit log entry for each dispatch
2026-04-17 11:37:35 -04:00
|
|
|
|
public DbSet<Dispatch> Dispatches { get; set; }
|
2026-04-17 15:16:28 -04:00
|
|
|
|
public DbSet<DispatchWorkOrder> DispatchWorkOrders { get; set; }
|
2026-04-17 15:28:52 -04:00
|
|
|
|
public DbSet<DispatchChecklistItem> DispatchChecklistItems { get; set; }
|
2026-04-17 15:51:20 -04:00
|
|
|
|
public DbSet<DispatchSignoff> DispatchSignoffs { get; set; }
|
2026-04-20 13:25:56 -04:00
|
|
|
|
public DbSet<DispatchUpliftRequest> DispatchUpliftRequests { get; set; }
|
2026-04-17 14:57:18 -04:00
|
|
|
|
public DbSet<TaskListTemplate> TaskListTemplates { get; set; }
|
|
|
|
|
|
public DbSet<TaskListTemplateItem> TaskListTemplateItems { get; set; }
|
2026-01-21 15:41:19 -06:00
|
|
|
|
|
|
|
|
|
|
// New DbSets for added entities
|
|
|
|
|
|
public DbSet<Accounts> Accounts { get; set; }
|
|
|
|
|
|
public DbSet<Addresses> Addresses { get; set; }
|
|
|
|
|
|
public DbSet<Assets> Assets { get; set; }
|
|
|
|
|
|
public DbSet<Employee> Employees { get; set; }
|
|
|
|
|
|
public DbSet<FollowUps> FollowUps { get; set; }
|
|
|
|
|
|
public DbSet<PMSchedules> PMSchedules { get; set; }
|
|
|
|
|
|
public DbSet<ContactDetails> ContactDetails { get; set; }
|
2026-06-22 18:46:46 -04:00
|
|
|
|
public DbSet<Events> Events { get; set; }
|
2026-03-30 06:49:40 -05:00
|
|
|
|
|
|
|
|
|
|
// Employee related DbSets
|
|
|
|
|
|
public DbSet<EmployeeAddress> EmployeeAddresses { get; set; }
|
|
|
|
|
|
public DbSet<EmployeePhone> EmployeePhones { get; set; }
|
|
|
|
|
|
public DbSet<EmployeeEmail> EmployeeEmails { get; set; }
|
|
|
|
|
|
public DbSet<Department> Departments { get; set; }
|
|
|
|
|
|
public DbSet<JobTitle> JobTitles { get; set; }
|
|
|
|
|
|
public DbSet<Region> Regions { get; set; }
|
2026-07-07 11:26:13 -03:00
|
|
|
|
|
|
|
|
|
|
public override int SaveChanges()
|
|
|
|
|
|
{
|
|
|
|
|
|
BumpRowVersionsForInMemoryProvider();
|
|
|
|
|
|
return base.SaveChanges();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
BumpRowVersionsForInMemoryProvider();
|
|
|
|
|
|
return base.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void BumpRowVersionsForInMemoryProvider()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Database.ProviderName?.Contains("InMemory", StringComparison.OrdinalIgnoreCase) ?? true)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var entry in ChangeTracker.Entries<WorkOrder>())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entry.State == EntityState.Modified)
|
|
|
|
|
|
entry.Entity.RowVersion = IncrementRowVersion(entry.Entity.RowVersion);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var entry in ChangeTracker.Entries<Dispatch>())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entry.State == EntityState.Modified)
|
|
|
|
|
|
entry.Entity.RowVersion = IncrementRowVersion(entry.Entity.RowVersion);
|
|
|
|
|
|
}
|
2026-07-24 22:13:25 -03:00
|
|
|
|
|
|
|
|
|
|
foreach (var entry in ChangeTracker.Entries<WorkOrderReconciliationJob>())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entry.State == EntityState.Modified)
|
|
|
|
|
|
entry.Entity.RowVersion = IncrementRowVersion(entry.Entity.RowVersion);
|
|
|
|
|
|
}
|
2026-07-07 11:26:13 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static byte[] IncrementRowVersion(byte[]? current)
|
|
|
|
|
|
{
|
|
|
|
|
|
var bytes = current != null && current.Length > 0
|
|
|
|
|
|
? (byte[])current.Clone()
|
|
|
|
|
|
: new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 };
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < bytes.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (++bytes[i] != 0)
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
|
|
}
|
2026-07-24 18:12:03 -03:00
|
|
|
|
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
public class ApplicationUser : IdentityUser
|
|
|
|
|
|
{
|
|
|
|
|
|
public string? FirstName { get; set; }
|
|
|
|
|
|
public string? LastName { get; set; }
|
|
|
|
|
|
public string? UniqueName { get; set; }
|
|
|
|
|
|
public string? Locations { get; set; }
|
|
|
|
|
|
public string? Contact { get; set; }
|
|
|
|
|
|
public DateTime? CreatedDate { get; set; }
|
|
|
|
|
|
public DateTime? DeletedDate { get; set; }
|
|
|
|
|
|
public bool? IsDeleted { get; set; }
|
|
|
|
|
|
public string? UserImage { get; set; }
|
2026-06-29 17:22:31 -03:00
|
|
|
|
public string? Initials { get; set; }
|
|
|
|
|
|
public string? Color { get; set; }
|
2024-09-28 14:58:36 -04:00
|
|
|
|
public int? Type { get; set; } // 1 for users 0 for admin
|
2026-07-24 18:12:03 -03:00
|
|
|
|
public ICollection<Template>? Templates { get; set; }
|
|
|
|
|
|
public ICollection<WorkOrder>? WorkOrders { get; set; }
|
2024-09-28 14:58:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
public class FullAuditEntity
|
|
|
|
|
|
{
|
|
|
|
|
|
[Key]
|
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
public bool? IsDeleted { get; set; }
|
|
|
|
|
|
public string? createdby { get; set; }
|
|
|
|
|
|
public string? DeleterUserId { get; set; }
|
|
|
|
|
|
public DateTime? DeletionTime { get; set; }
|
|
|
|
|
|
public DateTime? CreatedDate { get; set; }
|
|
|
|
|
|
public DateTime? LastModificationTime { get; set; }
|
|
|
|
|
|
public int? LastModifierUserId { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|