Add customer communication system
This commit is contained in:
@@ -17,6 +17,11 @@ public class LittleShopContext : DbContext
|
||||
public DbSet<OrderItem> OrderItems { get; set; }
|
||||
public DbSet<CryptoPayment> CryptoPayments { get; set; }
|
||||
public DbSet<ShippingRate> ShippingRates { get; set; }
|
||||
public DbSet<Bot> Bots { get; set; }
|
||||
public DbSet<BotMetric> BotMetrics { get; set; }
|
||||
public DbSet<BotSession> BotSessions { get; set; }
|
||||
public DbSet<Customer> Customers { get; set; }
|
||||
public DbSet<CustomerMessage> CustomerMessages { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
@@ -54,6 +59,12 @@ public class LittleShopContext : DbContext
|
||||
// Order entity
|
||||
modelBuilder.Entity<Order>(entity =>
|
||||
{
|
||||
entity.HasOne(o => o.Customer)
|
||||
.WithMany(c => c.Orders)
|
||||
.HasForeignKey(o => o.CustomerId)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired(false); // Make customer optional for transition
|
||||
|
||||
entity.HasMany(o => o.Items)
|
||||
.WithOne(oi => oi.Order)
|
||||
.HasForeignKey(oi => oi.OrderId)
|
||||
@@ -64,7 +75,9 @@ public class LittleShopContext : DbContext
|
||||
.HasForeignKey(cp => cp.OrderId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
entity.HasIndex(e => e.CustomerId);
|
||||
entity.HasIndex(e => e.IdentityReference);
|
||||
entity.HasIndex(e => e.CreatedAt);
|
||||
});
|
||||
|
||||
// OrderItem entity
|
||||
@@ -79,5 +92,92 @@ public class LittleShopContext : DbContext
|
||||
entity.HasIndex(e => e.BTCPayInvoiceId);
|
||||
entity.HasIndex(e => e.WalletAddress);
|
||||
});
|
||||
|
||||
// Bot entity
|
||||
modelBuilder.Entity<Bot>(entity =>
|
||||
{
|
||||
entity.HasIndex(e => e.BotKey).IsUnique();
|
||||
entity.HasIndex(e => e.Name);
|
||||
entity.HasIndex(e => e.Status);
|
||||
|
||||
entity.HasMany(b => b.Metrics)
|
||||
.WithOne(m => m.Bot)
|
||||
.HasForeignKey(m => m.BotId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
entity.HasMany(b => b.Sessions)
|
||||
.WithOne(s => s.Bot)
|
||||
.HasForeignKey(s => s.BotId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
});
|
||||
|
||||
// BotMetric entity
|
||||
modelBuilder.Entity<BotMetric>(entity =>
|
||||
{
|
||||
entity.HasIndex(e => new { e.BotId, e.RecordedAt });
|
||||
entity.HasIndex(e => e.MetricType);
|
||||
});
|
||||
|
||||
// BotSession entity
|
||||
modelBuilder.Entity<BotSession>(entity =>
|
||||
{
|
||||
entity.HasIndex(e => new { e.BotId, e.SessionIdentifier });
|
||||
entity.HasIndex(e => e.StartedAt);
|
||||
entity.HasIndex(e => e.LastActivityAt);
|
||||
});
|
||||
|
||||
// Customer entity
|
||||
modelBuilder.Entity<Customer>(entity =>
|
||||
{
|
||||
entity.HasIndex(e => e.TelegramUserId).IsUnique();
|
||||
entity.HasIndex(e => e.TelegramUsername);
|
||||
entity.HasIndex(e => e.Email);
|
||||
entity.HasIndex(e => e.CreatedAt);
|
||||
entity.HasIndex(e => e.LastActiveAt);
|
||||
entity.HasIndex(e => e.DataRetentionDate);
|
||||
|
||||
entity.HasMany(c => c.Orders)
|
||||
.WithOne(o => o.Customer)
|
||||
.HasForeignKey(o => o.CustomerId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
entity.HasMany(c => c.Messages)
|
||||
.WithOne(m => m.Customer)
|
||||
.HasForeignKey(m => m.CustomerId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
});
|
||||
|
||||
// CustomerMessage entity
|
||||
modelBuilder.Entity<CustomerMessage>(entity =>
|
||||
{
|
||||
entity.HasOne(m => m.Customer)
|
||||
.WithMany(c => c.Messages)
|
||||
.HasForeignKey(m => m.CustomerId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
entity.HasOne(m => m.Order)
|
||||
.WithMany()
|
||||
.HasForeignKey(m => m.OrderId)
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
entity.HasOne(m => m.AdminUser)
|
||||
.WithMany()
|
||||
.HasForeignKey(m => m.AdminUserId)
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
entity.HasOne(m => m.ParentMessage)
|
||||
.WithMany(m => m.Replies)
|
||||
.HasForeignKey(m => m.ParentMessageId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
entity.HasIndex(e => new { e.CustomerId, e.CreatedAt });
|
||||
entity.HasIndex(e => e.OrderId);
|
||||
entity.HasIndex(e => e.Status);
|
||||
entity.HasIndex(e => e.Direction);
|
||||
entity.HasIndex(e => e.Type);
|
||||
entity.HasIndex(e => e.ScheduledFor);
|
||||
entity.HasIndex(e => e.ThreadId);
|
||||
entity.HasIndex(e => e.ParentMessageId);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user