@punks/backend-entity-manager 0.0.223 → 0.0.224

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,9 +1,10 @@
1
1
  import { DeepPartial } from "../types";
2
+ import { IAuthenticationContext } from "./authentication";
2
3
  export interface IEntityCreateCommandAdapter<TEntity, TEntityCreateData> {
3
- createDataToEntity(data: TEntityCreateData): DeepPartial<TEntity>;
4
+ createDataToEntity(data: TEntityCreateData, context: IAuthenticationContext<unknown>): DeepPartial<TEntity>;
4
5
  }
5
6
  export interface IEntityUpdateCommandAdapter<TEntity, TEntityUpdateData> {
6
- updateDataToEntity(data: TEntityUpdateData): DeepPartial<TEntity>;
7
+ updateDataToEntity(data: TEntityUpdateData, context: IAuthenticationContext<unknown>): DeepPartial<TEntity>;
7
8
  }
8
9
  export interface IEntityAdapter<TEntity, TEntityCreateData, TEntityUpdateData> extends IEntityCreateCommandAdapter<TEntity, TEntityCreateData>, IEntityUpdateCommandAdapter<TEntity, TEntityUpdateData> {
9
10
  }
@@ -7,6 +7,7 @@ export declare class EntityCreateCommand<TEntity, TEntityId, TEntityCreateData>
7
7
  execute(input: TEntityCreateData): Promise<EntityReference<TEntityId>>;
8
8
  private adaptEntity;
9
9
  private authorize;
10
+ private getContext;
10
11
  private versionEntity;
11
12
  private isVersioningEnabled;
12
13
  }
@@ -8,6 +8,7 @@ export declare class EntityUpdateCommand<TEntity, TEntityId, TEntityUpdateData>
8
8
  }>;
9
9
  private adaptEntity;
10
10
  private authorize;
11
+ private getContext;
11
12
  private versionEntity;
12
13
  private isVersioningEnabled;
13
14
  }
@@ -9,6 +9,7 @@ export declare class EntityUpsertCommand<TEntity, TEntityId, TEntityUpdateData>
9
9
  private upsertEntity;
10
10
  private adaptEntity;
11
11
  private authorize;
12
+ private getContext;
12
13
  private versionEntity;
13
14
  private isVersioningEnabled;
14
15
  }
package/dist/esm/index.js CHANGED
@@ -549,7 +549,7 @@ class EntityCreateCommand {
549
549
  this.services = services;
550
550
  }
551
551
  async execute(input) {
552
- const entity = this.adaptEntity(input);
552
+ const entity = await this.adaptEntity(input);
553
553
  await this.authorize(entity);
554
554
  const createdItem = await this.services.resolveRepository().create(entity);
555
555
  // todo: parametrize id field
@@ -562,10 +562,11 @@ class EntityCreateCommand {
562
562
  id,
563
563
  };
564
564
  }
565
- adaptEntity(input) {
565
+ async adaptEntity(input) {
566
+ const context = await this.getContext();
566
567
  const adapter = this.services.resolveAdapter();
567
568
  return adapter
568
- ? adapter.createDataToEntity(input)
569
+ ? adapter.createDataToEntity(input, context)
569
570
  : input;
570
571
  }
571
572
  async authorize(entity) {
@@ -573,8 +574,7 @@ class EntityCreateCommand {
573
574
  if (!authorization) {
574
575
  return;
575
576
  }
576
- const contextService = this.services.resolveAuthenticationContextProvider();
577
- const context = await contextService?.getContext();
577
+ const context = await this.getContext();
578
578
  if (!context) {
579
579
  return;
580
580
  }
@@ -582,6 +582,14 @@ class EntityCreateCommand {
582
582
  if (!authorizationResult.isAuthorized)
583
583
  throw new EntityOperationUnauthorizedException(EntityOperationType.Create, this.services.getEntityName(), entity);
584
584
  }
585
+ async getContext() {
586
+ const authorization = this.services.resolveAuthorizationMiddleware();
587
+ if (!authorization) {
588
+ return;
589
+ }
590
+ const contextService = this.services.resolveAuthenticationContextProvider();
591
+ return await contextService?.getContext();
592
+ }
585
593
  async versionEntity(id, entity) {
586
594
  if (!this.isVersioningEnabled()) {
587
595
  return;
@@ -819,7 +827,7 @@ class EntityUpdateCommand {
819
827
  this.services = services;
820
828
  }
821
829
  async execute(id, input) {
822
- const entity = this.adaptEntity(input);
830
+ const entity = await this.adaptEntity(input);
823
831
  await this.authorize(id);
824
832
  const updatedEntity = await this.services
825
833
  .resolveRepository()
@@ -832,10 +840,11 @@ class EntityUpdateCommand {
832
840
  id,
833
841
  };
834
842
  }
835
- adaptEntity(input) {
843
+ async adaptEntity(input) {
844
+ const context = await this.getContext();
836
845
  const adapter = this.services.resolveAdapter();
837
846
  return adapter
838
- ? adapter.updateDataToEntity(input)
847
+ ? adapter.updateDataToEntity(input, context)
839
848
  : input;
840
849
  }
841
850
  async authorize(id) {
@@ -847,8 +856,7 @@ class EntityUpdateCommand {
847
856
  if (currentEntity == null) {
848
857
  throw new EntityNotFoundException();
849
858
  }
850
- const contextService = this.services.resolveAuthenticationContextProvider();
851
- const context = await contextService?.getContext();
859
+ const context = await this.getContext();
852
860
  if (!context) {
853
861
  return;
854
862
  }
@@ -856,6 +864,14 @@ class EntityUpdateCommand {
856
864
  if (!authorizationResult.isAuthorized)
857
865
  throw new EntityOperationUnauthorizedException(EntityOperationType.Create, this.services.getEntityName(), currentEntity);
858
866
  }
867
+ async getContext() {
868
+ const authorization = this.services.resolveAuthorizationMiddleware();
869
+ if (!authorization) {
870
+ return;
871
+ }
872
+ const contextService = this.services.resolveAuthenticationContextProvider();
873
+ return await contextService?.getContext();
874
+ }
859
875
  async versionEntity(id, entity) {
860
876
  if (!this.isVersioningEnabled()) {
861
877
  return;
@@ -876,7 +892,7 @@ class EntityUpsertCommand {
876
892
  this.services = services;
877
893
  }
878
894
  async execute(id, data) {
879
- const entity = this.adaptEntity(data);
895
+ const entity = await this.adaptEntity(data);
880
896
  await this.authorize(id, entity);
881
897
  const updatedEntity = await this.upsertEntity(id, entity);
882
898
  await this.versionEntity(id, updatedEntity);
@@ -890,10 +906,11 @@ class EntityUpsertCommand {
890
906
  async upsertEntity(id, entity) {
891
907
  return await this.services.resolveRepository().upsert(id, entity);
892
908
  }
893
- adaptEntity(input) {
909
+ async adaptEntity(input) {
910
+ const context = await this.getContext();
894
911
  const adapter = this.services.resolveAdapter();
895
912
  return adapter
896
- ? adapter.updateDataToEntity(input)
913
+ ? adapter.updateDataToEntity(input, context)
897
914
  : input;
898
915
  }
899
916
  async authorize(id, entity) {
@@ -902,8 +919,7 @@ class EntityUpsertCommand {
902
919
  return;
903
920
  }
904
921
  const currentEntity = await this.services.resolveRepository().get(id);
905
- const contextService = this.services.resolveAuthenticationContextProvider();
906
- const context = await contextService?.getContext();
922
+ const context = await this.getContext();
907
923
  if (!context) {
908
924
  return;
909
925
  }
@@ -917,6 +933,14 @@ class EntityUpsertCommand {
917
933
  if (!authorizationResult.isAuthorized)
918
934
  throw new EntityOperationUnauthorizedException(EntityOperationType.Create, this.services.getEntityName(), entity);
919
935
  }
936
+ async getContext() {
937
+ const authorization = this.services.resolveAuthorizationMiddleware();
938
+ if (!authorization) {
939
+ return;
940
+ }
941
+ const contextService = this.services.resolveAuthenticationContextProvider();
942
+ return await contextService?.getContext();
943
+ }
920
944
  async versionEntity(id, entity) {
921
945
  if (!this.isVersioningEnabled()) {
922
946
  return;