@processpuzzle/base-entity 0.7.4 → 0.7.5

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.
@@ -590,6 +590,11 @@ function BaseEntityContainerStore() {
590
590
  }));
591
591
  }
592
592
 
593
+ function assertPersistedEntity(entity) {
594
+ if (typeof entity.id !== 'string')
595
+ throw new Error('Persisted entity must have an id');
596
+ }
597
+
593
598
  let BaseEntityRestService = class BaseEntityRestService {
594
599
  entityMapper;
595
600
  urlProperty;
@@ -641,7 +646,7 @@ let BaseEntityRestService = class BaseEntityRestService {
641
646
  const fullUrl = this.buildFullUrl(this.resourceUrl, queryCondition);
642
647
  if (fullUrl) {
643
648
  return this.httpClient.get(fullUrl, { headers: this.headers }).pipe(map((httpResponse) => {
644
- return this.mapSimpleResponse(httpResponse);
649
+ return this.mapEntityResponse(httpResponse);
645
650
  }));
646
651
  }
647
652
  else
@@ -652,7 +657,7 @@ let BaseEntityRestService = class BaseEntityRestService {
652
657
  const fullUrl = this.buildFullUrl(this.resourceUrl, {});
653
658
  if (fullUrl) {
654
659
  return this.httpClient.post(fullUrl, dto, { headers: this.headers }).pipe(map((response) => {
655
- return this.entityMapper.fromDto(response, id);
660
+ return this.mapEntityResponse(response, id);
656
661
  }));
657
662
  }
658
663
  else
@@ -664,7 +669,7 @@ let BaseEntityRestService = class BaseEntityRestService {
664
669
  const fullUrl = this.buildFullUrl(this.resourceUrl + '/%{id}', { pathParams });
665
670
  if (fullUrl) {
666
671
  return this.httpClient.put(fullUrl, dto, { headers: this.headers }).pipe(map((response) => {
667
- return this.entityMapper.fromDto(response);
672
+ return this.mapEntityResponse(response);
668
673
  }));
669
674
  }
670
675
  else
@@ -703,7 +708,7 @@ let BaseEntityRestService = class BaseEntityRestService {
703
708
  mapPagedResponse(response) {
704
709
  const subjectPage = response[0];
705
710
  const content = subjectPage.content.map((dto, index) => {
706
- return this.entityMapper.fromDto(dto, index);
711
+ return this.mapEntityResponse(dto, index);
707
712
  });
708
713
  return {
709
714
  page: subjectPage.page,
@@ -715,13 +720,18 @@ let BaseEntityRestService = class BaseEntityRestService {
715
720
  mapSimpleResponse(response) {
716
721
  if (Object.prototype.toString.call(response) === '[object Array]') {
717
722
  return response.map((dto, index) => {
718
- return this.entityMapper.fromDto(dto, index);
723
+ return this.mapEntityResponse(dto, index);
719
724
  });
720
725
  }
721
726
  else {
722
- return new Array(this.entityMapper.fromDto(response));
727
+ return new Array(this.mapEntityResponse(response));
723
728
  }
724
729
  }
730
+ mapEntityResponse(response, index) {
731
+ const entity = this.entityMapper.fromDto(response, index);
732
+ assertPersistedEntity(entity);
733
+ return entity;
734
+ }
725
735
  };
726
736
  BaseEntityRestService = __decorate([
727
737
  __param(0, Inject('entityMapper'))
@@ -1721,7 +1731,11 @@ class EntityComponentsListComponent extends BaseFormControlComponent {
1721
1731
  }
1722
1732
  components() {
1723
1733
  const value = this.formGroup.get(this.config().attrName)?.value ?? this.value();
1724
- return Array.isArray(value) ? value : [];
1734
+ if (!Array.isArray(value)) {
1735
+ return [];
1736
+ }
1737
+ value.forEach((component) => assertPersistedEntity(component));
1738
+ return value;
1725
1739
  }
1726
1740
  addComponentTitle() {
1727
1741
  return 'Add ' + this.linkedEntityType().entityName;
@@ -1742,7 +1756,9 @@ class EntityComponentsListComponent extends BaseFormControlComponent {
1742
1756
  if (!responsePayload?.payload) {
1743
1757
  return;
1744
1758
  }
1745
- const components = [...this.components(), responsePayload.payload];
1759
+ const selectedComponent = responsePayload.payload;
1760
+ assertPersistedEntity(selectedComponent);
1761
+ const components = [...this.components(), selectedComponent];
1746
1762
  const attrName = this.config().attrName;
1747
1763
  const entity = this.entity();
1748
1764
  entity[attrName] = components;
@@ -2419,7 +2435,7 @@ let BaseEntityFirestoreService = class BaseEntityFirestoreService {
2419
2435
  const docRef = doc(this.firestore, this.collectionName, id);
2420
2436
  return from(getDoc(docRef)
2421
2437
  .then((document) => {
2422
- return this.mapDocument(document.data());
2438
+ return document.exists() ? this.mapDocument({ id: document.id, ...document.data() }) : undefined;
2423
2439
  })
2424
2440
  .catch((error) => {
2425
2441
  throw new Error(`Error: ${error} occurred while finding document by id: ${id}`);
@@ -2434,10 +2450,11 @@ let BaseEntityFirestoreService = class BaseEntityFirestoreService {
2434
2450
  // endregion
2435
2451
  // protected, private helper methods
2436
2452
  async addAsync(entity) {
2437
- const newDoc = doc(this.collection, entity.id);
2453
+ const newDoc = entity.id ? doc(this.collection, entity.id) : doc(this.collection);
2438
2454
  await setDoc(newDoc, this.mapEntity(entity));
2455
+ const savedEntity = { ...entity, id: newDoc.id };
2439
2456
  return new Promise((resolve, reject) => {
2440
- resolve(entity);
2457
+ resolve(this.mapDocument(savedEntity));
2441
2458
  reject(new Error('Failed to update entity: ' + entity));
2442
2459
  });
2443
2460
  }
@@ -2476,12 +2493,14 @@ let BaseEntityFirestoreService = class BaseEntityFirestoreService {
2476
2493
  mapCollection(collection) {
2477
2494
  const entities = [];
2478
2495
  collection.pipe(map((documents) => {
2479
- documents.forEach((document) => entities.push(this.entityMapper.fromDto(document)));
2496
+ documents.forEach((document) => entities.push(this.mapDocument(document)));
2480
2497
  }));
2481
2498
  return entities;
2482
2499
  }
2483
2500
  mapDocument(document) {
2484
- return this.entityMapper.fromDto(document);
2501
+ const entity = this.entityMapper.fromDto(document);
2502
+ assertPersistedEntity(entity);
2503
+ return entity;
2485
2504
  }
2486
2505
  mapEntity(entity) {
2487
2506
  return this.entityMapper.toDto(entity);