Refactored organisation entities and endpoints

This commit is contained in:
2026-10-04 10:11:28 +02:00
parent 8fed46961a
commit 5fb67df9ac
17 changed files with 2664 additions and 20 deletions
@@ -6,7 +6,7 @@ namespace PostFundManagement.Infrastructure.Database;
public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
ITenantContextService tenantService) : DbContext(options)
{
private bool BypassFilter => tenantService.IsAdmin || tenantService.ByPassTenantQueryFilter;
private bool BypassFilter => tenantService.ByPassTenantQueryFilter;
private long CurrentTenantId => tenantService.TenantId;
public DbSet<TenantUser> TenantUsers => Set<TenantUser>();
@@ -84,14 +84,14 @@ public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext>
}
}
modelBuilder.Entity<Organisation>().HasQueryFilter(o => BypassFilter == false &&
modelBuilder.Entity<Organisation>().HasQueryFilter(o => BypassFilter ||
Set<TenantOrganisation>().Any(to => to.TenantId == CurrentTenantId && to.OrganisationId == o.Id));
modelBuilder.Entity<TenantOrganisation>()
.HasQueryFilter(to => BypassFilter == false && to.TenantId == CurrentTenantId);
.HasQueryFilter(to => BypassFilter || to.TenantId == CurrentTenantId);
modelBuilder.Entity<TenantUser>()
.HasQueryFilter(to => BypassFilter == false && to.TenantId == CurrentTenantId);
.HasQueryFilter(to => BypassFilter || to.TenantId == CurrentTenantId);
}
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
@@ -0,0 +1,60 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PostFundManagement.Infrastructure.Database.Migrations
{
/// <inheritdoc />
public partial class AddedUserIdAndIdNumberTenantOrgAndUserObjects : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "IdNumber",
table: "Users",
type: "character varying(256)",
maxLength: 256,
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "UserId",
table: "Organisations",
type: "uuid",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Organisations_UserId",
table: "Organisations",
column: "UserId");
migrationBuilder.AddForeignKey(
name: "FK_Organisations_Users_UserId",
table: "Organisations",
column: "UserId",
principalTable: "Users",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Organisations_Users_UserId",
table: "Organisations");
migrationBuilder.DropIndex(
name: "IX_Organisations_UserId",
table: "Organisations");
migrationBuilder.DropColumn(
name: "IdNumber",
table: "Users");
migrationBuilder.DropColumn(
name: "UserId",
table: "Organisations");
}
}
}
@@ -1017,10 +1017,15 @@ namespace PostFundManagement.Infrastructure.Database.Migrations
.HasColumnType("integer")
.HasDefaultValueSql("5");
b.Property<Guid?>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("CreatedBy");
b.HasIndex("UserId");
b.ToTable("Organisations", (string)null);
});
@@ -1530,6 +1535,10 @@ namespace PostFundManagement.Infrastructure.Database.Migrations
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("IdNumber")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<DateTime?>("LastLoginAt")
.HasColumnType("timestamp with time zone");
@@ -2124,7 +2133,14 @@ namespace PostFundManagement.Infrastructure.Database.Migrations
.OnDelete(DeleteBehavior.NoAction)
.IsRequired();
b.HasOne("PostFundManagement.Domain.Entities.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.NoAction);
b.Navigation("Creator");
b.Navigation("User");
});
modelBuilder.Entity("PostFundManagement.Domain.Entities.Outcome", b =>
@@ -19,6 +19,9 @@ public sealed class TenantContextService : ITenantContextService
var user = httpContextAccessor.HttpContext?.User;
IsAdmin = user?.IsInRole("Admin") == true || user?.HasClaim("role", "Admin") == true;
ByPassTenantQueryFilter = user?.IsInRole("Partner") == true || user?.HasClaim("role", "Partner") == true;
ByPassTenantQueryFilter = ByPassTenantQueryFilter || IsAdmin;
HasTenantFilter = !IsAdmin;