-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddingBackendCode.txt
More file actions
1243 lines (1080 loc) · 49.8 KB
/
Copy pathaddingBackendCode.txt
File metadata and controls
1243 lines (1080 loc) · 49.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ============================================================================
// SUNSET ERP — SPRINT 11 BACKEND MODULES
// ============================================================================
// Folder structure to create:
//
// backend/src/modules/
// uom/
// dto/
// create-uom.dto.ts (not needed — read-only catalog)
// uom.module.ts
// uom.service.ts
// uom.controller.ts
//
// tenant-settings/
// dto/
// update-tenant-settings.dto.ts
// tenant-settings.module.ts
// tenant-settings.service.ts
// tenant-settings.controller.ts
//
// macro-categories/
// dto/
// create-macro-category.dto.ts
// update-macro-category.dto.ts
// macro-categories.module.ts
// macro-categories.service.ts
// macro-categories.controller.ts
//
// categories/
// dto/
// create-category.dto.ts
// update-category.dto.ts
// categories.module.ts
// categories.service.ts
// categories.controller.ts
//
// consumption-groups/
// dto/
// create-consumption-group.dto.ts
// update-consumption-group.dto.ts
// consumption-groups.module.ts
// consumption-groups.service.ts
// consumption-groups.controller.ts
//
// supplier-items/
// dto/
// create-supplier-item.dto.ts
// update-supplier-item.dto.ts
// supplier-items.module.ts
// supplier-items.service.ts
// supplier-items.controller.ts
// ============================================================================
// ============================================================================
// MODULE 1 — UOM (read-only catalog)
// Path: backend/src/modules/uom/
// Note: No create/update DTOs — catalog is managed via seed scripts only
// ============================================================================
// --- uom/uom.module.ts ---
import { Module } from '@nestjs/common';
import { UomService } from './uom.service';
import { UomController } from './uom.controller';
import { PrismaModule } from '../../database/prisma.module';
@Module({
imports: [PrismaModule],
controllers: [UomController],
providers: [UomService],
exports: [UomService],
})
export class UomModule {}
// --- uom/uom.service.ts ---
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../database/prisma.service';
@Injectable()
export class UomService {
constructor(private prisma: PrismaService) {}
async findAllUnits(filters?: { type?: string; system?: string }) {
const where: any = { isActive: true };
if (filters?.type) where.type = filters.type;
if (filters?.system) where.system = filters.system;
return this.prisma.uomUnit.findMany({
where,
orderBy: [{ type: 'asc' }, { system: 'asc' }, { isBase: 'desc' }, { code: 'asc' }],
});
}
async findOneUnit(id: string) {
const unit = await this.prisma.uomUnit.findUnique({ where: { id } });
if (!unit) throw new NotFoundException(`UOM unit ${id} not found`);
return unit;
}
async findUnitByCode(code: string) {
const unit = await this.prisma.uomUnit.findUnique({ where: { code } });
if (!unit) throw new NotFoundException(`UOM unit with code ${code} not found`);
return unit;
}
async findAllConversions() {
return this.prisma.uomConversion.findMany({
where: { isActive: true },
include: {
fromUom: { select: { code: true, name: true, type: true, system: true } },
toUom: { select: { code: true, name: true, type: true, system: true } },
},
orderBy: [{ fromUom: { type: 'asc' } }, { fromUom: { code: 'asc' } }],
});
}
async convert(fromCode: string, toCode: string, quantity: number): Promise<{
fromUom: string; toUom: string; inputQty: number;
outputQty: number; factor: number; isAutomatic: boolean;
}> {
if (fromCode === toCode) {
return { fromUom: fromCode, toUom: toCode, inputQty: quantity, outputQty: quantity, factor: 1, isAutomatic: true };
}
const from = await this.findUnitByCode(fromCode);
const to = await this.findUnitByCode(toCode);
const conversion = await this.prisma.uomConversion.findUnique({
where: { fromUomId_toUomId: { fromUomId: from.id, toUomId: to.id } },
});
if (!conversion) {
throw new NotFoundException(`No conversion found from ${fromCode} to ${toCode}. Manual factor required.`);
}
const factor = Number(conversion.factor);
const outputQty = Math.round(quantity * factor * 1_000_000) / 1_000_000;
return { fromUom: fromCode, toUom: toCode, inputQty: quantity, outputQty, factor, isAutomatic: true };
}
// Used internally by SupplierItemsService to auto-calculate conversion factors
async getConversionFactor(fromUomId: string, toUomId: string): Promise<number | null> {
if (fromUomId === toUomId) return 1;
const conversion = await this.prisma.uomConversion.findUnique({
where: { fromUomId_toUomId: { fromUomId, toUomId } },
});
return conversion ? Number(conversion.factor) : null;
}
}
// --- uom/uom.controller.ts ---
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiQuery, ApiParam } from '@nestjs/swagger';
import { UomService } from './uom.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { PermissionsGuard } from '../../common/guards/permissions.guard';
import { RequirePermissions } from '../../common/decorators/permissions.decorator';
@ApiTags('UOM')
@Controller('uom')
@UseGuards(JwtAuthGuard, PermissionsGuard)
@ApiBearerAuth('JWT-auth')
export class UomController {
constructor(private readonly uomService: UomService) {}
@Get('units')
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get all UOM units' })
@ApiQuery({ name: 'type', required: false, description: 'volume | mass | count | length | area | time' })
@ApiQuery({ name: 'system', required: false, description: 'metric | imperial | universal' })
async findAllUnits(@Query('type') type?: string, @Query('system') system?: string) {
return this.uomService.findAllUnits({ type, system });
}
@Get('units/:id')
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get UOM unit by ID' })
@ApiParam({ name: 'id', description: 'UOM unit UUID' })
async findOneUnit(@Param('id') id: string) {
return this.uomService.findOneUnit(id);
}
@Get('conversions')
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get all UOM conversion factors' })
async findAllConversions() {
return this.uomService.findAllConversions();
}
@Get('convert')
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Convert a quantity between two UOM units' })
@ApiQuery({ name: 'from', required: true, description: 'Source UOM code e.g. GAL' })
@ApiQuery({ name: 'to', required: true, description: 'Target UOM code e.g. LTR' })
@ApiQuery({ name: 'qty', required: true, description: 'Quantity to convert' })
async convert(@Query('from') from: string, @Query('to') to: string, @Query('qty') qty: string) {
return this.uomService.convert(from, to, Number(qty));
}
}
// ============================================================================
// MODULE 2 — TENANT SETTINGS
// Path: backend/src/modules/tenant-settings/
// ============================================================================
// --- tenant-settings/dto/update-tenant-settings.dto.ts ---
import { IsOptional, IsString, IsUUID } from 'class-validator';
import { ApiPropertyOptional } from '@nestjs/swagger';
export class UpdateTenantSettingsDto {
@ApiPropertyOptional({ example: 'metric', description: 'metric | imperial' })
@IsOptional()
@IsString()
defaultUomSystem?: string;
@ApiPropertyOptional({ description: 'Volume base UOM ID (LTR or GAL)' })
@IsOptional()
@IsUUID()
volumeBaseUomId?: string;
@ApiPropertyOptional({ description: 'Mass base UOM ID (KG or LB)' })
@IsOptional()
@IsUUID()
massBaseUomId?: string;
@ApiPropertyOptional({ description: 'Length base UOM ID (M or FT)' })
@IsOptional()
@IsUUID()
lengthBaseUomId?: string;
@ApiPropertyOptional({ description: 'Area base UOM ID (M2 or FT2)' })
@IsOptional()
@IsUUID()
areaBaseUomId?: string;
}
// --- tenant-settings/tenant-settings.module.ts ---
import { Module } from '@nestjs/common';
import { TenantSettingsService } from './tenant-settings.service';
import { TenantSettingsController } from './tenant-settings.controller';
import { PrismaModule } from '../../database/prisma.module';
@Module({
imports: [PrismaModule],
controllers: [TenantSettingsController],
providers: [TenantSettingsService],
exports: [TenantSettingsService],
})
export class TenantSettingsModule {}
// --- tenant-settings/tenant-settings.service.ts ---
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../database/prisma.service';
import { UpdateTenantSettingsDto } from './dto/update-tenant-settings.dto';
const INCLUDE = {
volumeBaseUom: true,
massBaseUom: true,
lengthBaseUom: true,
areaBaseUom: true,
};
@Injectable()
export class TenantSettingsService {
constructor(private prisma: PrismaService) {}
async getOrCreate(tenantId: string) {
const existing = await this.prisma.tenantSettings.findUnique({ where: { tenantId }, include: INCLUDE });
if (existing) return existing;
return this.prisma.tenantSettings.create({ data: { tenantId, defaultUomSystem: 'metric' }, include: INCLUDE });
}
async update(tenantId: string, userId: string, dto: UpdateTenantSettingsDto) {
await this.getOrCreate(tenantId);
return this.prisma.tenantSettings.update({
where: { tenantId },
data: { ...dto, updatedBy: userId },
include: INCLUDE,
});
}
}
// --- tenant-settings/tenant-settings.controller.ts ---
import { Controller, Get, Patch, Body, UseGuards, Request } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiResponse } from '@nestjs/swagger';
import { TenantSettingsService } from './tenant-settings.service';
import { UpdateTenantSettingsDto } from './dto/update-tenant-settings.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { PermissionsGuard } from '../../common/guards/permissions.guard';
import { RequirePermissions } from '../../common/decorators/permissions.decorator';
@ApiTags('Tenant Settings')
@Controller('tenant-settings')
@UseGuards(JwtAuthGuard, PermissionsGuard)
@ApiBearerAuth('JWT-auth')
export class TenantSettingsController {
constructor(private readonly tenantSettingsService: TenantSettingsService) {}
@Get()
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get tenant UOM and configuration settings' })
@ApiResponse({ status: 200, description: 'Tenant settings with UOM references' })
async get(@Request() req) {
return this.tenantSettingsService.getOrCreate(req.user.tenantId);
}
@Patch()
@RequirePermissions('INVENTORY:EDIT')
@ApiOperation({ summary: 'Update tenant UOM and configuration settings' })
@ApiResponse({ status: 200, description: 'Settings updated' })
@ApiResponse({ status: 403, description: 'Forbidden - missing permission' })
async update(@Request() req, @Body() dto: UpdateTenantSettingsDto) {
return this.tenantSettingsService.update(req.user.tenantId, req.user.id, dto);
}
}
// ============================================================================
// MODULE 3 — MACRO CATEGORIES
// Path: backend/src/modules/macro-categories/
// ============================================================================
// --- macro-categories/dto/create-macro-category.dto.ts ---
import { IsString, IsOptional, IsBoolean, MaxLength } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
export class CreateMacroCategoryDto {
@ApiProperty({ example: 'WOOD', description: 'Unique macro category code' })
@IsString()
@MaxLength(50)
code: string;
@ApiProperty({ example: 'Wood & Panels', description: 'Macro category name' })
@IsString()
@MaxLength(255)
name: string;
@ApiPropertyOptional({ example: 'Wood-based raw materials and finished goods' })
@IsOptional()
@IsString()
description?: string;
@ApiPropertyOptional({ default: true })
@IsOptional()
@IsBoolean()
isActive?: boolean;
}
// --- macro-categories/dto/update-macro-category.dto.ts ---
import { PartialType } from '@nestjs/swagger';
import { CreateMacroCategoryDto } from './create-macro-category.dto';
export class UpdateMacroCategoryDto extends PartialType(CreateMacroCategoryDto) {}
// --- macro-categories/macro-categories.module.ts ---
import { Module } from '@nestjs/common';
import { MacroCategoriesService } from './macro-categories.service';
import { MacroCategoriesController } from './macro-categories.controller';
import { PrismaModule } from '../../database/prisma.module';
@Module({
imports: [PrismaModule],
controllers: [MacroCategoriesController],
providers: [MacroCategoriesService],
exports: [MacroCategoriesService],
})
export class MacroCategoriesModule {}
// --- macro-categories/macro-categories.service.ts ---
import { Injectable, NotFoundException, ConflictException, BadRequestException } from '@nestjs/common';
import { PrismaService } from '../../database/prisma.service';
import { CreateMacroCategoryDto } from './dto/create-macro-category.dto';
import { UpdateMacroCategoryDto } from './dto/update-macro-category.dto';
@Injectable()
export class MacroCategoriesService {
constructor(private prisma: PrismaService) {}
async create(tenantId: string, userId: string, dto: CreateMacroCategoryDto) {
const existing = await this.prisma.macroCategory.findFirst({ where: { tenantId, code: dto.code, deletedAt: null } });
if (existing) throw new ConflictException(`MacroCategory code ${dto.code} already exists`);
return this.prisma.macroCategory.create({
data: { tenantId, ...dto, isActive: dto.isActive ?? true, createdBy: userId, updatedBy: userId },
include: { _count: { select: { categories: true } } },
});
}
async findAll(tenantId: string) {
return this.prisma.macroCategory.findMany({
where: { tenantId, deletedAt: null },
include: { _count: { select: { categories: true } } },
orderBy: { code: 'asc' },
});
}
async findOne(tenantId: string, id: string) {
const mc = await this.prisma.macroCategory.findFirst({
where: { id, tenantId, deletedAt: null },
include: { categories: { where: { deletedAt: null }, orderBy: { code: 'asc' }, include: { _count: { select: { items: true } } } } },
});
if (!mc) throw new NotFoundException(`MacroCategory ${id} not found`);
return mc;
}
async update(tenantId: string, userId: string, id: string, dto: UpdateMacroCategoryDto) {
await this.findOne(tenantId, id);
if (dto.code) {
const conflict = await this.prisma.macroCategory.findFirst({ where: { tenantId, code: dto.code, id: { not: id }, deletedAt: null } });
if (conflict) throw new ConflictException(`MacroCategory code ${dto.code} already exists`);
}
return this.prisma.macroCategory.update({
where: { id },
data: { ...dto, updatedBy: userId },
include: { _count: { select: { categories: true } } },
});
}
async remove(tenantId: string, userId: string, id: string) {
await this.findOne(tenantId, id);
const catCount = await this.prisma.category.count({ where: { macroCategoryId: id, deletedAt: null } });
if (catCount > 0) throw new BadRequestException(`Cannot delete: ${catCount} categories still assigned to this macro category`);
await this.prisma.macroCategory.update({ where: { id }, data: { deletedAt: new Date(), deletedBy: userId } });
return { message: 'Macro category deleted successfully', id };
}
}
// --- macro-categories/macro-categories.controller.ts ---
import { Controller, Get, Post, Patch, Delete, Body, Param, UseGuards, Request, HttpCode, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiResponse } from '@nestjs/swagger';
import { MacroCategoriesService } from './macro-categories.service';
import { CreateMacroCategoryDto } from './dto/create-macro-category.dto';
import { UpdateMacroCategoryDto } from './dto/update-macro-category.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { PermissionsGuard } from '../../common/guards/permissions.guard';
import { RequirePermissions } from '../../common/decorators/permissions.decorator';
@ApiTags('Macro Categories')
@Controller('macro-categories')
@UseGuards(JwtAuthGuard, PermissionsGuard)
@ApiBearerAuth('JWT-auth')
export class MacroCategoriesController {
constructor(private readonly macroCategoriesService: MacroCategoriesService) {}
@Post()
@RequirePermissions('INVENTORY:CREATE')
@ApiOperation({ summary: 'Create a new macro category' })
@ApiResponse({ status: 201, description: 'Macro category created successfully' })
@ApiResponse({ status: 409, description: 'Macro category code already exists' })
async create(@Request() req, @Body() dto: CreateMacroCategoryDto) {
return this.macroCategoriesService.create(req.user.tenantId, req.user.id, dto);
}
@Get()
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get all macro categories' })
@ApiResponse({ status: 200, description: 'List of macro categories with category counts' })
async findAll(@Request() req) {
return this.macroCategoriesService.findAll(req.user.tenantId);
}
@Get(':id')
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get macro category with its child categories' })
@ApiParam({ name: 'id', description: 'MacroCategory UUID' })
@ApiResponse({ status: 200, description: 'Macro category with child categories' })
@ApiResponse({ status: 404, description: 'Macro category not found' })
async findOne(@Request() req, @Param('id') id: string) {
return this.macroCategoriesService.findOne(req.user.tenantId, id);
}
@Patch(':id')
@RequirePermissions('INVENTORY:EDIT')
@ApiOperation({ summary: 'Update macro category' })
@ApiParam({ name: 'id', description: 'MacroCategory UUID' })
@ApiResponse({ status: 200, description: 'Macro category updated successfully' })
@ApiResponse({ status: 404, description: 'Macro category not found' })
@ApiResponse({ status: 409, description: 'Code already exists' })
async update(@Request() req, @Param('id') id: string, @Body() dto: UpdateMacroCategoryDto) {
return this.macroCategoriesService.update(req.user.tenantId, req.user.id, id, dto);
}
@Delete(':id')
@RequirePermissions('INVENTORY:DELETE')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Delete macro category (soft delete — fails if has categories)' })
@ApiParam({ name: 'id', description: 'MacroCategory UUID' })
@ApiResponse({ status: 200, description: 'Macro category deleted successfully' })
@ApiResponse({ status: 400, description: 'Cannot delete — has child categories' })
@ApiResponse({ status: 404, description: 'Macro category not found' })
async remove(@Request() req, @Param('id') id: string) {
return this.macroCategoriesService.remove(req.user.tenantId, req.user.id, id);
}
}
// ============================================================================
// MODULE 4 — CATEGORIES
// Path: backend/src/modules/categories/
// ============================================================================
// --- categories/dto/create-category.dto.ts ---
import { IsString, IsOptional, IsBoolean, IsUUID, MaxLength } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
export class CreateCategoryDto {
@ApiProperty({ description: 'Parent macro category ID' })
@IsUUID()
macroCategoryId: string;
@ApiProperty({ example: 'FG-FURNITURE', description: 'Unique category code' })
@IsString()
@MaxLength(50)
code: string;
@ApiProperty({ example: 'Finished Furniture', description: 'Category name' })
@IsString()
@MaxLength(255)
name: string;
@ApiPropertyOptional({ example: 'Finished goods ready for sale' })
@IsOptional()
@IsString()
description?: string;
@ApiPropertyOptional({ description: 'GL account ID for inventory (DR on receipt)' })
@IsOptional()
@IsUUID()
inventoryAccountId?: string;
@ApiPropertyOptional({ description: 'GL account ID for COGS (DR on shipment)' })
@IsOptional()
@IsUUID()
cogsAccountId?: string;
@ApiPropertyOptional({ default: true })
@IsOptional()
@IsBoolean()
isActive?: boolean;
}
// --- categories/dto/update-category.dto.ts ---
import { PartialType } from '@nestjs/swagger';
import { CreateCategoryDto } from './create-category.dto';
export class UpdateCategoryDto extends PartialType(CreateCategoryDto) {}
// --- categories/categories.module.ts ---
import { Module } from '@nestjs/common';
import { CategoriesService } from './categories.service';
import { CategoriesController } from './categories.controller';
import { PrismaModule } from '../../database/prisma.module';
@Module({
imports: [PrismaModule],
controllers: [CategoriesController],
providers: [CategoriesService],
exports: [CategoriesService],
})
export class CategoriesModule {}
// --- categories/categories.service.ts ---
import { Injectable, NotFoundException, ConflictException, BadRequestException } from '@nestjs/common';
import { PrismaService } from '../../database/prisma.service';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
const INCLUDE = {
macroCategory: { select: { id: true, code: true, name: true } },
inventoryAccount: { select: { accountNumber: true, name: true } },
cogsAccount: { select: { accountNumber: true, name: true } },
_count: { select: { items: true } },
};
@Injectable()
export class CategoriesService {
constructor(private prisma: PrismaService) {}
async create(tenantId: string, userId: string, dto: CreateCategoryDto) {
const existing = await this.prisma.category.findFirst({ where: { tenantId, code: dto.code, deletedAt: null } });
if (existing) throw new ConflictException(`Category code ${dto.code} already exists`);
const mc = await this.prisma.macroCategory.findFirst({ where: { id: dto.macroCategoryId, tenantId, deletedAt: null } });
if (!mc) throw new NotFoundException(`MacroCategory ${dto.macroCategoryId} not found`);
return this.prisma.category.create({
data: { tenantId, ...dto, isActive: dto.isActive ?? true, createdBy: userId, updatedBy: userId },
include: INCLUDE,
});
}
async findAll(tenantId: string, macroCategoryId?: string) {
const where: any = { tenantId, deletedAt: null };
if (macroCategoryId) where.macroCategoryId = macroCategoryId;
return this.prisma.category.findMany({
where,
include: INCLUDE,
orderBy: [{ macroCategory: { code: 'asc' } }, { code: 'asc' }],
});
}
async findOne(tenantId: string, id: string) {
const cat = await this.prisma.category.findFirst({
where: { id, tenantId, deletedAt: null },
include: { macroCategory: true, inventoryAccount: true, cogsAccount: true, _count: { select: { items: true } } },
});
if (!cat) throw new NotFoundException(`Category ${id} not found`);
return cat;
}
async update(tenantId: string, userId: string, id: string, dto: UpdateCategoryDto) {
await this.findOne(tenantId, id);
if (dto.code) {
const conflict = await this.prisma.category.findFirst({ where: { tenantId, code: dto.code, id: { not: id }, deletedAt: null } });
if (conflict) throw new ConflictException(`Category code ${dto.code} already exists`);
}
return this.prisma.category.update({ where: { id }, data: { ...dto, updatedBy: userId }, include: INCLUDE });
}
async remove(tenantId: string, userId: string, id: string) {
await this.findOne(tenantId, id);
const itemCount = await this.prisma.item.count({ where: { categoryId: id, deletedAt: null } });
if (itemCount > 0) throw new BadRequestException(`Cannot delete: ${itemCount} items still assigned to this category`);
await this.prisma.category.update({ where: { id }, data: { deletedAt: new Date(), deletedBy: userId } });
return { message: 'Category deleted successfully', id };
}
}
// --- categories/categories.controller.ts ---
import { Controller, Get, Post, Patch, Delete, Body, Param, Query, UseGuards, Request, HttpCode, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiQuery, ApiResponse } from '@nestjs/swagger';
import { CategoriesService } from './categories.service';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { PermissionsGuard } from '../../common/guards/permissions.guard';
import { RequirePermissions } from '../../common/decorators/permissions.decorator';
@ApiTags('Categories')
@Controller('categories')
@UseGuards(JwtAuthGuard, PermissionsGuard)
@ApiBearerAuth('JWT-auth')
export class CategoriesController {
constructor(private readonly categoriesService: CategoriesService) {}
@Post()
@RequirePermissions('INVENTORY:CREATE')
@ApiOperation({ summary: 'Create category (must belong to a MacroCategory)' })
@ApiResponse({ status: 201, description: 'Category created successfully' })
@ApiResponse({ status: 404, description: 'MacroCategory not found' })
@ApiResponse({ status: 409, description: 'Category code already exists' })
async create(@Request() req, @Body() dto: CreateCategoryDto) {
return this.categoriesService.create(req.user.tenantId, req.user.id, dto);
}
@Get()
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get all categories' })
@ApiQuery({ name: 'macroCategoryId', required: false, description: 'Filter by macro category' })
@ApiResponse({ status: 200, description: 'List of categories with GL accounts and item counts' })
async findAll(@Request() req, @Query('macroCategoryId') macroCategoryId?: string) {
return this.categoriesService.findAll(req.user.tenantId, macroCategoryId);
}
@Get(':id')
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get category by ID' })
@ApiParam({ name: 'id', description: 'Category UUID' })
@ApiResponse({ status: 200, description: 'Category details' })
@ApiResponse({ status: 404, description: 'Category not found' })
async findOne(@Request() req, @Param('id') id: string) {
return this.categoriesService.findOne(req.user.tenantId, id);
}
@Patch(':id')
@RequirePermissions('INVENTORY:EDIT')
@ApiOperation({ summary: 'Update category' })
@ApiParam({ name: 'id', description: 'Category UUID' })
@ApiResponse({ status: 200, description: 'Category updated successfully' })
@ApiResponse({ status: 404, description: 'Category not found' })
@ApiResponse({ status: 409, description: 'Code already exists' })
async update(@Request() req, @Param('id') id: string, @Body() dto: UpdateCategoryDto) {
return this.categoriesService.update(req.user.tenantId, req.user.id, id, dto);
}
@Delete(':id')
@RequirePermissions('INVENTORY:DELETE')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Delete category (soft delete — fails if has items)' })
@ApiParam({ name: 'id', description: 'Category UUID' })
@ApiResponse({ status: 200, description: 'Category deleted successfully' })
@ApiResponse({ status: 400, description: 'Cannot delete — has items assigned' })
@ApiResponse({ status: 404, description: 'Category not found' })
async remove(@Request() req, @Param('id') id: string) {
return this.categoriesService.remove(req.user.tenantId, req.user.id, id);
}
}
// ============================================================================
// MODULE 5 — CONSUMPTION GROUPS
// Path: backend/src/modules/consumption-groups/
// ============================================================================
// --- consumption-groups/dto/create-consumption-group.dto.ts ---
import { IsString, IsOptional, IsBoolean, IsUUID, MaxLength } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
export class CreateConsumptionGroupDto {
@ApiProperty({ example: 'ADH-INDUSTRIAL', description: 'Unique consumption group code' })
@IsString()
@MaxLength(50)
code: string;
@ApiProperty({ example: 'Industrial Adhesives', description: 'Group name' })
@IsString()
@MaxLength(255)
name: string;
@ApiPropertyOptional({ example: 'All adhesive materials expressed in LTR for production planning' })
@IsOptional()
@IsString()
description?: string;
@ApiProperty({ description: 'UOM ID that production uses — all items in group share this unit' })
@IsUUID()
consumptionUomId: string;
@ApiPropertyOptional({ default: true })
@IsOptional()
@IsBoolean()
isActive?: boolean;
}
// --- consumption-groups/dto/update-consumption-group.dto.ts ---
import { PartialType } from '@nestjs/swagger';
import { CreateConsumptionGroupDto } from './create-consumption-group.dto';
export class UpdateConsumptionGroupDto extends PartialType(CreateConsumptionGroupDto) {}
// --- consumption-groups/consumption-groups.module.ts ---
import { Module } from '@nestjs/common';
import { ConsumptionGroupsService } from './consumption-groups.service';
import { ConsumptionGroupsController } from './consumption-groups.controller';
import { PrismaModule } from '../../database/prisma.module';
@Module({
imports: [PrismaModule],
controllers: [ConsumptionGroupsController],
providers: [ConsumptionGroupsService],
exports: [ConsumptionGroupsService],
})
export class ConsumptionGroupsModule {}
// --- consumption-groups/consumption-groups.service.ts ---
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
import { PrismaService } from '../../database/prisma.service';
import { CreateConsumptionGroupDto } from './dto/create-consumption-group.dto';
import { UpdateConsumptionGroupDto } from './dto/update-consumption-group.dto';
@Injectable()
export class ConsumptionGroupsService {
constructor(private prisma: PrismaService) {}
async create(tenantId: string, userId: string, dto: CreateConsumptionGroupDto) {
const existing = await this.prisma.consumptionGroup.findFirst({ where: { tenantId, code: dto.code, deletedAt: null } });
if (existing) throw new ConflictException(`ConsumptionGroup code ${dto.code} already exists`);
return this.prisma.consumptionGroup.create({
data: { tenantId, ...dto, isActive: dto.isActive ?? true, createdBy: userId, updatedBy: userId },
include: { consumptionUom: true, _count: { select: { items: true } } },
});
}
async findAll(tenantId: string) {
return this.prisma.consumptionGroup.findMany({
where: { tenantId, deletedAt: null },
include: { consumptionUom: true, _count: { select: { items: true } } },
orderBy: { code: 'asc' },
});
}
async findOne(tenantId: string, id: string) {
const cg = await this.prisma.consumptionGroup.findFirst({
where: { id, tenantId, deletedAt: null },
include: {
consumptionUom: true,
items: {
where: { deletedAt: null },
select: {
id: true, code: true, name: true, baseUom: true,
purchaseToConsumptionFactor: true, storageToConsumptionFactor: true,
stock: { select: { onHandQuantity: true } },
},
},
},
});
if (!cg) throw new NotFoundException(`ConsumptionGroup ${id} not found`);
// Aggregate total qty in consumptionUom across all items in the group
const totalConsumptionQty = cg.items.reduce((sum, item) => {
const onHand = item.stock.reduce((s, st) => s + Number(st.onHandQuantity), 0);
return sum + onHand * Number(item.purchaseToConsumptionFactor);
}, 0);
return { ...cg, totalConsumptionQty: Math.round(totalConsumptionQty * 1000) / 1000 };
}
async update(tenantId: string, userId: string, id: string, dto: UpdateConsumptionGroupDto) {
await this.findOne(tenantId, id);
return this.prisma.consumptionGroup.update({
where: { id },
data: { ...dto, updatedBy: userId },
include: { consumptionUom: true, _count: { select: { items: true } } },
});
}
async remove(tenantId: string, userId: string, id: string) {
await this.findOne(tenantId, id);
await this.prisma.consumptionGroup.update({ where: { id }, data: { deletedAt: new Date(), deletedBy: userId } });
return { message: 'Consumption group deleted successfully', id };
}
}
// --- consumption-groups/consumption-groups.controller.ts ---
import { Controller, Get, Post, Patch, Delete, Body, Param, UseGuards, Request, HttpCode, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiResponse } from '@nestjs/swagger';
import { ConsumptionGroupsService } from './consumption-groups.service';
import { CreateConsumptionGroupDto } from './dto/create-consumption-group.dto';
import { UpdateConsumptionGroupDto } from './dto/update-consumption-group.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { PermissionsGuard } from '../../common/guards/permissions.guard';
import { RequirePermissions } from '../../common/decorators/permissions.decorator';
@ApiTags('Consumption Groups')
@Controller('consumption-groups')
@UseGuards(JwtAuthGuard, PermissionsGuard)
@ApiBearerAuth('JWT-auth')
export class ConsumptionGroupsController {
constructor(private readonly consumptionGroupsService: ConsumptionGroupsService) {}
@Post()
@RequirePermissions('INVENTORY:CREATE')
@ApiOperation({ summary: 'Create consumption group' })
@ApiResponse({ status: 201, description: 'Consumption group created successfully' })
@ApiResponse({ status: 409, description: 'Code already exists' })
async create(@Request() req, @Body() dto: CreateConsumptionGroupDto) {
return this.consumptionGroupsService.create(req.user.tenantId, req.user.id, dto);
}
@Get()
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get all consumption groups' })
@ApiResponse({ status: 200, description: 'List of consumption groups with UOM and item counts' })
async findAll(@Request() req) {
return this.consumptionGroupsService.findAll(req.user.tenantId);
}
@Get(':id')
@RequirePermissions('INVENTORY:VIEW')
@ApiOperation({ summary: 'Get consumption group with items and total ATP in consumption UOM' })
@ApiParam({ name: 'id', description: 'ConsumptionGroup UUID' })
@ApiResponse({ status: 200, description: 'Consumption group with aggregated stock qty' })
@ApiResponse({ status: 404, description: 'Consumption group not found' })
async findOne(@Request() req, @Param('id') id: string) {
return this.consumptionGroupsService.findOne(req.user.tenantId, id);
}
@Patch(':id')
@RequirePermissions('INVENTORY:EDIT')
@ApiOperation({ summary: 'Update consumption group' })
@ApiParam({ name: 'id', description: 'ConsumptionGroup UUID' })
@ApiResponse({ status: 200, description: 'Consumption group updated successfully' })
@ApiResponse({ status: 404, description: 'Consumption group not found' })
async update(@Request() req, @Param('id') id: string, @Body() dto: UpdateConsumptionGroupDto) {
return this.consumptionGroupsService.update(req.user.tenantId, req.user.id, id, dto);
}
@Delete(':id')
@RequirePermissions('INVENTORY:DELETE')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Delete consumption group (soft delete)' })
@ApiParam({ name: 'id', description: 'ConsumptionGroup UUID' })
@ApiResponse({ status: 200, description: 'Consumption group deleted successfully' })
@ApiResponse({ status: 404, description: 'Consumption group not found' })
async remove(@Request() req, @Param('id') id: string) {
return this.consumptionGroupsService.remove(req.user.tenantId, req.user.id, id);
}
}
// ============================================================================
// MODULE 6 — SUPPLIER ITEMS
// Path: backend/src/modules/supplier-items/
// ============================================================================
// --- supplier-items/dto/create-supplier-item.dto.ts ---
import { IsString, IsOptional, IsBoolean, IsUUID, IsNumber, IsInt, Min, MaxLength } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
export class CreateSupplierItemDto {
@ApiProperty({ description: 'Supplier ID' })
@IsUUID()
supplierId: string;
@ApiProperty({ description: 'Item ID' })
@IsUUID()
itemId: string;
@ApiPropertyOptional({ example: 'LOC-GAL-001', description: "Supplier's own code for this item" })
@IsOptional()
@IsString()
@MaxLength(100)
supplierItemCode?: string;
@ApiPropertyOptional({ example: 'Loctite Adhesive 1 Gallon', description: "Supplier's item description" })
@IsOptional()
@IsString()
@MaxLength(255)
supplierItemName?: string;
@ApiProperty({ description: 'Purchase UOM ID (from cfg_uom_units)' })
@IsUUID()
purchaseUomId: string;
@ApiPropertyOptional({ example: 1, description: 'Pack size in purchase UOM units', default: 1 })
@IsOptional()
@IsNumber()
@Min(0)
packSize?: number;
@ApiPropertyOptional({
example: 3.78541,
description: 'Conversion factor: how many consumptionUom per 1 purchaseUom. Auto-calculated from catalog if omitted.',
})
@IsOptional()
@IsNumber()
@Min(0)
conversionFactor?: number;
@ApiPropertyOptional({ example: 45.99, description: 'Last known price per purchase UOM' })
@IsOptional()
@IsNumber()
@Min(0)
lastPrice?: number;
@ApiPropertyOptional({ example: 7, description: 'Lead time in days', default: 0 })
@IsOptional()
@IsInt()
@Min(0)
leadTimeDays?: number;
@ApiPropertyOptional({ example: 1, description: 'Minimum order quantity in purchase UOM', default: 1 })
@IsOptional()
@IsNumber()
@Min(0)
moq?: number;
@ApiPropertyOptional({ default: false, description: 'Mark as preferred supplier for this item' })
@IsOptional()
@IsBoolean()
isPreferred?: boolean;
@ApiPropertyOptional({ default: true })
@IsOptional()
@IsBoolean()
isActive?: boolean;
@ApiPropertyOptional()
@IsOptional()
@IsString()
notes?: string;
}
// --- supplier-items/dto/update-supplier-item.dto.ts ---
import { PartialType, OmitType } from '@nestjs/swagger';
import { CreateSupplierItemDto } from './create-supplier-item.dto';
// supplierId and itemId cannot be changed after creation
export class UpdateSupplierItemDto extends PartialType(
OmitType(CreateSupplierItemDto, ['supplierId', 'itemId'] as const),
) {}
// --- supplier-items/supplier-items.module.ts ---
import { Module } from '@nestjs/common';
import { SupplierItemsService } from './supplier-items.service';
import { SupplierItemsController } from './supplier-items.controller';
import { PrismaModule } from '../../database/prisma.module';
import { UomModule } from '../uom/uom.module';
@Module({
imports: [PrismaModule, UomModule],
controllers: [SupplierItemsController],
providers: [SupplierItemsService],
exports: [SupplierItemsService],
})
export class SupplierItemsModule {}
// --- supplier-items/supplier-items.service.ts ---
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
import { PrismaService } from '../../database/prisma.service';
import { UomService } from '../uom/uom.service';
import { CreateSupplierItemDto } from './dto/create-supplier-item.dto';
import { UpdateSupplierItemDto } from './dto/update-supplier-item.dto';
const INCLUDE = {
supplier: { select: { id: true, code: true, name: true } },
item: { select: { id: true, code: true, name: true, consumptionUomId: true, baseUom: true } },
purchaseUom: { select: { id: true, code: true, name: true, type: true, system: true } },
};
@Injectable()
export class SupplierItemsService {
constructor(