@punks/backend-entity-manager 0.0.392 → 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)