From 8d201c5b5846e130c49272334d590c80e42a30be Mon Sep 17 00:00:00 2001 From: rckrdmrd Date: Tue, 20 Jan 2026 04:32:10 -0600 Subject: [PATCH] [TASK-2026-01-20-005] feat: Add multi-tenancy to UOM entities EPIC-P2-005: UOM Multi-tenancy Sync - Add tenant_id field to Uom entity - Add tenant_id field to UomCategory entity - Add Tenant relation to both entities - Add UpdateDateColumn for updated_at - Update indexes for tenant-based unique constraints Co-Authored-By: Claude Opus 4.5 --- .../core/entities/uom-category.entity.ts | 21 ++++++++++++++++--- src/modules/core/entities/uom.entity.ts | 17 +++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/modules/core/entities/uom-category.entity.ts b/src/modules/core/entities/uom-category.entity.ts index c115800..4bcd25a 100644 --- a/src/modules/core/entities/uom-category.entity.ts +++ b/src/modules/core/entities/uom-category.entity.ts @@ -3,28 +3,43 @@ import { PrimaryGeneratedColumn, Column, CreateDateColumn, + UpdateDateColumn, Index, OneToMany, + ManyToOne, + JoinColumn, } from 'typeorm'; import { Uom } from './uom.entity.js'; +import { Tenant } from '../../auth/entities/tenant.entity.js'; @Entity({ schema: 'core', name: 'uom_categories' }) -@Index('idx_uom_categories_name', ['name'], { unique: true }) +@Index('idx_uom_categories_tenant', ['tenantId']) +@Index('idx_uom_categories_tenant_name', ['tenantId', 'name'], { unique: true }) export class UomCategory { @PrimaryGeneratedColumn('uuid') id: string; - @Column({ type: 'varchar', length: 100, nullable: false, unique: true }) + @Column({ type: 'uuid', nullable: false, name: 'tenant_id' }) + tenantId: string; + + @Column({ type: 'varchar', length: 100, nullable: false }) name: string; @Column({ type: 'text', nullable: true }) description: string | null; // Relations + @ManyToOne(() => Tenant) + @JoinColumn({ name: 'tenant_id' }) + tenant: Tenant; + @OneToMany(() => Uom, (uom) => uom.category) uoms: Uom[]; // Audit fields - @CreateDateColumn({ name: 'created_at', type: 'timestamp' }) + @CreateDateColumn({ name: 'created_at', type: 'timestamptz' }) createdAt: Date; + + @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' }) + updatedAt: Date; } diff --git a/src/modules/core/entities/uom.entity.ts b/src/modules/core/entities/uom.entity.ts index 98ba8aa..3c70afa 100644 --- a/src/modules/core/entities/uom.entity.ts +++ b/src/modules/core/entities/uom.entity.ts @@ -3,11 +3,13 @@ import { PrimaryGeneratedColumn, Column, CreateDateColumn, + UpdateDateColumn, Index, ManyToOne, JoinColumn, } from 'typeorm'; import { UomCategory } from './uom-category.entity.js'; +import { Tenant } from '../../auth/entities/tenant.entity.js'; export enum UomType { REFERENCE = 'reference', @@ -16,14 +18,18 @@ export enum UomType { } @Entity({ schema: 'core', name: 'uom' }) +@Index('idx_uom_tenant', ['tenantId']) @Index('idx_uom_category_id', ['categoryId']) @Index('idx_uom_code', ['code']) @Index('idx_uom_active', ['active']) -@Index('idx_uom_name_category', ['categoryId', 'name'], { unique: true }) +@Index('idx_uom_tenant_category_name', ['tenantId', 'categoryId', 'name'], { unique: true }) export class Uom { @PrimaryGeneratedColumn('uuid') id: string; + @Column({ type: 'uuid', nullable: false, name: 'tenant_id' }) + tenantId: string; + @Column({ type: 'uuid', nullable: false, name: 'category_id' }) categoryId: string; @@ -64,6 +70,10 @@ export class Uom { active: boolean; // Relations + @ManyToOne(() => Tenant) + @JoinColumn({ name: 'tenant_id' }) + tenant: Tenant; + @ManyToOne(() => UomCategory, (category) => category.uoms, { nullable: false, }) @@ -71,6 +81,9 @@ export class Uom { category: UomCategory; // Audit fields - @CreateDateColumn({ name: 'created_at', type: 'timestamp' }) + @CreateDateColumn({ name: 'created_at', type: 'timestamptz' }) createdAt: Date; + + @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' }) + updatedAt: Date; }