rez_core 1.0.79 → 1.0.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module/meta/service/entity-service-impl.service.d.ts +3 -1
- package/dist/module/meta/service/entity-service-impl.service.js +12 -0
- package/dist/module/meta/service/entity-service-impl.service.js.map +1 -1
- package/dist/module/meta/service/entity-validation.service.d.ts +5 -3
- package/dist/module/meta/service/entity-validation.service.js +21 -16
- package/dist/module/meta/service/entity-validation.service.js.map +1 -1
- package/dist/module/meta/service/media-data.service.d.ts +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/module/meta/service/entity-service-impl.service.ts +15 -1
- package/src/module/meta/service/entity-validation.service.ts +23 -18
package/package.json
CHANGED
|
@@ -16,6 +16,7 @@ import { ExcelsheetData } from '../../../utils/dto/excelsheet-data.dto';
|
|
|
16
16
|
import { EntityTableService } from './entity-table.service';
|
|
17
17
|
import { EntityTableColumnService } from './entity-table-column.service';
|
|
18
18
|
import { ENTITYTYPE_ENTITYMASTER } from '../../../constant/global.constant';
|
|
19
|
+
import { EntityValidationService } from './entity-validation.service';
|
|
19
20
|
|
|
20
21
|
@Injectable()
|
|
21
22
|
export class EntityServiceImpl implements EntityService<BaseEntity> {
|
|
@@ -26,12 +27,15 @@ export class EntityServiceImpl implements EntityService<BaseEntity> {
|
|
|
26
27
|
@Inject() protected readonly reflectionHelper: ReflectionHelper;
|
|
27
28
|
@Inject() protected readonly entityListService: EntityListService;
|
|
28
29
|
@Inject() protected readonly loggingService: LoggingService;
|
|
30
|
+
@Inject()
|
|
31
|
+
protected readonly entityValidationService: EntityValidationService;
|
|
32
|
+
|
|
29
33
|
|
|
30
34
|
async createEntity(
|
|
31
35
|
entityData: BaseEntity,
|
|
32
36
|
loggedInUser: UserData | null,
|
|
33
37
|
manager?: EntityManager,
|
|
34
|
-
)
|
|
38
|
+
){
|
|
35
39
|
if (!entityData.entity_type) {
|
|
36
40
|
throw new BadRequestException(`EntityType is missing`);
|
|
37
41
|
}
|
|
@@ -39,6 +43,16 @@ export class EntityServiceImpl implements EntityService<BaseEntity> {
|
|
|
39
43
|
entityData.entity_type,
|
|
40
44
|
);
|
|
41
45
|
|
|
46
|
+
const validationErrors = await this.entityValidationService.validateEntityData(entityData,entityMaster);
|
|
47
|
+
if (validationErrors.length > 0) {
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
success: false,
|
|
51
|
+
errors: validationErrors,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
42
56
|
const repo = manager
|
|
43
57
|
? manager.getRepository(entityMaster.entity_data_class) // <-- Use transaction-safe repo
|
|
44
58
|
: this.reflectionHelper.getRepoService(entityMaster.entity_data_class);
|
|
@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
|
|
|
2
2
|
import { AttributeMasterService } from 'src/module/meta/service/attribute-master.service';
|
|
3
3
|
import { AttributeMaster } from '../entity/attribute-master.entity';
|
|
4
4
|
import { ReflectionHelper } from 'src/utils/service/reflection-helper.service';
|
|
5
|
+
import { DataSource } from 'typeorm';
|
|
5
6
|
|
|
6
7
|
interface ValidationError {
|
|
7
8
|
field: string;
|
|
@@ -12,7 +13,8 @@ interface ValidationError {
|
|
|
12
13
|
export class EntityValidationService {
|
|
13
14
|
constructor(
|
|
14
15
|
private readonly attributeMasterService: AttributeMasterService,
|
|
15
|
-
private readonly reflectionHelper: ReflectionHelper
|
|
16
|
+
private readonly reflectionHelper: ReflectionHelper,
|
|
17
|
+
private dataSource: DataSource
|
|
16
18
|
) {}
|
|
17
19
|
|
|
18
20
|
/**
|
|
@@ -27,11 +29,11 @@ export class EntityValidationService {
|
|
|
27
29
|
attributeData
|
|
28
30
|
.filter(attr => attr.required)
|
|
29
31
|
.forEach(attr => {
|
|
30
|
-
const value = entityData[attr.
|
|
32
|
+
const value = entityData[attr.attribute_key];
|
|
31
33
|
if (!this.hasValidValue(value)) {
|
|
32
34
|
errors.push({
|
|
33
|
-
field: attr.
|
|
34
|
-
message: `Field
|
|
35
|
+
field: attr.name,
|
|
36
|
+
message: `Field ${attr.name} is required.`,
|
|
35
37
|
});
|
|
36
38
|
}
|
|
37
39
|
});
|
|
@@ -45,24 +47,27 @@ export class EntityValidationService {
|
|
|
45
47
|
async validateUniqueFields(
|
|
46
48
|
entityData: Record<string, any>,
|
|
47
49
|
attributeData: AttributeMaster[],
|
|
48
|
-
entityType: string
|
|
50
|
+
entityType: string,
|
|
51
|
+
db_table_name: string
|
|
49
52
|
): Promise<ValidationError[]> {
|
|
50
53
|
const errors: ValidationError[] = [];
|
|
51
54
|
|
|
52
|
-
const repo = this.reflectionHelper.getRepoService(entityData.entity_type);
|
|
53
|
-
|
|
54
|
-
if (!repo) {
|
|
55
|
-
throw new Error(`Repository service not found for entityType: ${entityType}`);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
55
|
for (const attr of attributeData.filter(a => a.is_unique)) {
|
|
59
|
-
const value = entityData[attr.
|
|
56
|
+
const value = entityData[attr.attribute_key];
|
|
57
|
+
|
|
60
58
|
if (this.hasValidValue(value)) {
|
|
61
|
-
const existing = await
|
|
59
|
+
const existing = await this.dataSource
|
|
60
|
+
.createQueryBuilder()
|
|
61
|
+
.select("*")
|
|
62
|
+
.from(db_table_name, db_table_name)
|
|
63
|
+
.where(`${db_table_name}.${attr.attribute_key} = :value`, { value })
|
|
64
|
+
.limit(1)
|
|
65
|
+
.getRawOne();
|
|
66
|
+
|
|
62
67
|
if (existing) {
|
|
63
68
|
errors.push({
|
|
64
|
-
field: attr.
|
|
65
|
-
message: `Field
|
|
69
|
+
field: attr.name,
|
|
70
|
+
message: `Field ${attr.name} must be unique. Value ${value} already exists.`,
|
|
66
71
|
});
|
|
67
72
|
}
|
|
68
73
|
}
|
|
@@ -71,17 +76,17 @@ export class EntityValidationService {
|
|
|
71
76
|
return errors;
|
|
72
77
|
}
|
|
73
78
|
|
|
74
|
-
|
|
75
79
|
/**
|
|
76
80
|
* Validates both required and unique fields for a given entity type.
|
|
77
81
|
*/
|
|
78
82
|
async validateEntityData(
|
|
79
|
-
entityData: Record<string, any
|
|
83
|
+
entityData: Record<string, any>,
|
|
84
|
+
entityMaster
|
|
80
85
|
): Promise<ValidationError[]> {
|
|
81
86
|
const attributes = await this.attributeMasterService.findAttributesByMappedEntityType(entityData.entity_type);
|
|
82
87
|
|
|
83
88
|
const requiredErrors = this.validateRequiredFields(entityData, attributes);
|
|
84
|
-
const uniqueErrors = await this.validateUniqueFields(entityData, attributes, entityData.entity_type);
|
|
89
|
+
const uniqueErrors = await this.validateUniqueFields(entityData, attributes, entityData.entity_type,entityMaster.db_table_name);
|
|
85
90
|
|
|
86
91
|
return [...requiredErrors, ...uniqueErrors];
|
|
87
92
|
}
|