@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.
package/dist/cjs/index.js CHANGED
@@ -1115,37 +1115,53 @@ class EntityUpsertCommand {
1115
1115
  this.services = services;
1116
1116
  }
1117
1117
  async execute(id, data) {
1118
- const entity = await this.adaptEntity(data);
1118
+ const entity = await this.adaptEntity(id, data);
1119
1119
  await this.authorize(id, entity);
1120
- const updatedEntity = await this.upsertEntity(id, entity);
1121
- await this.versionEntity(id, updatedEntity);
1120
+ if (id) {
1121
+ const updatedEntity = await this.services
1122
+ .resolveRepository()
1123
+ .upsert(id, entity);
1124
+ await this.versionEntity(id, updatedEntity);
1125
+ await this.services
1126
+ .resolveEventsManager()
1127
+ .processEntityUpdatedEvent(updatedEntity);
1128
+ return {
1129
+ id,
1130
+ };
1131
+ }
1132
+ const createdItem = await this.services.resolveRepository().create(entity);
1133
+ // todo: parametrize id field
1134
+ const newId = createdItem.id;
1135
+ await this.versionEntity(newId, createdItem);
1122
1136
  await this.services
1123
1137
  .resolveEventsManager()
1124
- .processEntityUpdatedEvent(updatedEntity);
1138
+ .processEntityCreatedEvent(createdItem);
1125
1139
  return {
1126
- id,
1140
+ id: newId,
1127
1141
  };
1128
1142
  }
1129
- async upsertEntity(id, entity) {
1130
- return await this.services.resolveRepository().upsert(id, entity);
1131
- }
1132
- async adaptEntity(input) {
1143
+ async adaptEntity(id, input) {
1133
1144
  const context = await this.getContext();
1134
1145
  const adapter = this.services.resolveAdapter();
1135
- return adapter
1146
+ if (!adapter) {
1147
+ return input;
1148
+ }
1149
+ return id
1136
1150
  ? adapter.updateDataToEntity(input, context)
1137
- : input;
1151
+ : adapter.createDataToEntity(input, context);
1138
1152
  }
1139
1153
  async authorize(id, entity) {
1140
1154
  const authorization = this.services.resolveAuthorizationMiddleware();
1141
1155
  if (!authorization) {
1142
1156
  return;
1143
1157
  }
1144
- const currentEntity = await this.services.resolveRepository().get(id);
1145
1158
  const context = await this.getContext();
1146
1159
  if (!context) {
1147
1160
  return;
1148
1161
  }
1162
+ const currentEntity = id
1163
+ ? await this.services.resolveRepository().get(id)
1164
+ : undefined;
1149
1165
  if (currentEntity) {
1150
1166
  const updateResult = await authorization.canUpdate(currentEntity, context);
1151
1167
  if (!updateResult.isAuthorized)
@@ -40762,18 +40778,30 @@ const fieldTextValidator = (item, selector, params) => {
40762
40778
  const isRequiredValid = params.required
40763
40779
  ? typeof value === "string" && value.length >= 0
40764
40780
  : true;
40765
- const isMinLengthValid = typeof value === "string" && value.length >= (params.minLength ?? 0);
40766
- const isMaxLengthValid = typeof value === "string" && value.length <= (params.maxLength ?? Infinity);
40781
+ const length = typeof value === "string" ? value.length : 0;
40782
+ const isMinLengthValid = length >= (params.minLength ?? 0);
40783
+ const isMaxLengthValid = length <= (params.maxLength ?? Infinity);
40767
40784
  const isRegexValid = (typeof value === "string" && params.regex?.test(value)) ?? true;
40768
40785
  return {
40769
40786
  isValid: isMinLengthValid && isMaxLengthValid,
40770
40787
  validationErrors: [
40771
40788
  ...(isRequiredValid ? [] : [{ errorCode: "required" }]),
40772
- ...(isMinLengthValid && isMaxLengthValid
40789
+ ...(isMinLengthValid
40773
40790
  ? []
40774
- : [{ errorCode: "length" }]),
40775
- ...(isMinLengthValid ? [] : [{ errorCode: "minLength" }]),
40776
- ...(isMaxLengthValid ? [] : [{ errorCode: "maxLength" }]),
40791
+ : [
40792
+ {
40793
+ errorCode: "minLength",
40794
+ payload: { current: length, expected: params.minLength },
40795
+ },
40796
+ ]),
40797
+ ...(isMaxLengthValid
40798
+ ? []
40799
+ : [
40800
+ {
40801
+ errorCode: "maxLength",
40802
+ payload: { current: length, expected: params.maxLength },
40803
+ },
40804
+ ]),
40777
40805
  ...(isRegexValid ? [] : [{ errorCode: "regex" }]),
40778
40806
  ],
40779
40807
  };