rez_core 2.1.6 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rez_core",
3
- "version": "2.1.6",
3
+ "version": "2.1.10",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -1,4 +1,4 @@
1
- import { BadRequestException, Injectable } from '@nestjs/common';
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 [i, row] of sheetData.entries()) {
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
- try {
238
- const refEntity = await this.entityMasterService.getEntityData(
239
- attr.datasource_list,
240
- );
241
-
242
- const refData = await this.entityManager.query(
243
- `SELECT * FROM ${refEntity.db_table_name} WHERE code = ? LIMIT 1`,
244
- [row[attr.attribute_key]],
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
- const rowErrors = await this.entityValidationService.validateEntityData(
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('*')
@@ -476,6 +404,24 @@ export class MasterService {
476
404
  await entityService.updateEntity(row, loggedInUser);
477
405
  }
478
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
+ }
479
425
  await entityService.createEntity(row, loggedInUser);
480
426
  }
481
427
  }