feat: add operations content and audit persistence

This commit is contained in:
xiong
2026-07-26 05:46:02 +08:00
parent 07e2db04d0
commit 4b59b493e1
7 changed files with 14263 additions and 1 deletions

View File

@@ -0,0 +1,149 @@
using System.Text.Json;
using Tiku.Domain.Common;
namespace Tiku.Domain.Operations;
public sealed class Banner : AuditableTenantEntity
{
public Guid? RegionId { get; set; }
public string? LegacyId { get; set; }
public string? Title { get; set; }
public string? Subtitle { get; set; }
public string? Content { get; set; }
public string? ButtonText { get; set; }
public string? ButtonLink { get; set; }
public string? BackgroundColor { get; set; }
public string? BorderColor { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
}
public sealed class Faq : AuditableTenantEntity
{
public Guid? RegionId { get; set; }
public string? LegacyId { get; set; }
public string? Question { get; set; }
public string? Answer { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
}
public sealed class Announcement : AuditableTenantEntity
{
public string? LegacyId { get; set; }
public string? Content { get; set; }
public string? Link { get; set; }
public string? BackgroundColor { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
}
public sealed class AuditLog : Entity
{
public Guid? TenantId { get; set; }
public Guid? ActorUserId { get; set; }
public string Action { get; set; } = string.Empty;
public string? TargetType { get; set; }
public string? TargetId { get; set; }
public JsonElement Details { get; set; } = JsonDefaults.Object();
public string? IpAddress { get; set; }
public string? UserAgent { get; set; }
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
}
public sealed class UserNotification : AuditableTenantEntity
{
public Guid UserId { get; set; }
public Guid? SourceId { get; set; }
public Guid? CreatedBy { get; set; }
public string NotificationType { get; set; } = string.Empty;
public NotificationStatus Status { get; set; } = NotificationStatus.Unread;
public NotificationSeverity Severity { get; set; } = NotificationSeverity.Info;
public string Title { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
public string? ActionLabel { get; set; }
public string? ActionPath { get; set; }
public string? SourceType { get; set; }
public string? DedupeKey { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
public DateTimeOffset? ReadAt { get; set; }
}
public sealed class Badge : AuditableTenantEntity
{
public string? LegacyId { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public string? Category { get; set; }
public string? IconUrl { get; set; }
public int? Level { get; set; }
public string? UnlockType { get; set; }
public string? ConditionField { get; set; }
public string? ConditionOperator { get; set; }
public decimal? ConditionValue { get; set; }
public JsonElement ConditionExtra { get; set; } = JsonDefaults.Object();
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
}
public sealed class UserBadge : AuditableTenantEntity
{
public Guid? UserId { get; set; }
public Guid? BadgeId { get; set; }
public Guid? GrantedBy { get; set; }
public string? LegacyId { get; set; }
public string? Note { get; set; }
public DateTimeOffset? GrantedAt { get; set; }
}
public sealed class TenantContentNotification : AuditableTenantEntity
{
public Guid? AdoptionId { get; set; }
public Guid? SourceQuestionBankId { get; set; }
public Guid? CreatedBy { get; set; }
public Guid? ReadBy { get; set; }
public TenantContentNotificationType NotificationType { get; set; } = TenantContentNotificationType.PublicQuestionBankSynced;
public TenantContentNotificationStatus Status { get; set; } = TenantContentNotificationStatus.Unread;
public NotificationSeverity Severity { get; set; } = NotificationSeverity.Info;
public string Title { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
public string? ActionLabel { get; set; }
public string? ActionPath { get; set; }
public string? DedupeKey { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
public DateTimeOffset? ReadAt { get; set; }
public DateTimeOffset? ResolvedAt { get; set; }
}
public sealed class TenantThemeTemplate : AuditableEntity
{
public string Code { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public string? PreviewImageUrl { get; set; }
public JsonElement Theme { get; set; } = JsonDefaults.Object();
public JsonElement PublicAssets { get; set; } = JsonDefaults.Object();
public TenantThemeTemplateStatus Status { get; set; } = TenantThemeTemplateStatus.Active;
public int SortOrder { get; set; } = 100;
}
public sealed class TenantThemeConfig : AuditableTenantEntity
{
public string? ActiveTemplateCode { get; set; }
public JsonElement ActiveTheme { get; set; } = JsonDefaults.Object();
public JsonElement ActivePublicAssets { get; set; } = JsonDefaults.Object();
public string? DraftTemplateCode { get; set; }
public JsonElement DraftTheme { get; set; } = JsonDefaults.Object();
public JsonElement DraftPublicAssets { get; set; } = JsonDefaults.Object();
public TenantThemeConfigStatus Status { get; set; } = TenantThemeConfigStatus.Published;
public DateTimeOffset? PublishedAt { get; set; }
public Guid? PublishedBy { get; set; }
public Guid? DraftUpdatedBy { get; set; }
}
public enum NotificationStatus { Unread, Read, Dismissed, Archived }
public enum NotificationSeverity { Info, Success, Warning, Error }
public enum TenantContentNotificationType { PublicQuestionBankSynced, PublicQuestionBankConflict }
public enum TenantContentNotificationStatus { Unread, Read, Dismissed, Resolved }
public enum TenantThemeTemplateStatus { Active, Disabled }
public enum TenantThemeConfigStatus { Draft, Published }

View File

@@ -0,0 +1,216 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Tiku.Domain.Catalog;
using Tiku.Domain.Identity;
using Tiku.Domain.Operations;
using Tiku.Domain.QuestionBanks;
using Tiku.Domain.Tenancy;
namespace Tiku.Infrastructure.Persistence.Configurations;
internal sealed class BannerConfiguration : IEntityTypeConfiguration<Banner>
{
public void Configure(EntityTypeBuilder<Banner> builder)
{
builder.ConfigureTenantEntity("banners");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Title).HasMaxLength(300);
builder.Property(entity => entity.Subtitle).HasMaxLength(500);
builder.Property(entity => entity.ButtonText).HasMaxLength(100);
builder.Property(entity => entity.ButtonLink).HasMaxLength(2048);
builder.Property(entity => entity.BackgroundColor).HasMaxLength(50);
builder.Property(entity => entity.BorderColor).HasMaxLength(50);
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.RegionId, entity.IsActive, entity.SortOrder });
builder.HasOne<Region>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
}
}
internal sealed class FaqConfiguration : IEntityTypeConfiguration<Faq>
{
public void Configure(EntityTypeBuilder<Faq> builder)
{
builder.ConfigureTenantEntity("faqs");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.RegionId, entity.IsActive, entity.SortOrder });
builder.HasOne<Region>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
}
}
internal sealed class AnnouncementConfiguration : IEntityTypeConfiguration<Announcement>
{
public void Configure(EntityTypeBuilder<Announcement> builder)
{
builder.ConfigureTenantEntity("announcements");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Link).HasMaxLength(2048);
builder.Property(entity => entity.BackgroundColor).HasMaxLength(50);
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.IsActive, entity.SortOrder });
}
}
internal sealed class AuditLogConfiguration : IEntityTypeConfiguration<AuditLog>
{
public void Configure(EntityTypeBuilder<AuditLog> builder)
{
builder.ConfigureEntity("audit_logs");
builder.Property(entity => entity.Action).HasMaxLength(200);
builder.Property(entity => entity.TargetType).HasMaxLength(200);
builder.Property(entity => entity.TargetId).HasMaxLength(200);
builder.Property(entity => entity.Details).IsJson("{}");
builder.Property(entity => entity.IpAddress).HasMaxLength(100);
builder.Property(entity => entity.UserAgent).HasMaxLength(1024);
builder.Property(entity => entity.CreatedAt).HasDefaultValueSql("now()");
builder.HasIndex(entity => new { entity.TenantId, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.TargetType, entity.TargetId, entity.CreatedAt });
builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.ActorUserId).OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class UserNotificationConfiguration : IEntityTypeConfiguration<UserNotification>
{
public void Configure(EntityTypeBuilder<UserNotification> builder)
{
builder.ConfigureTenantEntity("user_notifications");
builder.ConfigureTimestamps();
builder.Property(entity => entity.NotificationType).HasMaxLength(100);
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.Severity).HasSnakeCaseEnum();
builder.Property(entity => entity.Title).HasMaxLength(300);
builder.Property(entity => entity.ActionLabel).HasMaxLength(100);
builder.Property(entity => entity.ActionPath).HasMaxLength(2048);
builder.Property(entity => entity.SourceType).HasMaxLength(100);
builder.Property(entity => entity.DedupeKey).HasMaxLength(200);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.UserId, entity.NotificationType, entity.DedupeKey })
.IsUnique()
.HasFilter("dedupe_key is not null");
builder.HasIndex(entity => new { entity.TenantId, entity.UserId, entity.Status, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.SourceType, entity.SourceId });
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.UserId).OnDelete(DeleteBehavior.Cascade);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.CreatedBy).OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class BadgeConfiguration : IEntityTypeConfiguration<Badge>
{
public void Configure(EntityTypeBuilder<Badge> builder)
{
builder.ConfigureTenantEntity("badges");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Name).HasMaxLength(200);
builder.Property(entity => entity.Category).HasMaxLength(100);
builder.Property(entity => entity.IconUrl).HasMaxLength(2048);
builder.Property(entity => entity.UnlockType).HasMaxLength(100);
builder.Property(entity => entity.ConditionField).HasMaxLength(100);
builder.Property(entity => entity.ConditionOperator).HasMaxLength(50);
builder.Property(entity => entity.ConditionValue).HasPrecision(18, 4);
builder.Property(entity => entity.ConditionExtra).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.Category, entity.IsActive, entity.SortOrder });
}
}
internal sealed class UserBadgeConfiguration : IEntityTypeConfiguration<UserBadge>
{
public void Configure(EntityTypeBuilder<UserBadge> builder)
{
builder.ConfigureTenantEntity("user_badges");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.UserId, entity.BadgeId }).IsUnique();
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.UserId).OnDelete(DeleteBehavior.Cascade);
builder.HasOne<Badge>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.BadgeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.GrantedBy).OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class TenantContentNotificationConfiguration : IEntityTypeConfiguration<TenantContentNotification>
{
public void Configure(EntityTypeBuilder<TenantContentNotification> builder)
{
builder.ConfigureTenantEntity("tenant_content_notifications");
builder.ConfigureTimestamps();
builder.Property(entity => entity.NotificationType).HasSnakeCaseEnum();
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.Severity).HasSnakeCaseEnum();
builder.Property(entity => entity.Title).HasMaxLength(300);
builder.Property(entity => entity.ActionLabel).HasMaxLength(100);
builder.Property(entity => entity.ActionPath).HasMaxLength(2048);
builder.Property(entity => entity.DedupeKey).HasMaxLength(200);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.NotificationType, entity.DedupeKey })
.IsUnique()
.HasFilter("dedupe_key is not null");
builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.AdoptionId, entity.NotificationType, entity.Status });
builder.HasOne<QuestionBank>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.SourceQuestionBankId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.CreatedBy).OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.ReadBy).OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class TenantThemeTemplateConfiguration : IEntityTypeConfiguration<TenantThemeTemplate>
{
public void Configure(EntityTypeBuilder<TenantThemeTemplate> builder)
{
builder.ConfigureEntity("tenant_theme_templates");
builder.ConfigureTimestamps();
builder.Property(entity => entity.Code).HasMaxLength(100);
builder.Property(entity => entity.Name).HasMaxLength(200);
builder.Property(entity => entity.PreviewImageUrl).HasMaxLength(2048);
builder.Property(entity => entity.Theme).IsJson("{}");
builder.Property(entity => entity.PublicAssets).IsJson("{}");
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.HasAlternateKey(entity => entity.Code);
builder.HasIndex(entity => entity.Code).IsUnique();
builder.HasIndex(entity => new { entity.Status, entity.SortOrder, entity.Code });
}
}
internal sealed class TenantThemeConfigConfiguration : IEntityTypeConfiguration<TenantThemeConfig>
{
public void Configure(EntityTypeBuilder<TenantThemeConfig> builder)
{
builder.ConfigureTenantEntity("tenant_theme_configs");
builder.ConfigureTimestamps();
builder.Property(entity => entity.ActiveTemplateCode).HasMaxLength(100);
builder.Property(entity => entity.ActiveTheme).IsJson("{}");
builder.Property(entity => entity.ActivePublicAssets).IsJson("{}");
builder.Property(entity => entity.DraftTemplateCode).HasMaxLength(100);
builder.Property(entity => entity.DraftTheme).IsJson("{}");
builder.Property(entity => entity.DraftPublicAssets).IsJson("{}");
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.HasIndex(entity => entity.TenantId).IsUnique();
builder.HasOne<TenantThemeTemplate>().WithMany()
.HasForeignKey(entity => entity.ActiveTemplateCode)
.HasPrincipalKey(entity => entity.Code)
.OnDelete(DeleteBehavior.SetNull);
builder.HasOne<TenantThemeTemplate>().WithMany()
.HasForeignKey(entity => entity.DraftTemplateCode)
.HasPrincipalKey(entity => entity.Code)
.OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.PublishedBy).OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.DraftUpdatedBy).OnDelete(DeleteBehavior.SetNull);
}
}

View File

@@ -0,0 +1,619 @@
using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Tiku.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class AddOperationsContentAndAuditPersistence : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "announcements",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
content = table.Column<string>(type: "text", nullable: true),
link = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
background_color = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_announcements", x => x.id);
table.UniqueConstraint("ak_announcements_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_announcements_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "audit_logs",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: true),
actor_user_id = table.Column<Guid>(type: "uuid", nullable: true),
action = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
target_type = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
target_id = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
details = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
ip_address = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
user_agent = table.Column<string>(type: "character varying(1024)", maxLength: 1024, nullable: true),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_audit_logs", x => x.id);
table.ForeignKey(
name: "fk_audit_logs_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_audit_logs_users_actor_user_id",
column: x => x.actor_user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "badges",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
description = table.Column<string>(type: "text", nullable: true),
category = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
icon_url = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
level = table.Column<int>(type: "integer", nullable: true),
unlock_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
condition_field = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
condition_operator = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
condition_value = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: true),
condition_extra = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_badges", x => x.id);
table.UniqueConstraint("ak_badges_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_badges_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "banners",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
title = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
subtitle = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
content = table.Column<string>(type: "text", nullable: true),
button_text = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
button_link = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
background_color = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
border_color = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_banners", x => x.id);
table.UniqueConstraint("ak_banners_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_banners_regions_tenant_id_region_id",
columns: x => new { x.tenant_id, x.region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_banners_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "faqs",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
question = table.Column<string>(type: "text", nullable: true),
answer = table.Column<string>(type: "text", nullable: true),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_faqs", x => x.id);
table.UniqueConstraint("ak_faqs_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_faqs_regions_tenant_id_region_id",
columns: x => new { x.tenant_id, x.region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_faqs_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "tenant_content_notifications",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
adoption_id = table.Column<Guid>(type: "uuid", nullable: true),
source_question_bank_id = table.Column<Guid>(type: "uuid", nullable: true),
created_by = table.Column<Guid>(type: "uuid", nullable: true),
read_by = table.Column<Guid>(type: "uuid", nullable: true),
notification_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
severity = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
title = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
message = table.Column<string>(type: "text", nullable: false),
action_label = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
action_path = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
dedupe_key = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
read_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
resolved_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_content_notifications", x => x.id);
table.UniqueConstraint("ak_tenant_content_notifications_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_tenant_content_notifications_question_banks_tenant_id_sourc~",
columns: x => new { x.tenant_id, x.source_question_bank_id },
principalTable: "question_banks",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_tenant_content_notifications_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_tenant_content_notifications_users_created_by",
column: x => x.created_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_tenant_content_notifications_users_read_by",
column: x => x.read_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "tenant_theme_templates",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
description = table.Column<string>(type: "text", nullable: true),
preview_image_url = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
theme = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
public_assets = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
sort_order = table.Column<int>(type: "integer", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_theme_templates", x => x.id);
table.UniqueConstraint("ak_tenant_theme_templates_code", x => x.code);
});
migrationBuilder.CreateTable(
name: "user_notifications",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
user_id = table.Column<Guid>(type: "uuid", nullable: false),
source_id = table.Column<Guid>(type: "uuid", nullable: true),
created_by = table.Column<Guid>(type: "uuid", nullable: true),
notification_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
severity = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
title = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
message = table.Column<string>(type: "text", nullable: false),
action_label = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
action_path = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
source_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
dedupe_key = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
read_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_user_notifications", x => x.id);
table.UniqueConstraint("ak_user_notifications_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_user_notifications_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_user_notifications_users_created_by",
column: x => x.created_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_user_notifications_users_user_id",
column: x => x.user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "user_badges",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
user_id = table.Column<Guid>(type: "uuid", nullable: true),
badge_id = table.Column<Guid>(type: "uuid", nullable: true),
granted_by = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
note = table.Column<string>(type: "text", nullable: true),
granted_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_user_badges", x => x.id);
table.UniqueConstraint("ak_user_badges_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_user_badges_badges_tenant_id_badge_id",
columns: x => new { x.tenant_id, x.badge_id },
principalTable: "badges",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_user_badges_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_user_badges_users_granted_by",
column: x => x.granted_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_user_badges_users_user_id",
column: x => x.user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "tenant_theme_configs",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
active_template_code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
active_theme = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
active_public_assets = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
draft_template_code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
draft_theme = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
draft_public_assets = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
published_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
published_by = table.Column<Guid>(type: "uuid", nullable: true),
draft_updated_by = table.Column<Guid>(type: "uuid", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_theme_configs", x => x.id);
table.UniqueConstraint("ak_tenant_theme_configs_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_tenant_theme_configs_tenant_theme_templates_active_template~",
column: x => x.active_template_code,
principalTable: "tenant_theme_templates",
principalColumn: "code",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_tenant_theme_configs_tenant_theme_templates_draft_template_~",
column: x => x.draft_template_code,
principalTable: "tenant_theme_templates",
principalColumn: "code",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_tenant_theme_configs_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_tenant_theme_configs_users_draft_updated_by",
column: x => x.draft_updated_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_tenant_theme_configs_users_published_by",
column: x => x.published_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateIndex(
name: "ix_announcements_tenant_id_is_active_sort_order",
table: "announcements",
columns: new[] { "tenant_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_announcements_tenant_id_legacy_id",
table: "announcements",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_audit_logs_actor_user_id",
table: "audit_logs",
column: "actor_user_id");
migrationBuilder.CreateIndex(
name: "ix_audit_logs_tenant_id_created_at",
table: "audit_logs",
columns: new[] { "tenant_id", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_audit_logs_tenant_id_target_type_target_id_created_at",
table: "audit_logs",
columns: new[] { "tenant_id", "target_type", "target_id", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_badges_tenant_id_category_is_active_sort_order",
table: "badges",
columns: new[] { "tenant_id", "category", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_badges_tenant_id_legacy_id",
table: "badges",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_banners_tenant_id_legacy_id",
table: "banners",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_banners_tenant_id_region_id_is_active_sort_order",
table: "banners",
columns: new[] { "tenant_id", "region_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_faqs_tenant_id_legacy_id",
table: "faqs",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_faqs_tenant_id_region_id_is_active_sort_order",
table: "faqs",
columns: new[] { "tenant_id", "region_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_tenant_content_notifications_created_by",
table: "tenant_content_notifications",
column: "created_by");
migrationBuilder.CreateIndex(
name: "ix_tenant_content_notifications_read_by",
table: "tenant_content_notifications",
column: "read_by");
migrationBuilder.CreateIndex(
name: "ix_tenant_content_notifications_tenant_id_adoption_id_notifica~",
table: "tenant_content_notifications",
columns: new[] { "tenant_id", "adoption_id", "notification_type", "status" });
migrationBuilder.CreateIndex(
name: "ix_tenant_content_notifications_tenant_id_notification_type_de~",
table: "tenant_content_notifications",
columns: new[] { "tenant_id", "notification_type", "dedupe_key" },
unique: true,
filter: "dedupe_key is not null");
migrationBuilder.CreateIndex(
name: "ix_tenant_content_notifications_tenant_id_source_question_bank~",
table: "tenant_content_notifications",
columns: new[] { "tenant_id", "source_question_bank_id" });
migrationBuilder.CreateIndex(
name: "ix_tenant_content_notifications_tenant_id_status_created_at",
table: "tenant_content_notifications",
columns: new[] { "tenant_id", "status", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_tenant_theme_configs_active_template_code",
table: "tenant_theme_configs",
column: "active_template_code");
migrationBuilder.CreateIndex(
name: "ix_tenant_theme_configs_draft_template_code",
table: "tenant_theme_configs",
column: "draft_template_code");
migrationBuilder.CreateIndex(
name: "ix_tenant_theme_configs_draft_updated_by",
table: "tenant_theme_configs",
column: "draft_updated_by");
migrationBuilder.CreateIndex(
name: "ix_tenant_theme_configs_published_by",
table: "tenant_theme_configs",
column: "published_by");
migrationBuilder.CreateIndex(
name: "ix_tenant_theme_configs_tenant_id",
table: "tenant_theme_configs",
column: "tenant_id",
unique: true);
migrationBuilder.CreateIndex(
name: "ix_tenant_theme_templates_code",
table: "tenant_theme_templates",
column: "code",
unique: true);
migrationBuilder.CreateIndex(
name: "ix_tenant_theme_templates_status_sort_order_code",
table: "tenant_theme_templates",
columns: new[] { "status", "sort_order", "code" });
migrationBuilder.CreateIndex(
name: "ix_user_badges_granted_by",
table: "user_badges",
column: "granted_by");
migrationBuilder.CreateIndex(
name: "ix_user_badges_tenant_id_badge_id",
table: "user_badges",
columns: new[] { "tenant_id", "badge_id" });
migrationBuilder.CreateIndex(
name: "ix_user_badges_tenant_id_legacy_id",
table: "user_badges",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_user_badges_tenant_id_user_id_badge_id",
table: "user_badges",
columns: new[] { "tenant_id", "user_id", "badge_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_user_badges_user_id",
table: "user_badges",
column: "user_id");
migrationBuilder.CreateIndex(
name: "ix_user_notifications_created_by",
table: "user_notifications",
column: "created_by");
migrationBuilder.CreateIndex(
name: "ix_user_notifications_tenant_id_source_type_source_id",
table: "user_notifications",
columns: new[] { "tenant_id", "source_type", "source_id" });
migrationBuilder.CreateIndex(
name: "ix_user_notifications_tenant_id_user_id_notification_type_dedu~",
table: "user_notifications",
columns: new[] { "tenant_id", "user_id", "notification_type", "dedupe_key" },
unique: true,
filter: "dedupe_key is not null");
migrationBuilder.CreateIndex(
name: "ix_user_notifications_tenant_id_user_id_status_created_at",
table: "user_notifications",
columns: new[] { "tenant_id", "user_id", "status", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_user_notifications_user_id",
table: "user_notifications",
column: "user_id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "announcements");
migrationBuilder.DropTable(
name: "audit_logs");
migrationBuilder.DropTable(
name: "banners");
migrationBuilder.DropTable(
name: "faqs");
migrationBuilder.DropTable(
name: "tenant_content_notifications");
migrationBuilder.DropTable(
name: "tenant_theme_configs");
migrationBuilder.DropTable(
name: "user_badges");
migrationBuilder.DropTable(
name: "user_notifications");
migrationBuilder.DropTable(
name: "tenant_theme_templates");
migrationBuilder.DropTable(
name: "badges");
}
}
}

View File

@@ -6,6 +6,7 @@ using Tiku.Domain.Content;
using Tiku.Domain.Growth;
using Tiku.Domain.Identity;
using Tiku.Domain.Learning;
using Tiku.Domain.Operations;
using Tiku.Domain.QuestionBanks;
using Tiku.Domain.Tenancy;
@@ -103,6 +104,16 @@ public sealed class TikuDbContext(DbContextOptions<TikuDbContext> options) : DbC
public DbSet<TenantCommissionSetting> TenantCommissionSettings => Set<TenantCommissionSetting>();
public DbSet<CommissionSettlement> CommissionSettlements => Set<CommissionSettlement>();
public DbSet<CommissionSettlementItem> CommissionSettlementItems => Set<CommissionSettlementItem>();
public DbSet<Banner> Banners => Set<Banner>();
public DbSet<Faq> Faqs => Set<Faq>();
public DbSet<Announcement> Announcements => Set<Announcement>();
public DbSet<AuditLog> AuditLogs => Set<AuditLog>();
public DbSet<UserNotification> UserNotifications => Set<UserNotification>();
public DbSet<Badge> Badges => Set<Badge>();
public DbSet<UserBadge> UserBadges => Set<UserBadge>();
public DbSet<TenantContentNotification> TenantContentNotifications => Set<TenantContentNotification>();
public DbSet<TenantThemeTemplate> TenantThemeTemplates => Set<TenantThemeTemplate>();
public DbSet<TenantThemeConfig> TenantThemeConfigs => Set<TenantThemeConfig>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{

View File

@@ -5,6 +5,7 @@ using Tiku.Domain.Commerce;
using Tiku.Domain.Content;
using Tiku.Domain.Growth;
using Tiku.Domain.Learning;
using Tiku.Domain.Operations;
using Tiku.Domain.QuestionBanks;
using Tiku.Domain.Tenancy;
using Tiku.Infrastructure.Persistence;
@@ -27,7 +28,7 @@ public sealed class PersistenceModelTests
.Select(entity => entity.GetTableName())
.ToHashSet(StringComparer.Ordinal);
Assert.Equal(90, tableNames.Count);
Assert.Equal(100, tableNames.Count);
Assert.Contains("tenants", tableNames);
Assert.Contains("tenant_settings", tableNames);
Assert.Contains("content_entries", tableNames);
@@ -96,6 +97,16 @@ public sealed class PersistenceModelTests
Assert.Contains("tenant_commission_settings", tableNames);
Assert.Contains("commission_settlements", tableNames);
Assert.Contains("commission_settlement_items", tableNames);
Assert.Contains("banners", tableNames);
Assert.Contains("faqs", tableNames);
Assert.Contains("announcements", tableNames);
Assert.Contains("audit_logs", tableNames);
Assert.Contains("user_notifications", tableNames);
Assert.Contains("badges", tableNames);
Assert.Contains("user_badges", tableNames);
Assert.Contains("tenant_content_notifications", tableNames);
Assert.Contains("tenant_theme_templates", tableNames);
Assert.Contains("tenant_theme_configs", tableNames);
Assert.Contains("questions", tableNames);
Assert.Contains("question_versions", tableNames);
Assert.Contains("answer_records", tableNames);
@@ -586,4 +597,68 @@ public sealed class PersistenceModelTests
Assert.Equal(scale, property.GetScale());
}
}
[Theory]
[InlineData(typeof(AuditLog), nameof(AuditLog.Details), "'{}'::jsonb")]
[InlineData(typeof(UserNotification), nameof(UserNotification.Metadata), "'{}'::jsonb")]
[InlineData(typeof(Badge), nameof(Badge.ConditionExtra), "'{}'::jsonb")]
[InlineData(typeof(TenantContentNotification), nameof(TenantContentNotification.Metadata), "'{}'::jsonb")]
[InlineData(typeof(TenantThemeTemplate), nameof(TenantThemeTemplate.Theme), "'{}'::jsonb")]
[InlineData(typeof(TenantThemeTemplate), nameof(TenantThemeTemplate.PublicAssets), "'{}'::jsonb")]
[InlineData(typeof(TenantThemeConfig), nameof(TenantThemeConfig.ActiveTheme), "'{}'::jsonb")]
[InlineData(typeof(TenantThemeConfig), nameof(TenantThemeConfig.ActivePublicAssets), "'{}'::jsonb")]
[InlineData(typeof(TenantThemeConfig), nameof(TenantThemeConfig.DraftTheme), "'{}'::jsonb")]
[InlineData(typeof(TenantThemeConfig), nameof(TenantThemeConfig.DraftPublicAssets), "'{}'::jsonb")]
public void Operations_json_properties_are_mapped_to_jsonb(
Type entityType,
string propertyName,
string expectedDefaultValueSql)
{
using var context = new TikuDbContext(Options);
var property = context.Model.FindEntityType(entityType)!
.FindProperty(propertyName)!;
Assert.Equal("jsonb", property.GetColumnType());
Assert.Equal(expectedDefaultValueSql, property.GetDefaultValueSql());
Assert.Equal(typeof(System.Text.Json.JsonElement), property.ClrType);
}
[Fact]
public void Operations_tables_have_expected_unique_indexes_and_condition_precision()
{
using var context = new TikuDbContext(Options);
AssertHasUniqueIndex<Banner>(nameof(Banner.TenantId), nameof(Banner.LegacyId));
AssertHasUniqueIndex<UserNotification>(
nameof(UserNotification.TenantId),
nameof(UserNotification.UserId),
nameof(UserNotification.NotificationType),
nameof(UserNotification.DedupeKey));
AssertHasUniqueIndex<Badge>(nameof(Badge.TenantId), nameof(Badge.LegacyId));
AssertHasUniqueIndex<UserBadge>(
nameof(UserBadge.TenantId),
nameof(UserBadge.UserId),
nameof(UserBadge.BadgeId));
AssertHasUniqueIndex<TenantThemeTemplate>(nameof(TenantThemeTemplate.Code));
AssertHasUniqueIndex<TenantThemeConfig>(nameof(TenantThemeConfig.TenantId));
var conditionValue = context.Model.FindEntityType(typeof(Badge))!
.FindProperty(nameof(Badge.ConditionValue))!;
Assert.Equal(18, conditionValue.GetPrecision());
Assert.Equal(4, conditionValue.GetScale());
void AssertHasUniqueIndex<TEntity>(params string[] propertyNames)
{
var indexes = context.Model.FindEntityType(typeof(TEntity))!.GetIndexes();
Assert.Contains(
indexes,
index => index.IsUnique
&& index.Properties
.Select(property => property.Name)
.SequenceEqual(propertyNames));
}
}
}