rez_core 2.1.5 → 2.1.10
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/master/service/master.service.d.ts +1 -4
- package/dist/module/master/service/master.service.js +19 -79
- package/dist/module/master/service/master.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/module/master/service/master.service.ts +44 -96
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
2
|
import { Brackets, EntityManager } from 'typeorm';
|
|
3
3
|
import { ExcelUtil } from 'src/utils/service/excelUtil.service';
|
|
4
4
|
import { EntityMasterService } from '../../meta/service/entity-master.service';
|
|
@@ -199,8 +199,6 @@ export class MasterService {
|
|
|
199
199
|
loggedInUser,
|
|
200
200
|
duplicateHandling: 'skip_duplicates' | 'overwrite_items',
|
|
201
201
|
) {
|
|
202
|
-
const errors: { row: number; errors: any[] }[] = [];
|
|
203
|
-
|
|
204
202
|
const data = ExcelUtil.readExcel(file.buffer);
|
|
205
203
|
const entityMaster =
|
|
206
204
|
await this.entityMasterService.findByMappedEntityType(entityType);
|
|
@@ -218,7 +216,6 @@ export class MasterService {
|
|
|
218
216
|
await this.attributeMasterService.findAttributesByMappedEntityType(
|
|
219
217
|
entityType,
|
|
220
218
|
);
|
|
221
|
-
|
|
222
219
|
const uniqueFields = attributes
|
|
223
220
|
.filter((attr) => attr.is_unique)
|
|
224
221
|
.map((attr) => attr.attribute_key);
|
|
@@ -227,71 +224,30 @@ export class MasterService {
|
|
|
227
224
|
throw new Error(`No unique fields found for entityType: ${entityType}`);
|
|
228
225
|
}
|
|
229
226
|
|
|
230
|
-
for (const
|
|
227
|
+
for (const row of sheetData) {
|
|
231
228
|
if (row.parent_type && row.parent_id) {
|
|
232
229
|
await this.resolveParent(row);
|
|
233
230
|
}
|
|
234
231
|
|
|
235
232
|
for (const attr of attributes) {
|
|
236
233
|
if (attr.data_source_type === 'entity' && row[attr.attribute_key]) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
234
|
+
const refEntity = await this.entityMasterService.getEntityData(
|
|
235
|
+
attr.datasource_list,
|
|
236
|
+
);
|
|
237
|
+
const refData = await this.entityManager.query(
|
|
238
|
+
`SELECT * FROM ${refEntity.db_table_name} WHERE code = ? LIMIT 1`,
|
|
239
|
+
[row[attr.attribute_key]],
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
//you will check the org id of refData and loggedInUser.organization_id
|
|
243
|
+
|
|
244
|
+
if (!refData.length) {
|
|
245
|
+
throw new Error(
|
|
246
|
+
`Reference entity not found for code: ${row[attr.attribute_key]}`,
|
|
245
247
|
);
|
|
246
|
-
|
|
247
|
-
if (!refData.length) {
|
|
248
|
-
errors.push({
|
|
249
|
-
row: i + 1,
|
|
250
|
-
errors: [
|
|
251
|
-
{
|
|
252
|
-
field: attr.name,
|
|
253
|
-
message: `Reference entity not found for code: ${row[attr.attribute_key]}`,
|
|
254
|
-
},
|
|
255
|
-
],
|
|
256
|
-
});
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const existingError = errors.find((e) => e.row === i + 1);
|
|
261
|
-
|
|
262
|
-
if (refData[0].organization_id !== loggedInUser.organization_id) {
|
|
263
|
-
if (existingError) {
|
|
264
|
-
existingError.errors.push({
|
|
265
|
-
field: attr.name,
|
|
266
|
-
message: `Reference entity not found for code: ${row[attr.attribute_key]} does not belong to the organization`,
|
|
267
|
-
});
|
|
268
|
-
continue;
|
|
269
|
-
} else {
|
|
270
|
-
errors.push({
|
|
271
|
-
row: i + 1,
|
|
272
|
-
errors: [
|
|
273
|
-
{
|
|
274
|
-
field: attr.name,
|
|
275
|
-
message: `Reference entity with code ${row[attr.attribute_key]} does not belong to the organization`,
|
|
276
|
-
},
|
|
277
|
-
],
|
|
278
|
-
});
|
|
279
|
-
continue;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
row[attr.attribute_key] = refData[0].id;
|
|
284
|
-
} catch (err) {
|
|
285
|
-
errors.push({
|
|
286
|
-
row: i + 1,
|
|
287
|
-
errors: [
|
|
288
|
-
{
|
|
289
|
-
field: attr.name,
|
|
290
|
-
message: `Error fetching reference entity for ${attr.attribute_key}`,
|
|
291
|
-
},
|
|
292
|
-
],
|
|
293
|
-
});
|
|
294
248
|
}
|
|
249
|
+
|
|
250
|
+
row[attr.attribute_key] = refData[0].id;
|
|
295
251
|
}
|
|
296
252
|
}
|
|
297
253
|
}
|
|
@@ -303,7 +259,6 @@ export class MasterService {
|
|
|
303
259
|
uniqueFields,
|
|
304
260
|
loggedInUser,
|
|
305
261
|
duplicateHandling,
|
|
306
|
-
errors,
|
|
307
262
|
);
|
|
308
263
|
|
|
309
264
|
return { message: 'Entity data uploaded successfully' };
|
|
@@ -394,7 +349,6 @@ export class MasterService {
|
|
|
394
349
|
uniqueFields: string[],
|
|
395
350
|
loggedInUser: any,
|
|
396
351
|
duplicateHandling: 'skip_duplicates' | 'overwrite_items',
|
|
397
|
-
errors: { row: number; errors: any[] }[],
|
|
398
352
|
): Promise<void> {
|
|
399
353
|
const entityMaster =
|
|
400
354
|
await this.entityMasterService.findByMappedEntityType(entityType);
|
|
@@ -407,39 +361,13 @@ export class MasterService {
|
|
|
407
361
|
if (!entityService)
|
|
408
362
|
throw new Error(`Entity service not found for ${entityType}`);
|
|
409
363
|
|
|
364
|
+
const errors: { row: number; errors: any[] }[] = [];
|
|
365
|
+
|
|
410
366
|
for (const [i, row] of data.entries()) {
|
|
411
367
|
row.entity_type = entityType;
|
|
412
368
|
row.organization_id = loggedInUser.organization_id;
|
|
413
369
|
|
|
414
|
-
|
|
415
|
-
row,
|
|
416
|
-
entityMaster,
|
|
417
|
-
loggedInUser,
|
|
418
|
-
);
|
|
419
|
-
|
|
420
|
-
const existingError = errors.find((e) => e.row === i + 1);
|
|
421
|
-
|
|
422
|
-
if (rowErrors?.length > 0) {
|
|
423
|
-
if (existingError) {
|
|
424
|
-
existingError.errors.push(...rowErrors);
|
|
425
|
-
} else {
|
|
426
|
-
errors.push({
|
|
427
|
-
row: i + 1,
|
|
428
|
-
errors: rowErrors,
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
if (errors.length > 0) {
|
|
435
|
-
throw new BadRequestException({
|
|
436
|
-
status: false,
|
|
437
|
-
message: `Validation errors found in the uploaded data.`,
|
|
438
|
-
error: errors,
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
for (const row of data) {
|
|
370
|
+
// 🧠 Check if this already exists
|
|
443
371
|
const qb = this.entityManager
|
|
444
372
|
.createQueryBuilder()
|
|
445
373
|
.select('*')
|
|
@@ -469,10 +397,6 @@ export class MasterService {
|
|
|
469
397
|
|
|
470
398
|
const existing = await qb.limit(1).getRawOne();
|
|
471
399
|
|
|
472
|
-
row.entity_type = entityType;
|
|
473
|
-
row.organization_id = loggedInUser.organization_id;
|
|
474
|
-
row.status = 'ACTIVE';
|
|
475
|
-
let errors = [];
|
|
476
400
|
if (existing) {
|
|
477
401
|
if (duplicateHandling === 'skip_duplicates') continue;
|
|
478
402
|
if (duplicateHandling === 'overwrite_items') {
|
|
@@ -480,8 +404,32 @@ export class MasterService {
|
|
|
480
404
|
await entityService.updateEntity(row, loggedInUser);
|
|
481
405
|
}
|
|
482
406
|
} else {
|
|
407
|
+
const rowErrors = await this.entityValidationService.validateEntityData(
|
|
408
|
+
row,
|
|
409
|
+
entityMaster,
|
|
410
|
+
loggedInUser,
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
if (rowErrors && rowErrors.length > 0) {
|
|
414
|
+
errors.push({
|
|
415
|
+
row: i + 1,
|
|
416
|
+
errors: rowErrors,
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (errors.length > 0) {
|
|
421
|
+
throw new Error(
|
|
422
|
+
`Validation errors found in the uploaded data: ${JSON.stringify(errors)}`,
|
|
423
|
+
);
|
|
424
|
+
}
|
|
483
425
|
await entityService.createEntity(row, loggedInUser);
|
|
484
426
|
}
|
|
485
427
|
}
|
|
428
|
+
|
|
429
|
+
// if (errors.length > 0) {
|
|
430
|
+
// throw new Error(
|
|
431
|
+
// `Validation errors found in the uploaded data: ${JSON.stringify(errors)}`,
|
|
432
|
+
// );
|
|
433
|
+
// }
|
|
486
434
|
}
|
|
487
435
|
}
|