@sphereon/ssi-sdk.data-store 0.36.1-feature.vdx24.einvoice.inbox.141 → 0.36.1-feature.vdx24.einvoice.inbox.142

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/index.js CHANGED
@@ -4958,10 +4958,54 @@ var ContactStore = class extends AbstractContactStore {
4958
4958
  return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`));
4959
4959
  }
4960
4960
  }
4961
- const identityEntity = identityEntityFrom(identity);
4961
+ const identityRepository = (await this.dbConnection).getRepository(IdentityEntity);
4962
+ const correlationIdentifierRepository = (await this.dbConnection).getRepository(CorrelationIdentifierEntity);
4963
+ const existingCorrelationIdentifier = await correlationIdentifierRepository.findOne({
4964
+ where: {
4965
+ correlationId: identity.identifier.correlationId
4966
+ }
4967
+ });
4968
+ if (existingCorrelationIdentifier) {
4969
+ const existingIdentity = await identityRepository.findOne({
4970
+ where: {
4971
+ identifier: {
4972
+ id: existingCorrelationIdentifier.id
4973
+ }
4974
+ }
4975
+ });
4976
+ if (existingIdentity) {
4977
+ debug("Identity with same correlationId already exists, returning existing identity", identity.identifier.correlationId);
4978
+ return identityFrom(existingIdentity);
4979
+ }
4980
+ }
4981
+ const existingAlias = await identityRepository.findOne({
4982
+ where: {
4983
+ alias: identity.alias
4984
+ }
4985
+ });
4986
+ let uniqueAlias = identity.alias;
4987
+ if (existingAlias) {
4988
+ let counter = 1;
4989
+ while (await identityRepository.findOne({
4990
+ where: {
4991
+ alias: `${identity.alias}_${counter}`
4992
+ }
4993
+ })) {
4994
+ counter++;
4995
+ }
4996
+ uniqueAlias = `${identity.alias}_${counter}`;
4997
+ debug("Alias collision detected, using unique alias", {
4998
+ original: identity.alias,
4999
+ unique: uniqueAlias
5000
+ });
5001
+ }
5002
+ const identityEntity = identityEntityFrom({
5003
+ ...identity,
5004
+ alias: uniqueAlias
5005
+ });
4962
5006
  identityEntity.party = party;
4963
5007
  debug("Adding identity", identity);
4964
- const result = await (await this.dbConnection).getRepository(IdentityEntity).save(identityEntity, {
5008
+ const result = await identityRepository.save(identityEntity, {
4965
5009
  transaction: true
4966
5010
  });
4967
5011
  return identityFrom(result);