rez_core 1.0.72 → 1.0.74

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": "1.0.72",
3
+ "version": "1.0.74",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  BadRequestException,
3
+ Body,
3
4
  Controller,
4
5
  Inject,
5
6
  Param,
@@ -46,13 +47,20 @@ export class MasterController {
46
47
 
47
48
  @Post('uploadEntity/:entityType')
48
49
  @UseInterceptors(FileInterceptor('file'))
49
-
50
- async uploadEntityDataXls(@Param('entityType') entityType: string, @UploadedFile() file: Express.Multer.File,@Req() req: Request & { user: any }) {
50
+ async uploadEntityDataXls(
51
+ @Param('entityType') entityType: string,
52
+ @UploadedFile() file: Express.Multer.File,
53
+ @Req() req: Request & { user: any },
54
+ @Body('duplicateHandling') duplicateHandling: 'skip_duplicates' | 'overwrite_items',
55
+ ) {
51
56
  let requestedUser = req.user;
52
- let loggedInUser = await this.entityService.getEntityData(ENTITYTYPE_USER,requestedUser.id);
57
+ let loggedInUser = await this.entityService.getEntityData(ENTITYTYPE_USER, requestedUser.id);
58
+
53
59
  if (!file) {
54
60
  throw new BadRequestException('File is required');
55
61
  }
56
- return await this.masterService.uploadEntityData(file.buffer,entityType,loggedInUser);
62
+
63
+ return await this.masterService.uploadEntityData(file.buffer, entityType, loggedInUser, duplicateHandling);
57
64
  }
65
+
58
66
  }
@@ -134,7 +134,7 @@ export class MasterService {
134
134
  };
135
135
  }
136
136
 
137
- async uploadEntityData(file, entityType, loggedInUser) {
137
+ async uploadEntityData(file, entityType, loggedInUser, duplicateHandling: 'skip_duplicates' | 'overwrite_items') {
138
138
  const data = ExcelUtil.readExcel(file.buffer);
139
139
  const entityMaster = await this.entityMasterService.findByMappedEntityType(entityType);
140
140
  if (!entityMaster) {
@@ -143,22 +143,17 @@ export class MasterService {
143
143
 
144
144
  const tableName = entityMaster.db_table_name;
145
145
  const sheetData = data[tableName];
146
-
147
146
  if (!sheetData) {
148
147
  throw new Error(`Sheet for ${tableName} not found in uploaded file`);
149
148
  }
150
149
 
151
150
  const attributes = await this.attributeMasterService.findAttributesByMappedEntityType(entityType);
152
-
153
- const uniqueFields = attributes
154
- .filter(attr => attr.is_unique)
155
- .map(attr => attr.attribute_key);
151
+ const uniqueFields = attributes.filter(attr => attr.is_unique).map(attr => attr.attribute_key);
156
152
 
157
153
  if (uniqueFields.length === 0) {
158
154
  throw new Error(`No unique fields found for entityType: ${entityType}`);
159
155
  }
160
156
 
161
- // Transform reference fields
162
157
  for (const row of sheetData) {
163
158
  if (row.parent_type && row.parent_id) {
164
159
  await this.resolveParent(row);
@@ -181,11 +176,12 @@ export class MasterService {
181
176
  }
182
177
  }
183
178
 
184
- await this.upsertViaService(entityType, sheetData, attributes, uniqueFields, loggedInUser);
179
+ await this.upsertViaService(entityType, sheetData, attributes, uniqueFields, loggedInUser, duplicateHandling);
185
180
 
186
181
  return { message: 'Entity data uploaded successfully' };
187
182
  }
188
183
 
184
+
189
185
 
190
186
  private isMetaSheet(sheetName: string): boolean {
191
187
  return this.metaSheets.includes(sheetName.toLowerCase());
@@ -265,45 +261,57 @@ export class MasterService {
265
261
  }
266
262
  }
267
263
  }
268
-
269
264
  async upsertViaService(
270
265
  entityType: string,
271
266
  data: any[],
272
267
  attributes: any[],
273
268
  uniqueFields: string[],
274
269
  loggedInUser: any,
270
+ duplicateHandling: 'skip_duplicates' | 'overwrite_items',
275
271
  ): Promise<void> {
276
272
  const entityMaster = await this.entityMasterService.findByMappedEntityType(entityType);
277
273
  if (!entityMaster) throw new Error(`Entity master not found for ${entityType}`);
278
274
 
279
275
  const serviceName = entityMaster.entity_service;
280
276
  const entityService = await this.reflectionHelper.getBean<EntityServiceImpl>(serviceName);
281
-
282
277
  if (!entityService) throw new Error(`Entity service not found for ${entityType}`);
283
278
 
284
279
  for (const row of data) {
285
- const whereClause = uniqueFields.reduce((acc, field) => {
286
- acc[field] = row[field];
287
- return acc;
288
- }, {});
289
-
290
- const existing = await this.entityManager
280
+ const qb = this.entityManager
291
281
  .createQueryBuilder()
292
282
  .select('*')
293
- .from(entityMaster.db_table_name, entityMaster.db_table_name)
294
- .where(whereClause)
295
- .limit(1)
296
- .getRawOne();
283
+ .from(entityMaster.db_table_name, entityMaster.db_table_name);
284
+
285
+ // Build OR conditions dynamically for unique fields
286
+ uniqueFields.forEach((field, index) => {
287
+ const condition = `${entityMaster.db_table_name}.${field} = :val${index}`;
288
+ const param = { [`val${index}`]: row[field] };
289
+
290
+ if (index === 0) {
291
+ qb.where(condition, param);
292
+ } else {
293
+ qb.orWhere(condition, param);
294
+ }
295
+ });
296
+
297
+ const existing = await qb.limit(1).getRawOne();
297
298
 
298
299
  row.entity_type = entityType;
300
+
299
301
  if (existing) {
300
- row.id = existing.id;
301
- await entityService.updateEntity(row, loggedInUser);
302
+ if (duplicateHandling === 'skip_duplicates') {
303
+ continue; // Skip this row
304
+ } else if (duplicateHandling === 'overwrite_items') {
305
+ row.id = existing.id;
306
+ await entityService.updateEntity(row, loggedInUser);
307
+ }
302
308
  } else {
303
309
  await entityService.createEntity(row, loggedInUser);
304
310
  }
305
311
  }
306
312
  }
307
313
 
314
+
315
+
308
316
 
309
317
  }