Add contract and user contract entities with relationships; update JWT token claims

This commit is contained in:
masoodafar-web
2025-11-16 17:17:17 +03:30
parent 75df0c1df0
commit 79e5871899
7 changed files with 1353 additions and 1 deletions

View File

@@ -18,8 +18,11 @@ public class AdminGetJwtTokenQueryHandler : IRequestHandler<AdminGetJwtTokenQuer
CancellationToken cancellationToken)
{
var user = await _context.Users
.Include(u => u.UserContracts)
.ThenInclude(u => u.Contract)
.Include(u => u.UserRoles)
.ThenInclude(ur => ur.Role)
.FirstOrDefaultAsync(f => f.Mobile == request.Username, cancellationToken);
if (user == null)
throw new Exception("Invalid username or password.");

View File

@@ -13,6 +13,8 @@ public class GetJwtTokenQueryHandler : IRequestHandler<GetJwtTokenQuery, GetJwtT
public async Task<GetJwtTokenResponseDto> Handle(GetJwtTokenQuery request, CancellationToken cancellationToken)
{
var user = await _context.Users
.Include(u => u.UserContracts)
.ThenInclude(u => u.Contract)
.Include(u => u.UserRoles)
.ThenInclude(ur => ur.Role)
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(User), request.Id);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,97 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace CMSMicroservice.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class u07 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Contracts",
schema: "CMS",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(max)", nullable: false),
Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
HtmlContent = table.Column<string>(type: "nvarchar(max)", nullable: false),
Type = table.Column<int>(type: "int", nullable: false),
Created = table.Column<DateTime>(type: "datetime2", nullable: false),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
LastModified = table.Column<DateTime>(type: "datetime2", nullable: true),
LastModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Contracts", x => x.Id);
});
migrationBuilder.CreateTable(
name: "UserContracts",
schema: "CMS",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
UserId = table.Column<long>(type: "bigint", nullable: false),
ContractId = table.Column<long>(type: "bigint", nullable: false),
SignGuid = table.Column<string>(type: "nvarchar(max)", nullable: false),
SignedPdfFile = table.Column<string>(type: "nvarchar(max)", nullable: false),
Created = table.Column<DateTime>(type: "datetime2", nullable: false),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
LastModified = table.Column<DateTime>(type: "datetime2", nullable: true),
LastModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_UserContracts", x => x.Id);
table.ForeignKey(
name: "FK_UserContracts_Contracts_ContractId",
column: x => x.ContractId,
principalSchema: "CMS",
principalTable: "Contracts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_UserContracts_Users_UserId",
column: x => x.UserId,
principalSchema: "CMS",
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_UserContracts_ContractId",
schema: "CMS",
table: "UserContracts",
column: "ContractId");
migrationBuilder.CreateIndex(
name: "IX_UserContracts_UserId",
schema: "CMS",
table: "UserContracts",
column: "UserId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "UserContracts",
schema: "CMS");
migrationBuilder.DropTable(
name: "Contracts",
schema: "CMS");
}
}
}

View File

@@ -23,6 +23,49 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Contract", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
b.Property<DateTime>("Created")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("HtmlContent")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<DateTime?>("LastModified")
.HasColumnType("datetime2");
b.Property<string>("LastModifiedBy")
.HasColumnType("nvarchar(max)");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Type")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Contracts", "CMS");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.FactorDetails", b =>
{
b.Property<long>("Id")
@@ -620,6 +663,52 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.ToTable("UserCartss", "CMS");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.UserContract", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
b.Property<long>("ContractId")
.HasColumnType("bigint");
b.Property<DateTime>("Created")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<DateTime?>("LastModified")
.HasColumnType("datetime2");
b.Property<string>("LastModifiedBy")
.HasColumnType("nvarchar(max)");
b.Property<string>("SignGuid")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("SignedPdfFile")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<long>("UserId")
.HasColumnType("bigint");
b.HasKey("Id");
b.HasIndex("ContractId");
b.HasIndex("UserId");
b.ToTable("UserContracts", "CMS");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.UserOrder", b =>
{
b.Property<long>("Id")
@@ -880,6 +969,25 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.Navigation("User");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.UserContract", b =>
{
b.HasOne("CMSMicroservice.Domain.Entities.Contract", "Contract")
.WithMany("UserContracts")
.HasForeignKey("ContractId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("CMSMicroservice.Domain.Entities.User", "User")
.WithMany("UserContracts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Contract");
b.Navigation("User");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.UserOrder", b =>
{
b.HasOne("CMSMicroservice.Domain.Entities.Package", "Package")
@@ -948,6 +1056,11 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.Navigation("Wallet");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Contract", b =>
{
b.Navigation("UserContracts");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Package", b =>
{
b.Navigation("UserOrders");
@@ -978,6 +1091,8 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.Navigation("UserCartss");
b.Navigation("UserContracts");
b.Navigation("UserOrders");
b.Navigation("UserRoles");

View File

@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using CMSMicroservice.Domain.Enums;
namespace CMSMicroservice.Infrastructure.Services;
@@ -37,6 +38,10 @@ public class GenerateJwtTokenService : IGenerateJwtToken
if (!string.IsNullOrWhiteSpace(user.LastName))
claims.Add(new Claim("LastName", user.LastName));
if (user.UserContracts.Any(a => a.Contract.Type == ContractType.Main))
claims.Add(new Claim("IsSignMainContract", true.ToString()));
else
claims.Add(new Claim("IsSignMainContract", false.ToString()));
if (user.UserRoles != null && user.UserRoles.Any())
{

View File

@@ -3,7 +3,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>0.0.118</Version>
<Version>0.0.119</Version>
<DebugType>None</DebugType>
<DebugSymbols>False</DebugSymbols>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
@@ -36,6 +36,9 @@
<Protobuf Include="Protos\products.proto" ProtoRoot="Protos\" GrpcServices="Both" />
<Protobuf Include="Protos\productimages.proto" ProtoRoot="Protos\" GrpcServices="Both" />
<Protobuf Include="Protos\transactions.proto" ProtoRoot="Protos\" GrpcServices="Both" />
<!-- Added missing proto definitions so validators compile -->
<Protobuf Include="Protos\contract.proto" ProtoRoot="Protos\" GrpcServices="Both" />
<Protobuf Include="Protos\usercontract.proto" ProtoRoot="Protos\" GrpcServices="Both" />
</ItemGroup>
<Target Name="PushToFoursatNuget" AfterTargets="Pack">