@punks/backend-entity-manager 0.0.391 → 0.0.393

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.
@@ -2,6 +2,7 @@
2
2
  export type EntryValidationError = {
3
3
  errorCode: string;
4
4
  value?: any;
5
+ payload?: any;
5
6
  };
6
7
  export type EntryValidationResult = {
7
8
  isValid: boolean;
@@ -3,10 +3,9 @@ import { EntityServiceLocator } from "../providers/services";
3
3
  export declare class EntityUpsertCommand<TEntity, TEntityId, TEntityUpdateData> implements IEntityUpsertCommand<TEntity, TEntityId, TEntityUpdateData> {
4
4
  private readonly services;
5
5
  constructor(services: EntityServiceLocator<TEntity, TEntityId>);
6
- execute(id: TEntityId, data: TEntityUpdateData): Promise<{
7
- id: TEntityId;
6
+ execute(id: TEntityId | undefined, data: TEntityUpdateData): Promise<{
7
+ id: any;
8
8
  }>;
9
- private upsertEntity;
10
9
  private adaptEntity;
11
10
  private authorize;
12
11
  private getContext;
package/dist/esm/index.js CHANGED
@@ -1100,37 +1100,53 @@ class EntityUpsertCommand {
1100
1100
  this.services = services;
1101
1101
  }
1102
1102
  async execute(id, data) {
1103
- const entity = await this.adaptEntity(data);
1103
+ const entity = await this.adaptEntity(id, data);
1104
1104
  await this.authorize(id, entity);
1105
- const updatedEntity = await this.upsertEntity(id, entity);
1106
- await this.versionEntity(id, updatedEntity);
1105
+ if (id) {
1106
+ const updatedEntity = await this.services
1107
+ .resolveRepository()
1108
+ .upsert(id, entity);
1109
+ await this.versionEntity(id, updatedEntity);
1110
+ await this.services
1111
+ .resolveEventsManager()
1112
+ .processEntityUpdatedEvent(updatedEntity);
1113
+ return {
1114
+ id,
1115
+ };
1116
+ }
1117
+ const createdItem = await this.services.resolveRepository().create(entity);
1118
+ // todo: parametrize id field
1119
+ const newId = createdItem.id;
1120
+ await this.versionEntity(newId, createdItem);
1107
1121
  await this.services
1108
1122
  .resolveEventsManager()
1109
- .processEntityUpdatedEvent(updatedEntity);
1123
+ .processEntityCreatedEvent(createdItem);
1110
1124
  return {
1111
- id,
1125
+ id: newId,
1112
1126
  };
1113
1127
  }
1114
- async upsertEntity(id, entity) {
1115
- return await this.services.resolveRepository().upsert(id, entity);
1116
- }
1117
- async adaptEntity(input) {
1128
+ async adaptEntity(id, input) {
1118
1129
  const context = await this.getContext();
1119
1130
  const adapter = this.services.resolveAdapter();
1120
- return adapter
1131
+ if (!adapter) {
1132
+ return input;
1133
+ }
1134
+ return id
1121
1135
  ? adapter.updateDataToEntity(input, context)
1122
- : input;
1136
+ : adapter.createDataToEntity(input, context);
1123
1137
  }
1124
1138
  async authorize(id, entity) {
1125
1139
  const authorization = this.services.resolveAuthorizationMiddleware();
1126
1140
  if (!authorization) {
1127
1141
  return;
1128
1142
  }
1129
- const currentEntity = await this.services.resolveRepository().get(id);
1130
1143
  const context = await this.getContext();
1131
1144
  if (!context) {
1132
1145
  return;
1133
1146
  }
1147
+ const currentEntity = id
1148
+ ? await this.services.resolveRepository().get(id)
1149
+ : undefined;
1134
1150
  if (currentEntity) {
1135
1151
  const updateResult = await authorization.canUpdate(currentEntity, context);
1136
1152
  if (!updateResult.isAuthorized)
@@ -40747,18 +40763,30 @@ const fieldTextValidator = (item, selector, params) => {
40747
40763
  const isRequiredValid = params.required
40748
40764
  ? typeof value === "string" && value.length >= 0
40749
40765
  : true;
40750
- const isMinLengthValid = typeof value === "string" && value.length >= (params.minLength ?? 0);
40751
- const isMaxLengthValid = typeof value === "string" && value.length <= (params.maxLength ?? Infinity);
40766
+ const length = typeof value === "string" ? value.length : 0;
40767
+ const isMinLengthValid = length >= (params.minLength ?? 0);
40768
+ const isMaxLengthValid = length <= (params.maxLength ?? Infinity);
40752
40769
  const isRegexValid = (typeof value === "string" && params.regex?.test(value)) ?? true;
40753
40770
  return {
40754
40771
  isValid: isMinLengthValid && isMaxLengthValid,
40755
40772
  validationErrors: [
40756
40773
  ...(isRequiredValid ? [] : [{ errorCode: "required" }]),
40757
- ...(isMinLengthValid && isMaxLengthValid
40774
+ ...(isMinLengthValid
40758
40775
  ? []
40759
- : [{ errorCode: "length" }]),
40760
- ...(isMinLengthValid ? [] : [{ errorCode: "minLength" }]),
40761
- ...(isMaxLengthValid ? [] : [{ errorCode: "maxLength" }]),
40776
+ : [
40777
+ {
40778
+ errorCode: "minLength",
40779
+ payload: { current: length, expected: params.minLength },
40780
+ },
40781
+ ]),
40782
+ ...(isMaxLengthValid
40783
+ ? []
40784
+ : [
40785
+ {
40786
+ errorCode: "maxLength",
40787
+ payload: { current: length, expected: params.maxLength },
40788
+ },
40789
+ ]),
40762
40790
  ...(isRegexValid ? [] : [{ errorCode: "regex" }]),
40763
40791
  ],
40764
40792
  };