@punks/backend-entity-manager 0.0.223 → 0.0.225

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
  }
@@ -1,4 +1,5 @@
1
1
  import { DeepPartial } from "../types";
2
+ import { IAuthenticationContext } from "./authentication";
2
3
  export interface IEntitiesSearchActionConverter<TEntity, TListItemDto> {
3
4
  toListItemDto(entity: TEntity): TListItemDto;
4
5
  }
@@ -6,12 +7,12 @@ export interface IEntityGetActionConverter<TEntity, TEntityDto> {
6
7
  toEntityDto(entity: TEntity): TEntityDto;
7
8
  }
8
9
  export interface IEntityCreateActionConverter<TEntity, TEntityDto, TEntityCreateDto> {
9
- createDtoToEntity(data: TEntityCreateDto): DeepPartial<TEntity>;
10
+ createDtoToEntity(data: TEntityCreateDto, context?: IAuthenticationContext<unknown>): DeepPartial<TEntity>;
10
11
  toEntityDto(entity: TEntity): TEntityDto;
11
12
  }
12
13
  export interface IEntityUpdateActionConverter<TEntity, TEntityDto, TEntityUpdateDto> {
13
14
  toEntityDto(entity: TEntity): TEntityDto;
14
- updateDtoToEntity(data: TEntityUpdateDto): DeepPartial<TEntity>;
15
+ updateDtoToEntity(data: TEntityUpdateDto, context?: IAuthenticationContext<unknown>): DeepPartial<TEntity>;
15
16
  }
16
17
  export interface IEntityConverter<TEntity, TEntityDto, TListItemDto, TEntityCreateDto, TEntityUpdateDto> extends IEntitiesSearchActionConverter<TEntity, TListItemDto>, IEntityGetActionConverter<TEntity, TEntityDto>, IEntityCreateActionConverter<TEntity, TEntityDto, TEntityCreateDto>, IEntityUpdateActionConverter<TEntity, TEntityDto, TEntityUpdateDto> {
17
18
  }
@@ -5,4 +5,5 @@ export declare class EntityCreateAction<TEntity, TEntityId, TEntityCreateDto, TE
5
5
  private readonly logger;
6
6
  constructor(services: EntityServiceLocator<TEntity, TEntityId>);
7
7
  execute(input: TEntityCreateDto): Promise<TEntityDto>;
8
+ private getContext;
8
9
  }
@@ -5,4 +5,5 @@ export declare class EntityUpdateAction<TEntity, TEntityId, TEntityUpdateData, T
5
5
  private readonly logger;
6
6
  constructor(services: EntityServiceLocator<TEntity, TEntityId>);
7
7
  execute(id: TEntityId, input: TEntityUpdateData): Promise<TEntityDto>;
8
+ private getContext;
8
9
  }
@@ -5,4 +5,5 @@ export declare class EntityUpsertAction<TEntity, TEntityId, TEntityUpdateData, T
5
5
  private readonly logger;
6
6
  constructor(services: EntityServiceLocator<TEntity, TEntityId>);
7
7
  execute(id: TEntityId, input: TEntityUpdateData): Promise<TEntityDto>;
8
+ private getContext;
8
9
  }
@@ -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
@@ -362,7 +362,9 @@ class EntityCreateAction {
362
362
  async execute(input) {
363
363
  this.logger.debug("Create action started", { input });
364
364
  const converter = this.services.resolveConverter();
365
- const createInput = converter?.createDtoToEntity(input) ?? input;
365
+ const context = await this.getContext();
366
+ const createInput = converter?.createDtoToEntity(input, context) ??
367
+ input;
366
368
  const ref = await this.services
367
369
  .resolveCreateCommand()
368
370
  .execute(createInput);
@@ -376,6 +378,14 @@ class EntityCreateAction {
376
378
  this.logger.debug("Create action completed", { input, result });
377
379
  return result;
378
380
  }
381
+ async getContext() {
382
+ const authorization = this.services.resolveAuthorizationMiddleware();
383
+ if (!authorization) {
384
+ return;
385
+ }
386
+ const contextService = this.services.resolveAuthenticationContextProvider();
387
+ return await contextService?.getContext();
388
+ }
379
389
  }
380
390
 
381
391
  class EntityDeleteAction {
@@ -491,7 +501,9 @@ class EntityUpdateAction {
491
501
  async execute(id, input) {
492
502
  this.logger.debug("Update action started", { id, input });
493
503
  const converter = this.services.resolveConverter();
494
- const updateInput = converter?.updateDtoToEntity(input) ?? input;
504
+ const context = await this.getContext();
505
+ const updateInput = converter?.updateDtoToEntity(input, context) ??
506
+ input;
495
507
  await this.services
496
508
  .resolveUpdateCommand()
497
509
  .execute(id, updateInput);
@@ -503,6 +515,14 @@ class EntityUpdateAction {
503
515
  this.logger.debug("Update action started", { id, input, result });
504
516
  return result;
505
517
  }
518
+ async getContext() {
519
+ const authorization = this.services.resolveAuthorizationMiddleware();
520
+ if (!authorization) {
521
+ return;
522
+ }
523
+ const contextService = this.services.resolveAuthenticationContextProvider();
524
+ return await contextService?.getContext();
525
+ }
506
526
  }
507
527
 
508
528
  class EntityUpsertAction {
@@ -513,7 +533,9 @@ class EntityUpsertAction {
513
533
  async execute(id, input) {
514
534
  this.logger.debug("Upsert action started", { id, input });
515
535
  const converter = this.services.resolveConverter();
516
- const updateInput = converter?.updateDtoToEntity(input) ?? input;
536
+ const context = await this.getContext();
537
+ const updateInput = converter?.updateDtoToEntity(input, context) ??
538
+ input;
517
539
  await this.services
518
540
  .resolveUpsertCommand()
519
541
  .execute(id, updateInput);
@@ -525,6 +547,14 @@ class EntityUpsertAction {
525
547
  this.logger.debug("Upsert action started", { id, input, result });
526
548
  return result;
527
549
  }
550
+ async getContext() {
551
+ const authorization = this.services.resolveAuthorizationMiddleware();
552
+ if (!authorization) {
553
+ return;
554
+ }
555
+ const contextService = this.services.resolveAuthenticationContextProvider();
556
+ return await contextService?.getContext();
557
+ }
528
558
  }
529
559
 
530
560
  class EntityVersionsSearchAction {
@@ -549,7 +579,7 @@ class EntityCreateCommand {
549
579
  this.services = services;
550
580
  }
551
581
  async execute(input) {
552
- const entity = this.adaptEntity(input);
582
+ const entity = await this.adaptEntity(input);
553
583
  await this.authorize(entity);
554
584
  const createdItem = await this.services.resolveRepository().create(entity);
555
585
  // todo: parametrize id field
@@ -562,10 +592,11 @@ class EntityCreateCommand {
562
592
  id,
563
593
  };
564
594
  }
565
- adaptEntity(input) {
595
+ async adaptEntity(input) {
596
+ const context = await this.getContext();
566
597
  const adapter = this.services.resolveAdapter();
567
598
  return adapter
568
- ? adapter.createDataToEntity(input)
599
+ ? adapter.createDataToEntity(input, context)
569
600
  : input;
570
601
  }
571
602
  async authorize(entity) {
@@ -573,8 +604,7 @@ class EntityCreateCommand {
573
604
  if (!authorization) {
574
605
  return;
575
606
  }
576
- const contextService = this.services.resolveAuthenticationContextProvider();
577
- const context = await contextService?.getContext();
607
+ const context = await this.getContext();
578
608
  if (!context) {
579
609
  return;
580
610
  }
@@ -582,6 +612,14 @@ class EntityCreateCommand {
582
612
  if (!authorizationResult.isAuthorized)
583
613
  throw new EntityOperationUnauthorizedException(EntityOperationType.Create, this.services.getEntityName(), entity);
584
614
  }
615
+ async getContext() {
616
+ const authorization = this.services.resolveAuthorizationMiddleware();
617
+ if (!authorization) {
618
+ return;
619
+ }
620
+ const contextService = this.services.resolveAuthenticationContextProvider();
621
+ return await contextService?.getContext();
622
+ }
585
623
  async versionEntity(id, entity) {
586
624
  if (!this.isVersioningEnabled()) {
587
625
  return;
@@ -819,7 +857,7 @@ class EntityUpdateCommand {
819
857
  this.services = services;
820
858
  }
821
859
  async execute(id, input) {
822
- const entity = this.adaptEntity(input);
860
+ const entity = await this.adaptEntity(input);
823
861
  await this.authorize(id);
824
862
  const updatedEntity = await this.services
825
863
  .resolveRepository()
@@ -832,10 +870,11 @@ class EntityUpdateCommand {
832
870
  id,
833
871
  };
834
872
  }
835
- adaptEntity(input) {
873
+ async adaptEntity(input) {
874
+ const context = await this.getContext();
836
875
  const adapter = this.services.resolveAdapter();
837
876
  return adapter
838
- ? adapter.updateDataToEntity(input)
877
+ ? adapter.updateDataToEntity(input, context)
839
878
  : input;
840
879
  }
841
880
  async authorize(id) {
@@ -847,8 +886,7 @@ class EntityUpdateCommand {
847
886
  if (currentEntity == null) {
848
887
  throw new EntityNotFoundException();
849
888
  }
850
- const contextService = this.services.resolveAuthenticationContextProvider();
851
- const context = await contextService?.getContext();
889
+ const context = await this.getContext();
852
890
  if (!context) {
853
891
  return;
854
892
  }
@@ -856,6 +894,14 @@ class EntityUpdateCommand {
856
894
  if (!authorizationResult.isAuthorized)
857
895
  throw new EntityOperationUnauthorizedException(EntityOperationType.Create, this.services.getEntityName(), currentEntity);
858
896
  }
897
+ async getContext() {
898
+ const authorization = this.services.resolveAuthorizationMiddleware();
899
+ if (!authorization) {
900
+ return;
901
+ }
902
+ const contextService = this.services.resolveAuthenticationContextProvider();
903
+ return await contextService?.getContext();
904
+ }
859
905
  async versionEntity(id, entity) {
860
906
  if (!this.isVersioningEnabled()) {
861
907
  return;
@@ -876,7 +922,7 @@ class EntityUpsertCommand {
876
922
  this.services = services;
877
923
  }
878
924
  async execute(id, data) {
879
- const entity = this.adaptEntity(data);
925
+ const entity = await this.adaptEntity(data);
880
926
  await this.authorize(id, entity);
881
927
  const updatedEntity = await this.upsertEntity(id, entity);
882
928
  await this.versionEntity(id, updatedEntity);
@@ -890,10 +936,11 @@ class EntityUpsertCommand {
890
936
  async upsertEntity(id, entity) {
891
937
  return await this.services.resolveRepository().upsert(id, entity);
892
938
  }
893
- adaptEntity(input) {
939
+ async adaptEntity(input) {
940
+ const context = await this.getContext();
894
941
  const adapter = this.services.resolveAdapter();
895
942
  return adapter
896
- ? adapter.updateDataToEntity(input)
943
+ ? adapter.updateDataToEntity(input, context)
897
944
  : input;
898
945
  }
899
946
  async authorize(id, entity) {
@@ -902,8 +949,7 @@ class EntityUpsertCommand {
902
949
  return;
903
950
  }
904
951
  const currentEntity = await this.services.resolveRepository().get(id);
905
- const contextService = this.services.resolveAuthenticationContextProvider();
906
- const context = await contextService?.getContext();
952
+ const context = await this.getContext();
907
953
  if (!context) {
908
954
  return;
909
955
  }
@@ -917,6 +963,14 @@ class EntityUpsertCommand {
917
963
  if (!authorizationResult.isAuthorized)
918
964
  throw new EntityOperationUnauthorizedException(EntityOperationType.Create, this.services.getEntityName(), entity);
919
965
  }
966
+ async getContext() {
967
+ const authorization = this.services.resolveAuthorizationMiddleware();
968
+ if (!authorization) {
969
+ return;
970
+ }
971
+ const contextService = this.services.resolveAuthenticationContextProvider();
972
+ return await contextService?.getContext();
973
+ }
920
974
  async versionEntity(id, entity) {
921
975
  if (!this.isVersioningEnabled()) {
922
976
  return;