@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.
@@ -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)