@punks/backend-entity-manager 0.0.355 → 0.0.357

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.
@@ -1,6 +1,17 @@
1
1
  /// <reference types="node" />
2
+ export type EntryValidationError = {
3
+ errorCode: string;
4
+ };
5
+ export type EntryValidationResult = {
6
+ isValid: boolean;
7
+ validationErrors: EntryValidationError[];
8
+ };
2
9
  export type ImportEntryValidationError = {
3
10
  errorCode: string;
11
+ column: {
12
+ name: string;
13
+ key: string;
14
+ };
4
15
  };
5
16
  export type ImportEntryValidationResult = {
6
17
  isValid: boolean;
@@ -11,11 +22,12 @@ export type EntitySerializerColumnParseAction<TSheetItem> = (value: any, item: T
11
22
  export type EntitySerializerColumnConverter<TSheetItem> = (value: any, item: TSheetItem) => any;
12
23
  export type EntitySerializerColumnValidator<TSheetItem> = {
13
24
  key: string;
14
- fn: (item: TSheetItem) => ImportEntryValidationResult;
25
+ fn: (item: TSheetItem) => EntryValidationResult;
15
26
  };
16
27
  export type EntitySerializerColumnDefinition<TSheetItem> = {
17
28
  name: string;
18
- selector: keyof TSheetItem;
29
+ key: string;
30
+ selector: keyof TSheetItem | ((item: TSheetItem) => any);
19
31
  colSpan?: number;
20
32
  sampleValue?: any;
21
33
  parser?: EntitySerializerColumnParser;
@@ -31,5 +31,6 @@ export declare abstract class EntitySerializer<TEntity, TEntityId, TEntitySearch
31
31
  protected getContext(): Promise<TContext>;
32
32
  private buildExportFile;
33
33
  private getColumnValue;
34
+ private selectColumnValue;
34
35
  private buildExportFileName;
35
36
  }
@@ -1,5 +1,10 @@
1
+ export declare class EntityParseValidationColumn {
2
+ name: string;
3
+ key: string;
4
+ }
1
5
  export declare class EntityParseValidationError {
2
6
  errorCode: string;
7
+ column: EntityParseValidationColumn;
3
8
  }
4
9
  export declare class EntityParseStatus {
5
10
  isValid: boolean;
@@ -1,2 +1,2 @@
1
- import { ImportEntryValidationResult } from "../abstractions/serializer";
2
- export declare const fieldRequiredValidator: <TSheetItem>(item: TSheetItem, selector: (item: TSheetItem) => any) => ImportEntryValidationResult;
1
+ import { EntryValidationResult } from "../abstractions/serializer";
2
+ export declare const fieldRequiredValidator: <TSheetItem>(item: TSheetItem, selector: (item: TSheetItem) => any) => EntryValidationResult;
package/dist/esm/index.js CHANGED
@@ -219,17 +219,20 @@ class EntitySerializer {
219
219
  return records.map((x, i) => this.convertSheetRecord(x, definition, i));
220
220
  }
221
221
  convertSheetRecord(record, definition, rowIndex) {
222
- if (!record._type || record._type !== this.entityName) {
222
+ if (this.options?.useTypeColumn &&
223
+ (!record._type || record._type !== this.entityName)) {
223
224
  throw new Error(`Invalid record type ${record._type} -> record: \n${JSON.stringify(record)}`);
224
225
  }
225
226
  const entity = {};
226
227
  for (const column of definition.columns) {
227
228
  if (column.parseAction) {
228
229
  column.parseAction(record[column.name], entity);
230
+ continue;
229
231
  }
230
- else {
231
- entity[column.selector] = this.parseColumnValue(record, column);
232
+ if (typeof column.selector === "function") {
233
+ throw new Error("Function selectors are not supported with parseAction specified");
232
234
  }
235
+ entity[column.selector] = this.parseColumnValue(record, column);
233
236
  }
234
237
  const validationErrors = [];
235
238
  for (const column of definition.columns) {
@@ -238,13 +241,19 @@ class EntitySerializer {
238
241
  if (!result.isValid) {
239
242
  validationErrors.push({
240
243
  errorCode: validator.key,
244
+ column: {
245
+ name: column.name,
246
+ key: column.key,
247
+ },
241
248
  });
242
249
  }
243
250
  }
244
251
  }
245
252
  const idField = definition.columns.find((x) => x.idColumn || x.selector === "id");
246
253
  return {
247
- id: idField ? entity[idField.selector] : undefined,
254
+ id: idField
255
+ ? this.selectColumnValue(entity, idField.selector)
256
+ : undefined,
248
257
  item: entity,
249
258
  rowIndex,
250
259
  status: {
@@ -339,10 +348,14 @@ class EntitySerializer {
339
348
  fileName,
340
349
  contentType: "text/csv",
341
350
  content: Buffer.from(csvBuild(data, [
342
- {
343
- name: "_type",
344
- value: () => this.entityName,
345
- },
351
+ ...(this.options?.useTypeColumn
352
+ ? [
353
+ {
354
+ name: "_type",
355
+ value: () => this.entityName,
356
+ },
357
+ ]
358
+ : []),
346
359
  ...definition.columns.map((c) => ({
347
360
  name: c.name,
348
361
  value: (item) => this.getColumnValue(item, c),
@@ -380,11 +393,14 @@ class EntitySerializer {
380
393
  }
381
394
  }
382
395
  getColumnValue(item, definition) {
383
- const rawValue = item[definition.selector];
396
+ const rawValue = this.selectColumnValue(item, definition.selector);
384
397
  return definition.converter
385
398
  ? definition.converter(rawValue, item)
386
399
  : rawValue;
387
400
  }
401
+ selectColumnValue(item, selector) {
402
+ return typeof selector === "function" ? selector(item) : item[selector];
403
+ }
388
404
  buildExportFileName(input) {
389
405
  return `${input.refDate.toISOString().replaceAll(":", "_")}_${this.entityName}.${input.format}`;
390
406
  }
@@ -34826,12 +34842,26 @@ class AppExceptionsFilterBase {
34826
34842
  }
34827
34843
  }
34828
34844
 
34845
+ class EntityParseValidationColumn {
34846
+ }
34847
+ __decorate([
34848
+ ApiProperty(),
34849
+ __metadata("design:type", String)
34850
+ ], EntityParseValidationColumn.prototype, "name", void 0);
34851
+ __decorate([
34852
+ ApiProperty(),
34853
+ __metadata("design:type", String)
34854
+ ], EntityParseValidationColumn.prototype, "key", void 0);
34829
34855
  class EntityParseValidationError {
34830
34856
  }
34831
34857
  __decorate([
34832
34858
  ApiProperty(),
34833
34859
  __metadata("design:type", String)
34834
34860
  ], EntityParseValidationError.prototype, "errorCode", void 0);
34861
+ __decorate([
34862
+ ApiProperty(),
34863
+ __metadata("design:type", EntityParseValidationColumn)
34864
+ ], EntityParseValidationError.prototype, "column", void 0);
34835
34865
  class EntityParseStatus {
34836
34866
  }
34837
34867
  __decorate([