@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.
package/dist/cjs/index.js CHANGED
@@ -376,7 +376,9 @@ class EntityCreateAction {
376
376
  async execute(input) {
377
377
  this.logger.debug("Create action started", { input });
378
378
  const converter = this.services.resolveConverter();
379
- const createInput = converter?.createDtoToEntity(input) ?? input;
379
+ const context = await this.getContext();
380
+ const createInput = converter?.createDtoToEntity(input, context) ??
381
+ input;
380
382
  const ref = await this.services
381
383
  .resolveCreateCommand()
382
384
  .execute(createInput);
@@ -390,6 +392,14 @@ class EntityCreateAction {
390
392
  this.logger.debug("Create action completed", { input, result });
391
393
  return result;
392
394
  }
395
+ async getContext() {
396
+ const authorization = this.services.resolveAuthorizationMiddleware();
397
+ if (!authorization) {
398
+ return;
399
+ }
400
+ const contextService = this.services.resolveAuthenticationContextProvider();
401
+ return await contextService?.getContext();
402
+ }
393
403
  }
394
404
 
395
405
  class EntityDeleteAction {
@@ -505,7 +515,9 @@ class EntityUpdateAction {
505
515
  async execute(id, input) {
506
516
  this.logger.debug("Update action started", { id, input });
507
517
  const converter = this.services.resolveConverter();
508
- const updateInput = converter?.updateDtoToEntity(input) ?? input;
518
+ const context = await this.getContext();
519
+ const updateInput = converter?.updateDtoToEntity(input, context) ??
520
+ input;
509
521
  await this.services
510
522
  .resolveUpdateCommand()
511
523
  .execute(id, updateInput);
@@ -517,6 +529,14 @@ class EntityUpdateAction {
517
529
  this.logger.debug("Update action started", { id, input, result });
518
530
  return result;
519
531
  }
532
+ async getContext() {
533
+ const authorization = this.services.resolveAuthorizationMiddleware();
534
+ if (!authorization) {
535
+ return;
536
+ }
537
+ const contextService = this.services.resolveAuthenticationContextProvider();
538
+ return await contextService?.getContext();
539
+ }
520
540
  }
521
541
 
522
542
  class EntityUpsertAction {
@@ -527,7 +547,9 @@ class EntityUpsertAction {
527
547
  async execute(id, input) {
528
548
  this.logger.debug("Upsert action started", { id, input });
529
549
  const converter = this.services.resolveConverter();
530
- const updateInput = converter?.updateDtoToEntity(input) ?? input;
550
+ const context = await this.getContext();
551
+ const updateInput = converter?.updateDtoToEntity(input, context) ??
552
+ input;
531
553
  await this.services
532
554
  .resolveUpsertCommand()
533
555
  .execute(id, updateInput);
@@ -539,6 +561,14 @@ class EntityUpsertAction {
539
561
  this.logger.debug("Upsert action started", { id, input, result });
540
562
  return result;
541
563
  }
564
+ async getContext() {
565
+ const authorization = this.services.resolveAuthorizationMiddleware();
566
+ if (!authorization) {
567
+ return;
568
+ }
569
+ const contextService = this.services.resolveAuthenticationContextProvider();
570
+ return await contextService?.getContext();
571
+ }
542
572
  }
543
573
 
544
574
  class EntityVersionsSearchAction {
@@ -563,7 +593,7 @@ class EntityCreateCommand {
563
593
  this.services = services;
564
594
  }
565
595
  async execute(input) {
566
- const entity = this.adaptEntity(input);
596
+ const entity = await this.adaptEntity(input);
567
597
  await this.authorize(entity);
568
598
  const createdItem = await this.services.resolveRepository().create(entity);
569
599
  // todo: parametrize id field
@@ -576,10 +606,11 @@ class EntityCreateCommand {
576
606
  id,
577
607
  };
578
608
  }
579
- adaptEntity(input) {
609
+ async adaptEntity(input) {
610
+ const context = await this.getContext();
580
611
  const adapter = this.services.resolveAdapter();
581
612
  return adapter
582
- ? adapter.createDataToEntity(input)
613
+ ? adapter.createDataToEntity(input, context)
583
614
  : input;
584
615
  }
585
616
  async authorize(entity) {
@@ -587,8 +618,7 @@ class EntityCreateCommand {
587
618
  if (!authorization) {
588
619
  return;
589
620
  }
590
- const contextService = this.services.resolveAuthenticationContextProvider();
591
- const context = await contextService?.getContext();
621
+ const context = await this.getContext();
592
622
  if (!context) {
593
623
  return;
594
624
  }
@@ -596,6 +626,14 @@ class EntityCreateCommand {
596
626
  if (!authorizationResult.isAuthorized)
597
627
  throw new EntityOperationUnauthorizedException(exports.EntityOperationType.Create, this.services.getEntityName(), entity);
598
628
  }
629
+ async getContext() {
630
+ const authorization = this.services.resolveAuthorizationMiddleware();
631
+ if (!authorization) {
632
+ return;
633
+ }
634
+ const contextService = this.services.resolveAuthenticationContextProvider();
635
+ return await contextService?.getContext();
636
+ }
599
637
  async versionEntity(id, entity) {
600
638
  if (!this.isVersioningEnabled()) {
601
639
  return;
@@ -833,7 +871,7 @@ class EntityUpdateCommand {
833
871
  this.services = services;
834
872
  }
835
873
  async execute(id, input) {
836
- const entity = this.adaptEntity(input);
874
+ const entity = await this.adaptEntity(input);
837
875
  await this.authorize(id);
838
876
  const updatedEntity = await this.services
839
877
  .resolveRepository()
@@ -846,10 +884,11 @@ class EntityUpdateCommand {
846
884
  id,
847
885
  };
848
886
  }
849
- adaptEntity(input) {
887
+ async adaptEntity(input) {
888
+ const context = await this.getContext();
850
889
  const adapter = this.services.resolveAdapter();
851
890
  return adapter
852
- ? adapter.updateDataToEntity(input)
891
+ ? adapter.updateDataToEntity(input, context)
853
892
  : input;
854
893
  }
855
894
  async authorize(id) {
@@ -861,8 +900,7 @@ class EntityUpdateCommand {
861
900
  if (currentEntity == null) {
862
901
  throw new EntityNotFoundException();
863
902
  }
864
- const contextService = this.services.resolveAuthenticationContextProvider();
865
- const context = await contextService?.getContext();
903
+ const context = await this.getContext();
866
904
  if (!context) {
867
905
  return;
868
906
  }
@@ -870,6 +908,14 @@ class EntityUpdateCommand {
870
908
  if (!authorizationResult.isAuthorized)
871
909
  throw new EntityOperationUnauthorizedException(exports.EntityOperationType.Create, this.services.getEntityName(), currentEntity);
872
910
  }
911
+ async getContext() {
912
+ const authorization = this.services.resolveAuthorizationMiddleware();
913
+ if (!authorization) {
914
+ return;
915
+ }
916
+ const contextService = this.services.resolveAuthenticationContextProvider();
917
+ return await contextService?.getContext();
918
+ }
873
919
  async versionEntity(id, entity) {
874
920
  if (!this.isVersioningEnabled()) {
875
921
  return;
@@ -890,7 +936,7 @@ class EntityUpsertCommand {
890
936
  this.services = services;
891
937
  }
892
938
  async execute(id, data) {
893
- const entity = this.adaptEntity(data);
939
+ const entity = await this.adaptEntity(data);
894
940
  await this.authorize(id, entity);
895
941
  const updatedEntity = await this.upsertEntity(id, entity);
896
942
  await this.versionEntity(id, updatedEntity);
@@ -904,10 +950,11 @@ class EntityUpsertCommand {
904
950
  async upsertEntity(id, entity) {
905
951
  return await this.services.resolveRepository().upsert(id, entity);
906
952
  }
907
- adaptEntity(input) {
953
+ async adaptEntity(input) {
954
+ const context = await this.getContext();
908
955
  const adapter = this.services.resolveAdapter();
909
956
  return adapter
910
- ? adapter.updateDataToEntity(input)
957
+ ? adapter.updateDataToEntity(input, context)
911
958
  : input;
912
959
  }
913
960
  async authorize(id, entity) {
@@ -916,8 +963,7 @@ class EntityUpsertCommand {
916
963
  return;
917
964
  }
918
965
  const currentEntity = await this.services.resolveRepository().get(id);
919
- const contextService = this.services.resolveAuthenticationContextProvider();
920
- const context = await contextService?.getContext();
966
+ const context = await this.getContext();
921
967
  if (!context) {
922
968
  return;
923
969
  }
@@ -931,6 +977,14 @@ class EntityUpsertCommand {
931
977
  if (!authorizationResult.isAuthorized)
932
978
  throw new EntityOperationUnauthorizedException(exports.EntityOperationType.Create, this.services.getEntityName(), entity);
933
979
  }
980
+ async getContext() {
981
+ const authorization = this.services.resolveAuthorizationMiddleware();
982
+ if (!authorization) {
983
+ return;
984
+ }
985
+ const contextService = this.services.resolveAuthenticationContextProvider();
986
+ return await contextService?.getContext();
987
+ }
934
988
  async versionEntity(id, entity) {
935
989
  if (!this.isVersioningEnabled()) {
936
990
  return;