@icure/api 6.4.0 → 6.4.1

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.
@@ -85,7 +85,7 @@ class IccPatientXApi extends icc_api_1.IccPatientApi {
85
85
  rev: dataOwner.rev,
86
86
  publicKey: dataOwner.publicKey,
87
87
  aesExchangeKeys: dataOwner.aesExchangeKeys,
88
- hcPartyKeys: dataOwner.hcPartyKeys
88
+ hcPartyKeys: dataOwner.hcPartyKeys,
89
89
  });
90
90
  }
91
91
  }
@@ -306,12 +306,16 @@ class IccPatientXApi extends icc_api_1.IccPatientApi {
306
306
  tryEncrypt(user, pats, requireAccessibleKey) {
307
307
  const dataOwnerId = this.dataOwnerApi.getDataOwnerOf(user);
308
308
  return Promise.all(pats.map((p) => __awaiter(this, void 0, void 0, function* () {
309
- const patientWithInitialisedEks = (p.encryptionKeys && Object.values(p.encryptionKeys).some((v) => !!v.length)
310
- ? p
311
- : yield this.initEncryptionKeys(user, p));
312
- const seks = yield this.crypto.extractKeysFromDelegationsForHcpHierarchy(dataOwnerId, patientWithInitialisedEks.id, patientWithInitialisedEks.encryptionKeys).catch((e) => { console.error(e); throw e; });
313
- if (seks.extractedKeys.length) {
314
- const key = yield this.crypto.AES.importKey('raw', (0, binary_utils_1.hex2ua)(seks.extractedKeys[0].replace(/-/g, '')));
309
+ const patientWithInitialisedEks = p.encryptionKeys && Object.values(p.encryptionKeys).some((v) => !!v.length) ? p : yield this.initEncryptionKeys(user, p);
310
+ const decryptedKeys = yield this.crypto
311
+ .extractKeysFromDelegationsForHcpHierarchy(dataOwnerId, patientWithInitialisedEks.id, patientWithInitialisedEks.encryptionKeys)
312
+ .catch((e) => {
313
+ console.error(e);
314
+ throw e;
315
+ });
316
+ const fixedKeys = this.crypto.filterAndFixValidEntityEncryptionKeyStrings(decryptedKeys.extractedKeys);
317
+ if (fixedKeys.length) {
318
+ const key = yield this.crypto.AES.importKey('raw', (0, binary_utils_1.hex2ua)(fixedKeys[0]));
315
319
  return (0, utils_1.crypt)(patientWithInitialisedEks, (obj) => this.crypto.AES.encrypt(key, (0, binary_utils_1.utf8_2ua)(JSON.stringify(obj, (k, v) => {
316
320
  return v instanceof ArrayBuffer || v instanceof Uint8Array
317
321
  ? (0, binary_utils_1.b2a)(new Uint8Array(v).reduce((d, b) => d + String.fromCharCode(b), ''))
@@ -337,7 +341,9 @@ class IccPatientXApi extends icc_api_1.IccPatientApi {
337
341
  tryDecryptOrReturnOriginal(user, patients, fillDelegations = true) {
338
342
  return __awaiter(this, void 0, void 0, function* () {
339
343
  const dataOwnerId = this.dataOwnerApi.getDataOwnerOf(user);
340
- const ids = yield (user.healthcarePartyId ? this.hcpartyApi.getHealthcarePartyHierarchyIds(user.healthcarePartyId) : Promise.resolve([dataOwnerId]));
344
+ const ids = yield (user.healthcarePartyId
345
+ ? this.hcpartyApi.getHealthcarePartyHierarchyIds(user.healthcarePartyId)
346
+ : Promise.resolve([dataOwnerId]));
341
347
  //First check that we have no dangling delegation
342
348
  const patsWithMissingDelegations = patients.filter((p) => p.delegations &&
343
349
  ids.some((id) => p.delegations[id] && !p.delegations[id].length) &&
@@ -359,10 +365,11 @@ class IccPatientXApi extends icc_api_1.IccPatientApi {
359
365
  ? yield this.crypto
360
366
  .extractKeysFromDelegationsForHcpHierarchy(hcpId, p.id, _.size(p.encryptionKeys) ? p.encryptionKeys : p.delegations)
361
367
  .then(({ extractedKeys: sfks }) => {
368
+ sfks = this.crypto.filterAndFixValidEntityEncryptionKeyStrings(sfks);
362
369
  if (!sfks || !sfks.length) {
363
370
  return Promise.resolve(undefined);
364
371
  }
365
- return this.crypto.AES.importKey('raw', (0, binary_utils_1.hex2ua)(sfks[0].replace(/-/g, ''))).then((key) => (0, utils_1.decrypt)(p, (ec) => this.crypto.AES.decrypt(key, ec)
372
+ return this.crypto.AES.importKey('raw', (0, binary_utils_1.hex2ua)(sfks[0])).then((key) => (0, utils_1.decrypt)(p, (ec) => this.crypto.AES.decrypt(key, ec)
366
373
  .then((dec) => {
367
374
  const jsonContent = dec && (0, binary_utils_1.ua2utf8)(dec);
368
375
  try {
@@ -486,31 +493,30 @@ class IccPatientXApi extends icc_api_1.IccPatientApi {
486
493
  return this.crypto.extractDelegationsSFKsAndEncryptionSKs(patient, ownerId).then(([delSfks, ecKeys]) => {
487
494
  return delSfks.length
488
495
  ? Promise.all([
489
- (0, utils_1.retry)(() => (usingPost ? this.helementApi
490
- .findHealthElementsDelegationsStubsByHCPartyPatientForeignKeysUsingPost(ownerId, _.uniq(delSfks)) : this.helementApi
491
- .findHealthElementsDelegationsStubsByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(',')))
492
- .then((hes) => parentId
493
- ? (usingPost ? this.helementApi
494
- .findHealthElementsDelegationsStubsByHCPartyPatientForeignKeysUsingPost(parentId, _.uniq(delSfks)) : this.helementApi
495
- .findHealthElementsDelegationsStubsByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))).then((moreHes) => _.uniqBy(hes.concat(moreHes), 'id'))
496
+ (0, utils_1.retry)(() => (usingPost
497
+ ? this.helementApi.findHealthElementsDelegationsStubsByHCPartyPatientForeignKeysUsingPost(ownerId, _.uniq(delSfks))
498
+ : this.helementApi.findHealthElementsDelegationsStubsByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))).then((hes) => parentId
499
+ ? (usingPost
500
+ ? this.helementApi.findHealthElementsDelegationsStubsByHCPartyPatientForeignKeysUsingPost(parentId, _.uniq(delSfks))
501
+ : this.helementApi.findHealthElementsDelegationsStubsByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))).then((moreHes) => _.uniqBy(hes.concat(moreHes), 'id'))
496
502
  : hes)),
497
- (0, utils_1.retry)(() => (usingPost ? this.formApi
498
- .findFormsDelegationsStubsByHCPartyPatientForeignKeysUsingPost(ownerId, _.uniq(delSfks)) : this.formApi
499
- .findFormsDelegationsStubsByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))).then((frms) => parentId
500
- ? (usingPost ? this.formApi
501
- .findFormsDelegationsStubsByHCPartyPatientForeignKeysUsingPost(parentId, _.uniq(delSfks)) : this.formApi
502
- .findFormsDelegationsStubsByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))).then((moreFrms) => _.uniqBy(frms.concat(moreFrms), 'id'))
503
+ (0, utils_1.retry)(() => (usingPost
504
+ ? this.formApi.findFormsDelegationsStubsByHCPartyPatientForeignKeysUsingPost(ownerId, _.uniq(delSfks))
505
+ : this.formApi.findFormsDelegationsStubsByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))).then((frms) => parentId
506
+ ? (usingPost
507
+ ? this.formApi.findFormsDelegationsStubsByHCPartyPatientForeignKeysUsingPost(parentId, _.uniq(delSfks))
508
+ : this.formApi.findFormsDelegationsStubsByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))).then((moreFrms) => _.uniqBy(frms.concat(moreFrms), 'id'))
503
509
  : frms)),
504
- (0, utils_1.retry)(() => (usingPost ? this.contactApi
505
- .findByHCPartyPatientSecretFKeysUsingPost(ownerId, undefined, undefined, _.uniq(delSfks)) : this.contactApi
506
- .findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(','))).then((ctcs) => parentId
507
- ? (usingPost ? this.contactApi
508
- .findByHCPartyPatientSecretFKeysUsingPost(parentId, undefined, undefined, _.uniq(delSfks)) : this.contactApi
509
- .findByHCPartyPatientSecretFKeys(parentId, _.uniq(delSfks).join(','))).then((moreCtcs) => _.uniqBy(ctcs.concat(moreCtcs), 'id'))
510
+ (0, utils_1.retry)(() => (usingPost
511
+ ? this.contactApi.findByHCPartyPatientSecretFKeysUsingPost(ownerId, undefined, undefined, _.uniq(delSfks))
512
+ : this.contactApi.findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(','))).then((ctcs) => parentId
513
+ ? (usingPost
514
+ ? this.contactApi.findByHCPartyPatientSecretFKeysUsingPost(parentId, undefined, undefined, _.uniq(delSfks))
515
+ : this.contactApi.findByHCPartyPatientSecretFKeys(parentId, _.uniq(delSfks).join(','))).then((moreCtcs) => _.uniqBy(ctcs.concat(moreCtcs), 'id'))
510
516
  : ctcs)),
511
- (0, utils_1.retry)(() => (usingPost ? this.invoiceApi
512
- .findInvoicesDelegationsStubsByHCPartyPatientForeignKeysUsingPost(ownerId, _.uniq(delSfks)) : this.invoiceApi
513
- .findInvoicesDelegationsStubsByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))).then((ivs) => parentId
517
+ (0, utils_1.retry)(() => (usingPost
518
+ ? this.invoiceApi.findInvoicesDelegationsStubsByHCPartyPatientForeignKeysUsingPost(ownerId, _.uniq(delSfks))
519
+ : this.invoiceApi.findInvoicesDelegationsStubsByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))).then((ivs) => parentId
514
520
  ? this.invoiceApi
515
521
  .findInvoicesDelegationsStubsByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))
516
522
  .then((moreIvs) => _.uniqBy(ivs.concat(moreIvs), 'id'))
@@ -522,12 +528,12 @@ class IccPatientXApi extends icc_api_1.IccPatientApi {
522
528
  .findClassificationsByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))
523
529
  .then((moreCls) => _.uniqBy(cls.concat(moreCls), 'id'))
524
530
  : cls)),
525
- (0, utils_1.retry)(() => (usingPost ? this.calendarItemApi
526
- .findByHCPartyPatientSecretFKeysArray(ownerId, _.uniq(delSfks)) : this.calendarItemApi
527
- .findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(','))).then((cls) => parentId
528
- ? (usingPost ? this.calendarItemApi
529
- .findByHCPartyPatientSecretFKeysArray(parentId, _.uniq(delSfks)) : this.calendarItemApi
530
- .findByHCPartyPatientSecretFKeys(parentId, _.uniq(delSfks).join(','))).then((moreCls) => _.uniqBy(cls.concat(moreCls), 'id'))
531
+ (0, utils_1.retry)(() => (usingPost
532
+ ? this.calendarItemApi.findByHCPartyPatientSecretFKeysArray(ownerId, _.uniq(delSfks))
533
+ : this.calendarItemApi.findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(','))).then((cls) => parentId
534
+ ? (usingPost
535
+ ? this.calendarItemApi.findByHCPartyPatientSecretFKeysArray(parentId, _.uniq(delSfks))
536
+ : this.calendarItemApi.findByHCPartyPatientSecretFKeys(parentId, _.uniq(delSfks).join(','))).then((moreCls) => _.uniqBy(cls.concat(moreCls), 'id'))
531
537
  : cls)),
532
538
  ]).then(([hes, frms, ctcs, ivs, cls, cis]) => {
533
539
  const cloneKeysAndDelegations = function (x) {
@@ -746,33 +752,33 @@ class IccPatientXApi extends icc_api_1.IccPatientApi {
746
752
  return this.crypto.extractDelegationsSFKsAndEncryptionSKs(patient, ownerId).then(([delSfks, ecKeys]) => {
747
753
  return delSfks.length
748
754
  ? Promise.all([
749
- (0, utils_1.retry)(() => (usingPost ? this.helementApi
750
- .findByHCPartyPatientSecretFKeysArray(ownerId, _.uniq(delSfks)) : this.helementApi
751
- .findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(','))).then((hes) => parentId
755
+ (0, utils_1.retry)(() => (usingPost
756
+ ? this.helementApi.findByHCPartyPatientSecretFKeysArray(ownerId, _.uniq(delSfks))
757
+ : this.helementApi.findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(','))).then((hes) => parentId
752
758
  ? this.helementApi
753
759
  .findByHCPartyPatientSecretFKeys(parentId, _.uniq(delSfks).join(','))
754
760
  .then((moreHes) => _.uniqBy(hes.concat(moreHes), 'id'))
755
761
  : hes)),
756
- (0, utils_1.retry)(() => (usingPost ? this.formApi
757
- .findFormsByHCPartyPatientForeignKeysUsingPost(ownerId, undefined, undefined, undefined, _.uniq(delSfks)) : this.formApi
758
- .findFormsByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))).then((frms) => parentId
759
- ? (usingPost ? this.formApi
760
- .findFormsByHCPartyPatientForeignKeysUsingPost(parentId, undefined, undefined, undefined, _.uniq(delSfks)) : this.formApi
761
- .findFormsByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))).then((moreFrms) => _.uniqBy(frms.concat(moreFrms), 'id'))
762
+ (0, utils_1.retry)(() => (usingPost
763
+ ? this.formApi.findFormsByHCPartyPatientForeignKeysUsingPost(ownerId, undefined, undefined, undefined, _.uniq(delSfks))
764
+ : this.formApi.findFormsByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))).then((frms) => parentId
765
+ ? (usingPost
766
+ ? this.formApi.findFormsByHCPartyPatientForeignKeysUsingPost(parentId, undefined, undefined, undefined, _.uniq(delSfks))
767
+ : this.formApi.findFormsByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))).then((moreFrms) => _.uniqBy(frms.concat(moreFrms), 'id'))
762
768
  : frms)),
763
- (0, utils_1.retry)(() => (usingPost ? this.contactApi
764
- .findByHCPartyPatientSecretFKeysArray(ownerId, _.uniq(delSfks)) : this.contactApi
765
- .findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(','))).then((ctcs) => parentId
766
- ? (usingPost ? this.contactApi
767
- .findByHCPartyPatientSecretFKeysArray(parentId, _.uniq(delSfks)) : this.contactApi
768
- .findByHCPartyPatientSecretFKeys(parentId, _.uniq(delSfks).join(','))).then((moreCtcs) => _.uniqBy(ctcs.concat(moreCtcs), 'id'))
769
+ (0, utils_1.retry)(() => (usingPost
770
+ ? this.contactApi.findByHCPartyPatientSecretFKeysArray(ownerId, _.uniq(delSfks))
771
+ : this.contactApi.findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(','))).then((ctcs) => parentId
772
+ ? (usingPost
773
+ ? this.contactApi.findByHCPartyPatientSecretFKeysArray(parentId, _.uniq(delSfks))
774
+ : this.contactApi.findByHCPartyPatientSecretFKeys(parentId, _.uniq(delSfks).join(','))).then((moreCtcs) => _.uniqBy(ctcs.concat(moreCtcs), 'id'))
769
775
  : ctcs)),
770
- (0, utils_1.retry)(() => (usingPost ? this.invoiceApi
771
- .findInvoicesByHCPartyPatientForeignKeysUsingPost(ownerId, _.uniq(delSfks)) : this.invoiceApi
772
- .findInvoicesByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))).then((ivs) => parentId
773
- ? (usingPost ? this.invoiceApi
774
- .findInvoicesByHCPartyPatientForeignKeysUsingPost(parentId, _.uniq(delSfks)) : this.invoiceApi
775
- .findInvoicesByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))).then((moreIvs) => _.uniqBy(ivs.concat(moreIvs), 'id'))
776
+ (0, utils_1.retry)(() => (usingPost
777
+ ? this.invoiceApi.findInvoicesByHCPartyPatientForeignKeysUsingPost(ownerId, _.uniq(delSfks))
778
+ : this.invoiceApi.findInvoicesByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))).then((ivs) => parentId
779
+ ? (usingPost
780
+ ? this.invoiceApi.findInvoicesByHCPartyPatientForeignKeysUsingPost(parentId, _.uniq(delSfks))
781
+ : this.invoiceApi.findInvoicesByHCPartyPatientForeignKeys(parentId, _.uniq(delSfks).join(','))).then((moreIvs) => _.uniqBy(ivs.concat(moreIvs), 'id'))
776
782
  : ivs)),
777
783
  (0, utils_1.retry)(() => this.classificationApi
778
784
  .findClassificationsByHCPartyPatientForeignKeys(ownerId, _.uniq(delSfks).join(','))
@@ -784,13 +790,13 @@ class IccPatientXApi extends icc_api_1.IccPatientApi {
784
790
  (0, utils_1.retry)(() => __awaiter(this, void 0, void 0, function* () {
785
791
  const delegationSFKs = _.uniq(delSfks).join(',');
786
792
  try {
787
- let calendarItems = yield (usingPost ?
788
- this.calendarItemApi.findByHCPartyPatientSecretFKeysArray(ownerId, _.uniq(delSfks)) :
789
- this.calendarItemApi.findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(',')));
793
+ let calendarItems = yield (usingPost
794
+ ? this.calendarItemApi.findByHCPartyPatientSecretFKeysArray(ownerId, _.uniq(delSfks))
795
+ : this.calendarItemApi.findByHCPartyPatientSecretFKeys(ownerId, _.uniq(delSfks).join(',')));
790
796
  if (parentId) {
791
- const moreCalendarItems = yield (usingPost ?
792
- this.calendarItemApi.findByHCPartyPatientSecretFKeysArray(parentId, _.uniq(delSfks)) :
793
- this.calendarItemApi.findByHCPartyPatientSecretFKeys(parentId, _.uniq(delSfks).join(',')));
797
+ const moreCalendarItems = yield (usingPost
798
+ ? this.calendarItemApi.findByHCPartyPatientSecretFKeysArray(parentId, _.uniq(delSfks))
799
+ : this.calendarItemApi.findByHCPartyPatientSecretFKeys(parentId, _.uniq(delSfks).join(',')));
794
800
  calendarItems = _.uniqBy(calendarItems.concat(moreCalendarItems), 'id');
795
801
  }
796
802
  return calendarItems;