@sphereon/ssi-sdk.data-store 0.23.5-unstable.88 → 0.24.0

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.
Files changed (146) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +77 -77
  3. package/dist/index.d.ts +3 -9
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +2 -15
  6. package/dist/index.js.map +1 -1
  7. package/dist/migrations/generic/index.d.ts +0 -2
  8. package/dist/migrations/generic/index.d.ts.map +1 -1
  9. package/dist/migrations/generic/index.js +1 -4
  10. package/dist/migrations/generic/index.js.map +1 -1
  11. package/dist/migrations/index.d.ts +1 -1
  12. package/dist/migrations/index.d.ts.map +1 -1
  13. package/dist/migrations/index.js +1 -2
  14. package/dist/migrations/index.js.map +1 -1
  15. package/dist/migrations/postgres/1690925872592-CreateContacts.js +1 -1
  16. package/dist/migrations/postgres/1690925872592-CreateContacts.js.map +1 -1
  17. package/dist/migrations/postgres/1708525189001-CreateDigitalCredential.js +22 -22
  18. package/dist/migrations/postgres/1708797018115-CreateMachineStateStore.js +16 -16
  19. package/dist/migrations/sqlite/1708525189002-CreateDigitalCredential.js +21 -21
  20. package/dist/migrations/sqlite/1708796002272-CreateMachineStateStore.js +15 -15
  21. package/dist/types/index.d.ts +0 -2
  22. package/dist/types/index.d.ts.map +1 -1
  23. package/dist/types/index.js +0 -2
  24. package/dist/types/index.js.map +1 -1
  25. package/package.json +4 -5
  26. package/src/__tests__/contact.entities.test.ts +2542 -2542
  27. package/src/__tests__/contact.store.test.ts +2471 -2471
  28. package/src/__tests__/digitalCredential.entities.test.ts +254 -254
  29. package/src/__tests__/digitalCredential.store.test.ts +294 -294
  30. package/src/__tests__/eventLogger.entities.test.ts +73 -73
  31. package/src/__tests__/eventLogger.store.test.ts +136 -136
  32. package/src/__tests__/issuanceBranding.entities.test.ts +844 -844
  33. package/src/__tests__/issuanceBranding.store.test.ts +1884 -1884
  34. package/src/__tests__/machineState.entities.test.ts +51 -51
  35. package/src/__tests__/machineState.store.test.ts +174 -174
  36. package/src/contact/AbstractContactStore.ts +71 -71
  37. package/src/contact/ContactStore.ts +723 -723
  38. package/src/digitalCredential/AbstractDigitalCredentialStore.ts +17 -17
  39. package/src/digitalCredential/DigitalCredentialStore.ts +127 -127
  40. package/src/entities/contact/BaseContactEntity.ts +39 -39
  41. package/src/entities/contact/ConnectionEntity.ts +29 -29
  42. package/src/entities/contact/CorrelationIdentifierEntity.ts +37 -37
  43. package/src/entities/contact/DidAuthConfigEntity.ts +14 -14
  44. package/src/entities/contact/ElectronicAddressEntity.ts +63 -63
  45. package/src/entities/contact/IdentityEntity.ts +97 -97
  46. package/src/entities/contact/IdentityMetadataItemEntity.ts +35 -35
  47. package/src/entities/contact/NaturalPersonEntity.ts +38 -38
  48. package/src/entities/contact/OpenIdConfigEntity.ts +26 -26
  49. package/src/entities/contact/OrganizationEntity.ts +34 -34
  50. package/src/entities/contact/PartyEntity.ts +110 -110
  51. package/src/entities/contact/PartyRelationshipEntity.ts +61 -61
  52. package/src/entities/contact/PartyTypeEntity.ts +62 -62
  53. package/src/entities/contact/PhysicalAddressEntity.ts +87 -87
  54. package/src/entities/digitalCredential/DigitalCredentialEntity.ts +64 -64
  55. package/src/entities/eventLogger/AuditEventEntity.ts +99 -99
  56. package/src/entities/issuanceBranding/CredentialBrandingEntity.ts +78 -78
  57. package/src/entities/issuanceBranding/ImageAttributesEntity.ts +57 -57
  58. package/src/entities/issuanceBranding/IssuerBrandingEntity.ts +72 -72
  59. package/src/entities/machineState/MachineStateInfoEntity.ts +58 -58
  60. package/src/entities/statusList2021/StatusList2021Entity.ts +96 -96
  61. package/src/eventLogger/AbstractEventLoggerStore.ts +7 -7
  62. package/src/eventLogger/EventLoggerStore.ts +62 -62
  63. package/src/index.ts +141 -154
  64. package/src/issuanceBranding/IssuanceBrandingStore.ts +559 -559
  65. package/src/machineState/IAbstractMachineStateStore.ts +65 -65
  66. package/src/machineState/MachineStateStore.ts +149 -149
  67. package/src/migrations/generic/1-CreateContacts.ts +66 -66
  68. package/src/migrations/generic/2-CreateIssuanceBranding.ts +64 -64
  69. package/src/migrations/generic/3-CreateContacts.ts +66 -66
  70. package/src/migrations/generic/4-CreateStatusList.ts +54 -54
  71. package/src/migrations/generic/5-CreateAuditEvents.ts +66 -66
  72. package/src/migrations/generic/6-CreateDigitalCredential.ts +66 -66
  73. package/src/migrations/generic/7-CreateMachineStateStore.ts +66 -66
  74. package/src/migrations/generic/index.ts +33 -36
  75. package/src/migrations/index.ts +9 -10
  76. package/src/migrations/postgres/1659463079428-CreateContacts.ts +63 -63
  77. package/src/migrations/postgres/1685628974232-CreateIssuanceBranding.ts +85 -85
  78. package/src/migrations/postgres/1690925872592-CreateContacts.ts +104 -104
  79. package/src/migrations/postgres/1693866470001-CreateStatusList.ts +24 -24
  80. package/src/migrations/postgres/1701634812183-CreateAuditEvents.ts +33 -33
  81. package/src/migrations/postgres/1708525189001-CreateDigitalCredential.ts +44 -44
  82. package/src/migrations/postgres/1708797018115-CreateMachineStateStore.ts +29 -29
  83. package/src/migrations/sqlite/1659463069549-CreateContacts.ts +110 -110
  84. package/src/migrations/sqlite/1685628973231-CreateIssuanceBranding.ts +119 -119
  85. package/src/migrations/sqlite/1690925872693-CreateContacts.ts +161 -161
  86. package/src/migrations/sqlite/1693866470000-CreateStatusList.ts +24 -24
  87. package/src/migrations/sqlite/1701634819487-CreateAuditEvents.ts +15 -15
  88. package/src/migrations/sqlite/1708525189002-CreateDigitalCredential.ts +34 -34
  89. package/src/migrations/sqlite/1708796002272-CreateMachineStateStore.ts +28 -28
  90. package/src/statusList/StatusListStore.ts +237 -237
  91. package/src/types/contact/IAbstractContactStore.ts +161 -161
  92. package/src/types/contact/contact.ts +237 -237
  93. package/src/types/digitalCredential/IAbstractDigitalCredentialStore.ts +37 -37
  94. package/src/types/digitalCredential/digitalCredential.ts +46 -46
  95. package/src/types/eventLogger/IAbstractEventLoggerStore.ts +12 -12
  96. package/src/types/eventLogger/eventLogger.ts +3 -3
  97. package/src/types/index.ts +10 -12
  98. package/src/types/machineState/IAbstractMachineStateStore.ts +68 -68
  99. package/src/utils/SortingUtils.ts +16 -16
  100. package/src/utils/contact/MappingUtils.ts +385 -385
  101. package/src/utils/digitalCredential/MappingUtils.ts +122 -122
  102. package/dist/entities/presentationDefinitions/PresentationDefinitionItemEntity.d.ts +0 -13
  103. package/dist/entities/presentationDefinitions/PresentationDefinitionItemEntity.d.ts.map +0 -1
  104. package/dist/entities/presentationDefinitions/PresentationDefinitionItemEntity.js +0 -71
  105. package/dist/entities/presentationDefinitions/PresentationDefinitionItemEntity.js.map +0 -1
  106. package/dist/migrations/generic/8-CreatePresentationDefinitions.d.ts +0 -7
  107. package/dist/migrations/generic/8-CreatePresentationDefinitions.d.ts.map +0 -1
  108. package/dist/migrations/generic/8-CreatePresentationDefinitions.js +0 -78
  109. package/dist/migrations/generic/8-CreatePresentationDefinitions.js.map +0 -1
  110. package/dist/migrations/postgres/1716475165345-CreatePresentationDefinitions.d.ts +0 -7
  111. package/dist/migrations/postgres/1716475165345-CreatePresentationDefinitions.d.ts.map +0 -1
  112. package/dist/migrations/postgres/1716475165345-CreatePresentationDefinitions.js +0 -40
  113. package/dist/migrations/postgres/1716475165345-CreatePresentationDefinitions.js.map +0 -1
  114. package/dist/migrations/sqlite/1716475165344-CreatePresentationDefinitions.d.ts +0 -7
  115. package/dist/migrations/sqlite/1716475165344-CreatePresentationDefinitions.d.ts.map +0 -1
  116. package/dist/migrations/sqlite/1716475165344-CreatePresentationDefinitions.js +0 -37
  117. package/dist/migrations/sqlite/1716475165344-CreatePresentationDefinitions.js.map +0 -1
  118. package/dist/pd/AbstractPDStore.d.ts +0 -10
  119. package/dist/pd/AbstractPDStore.d.ts.map +0 -1
  120. package/dist/pd/AbstractPDStore.js +0 -7
  121. package/dist/pd/AbstractPDStore.js.map +0 -1
  122. package/dist/pd/PDStore.d.ts +0 -14
  123. package/dist/pd/PDStore.d.ts.map +0 -1
  124. package/dist/pd/PDStore.js +0 -90
  125. package/dist/pd/PDStore.js.map +0 -1
  126. package/dist/types/pd/IAbstractPDStore.d.ts +0 -14
  127. package/dist/types/pd/IAbstractPDStore.d.ts.map +0 -1
  128. package/dist/types/pd/IAbstractPDStore.js +0 -3
  129. package/dist/types/pd/IAbstractPDStore.js.map +0 -1
  130. package/dist/types/pd/pd.d.ts +0 -15
  131. package/dist/types/pd/pd.d.ts.map +0 -1
  132. package/dist/types/pd/pd.js +0 -3
  133. package/dist/types/pd/pd.js.map +0 -1
  134. package/dist/utils/presentationDefinitions/MappingUtils.d.ts +0 -6
  135. package/dist/utils/presentationDefinitions/MappingUtils.d.ts.map +0 -1
  136. package/dist/utils/presentationDefinitions/MappingUtils.js +0 -50
  137. package/dist/utils/presentationDefinitions/MappingUtils.js.map +0 -1
  138. package/src/entities/presentationDefinitions/PresentationDefinitionItemEntity.ts +0 -41
  139. package/src/migrations/generic/8-CreatePresentationDefinitions.ts +0 -66
  140. package/src/migrations/postgres/1716475165345-CreatePresentationDefinitions.ts +0 -24
  141. package/src/migrations/sqlite/1716475165344-CreatePresentationDefinitions.ts +0 -23
  142. package/src/pd/AbstractPDStore.ts +0 -10
  143. package/src/pd/PDStore.ts +0 -103
  144. package/src/types/pd/IAbstractPDStore.ts +0 -19
  145. package/src/types/pd/pd.ts +0 -16
  146. package/src/utils/presentationDefinitions/MappingUtils.ts +0 -54
@@ -1,723 +1,723 @@
1
- import { OrPromise } from '@sphereon/ssi-types'
2
- import { DataSource, In, Repository } from 'typeorm'
3
- import Debug from 'debug'
4
- import { AbstractContactStore } from './AbstractContactStore'
5
- import { PartyEntity } from '../entities/contact/PartyEntity'
6
- import { IdentityEntity } from '../entities/contact/IdentityEntity'
7
- import { IdentityMetadataItemEntity } from '../entities/contact/IdentityMetadataItemEntity'
8
- import { CorrelationIdentifierEntity } from '../entities/contact/CorrelationIdentifierEntity'
9
- import { ConnectionEntity } from '../entities/contact/ConnectionEntity'
10
- import { BaseConfigEntity } from '../entities/contact/BaseConfigEntity'
11
- import { PartyRelationshipEntity } from '../entities/contact/PartyRelationshipEntity'
12
- import { PartyTypeEntity } from '../entities/contact/PartyTypeEntity'
13
- import { BaseContactEntity } from '../entities/contact/BaseContactEntity'
14
- import { ElectronicAddressEntity } from '../entities/contact/ElectronicAddressEntity'
15
- import { PhysicalAddressEntity } from '../entities/contact/PhysicalAddressEntity'
16
- import {
17
- electronicAddressEntityFrom,
18
- electronicAddressFrom,
19
- identityEntityFrom,
20
- identityFrom,
21
- isDidAuthConfig,
22
- isNaturalPerson,
23
- isOpenIdConfig,
24
- isOrganization,
25
- partyEntityFrom,
26
- partyFrom,
27
- partyRelationshipEntityFrom,
28
- partyRelationshipFrom,
29
- partyTypeEntityFrom,
30
- partyTypeFrom,
31
- physicalAddressEntityFrom,
32
- physicalAddressFrom,
33
- } from '../utils/contact/MappingUtils'
34
- import {
35
- AddElectronicAddressArgs,
36
- AddIdentityArgs,
37
- AddPartyArgs,
38
- AddPartyTypeArgs,
39
- AddPhysicalAddressArgs,
40
- AddRelationshipArgs,
41
- ConnectionType,
42
- CorrelationIdentifierType,
43
- ElectronicAddress,
44
- GetElectronicAddressArgs,
45
- GetElectronicAddressesArgs,
46
- GetIdentitiesArgs,
47
- GetIdentityArgs,
48
- GetPartiesArgs,
49
- GetPartyArgs,
50
- GetPartyTypeArgs,
51
- GetPartyTypesArgs,
52
- GetPhysicalAddressArgs,
53
- GetPhysicalAddressesArgs,
54
- GetRelationshipArgs,
55
- GetRelationshipsArgs,
56
- Identity,
57
- NonPersistedConnectionConfig,
58
- NonPersistedContact,
59
- Party,
60
- PartyRelationship,
61
- PartyType,
62
- PartyTypeType,
63
- PhysicalAddress,
64
- RemoveElectronicAddressArgs,
65
- RemoveIdentityArgs,
66
- RemovePartyArgs,
67
- RemovePartyTypeArgs,
68
- RemovePhysicalAddressArgs,
69
- RemoveRelationshipArgs,
70
- UpdateElectronicAddressArgs,
71
- UpdateIdentityArgs,
72
- UpdatePartyArgs,
73
- UpdatePartyTypeArgs,
74
- UpdatePhysicalAddressArgs,
75
- UpdateRelationshipArgs,
76
- } from '../types'
77
-
78
- const debug: Debug.Debugger = Debug('sphereon:ssi-sdk:contact-store')
79
-
80
- export class ContactStore extends AbstractContactStore {
81
- private readonly dbConnection: OrPromise<DataSource>
82
-
83
- constructor(dbConnection: OrPromise<DataSource>) {
84
- super()
85
- this.dbConnection = dbConnection
86
- }
87
-
88
- getParty = async (args: GetPartyArgs): Promise<Party> => {
89
- const { partyId } = args
90
- const result: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
91
- where: { id: partyId },
92
- })
93
-
94
- if (!result) {
95
- return Promise.reject(Error(`No party found for id: ${partyId}`))
96
- }
97
-
98
- return partyFrom(result)
99
- }
100
-
101
- getParties = async (args?: GetPartiesArgs): Promise<Array<Party>> => {
102
- debug(`getParties()`, args)
103
- const { filter } = args ?? {}
104
- const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
105
- const initialResult: Array<PartyEntity> = await partyRepository.find({
106
- ...(filter && { where: filter }),
107
- })
108
-
109
- const result: Array<PartyEntity> = await partyRepository.find({
110
- where: {
111
- id: In(initialResult.map((party: PartyEntity) => party.id)),
112
- },
113
- })
114
- debug(`getParties() resulted in ${result.length} parties`)
115
-
116
- return result.map((party: PartyEntity) => partyFrom(party))
117
- }
118
-
119
- addParty = async (args: AddPartyArgs): Promise<Party> => {
120
- const { identities, contact, partyType } = args
121
-
122
- const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
123
-
124
- if (!this.hasCorrectPartyType(partyType.type, contact)) {
125
- return Promise.reject(Error(`Party type ${partyType.type}, does not match for provided contact`))
126
- }
127
-
128
- for (const identity of identities ?? []) {
129
- if (identity.identifier.type === CorrelationIdentifierType.URL) {
130
- if (!identity.connection) {
131
- return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierType.URL} should contain a connection`))
132
- }
133
-
134
- if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
135
- return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
136
- }
137
- }
138
- }
139
-
140
- const partyEntity: PartyEntity = partyEntityFrom(args)
141
- debug('Adding party', args)
142
- const createdResult: PartyEntity = await partyRepository.save(partyEntity)
143
-
144
- return partyFrom(createdResult)
145
- }
146
-
147
- updateParty = async (args: UpdatePartyArgs): Promise<Party> => {
148
- const { party } = args
149
- const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
150
- const result: PartyEntity | null = await partyRepository.findOne({
151
- where: { id: party.id },
152
- })
153
-
154
- if (!result) {
155
- return Promise.reject(Error(`No party found for id: ${party.id}`))
156
- }
157
-
158
- const updatedParty = {
159
- ...party,
160
- identities: result.identities,
161
- type: result.partyType,
162
- relationships: result.relationships,
163
- electronicAddresses: result.electronicAddresses,
164
- }
165
-
166
- debug('Updating party', party)
167
- const updatedResult: PartyEntity = await partyRepository.save(updatedParty, { transaction: true })
168
-
169
- return partyFrom(updatedResult)
170
- }
171
-
172
- removeParty = async (args: RemovePartyArgs): Promise<void> => {
173
- const { partyId } = args
174
- const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
175
- debug('Removing party', partyId)
176
- partyRepository
177
- .findOneById(partyId)
178
- .then(async (party: PartyEntity | null): Promise<void> => {
179
- if (!party) {
180
- await Promise.reject(Error(`Unable to find the party with id to remove: ${partyId}`))
181
- } else {
182
- await this.deleteIdentities(party.identities)
183
- await this.deleteElectronicAddresses(party.electronicAddresses)
184
- await this.deletePhysicalAddresses(party.physicalAddresses)
185
-
186
- await partyRepository
187
- .delete({ id: partyId })
188
- .catch((error) => Promise.reject(Error(`Unable to remove party with id: ${partyId}. ${error}`)))
189
-
190
- const partyContactRepository: Repository<BaseContactEntity> = (await this.dbConnection).getRepository(BaseContactEntity)
191
- await partyContactRepository
192
- .delete({ id: party.contact.id })
193
- .catch((error) => Promise.reject(Error(`Unable to remove party contact with id: ${party.contact.id}. ${error}`)))
194
- }
195
- })
196
- .catch((error) => Promise.reject(Error(`Unable to remove party with id: ${partyId}. ${error}`)))
197
- }
198
-
199
- getIdentity = async (args: GetIdentityArgs): Promise<Identity> => {
200
- const { identityId } = args
201
- const result: IdentityEntity | null = await (await this.dbConnection).getRepository(IdentityEntity).findOne({
202
- where: { id: identityId },
203
- })
204
-
205
- if (!result) {
206
- return Promise.reject(Error(`No identity found for id: ${identityId}`))
207
- }
208
-
209
- return identityFrom(result)
210
- }
211
-
212
- getIdentities = async (args?: GetIdentitiesArgs): Promise<Array<Identity>> => {
213
- const { filter } = args ?? {}
214
- const identityRepository: Repository<IdentityEntity> = (await this.dbConnection).getRepository(IdentityEntity)
215
- const initialResult: Array<IdentityEntity> = await identityRepository.find({
216
- ...(filter && { where: filter }),
217
- })
218
-
219
- const result: Array<IdentityEntity> = await identityRepository.find({
220
- where: {
221
- id: In(initialResult.map((identity: IdentityEntity) => identity.id)),
222
- },
223
- })
224
-
225
- return result.map((identity: IdentityEntity) => identityFrom(identity))
226
- }
227
-
228
- addIdentity = async (args: AddIdentityArgs): Promise<Identity> => {
229
- const { identity, partyId } = args
230
- const party: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
231
- where: { id: partyId },
232
- })
233
-
234
- if (!party) {
235
- return Promise.reject(Error(`No party found for id: ${partyId}`))
236
- }
237
-
238
- if (identity.identifier.type === CorrelationIdentifierType.URL) {
239
- if (!identity.connection) {
240
- return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierType.URL} should contain a connection`))
241
- }
242
-
243
- if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
244
- return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
245
- }
246
- }
247
-
248
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
249
- identityEntity.party = party
250
- debug('Adding identity', identity)
251
- const result: IdentityEntity = await (await this.dbConnection).getRepository(IdentityEntity).save(identityEntity, {
252
- transaction: true,
253
- })
254
-
255
- return identityFrom(result)
256
- }
257
-
258
- updateIdentity = async (args: UpdateIdentityArgs): Promise<Identity> => {
259
- const { identity } = args
260
- const identityRepository: Repository<IdentityEntity> = (await this.dbConnection).getRepository(IdentityEntity)
261
- const result: IdentityEntity | null = await identityRepository.findOne({
262
- where: { id: identity.id },
263
- })
264
-
265
- if (!result) {
266
- return Promise.reject(Error(`No identity found for id: ${identity.id}`))
267
- }
268
-
269
- if (identity.identifier.type === CorrelationIdentifierType.URL) {
270
- if (!identity.connection) {
271
- return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierType.URL} should contain a connection`))
272
- }
273
-
274
- if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
275
- return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
276
- }
277
- }
278
-
279
- debug('Updating identity', identity)
280
- const updatedResult: IdentityEntity = await identityRepository.save(identity, { transaction: true })
281
-
282
- return identityFrom(updatedResult)
283
- }
284
-
285
- removeIdentity = async (args: RemoveIdentityArgs): Promise<void> => {
286
- const { identityId } = args
287
- const identity: IdentityEntity | null = await (await this.dbConnection).getRepository(IdentityEntity).findOne({
288
- where: { id: identityId },
289
- })
290
-
291
- if (!identity) {
292
- return Promise.reject(Error(`No identity found for id: ${identityId}`))
293
- }
294
-
295
- debug('Removing identity', identityId)
296
-
297
- await this.deleteIdentities([identity])
298
- }
299
-
300
- addRelationship = async (args: AddRelationshipArgs): Promise<PartyRelationship> => {
301
- const { leftId, rightId } = args
302
- return this.assertRelationshipSides(leftId, rightId).then(async (): Promise<PartyRelationship> => {
303
- const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
304
- leftId,
305
- rightId,
306
- })
307
- debug('Adding party relationship', relationship)
308
-
309
- const createdResult: PartyRelationshipEntity = await (await this.dbConnection).getRepository(PartyRelationshipEntity).save(relationship)
310
-
311
- return partyRelationshipFrom(createdResult)
312
- })
313
- }
314
-
315
- getRelationship = async (args: GetRelationshipArgs): Promise<PartyRelationship> => {
316
- const { relationshipId } = args
317
- const result: PartyRelationshipEntity | null = await (await this.dbConnection).getRepository(PartyRelationshipEntity).findOne({
318
- where: { id: relationshipId },
319
- })
320
-
321
- if (!result) {
322
- return Promise.reject(Error(`No relationship found for id: ${relationshipId}`))
323
- }
324
-
325
- return partyRelationshipFrom(result)
326
- }
327
-
328
- getRelationships = async (args?: GetRelationshipsArgs): Promise<Array<PartyRelationship>> => {
329
- const { filter } = args ?? {}
330
- const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
331
- const initialResult: Array<PartyRelationshipEntity> = await partyRelationshipRepository.find({
332
- ...(filter && { where: filter }),
333
- })
334
-
335
- const result: Array<PartyRelationshipEntity> = await partyRelationshipRepository.find({
336
- where: {
337
- id: In(initialResult.map((partyRelationship: PartyRelationshipEntity) => partyRelationship.id)),
338
- },
339
- })
340
-
341
- return result.map((partyRelationship: PartyRelationshipEntity) => partyRelationshipFrom(partyRelationship))
342
- }
343
-
344
- updateRelationship = async (args: UpdateRelationshipArgs): Promise<PartyRelationship> => {
345
- const { relationship } = args
346
- const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
347
- const result: PartyRelationshipEntity | null = await partyRelationshipRepository.findOne({
348
- where: { id: relationship.id },
349
- })
350
-
351
- if (!result) {
352
- return Promise.reject(Error(`No party relationship found for id: ${relationship.id}`))
353
- }
354
-
355
- return this.assertRelationshipSides(relationship.leftId, relationship.rightId).then(async (): Promise<PartyRelationship> => {
356
- debug('Updating party relationship', relationship)
357
- const updatedResult: PartyRelationshipEntity = await partyRelationshipRepository.save(relationship, { transaction: true })
358
-
359
- return partyRelationshipFrom(updatedResult)
360
- })
361
- }
362
-
363
- removeRelationship = async (args: RemoveRelationshipArgs): Promise<void> => {
364
- const { relationshipId } = args
365
- const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
366
- const relationship: PartyRelationshipEntity | null = await partyRelationshipRepository.findOne({
367
- where: { id: relationshipId },
368
- })
369
-
370
- if (!relationship) {
371
- return Promise.reject(Error(`No relationship found for id: ${relationshipId}`))
372
- }
373
-
374
- debug('Removing relationship', relationshipId)
375
-
376
- await partyRelationshipRepository.delete(relationshipId)
377
- }
378
-
379
- addPartyType = async (args: AddPartyTypeArgs): Promise<PartyType> => {
380
- const partyEntity: PartyTypeEntity = partyTypeEntityFrom(args)
381
- debug('Adding party type', args)
382
- const createdResult: PartyTypeEntity = await (await this.dbConnection).getRepository(PartyTypeEntity).save(partyEntity)
383
-
384
- return partyTypeFrom(createdResult)
385
- }
386
-
387
- getPartyType = async (args: GetPartyTypeArgs): Promise<PartyType> => {
388
- const { partyTypeId } = args
389
- const result: PartyTypeEntity | null = await (await this.dbConnection).getRepository(PartyTypeEntity).findOne({
390
- where: { id: partyTypeId },
391
- })
392
-
393
- if (!result) {
394
- return Promise.reject(Error(`No party type found for id: ${partyTypeId}`))
395
- }
396
-
397
- return partyTypeFrom(result)
398
- }
399
-
400
- getPartyTypes = async (args?: GetPartyTypesArgs): Promise<Array<PartyType>> => {
401
- const { filter } = args ?? {}
402
- const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
403
- const initialResult: Array<PartyTypeEntity> = await partyTypeRepository.find({
404
- ...(filter && { where: filter }),
405
- })
406
-
407
- const result: Array<PartyTypeEntity> = await partyTypeRepository.find({
408
- where: {
409
- id: In(initialResult.map((partyType: PartyTypeEntity) => partyType.id)),
410
- },
411
- })
412
-
413
- return result.map((partyType: PartyTypeEntity) => partyTypeFrom(partyType))
414
- }
415
-
416
- updatePartyType = async (args: UpdatePartyTypeArgs): Promise<PartyType> => {
417
- const { partyType } = args
418
- const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
419
- const result: PartyTypeEntity | null = await partyTypeRepository.findOne({
420
- where: { id: partyType.id },
421
- })
422
-
423
- if (!result) {
424
- return Promise.reject(Error(`No party type found for id: ${partyType.id}`))
425
- }
426
-
427
- debug('Updating party type', partyType)
428
- const updatedResult: PartyTypeEntity = await partyTypeRepository.save(partyType, { transaction: true })
429
-
430
- return partyTypeFrom(updatedResult)
431
- }
432
-
433
- removePartyType = async (args: RemovePartyTypeArgs): Promise<void> => {
434
- const { partyTypeId } = args
435
- const parties: Array<PartyEntity> = await (await this.dbConnection).getRepository(PartyEntity).find({
436
- where: {
437
- partyType: {
438
- id: partyTypeId,
439
- },
440
- },
441
- })
442
-
443
- if (parties.length > 0) {
444
- return Promise.reject(Error(`Unable to remove party type with id: ${partyTypeId}. Party type is in use`))
445
- }
446
-
447
- const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
448
- const partyType: PartyTypeEntity | null = await partyTypeRepository.findOne({
449
- where: { id: partyTypeId },
450
- })
451
-
452
- if (!partyType) {
453
- return Promise.reject(Error(`No party type found for id: ${partyTypeId}`))
454
- }
455
-
456
- debug('Removing party type', partyTypeId)
457
-
458
- await partyTypeRepository.delete(partyTypeId)
459
- }
460
-
461
- getElectronicAddress = async (args: GetElectronicAddressArgs): Promise<ElectronicAddress> => {
462
- const { electronicAddressId } = args
463
- const result: ElectronicAddressEntity | null = await (await this.dbConnection).getRepository(ElectronicAddressEntity).findOne({
464
- where: { id: electronicAddressId },
465
- })
466
-
467
- if (!result) {
468
- return Promise.reject(Error(`No electronic address found for id: ${electronicAddressId}`))
469
- }
470
-
471
- return electronicAddressFrom(result)
472
- }
473
-
474
- getElectronicAddresses = async (args?: GetElectronicAddressesArgs): Promise<Array<ElectronicAddress>> => {
475
- const { filter } = args ?? {}
476
- const electronicAddressRepository: Repository<ElectronicAddressEntity> = (await this.dbConnection).getRepository(ElectronicAddressEntity)
477
- const initialResult: Array<ElectronicAddressEntity> = await electronicAddressRepository.find({
478
- ...(filter && { where: filter }),
479
- })
480
-
481
- const result: Array<ElectronicAddressEntity> = await electronicAddressRepository.find({
482
- where: {
483
- id: In(initialResult.map((electronicAddress: ElectronicAddressEntity) => electronicAddress.id)),
484
- },
485
- })
486
-
487
- return result.map((electronicAddress: ElectronicAddressEntity) => electronicAddressFrom(electronicAddress))
488
- }
489
-
490
- addElectronicAddress = async (args: AddElectronicAddressArgs): Promise<ElectronicAddress> => {
491
- const { electronicAddress, partyId } = args
492
- const party: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
493
- where: { id: partyId },
494
- })
495
-
496
- if (!party) {
497
- return Promise.reject(Error(`No party found for id: ${partyId}`))
498
- }
499
-
500
- const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
501
- electronicAddressEntity.party = party
502
- debug('Adding electronic address', electronicAddress)
503
- const result: ElectronicAddressEntity = await (await this.dbConnection).getRepository(ElectronicAddressEntity).save(electronicAddressEntity, {
504
- transaction: true,
505
- })
506
-
507
- return electronicAddressFrom(result)
508
- }
509
-
510
- updateElectronicAddress = async (args: UpdateElectronicAddressArgs): Promise<ElectronicAddress> => {
511
- const { electronicAddress } = args
512
- const electronicAddressRepository: Repository<ElectronicAddressEntity> = (await this.dbConnection).getRepository(ElectronicAddressEntity)
513
- const result: ElectronicAddressEntity | null = await electronicAddressRepository.findOne({
514
- where: { id: electronicAddress.id },
515
- })
516
-
517
- if (!result) {
518
- return Promise.reject(Error(`No electronic address found for id: ${electronicAddress.id}`))
519
- }
520
-
521
- debug('Updating electronic address', electronicAddress)
522
- const updatedResult: ElectronicAddressEntity = await electronicAddressRepository.save(electronicAddress, { transaction: true })
523
-
524
- return electronicAddressFrom(updatedResult)
525
- }
526
-
527
- removeElectronicAddress = async (args: RemoveElectronicAddressArgs): Promise<void> => {
528
- const { electronicAddressId } = args
529
- const electronicAddressRepository: Repository<ElectronicAddressEntity> = (await this.dbConnection).getRepository(ElectronicAddressEntity)
530
- const electronicAddress: ElectronicAddressEntity | null = await electronicAddressRepository.findOne({
531
- where: { id: electronicAddressId },
532
- })
533
-
534
- if (!electronicAddress) {
535
- return Promise.reject(Error(`No electronic address found for id: ${electronicAddressId}`))
536
- }
537
-
538
- debug('Removing electronic address', electronicAddressId)
539
-
540
- await electronicAddressRepository.delete(electronicAddressId)
541
- }
542
-
543
- getPhysicalAddress = async (args: GetPhysicalAddressArgs): Promise<PhysicalAddress> => {
544
- const { physicalAddressId } = args
545
- const result: PhysicalAddressEntity | null = await (await this.dbConnection).getRepository(PhysicalAddressEntity).findOne({
546
- where: { id: physicalAddressId },
547
- })
548
-
549
- if (!result) {
550
- return Promise.reject(Error(`No physical address found for id: ${physicalAddressId}`))
551
- }
552
-
553
- return physicalAddressFrom(result)
554
- }
555
-
556
- getPhysicalAddresses = async (args?: GetPhysicalAddressesArgs): Promise<Array<PhysicalAddress>> => {
557
- const { filter } = args ?? {}
558
- const physicalAddressRepository: Repository<PhysicalAddressEntity> = (await this.dbConnection).getRepository(PhysicalAddressEntity)
559
- const initialResult: Array<PhysicalAddressEntity> = await physicalAddressRepository.find({
560
- ...(filter && { where: filter }),
561
- })
562
-
563
- const result: Array<PhysicalAddressEntity> = await physicalAddressRepository.find({
564
- where: {
565
- id: In(initialResult.map((physicalAddress: PhysicalAddressEntity) => physicalAddress.id)),
566
- },
567
- })
568
-
569
- return result.map((physicalAddress: PhysicalAddressEntity) => physicalAddressFrom(physicalAddress))
570
- }
571
-
572
- addPhysicalAddress = async (args: AddPhysicalAddressArgs): Promise<PhysicalAddress> => {
573
- const { physicalAddress, partyId } = args
574
- const party: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
575
- where: { id: partyId },
576
- })
577
-
578
- if (!party) {
579
- return Promise.reject(Error(`No party found for id: ${partyId}`))
580
- }
581
-
582
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
583
- physicalAddressEntity.party = party
584
- debug('Adding physical address', physicalAddress)
585
- const result: PhysicalAddressEntity = await (await this.dbConnection).getRepository(PhysicalAddressEntity).save(physicalAddressEntity, {
586
- transaction: true,
587
- })
588
-
589
- return physicalAddressFrom(result)
590
- }
591
-
592
- updatePhysicalAddress = async (args: UpdatePhysicalAddressArgs): Promise<PhysicalAddress> => {
593
- const { physicalAddress } = args
594
- const physicalAddressRepository: Repository<PhysicalAddressEntity> = (await this.dbConnection).getRepository(PhysicalAddressEntity)
595
- const result: PhysicalAddressEntity | null = await physicalAddressRepository.findOne({
596
- where: { id: physicalAddress.id },
597
- })
598
-
599
- if (!result) {
600
- return Promise.reject(Error(`No physical address found for id: ${physicalAddress.id}`))
601
- }
602
-
603
- debug('Updating physical address', physicalAddress)
604
- const updatedResult: PhysicalAddressEntity = await physicalAddressRepository.save(physicalAddress, { transaction: true })
605
-
606
- return physicalAddressFrom(updatedResult)
607
- }
608
-
609
- removePhysicalAddress = async (args: RemovePhysicalAddressArgs): Promise<void> => {
610
- const { physicalAddressId } = args
611
- const physicalAddressRepository: Repository<PhysicalAddressEntity> = (await this.dbConnection).getRepository(PhysicalAddressEntity)
612
- const physicalAddress: PhysicalAddressEntity | null = await physicalAddressRepository.findOne({
613
- where: { id: physicalAddressId },
614
- })
615
-
616
- if (!physicalAddress) {
617
- return Promise.reject(Error(`No physical address found for id: ${physicalAddressId}`))
618
- }
619
-
620
- debug('Removing physical address', physicalAddressId)
621
-
622
- await physicalAddressRepository.delete(physicalAddressId)
623
- }
624
-
625
- private hasCorrectConnectionConfig(type: ConnectionType, config: NonPersistedConnectionConfig): boolean {
626
- switch (type) {
627
- case ConnectionType.OPENID_CONNECT:
628
- return isOpenIdConfig(config)
629
- case ConnectionType.SIOPv2:
630
- return isDidAuthConfig(config)
631
- default:
632
- throw new Error('Connection type not supported')
633
- }
634
- }
635
-
636
- private hasCorrectPartyType(type: PartyTypeType, contact: NonPersistedContact): boolean {
637
- switch (type) {
638
- case PartyTypeType.NATURAL_PERSON:
639
- return isNaturalPerson(contact)
640
- case PartyTypeType.ORGANIZATION:
641
- return isOrganization(contact)
642
- default:
643
- throw new Error('Party type not supported')
644
- }
645
- }
646
-
647
- private async deleteIdentities(identities: Array<IdentityEntity>): Promise<void> {
648
- debug('Removing identities', identities)
649
-
650
- const connection: DataSource = await this.dbConnection
651
- const correlationIdentifierRepository: Repository<CorrelationIdentifierEntity> = connection.getRepository(CorrelationIdentifierEntity)
652
- const baseConfigRepository: Repository<BaseConfigEntity> = connection.getRepository(BaseConfigEntity)
653
- const connectionRepository: Repository<ConnectionEntity> = connection.getRepository(ConnectionEntity)
654
- const identityMetadataItemRepository: Repository<IdentityMetadataItemEntity> = connection.getRepository(IdentityMetadataItemEntity)
655
- const identityRepository: Repository<IdentityEntity> = connection.getRepository(IdentityEntity)
656
-
657
- identities.map(async (identity: IdentityEntity): Promise<void> => {
658
- await correlationIdentifierRepository
659
- .delete(identity.identifier.id)
660
- .catch((error) => Promise.reject(Error(`Unable to remove identity.identifier with id ${identity.identifier.id}. ${error}`)))
661
-
662
- if (identity.connection) {
663
- await baseConfigRepository.delete(identity.connection.config.id)
664
- await connectionRepository
665
- .delete(identity.connection.id)
666
- .catch((error) => Promise.reject(Error(`Unable to remove identity.connection with id ${identity.connection?.id}. ${error}`)))
667
- }
668
-
669
- if (identity.metadata) {
670
- identity.metadata.map(async (metadataItem: IdentityMetadataItemEntity): Promise<void> => {
671
- await identityMetadataItemRepository
672
- .delete(metadataItem.id)
673
- .catch((error) => Promise.reject(Error(`Unable to remove identity.metadataItem with id ${metadataItem.id}. ${error}`)))
674
- })
675
- }
676
-
677
- await identityRepository
678
- .delete(identity.id)
679
- .catch((error) => Promise.reject(Error(`Unable to remove identity with id ${identity.id}. ${error}`)))
680
- })
681
- }
682
-
683
- private async deleteElectronicAddresses(electronicAddresses: Array<ElectronicAddressEntity>): Promise<void> {
684
- debug('Removing electronic addresses', electronicAddresses)
685
-
686
- const electronicAddressRepository: Repository<ElectronicAddressEntity> = (await this.dbConnection).getRepository(ElectronicAddressEntity)
687
- electronicAddresses.map(async (electronicAddress: ElectronicAddressEntity): Promise<void> => {
688
- await electronicAddressRepository
689
- .delete(electronicAddress.id)
690
- .catch((error) => Promise.reject(Error(`Unable to remove electronic address with id ${electronicAddress.id}. ${error}`)))
691
- })
692
- }
693
-
694
- private async deletePhysicalAddresses(physicalAddresses: Array<PhysicalAddressEntity>): Promise<void> {
695
- debug('Removing physical addresses', physicalAddresses)
696
-
697
- const physicalAddressRepository: Repository<PhysicalAddressEntity> = (await this.dbConnection).getRepository(PhysicalAddressEntity)
698
- physicalAddresses.map(async (physicalAddress: PhysicalAddressEntity): Promise<void> => {
699
- await physicalAddressRepository
700
- .delete(physicalAddress.id)
701
- .catch((error) => Promise.reject(Error(`Unable to remove physical address with id ${physicalAddress.id}. ${error}`)))
702
- })
703
- }
704
-
705
- private async assertRelationshipSides(leftId: string, rightId: string): Promise<void> {
706
- const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
707
- const leftParty: PartyEntity | null = await partyRepository.findOne({
708
- where: { id: leftId },
709
- })
710
-
711
- if (!leftParty) {
712
- return Promise.reject(Error(`No party found for left side of the relationship, party id: ${leftId}`))
713
- }
714
-
715
- const rightParty: PartyEntity | null = await partyRepository.findOne({
716
- where: { id: rightId },
717
- })
718
-
719
- if (!rightParty) {
720
- return Promise.reject(Error(`No party found for right side of the relationship, party id: ${rightId}`))
721
- }
722
- }
723
- }
1
+ import { OrPromise } from '@sphereon/ssi-types'
2
+ import { DataSource, In, Repository } from 'typeorm'
3
+ import Debug from 'debug'
4
+ import { AbstractContactStore } from './AbstractContactStore'
5
+ import { PartyEntity } from '../entities/contact/PartyEntity'
6
+ import { IdentityEntity } from '../entities/contact/IdentityEntity'
7
+ import { IdentityMetadataItemEntity } from '../entities/contact/IdentityMetadataItemEntity'
8
+ import { CorrelationIdentifierEntity } from '../entities/contact/CorrelationIdentifierEntity'
9
+ import { ConnectionEntity } from '../entities/contact/ConnectionEntity'
10
+ import { BaseConfigEntity } from '../entities/contact/BaseConfigEntity'
11
+ import { PartyRelationshipEntity } from '../entities/contact/PartyRelationshipEntity'
12
+ import { PartyTypeEntity } from '../entities/contact/PartyTypeEntity'
13
+ import { BaseContactEntity } from '../entities/contact/BaseContactEntity'
14
+ import { ElectronicAddressEntity } from '../entities/contact/ElectronicAddressEntity'
15
+ import { PhysicalAddressEntity } from '../entities/contact/PhysicalAddressEntity'
16
+ import {
17
+ electronicAddressEntityFrom,
18
+ electronicAddressFrom,
19
+ identityEntityFrom,
20
+ identityFrom,
21
+ isDidAuthConfig,
22
+ isNaturalPerson,
23
+ isOpenIdConfig,
24
+ isOrganization,
25
+ partyEntityFrom,
26
+ partyFrom,
27
+ partyRelationshipEntityFrom,
28
+ partyRelationshipFrom,
29
+ partyTypeEntityFrom,
30
+ partyTypeFrom,
31
+ physicalAddressEntityFrom,
32
+ physicalAddressFrom,
33
+ } from '../utils/contact/MappingUtils'
34
+ import {
35
+ AddElectronicAddressArgs,
36
+ AddIdentityArgs,
37
+ AddPartyArgs,
38
+ AddPartyTypeArgs,
39
+ AddPhysicalAddressArgs,
40
+ AddRelationshipArgs,
41
+ ConnectionType,
42
+ CorrelationIdentifierType,
43
+ ElectronicAddress,
44
+ GetElectronicAddressArgs,
45
+ GetElectronicAddressesArgs,
46
+ GetIdentitiesArgs,
47
+ GetIdentityArgs,
48
+ GetPartiesArgs,
49
+ GetPartyArgs,
50
+ GetPartyTypeArgs,
51
+ GetPartyTypesArgs,
52
+ GetPhysicalAddressArgs,
53
+ GetPhysicalAddressesArgs,
54
+ GetRelationshipArgs,
55
+ GetRelationshipsArgs,
56
+ Identity,
57
+ NonPersistedConnectionConfig,
58
+ NonPersistedContact,
59
+ Party,
60
+ PartyRelationship,
61
+ PartyType,
62
+ PartyTypeType,
63
+ PhysicalAddress,
64
+ RemoveElectronicAddressArgs,
65
+ RemoveIdentityArgs,
66
+ RemovePartyArgs,
67
+ RemovePartyTypeArgs,
68
+ RemovePhysicalAddressArgs,
69
+ RemoveRelationshipArgs,
70
+ UpdateElectronicAddressArgs,
71
+ UpdateIdentityArgs,
72
+ UpdatePartyArgs,
73
+ UpdatePartyTypeArgs,
74
+ UpdatePhysicalAddressArgs,
75
+ UpdateRelationshipArgs,
76
+ } from '../types'
77
+
78
+ const debug: Debug.Debugger = Debug('sphereon:ssi-sdk:contact-store')
79
+
80
+ export class ContactStore extends AbstractContactStore {
81
+ private readonly dbConnection: OrPromise<DataSource>
82
+
83
+ constructor(dbConnection: OrPromise<DataSource>) {
84
+ super()
85
+ this.dbConnection = dbConnection
86
+ }
87
+
88
+ getParty = async (args: GetPartyArgs): Promise<Party> => {
89
+ const { partyId } = args
90
+ const result: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
91
+ where: { id: partyId },
92
+ })
93
+
94
+ if (!result) {
95
+ return Promise.reject(Error(`No party found for id: ${partyId}`))
96
+ }
97
+
98
+ return partyFrom(result)
99
+ }
100
+
101
+ getParties = async (args?: GetPartiesArgs): Promise<Array<Party>> => {
102
+ debug(`getParties()`, args)
103
+ const { filter } = args ?? {}
104
+ const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
105
+ const initialResult: Array<PartyEntity> = await partyRepository.find({
106
+ ...(filter && { where: filter }),
107
+ })
108
+
109
+ const result: Array<PartyEntity> = await partyRepository.find({
110
+ where: {
111
+ id: In(initialResult.map((party: PartyEntity) => party.id)),
112
+ },
113
+ })
114
+ debug(`getParties() resulted in ${result.length} parties`)
115
+
116
+ return result.map((party: PartyEntity) => partyFrom(party))
117
+ }
118
+
119
+ addParty = async (args: AddPartyArgs): Promise<Party> => {
120
+ const { identities, contact, partyType } = args
121
+
122
+ const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
123
+
124
+ if (!this.hasCorrectPartyType(partyType.type, contact)) {
125
+ return Promise.reject(Error(`Party type ${partyType.type}, does not match for provided contact`))
126
+ }
127
+
128
+ for (const identity of identities ?? []) {
129
+ if (identity.identifier.type === CorrelationIdentifierType.URL) {
130
+ if (!identity.connection) {
131
+ return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierType.URL} should contain a connection`))
132
+ }
133
+
134
+ if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
135
+ return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
136
+ }
137
+ }
138
+ }
139
+
140
+ const partyEntity: PartyEntity = partyEntityFrom(args)
141
+ debug('Adding party', args)
142
+ const createdResult: PartyEntity = await partyRepository.save(partyEntity)
143
+
144
+ return partyFrom(createdResult)
145
+ }
146
+
147
+ updateParty = async (args: UpdatePartyArgs): Promise<Party> => {
148
+ const { party } = args
149
+ const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
150
+ const result: PartyEntity | null = await partyRepository.findOne({
151
+ where: { id: party.id },
152
+ })
153
+
154
+ if (!result) {
155
+ return Promise.reject(Error(`No party found for id: ${party.id}`))
156
+ }
157
+
158
+ const updatedParty = {
159
+ ...party,
160
+ identities: result.identities,
161
+ type: result.partyType,
162
+ relationships: result.relationships,
163
+ electronicAddresses: result.electronicAddresses,
164
+ }
165
+
166
+ debug('Updating party', party)
167
+ const updatedResult: PartyEntity = await partyRepository.save(updatedParty, { transaction: true })
168
+
169
+ return partyFrom(updatedResult)
170
+ }
171
+
172
+ removeParty = async (args: RemovePartyArgs): Promise<void> => {
173
+ const { partyId } = args
174
+ const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
175
+ debug('Removing party', partyId)
176
+ partyRepository
177
+ .findOneById(partyId)
178
+ .then(async (party: PartyEntity | null): Promise<void> => {
179
+ if (!party) {
180
+ await Promise.reject(Error(`Unable to find the party with id to remove: ${partyId}`))
181
+ } else {
182
+ await this.deleteIdentities(party.identities)
183
+ await this.deleteElectronicAddresses(party.electronicAddresses)
184
+ await this.deletePhysicalAddresses(party.physicalAddresses)
185
+
186
+ await partyRepository
187
+ .delete({ id: partyId })
188
+ .catch((error) => Promise.reject(Error(`Unable to remove party with id: ${partyId}. ${error}`)))
189
+
190
+ const partyContactRepository: Repository<BaseContactEntity> = (await this.dbConnection).getRepository(BaseContactEntity)
191
+ await partyContactRepository
192
+ .delete({ id: party.contact.id })
193
+ .catch((error) => Promise.reject(Error(`Unable to remove party contact with id: ${party.contact.id}. ${error}`)))
194
+ }
195
+ })
196
+ .catch((error) => Promise.reject(Error(`Unable to remove party with id: ${partyId}. ${error}`)))
197
+ }
198
+
199
+ getIdentity = async (args: GetIdentityArgs): Promise<Identity> => {
200
+ const { identityId } = args
201
+ const result: IdentityEntity | null = await (await this.dbConnection).getRepository(IdentityEntity).findOne({
202
+ where: { id: identityId },
203
+ })
204
+
205
+ if (!result) {
206
+ return Promise.reject(Error(`No identity found for id: ${identityId}`))
207
+ }
208
+
209
+ return identityFrom(result)
210
+ }
211
+
212
+ getIdentities = async (args?: GetIdentitiesArgs): Promise<Array<Identity>> => {
213
+ const { filter } = args ?? {}
214
+ const identityRepository: Repository<IdentityEntity> = (await this.dbConnection).getRepository(IdentityEntity)
215
+ const initialResult: Array<IdentityEntity> = await identityRepository.find({
216
+ ...(filter && { where: filter }),
217
+ })
218
+
219
+ const result: Array<IdentityEntity> = await identityRepository.find({
220
+ where: {
221
+ id: In(initialResult.map((identity: IdentityEntity) => identity.id)),
222
+ },
223
+ })
224
+
225
+ return result.map((identity: IdentityEntity) => identityFrom(identity))
226
+ }
227
+
228
+ addIdentity = async (args: AddIdentityArgs): Promise<Identity> => {
229
+ const { identity, partyId } = args
230
+ const party: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
231
+ where: { id: partyId },
232
+ })
233
+
234
+ if (!party) {
235
+ return Promise.reject(Error(`No party found for id: ${partyId}`))
236
+ }
237
+
238
+ if (identity.identifier.type === CorrelationIdentifierType.URL) {
239
+ if (!identity.connection) {
240
+ return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierType.URL} should contain a connection`))
241
+ }
242
+
243
+ if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
244
+ return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
245
+ }
246
+ }
247
+
248
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
249
+ identityEntity.party = party
250
+ debug('Adding identity', identity)
251
+ const result: IdentityEntity = await (await this.dbConnection).getRepository(IdentityEntity).save(identityEntity, {
252
+ transaction: true,
253
+ })
254
+
255
+ return identityFrom(result)
256
+ }
257
+
258
+ updateIdentity = async (args: UpdateIdentityArgs): Promise<Identity> => {
259
+ const { identity } = args
260
+ const identityRepository: Repository<IdentityEntity> = (await this.dbConnection).getRepository(IdentityEntity)
261
+ const result: IdentityEntity | null = await identityRepository.findOne({
262
+ where: { id: identity.id },
263
+ })
264
+
265
+ if (!result) {
266
+ return Promise.reject(Error(`No identity found for id: ${identity.id}`))
267
+ }
268
+
269
+ if (identity.identifier.type === CorrelationIdentifierType.URL) {
270
+ if (!identity.connection) {
271
+ return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierType.URL} should contain a connection`))
272
+ }
273
+
274
+ if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
275
+ return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
276
+ }
277
+ }
278
+
279
+ debug('Updating identity', identity)
280
+ const updatedResult: IdentityEntity = await identityRepository.save(identity, { transaction: true })
281
+
282
+ return identityFrom(updatedResult)
283
+ }
284
+
285
+ removeIdentity = async (args: RemoveIdentityArgs): Promise<void> => {
286
+ const { identityId } = args
287
+ const identity: IdentityEntity | null = await (await this.dbConnection).getRepository(IdentityEntity).findOne({
288
+ where: { id: identityId },
289
+ })
290
+
291
+ if (!identity) {
292
+ return Promise.reject(Error(`No identity found for id: ${identityId}`))
293
+ }
294
+
295
+ debug('Removing identity', identityId)
296
+
297
+ await this.deleteIdentities([identity])
298
+ }
299
+
300
+ addRelationship = async (args: AddRelationshipArgs): Promise<PartyRelationship> => {
301
+ const { leftId, rightId } = args
302
+ return this.assertRelationshipSides(leftId, rightId).then(async (): Promise<PartyRelationship> => {
303
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
304
+ leftId,
305
+ rightId,
306
+ })
307
+ debug('Adding party relationship', relationship)
308
+
309
+ const createdResult: PartyRelationshipEntity = await (await this.dbConnection).getRepository(PartyRelationshipEntity).save(relationship)
310
+
311
+ return partyRelationshipFrom(createdResult)
312
+ })
313
+ }
314
+
315
+ getRelationship = async (args: GetRelationshipArgs): Promise<PartyRelationship> => {
316
+ const { relationshipId } = args
317
+ const result: PartyRelationshipEntity | null = await (await this.dbConnection).getRepository(PartyRelationshipEntity).findOne({
318
+ where: { id: relationshipId },
319
+ })
320
+
321
+ if (!result) {
322
+ return Promise.reject(Error(`No relationship found for id: ${relationshipId}`))
323
+ }
324
+
325
+ return partyRelationshipFrom(result)
326
+ }
327
+
328
+ getRelationships = async (args?: GetRelationshipsArgs): Promise<Array<PartyRelationship>> => {
329
+ const { filter } = args ?? {}
330
+ const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
331
+ const initialResult: Array<PartyRelationshipEntity> = await partyRelationshipRepository.find({
332
+ ...(filter && { where: filter }),
333
+ })
334
+
335
+ const result: Array<PartyRelationshipEntity> = await partyRelationshipRepository.find({
336
+ where: {
337
+ id: In(initialResult.map((partyRelationship: PartyRelationshipEntity) => partyRelationship.id)),
338
+ },
339
+ })
340
+
341
+ return result.map((partyRelationship: PartyRelationshipEntity) => partyRelationshipFrom(partyRelationship))
342
+ }
343
+
344
+ updateRelationship = async (args: UpdateRelationshipArgs): Promise<PartyRelationship> => {
345
+ const { relationship } = args
346
+ const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
347
+ const result: PartyRelationshipEntity | null = await partyRelationshipRepository.findOne({
348
+ where: { id: relationship.id },
349
+ })
350
+
351
+ if (!result) {
352
+ return Promise.reject(Error(`No party relationship found for id: ${relationship.id}`))
353
+ }
354
+
355
+ return this.assertRelationshipSides(relationship.leftId, relationship.rightId).then(async (): Promise<PartyRelationship> => {
356
+ debug('Updating party relationship', relationship)
357
+ const updatedResult: PartyRelationshipEntity = await partyRelationshipRepository.save(relationship, { transaction: true })
358
+
359
+ return partyRelationshipFrom(updatedResult)
360
+ })
361
+ }
362
+
363
+ removeRelationship = async (args: RemoveRelationshipArgs): Promise<void> => {
364
+ const { relationshipId } = args
365
+ const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
366
+ const relationship: PartyRelationshipEntity | null = await partyRelationshipRepository.findOne({
367
+ where: { id: relationshipId },
368
+ })
369
+
370
+ if (!relationship) {
371
+ return Promise.reject(Error(`No relationship found for id: ${relationshipId}`))
372
+ }
373
+
374
+ debug('Removing relationship', relationshipId)
375
+
376
+ await partyRelationshipRepository.delete(relationshipId)
377
+ }
378
+
379
+ addPartyType = async (args: AddPartyTypeArgs): Promise<PartyType> => {
380
+ const partyEntity: PartyTypeEntity = partyTypeEntityFrom(args)
381
+ debug('Adding party type', args)
382
+ const createdResult: PartyTypeEntity = await (await this.dbConnection).getRepository(PartyTypeEntity).save(partyEntity)
383
+
384
+ return partyTypeFrom(createdResult)
385
+ }
386
+
387
+ getPartyType = async (args: GetPartyTypeArgs): Promise<PartyType> => {
388
+ const { partyTypeId } = args
389
+ const result: PartyTypeEntity | null = await (await this.dbConnection).getRepository(PartyTypeEntity).findOne({
390
+ where: { id: partyTypeId },
391
+ })
392
+
393
+ if (!result) {
394
+ return Promise.reject(Error(`No party type found for id: ${partyTypeId}`))
395
+ }
396
+
397
+ return partyTypeFrom(result)
398
+ }
399
+
400
+ getPartyTypes = async (args?: GetPartyTypesArgs): Promise<Array<PartyType>> => {
401
+ const { filter } = args ?? {}
402
+ const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
403
+ const initialResult: Array<PartyTypeEntity> = await partyTypeRepository.find({
404
+ ...(filter && { where: filter }),
405
+ })
406
+
407
+ const result: Array<PartyTypeEntity> = await partyTypeRepository.find({
408
+ where: {
409
+ id: In(initialResult.map((partyType: PartyTypeEntity) => partyType.id)),
410
+ },
411
+ })
412
+
413
+ return result.map((partyType: PartyTypeEntity) => partyTypeFrom(partyType))
414
+ }
415
+
416
+ updatePartyType = async (args: UpdatePartyTypeArgs): Promise<PartyType> => {
417
+ const { partyType } = args
418
+ const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
419
+ const result: PartyTypeEntity | null = await partyTypeRepository.findOne({
420
+ where: { id: partyType.id },
421
+ })
422
+
423
+ if (!result) {
424
+ return Promise.reject(Error(`No party type found for id: ${partyType.id}`))
425
+ }
426
+
427
+ debug('Updating party type', partyType)
428
+ const updatedResult: PartyTypeEntity = await partyTypeRepository.save(partyType, { transaction: true })
429
+
430
+ return partyTypeFrom(updatedResult)
431
+ }
432
+
433
+ removePartyType = async (args: RemovePartyTypeArgs): Promise<void> => {
434
+ const { partyTypeId } = args
435
+ const parties: Array<PartyEntity> = await (await this.dbConnection).getRepository(PartyEntity).find({
436
+ where: {
437
+ partyType: {
438
+ id: partyTypeId,
439
+ },
440
+ },
441
+ })
442
+
443
+ if (parties.length > 0) {
444
+ return Promise.reject(Error(`Unable to remove party type with id: ${partyTypeId}. Party type is in use`))
445
+ }
446
+
447
+ const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
448
+ const partyType: PartyTypeEntity | null = await partyTypeRepository.findOne({
449
+ where: { id: partyTypeId },
450
+ })
451
+
452
+ if (!partyType) {
453
+ return Promise.reject(Error(`No party type found for id: ${partyTypeId}`))
454
+ }
455
+
456
+ debug('Removing party type', partyTypeId)
457
+
458
+ await partyTypeRepository.delete(partyTypeId)
459
+ }
460
+
461
+ getElectronicAddress = async (args: GetElectronicAddressArgs): Promise<ElectronicAddress> => {
462
+ const { electronicAddressId } = args
463
+ const result: ElectronicAddressEntity | null = await (await this.dbConnection).getRepository(ElectronicAddressEntity).findOne({
464
+ where: { id: electronicAddressId },
465
+ })
466
+
467
+ if (!result) {
468
+ return Promise.reject(Error(`No electronic address found for id: ${electronicAddressId}`))
469
+ }
470
+
471
+ return electronicAddressFrom(result)
472
+ }
473
+
474
+ getElectronicAddresses = async (args?: GetElectronicAddressesArgs): Promise<Array<ElectronicAddress>> => {
475
+ const { filter } = args ?? {}
476
+ const electronicAddressRepository: Repository<ElectronicAddressEntity> = (await this.dbConnection).getRepository(ElectronicAddressEntity)
477
+ const initialResult: Array<ElectronicAddressEntity> = await electronicAddressRepository.find({
478
+ ...(filter && { where: filter }),
479
+ })
480
+
481
+ const result: Array<ElectronicAddressEntity> = await electronicAddressRepository.find({
482
+ where: {
483
+ id: In(initialResult.map((electronicAddress: ElectronicAddressEntity) => electronicAddress.id)),
484
+ },
485
+ })
486
+
487
+ return result.map((electronicAddress: ElectronicAddressEntity) => electronicAddressFrom(electronicAddress))
488
+ }
489
+
490
+ addElectronicAddress = async (args: AddElectronicAddressArgs): Promise<ElectronicAddress> => {
491
+ const { electronicAddress, partyId } = args
492
+ const party: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
493
+ where: { id: partyId },
494
+ })
495
+
496
+ if (!party) {
497
+ return Promise.reject(Error(`No party found for id: ${partyId}`))
498
+ }
499
+
500
+ const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
501
+ electronicAddressEntity.party = party
502
+ debug('Adding electronic address', electronicAddress)
503
+ const result: ElectronicAddressEntity = await (await this.dbConnection).getRepository(ElectronicAddressEntity).save(electronicAddressEntity, {
504
+ transaction: true,
505
+ })
506
+
507
+ return electronicAddressFrom(result)
508
+ }
509
+
510
+ updateElectronicAddress = async (args: UpdateElectronicAddressArgs): Promise<ElectronicAddress> => {
511
+ const { electronicAddress } = args
512
+ const electronicAddressRepository: Repository<ElectronicAddressEntity> = (await this.dbConnection).getRepository(ElectronicAddressEntity)
513
+ const result: ElectronicAddressEntity | null = await electronicAddressRepository.findOne({
514
+ where: { id: electronicAddress.id },
515
+ })
516
+
517
+ if (!result) {
518
+ return Promise.reject(Error(`No electronic address found for id: ${electronicAddress.id}`))
519
+ }
520
+
521
+ debug('Updating electronic address', electronicAddress)
522
+ const updatedResult: ElectronicAddressEntity = await electronicAddressRepository.save(electronicAddress, { transaction: true })
523
+
524
+ return electronicAddressFrom(updatedResult)
525
+ }
526
+
527
+ removeElectronicAddress = async (args: RemoveElectronicAddressArgs): Promise<void> => {
528
+ const { electronicAddressId } = args
529
+ const electronicAddressRepository: Repository<ElectronicAddressEntity> = (await this.dbConnection).getRepository(ElectronicAddressEntity)
530
+ const electronicAddress: ElectronicAddressEntity | null = await electronicAddressRepository.findOne({
531
+ where: { id: electronicAddressId },
532
+ })
533
+
534
+ if (!electronicAddress) {
535
+ return Promise.reject(Error(`No electronic address found for id: ${electronicAddressId}`))
536
+ }
537
+
538
+ debug('Removing electronic address', electronicAddressId)
539
+
540
+ await electronicAddressRepository.delete(electronicAddressId)
541
+ }
542
+
543
+ getPhysicalAddress = async (args: GetPhysicalAddressArgs): Promise<PhysicalAddress> => {
544
+ const { physicalAddressId } = args
545
+ const result: PhysicalAddressEntity | null = await (await this.dbConnection).getRepository(PhysicalAddressEntity).findOne({
546
+ where: { id: physicalAddressId },
547
+ })
548
+
549
+ if (!result) {
550
+ return Promise.reject(Error(`No physical address found for id: ${physicalAddressId}`))
551
+ }
552
+
553
+ return physicalAddressFrom(result)
554
+ }
555
+
556
+ getPhysicalAddresses = async (args?: GetPhysicalAddressesArgs): Promise<Array<PhysicalAddress>> => {
557
+ const { filter } = args ?? {}
558
+ const physicalAddressRepository: Repository<PhysicalAddressEntity> = (await this.dbConnection).getRepository(PhysicalAddressEntity)
559
+ const initialResult: Array<PhysicalAddressEntity> = await physicalAddressRepository.find({
560
+ ...(filter && { where: filter }),
561
+ })
562
+
563
+ const result: Array<PhysicalAddressEntity> = await physicalAddressRepository.find({
564
+ where: {
565
+ id: In(initialResult.map((physicalAddress: PhysicalAddressEntity) => physicalAddress.id)),
566
+ },
567
+ })
568
+
569
+ return result.map((physicalAddress: PhysicalAddressEntity) => physicalAddressFrom(physicalAddress))
570
+ }
571
+
572
+ addPhysicalAddress = async (args: AddPhysicalAddressArgs): Promise<PhysicalAddress> => {
573
+ const { physicalAddress, partyId } = args
574
+ const party: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
575
+ where: { id: partyId },
576
+ })
577
+
578
+ if (!party) {
579
+ return Promise.reject(Error(`No party found for id: ${partyId}`))
580
+ }
581
+
582
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
583
+ physicalAddressEntity.party = party
584
+ debug('Adding physical address', physicalAddress)
585
+ const result: PhysicalAddressEntity = await (await this.dbConnection).getRepository(PhysicalAddressEntity).save(physicalAddressEntity, {
586
+ transaction: true,
587
+ })
588
+
589
+ return physicalAddressFrom(result)
590
+ }
591
+
592
+ updatePhysicalAddress = async (args: UpdatePhysicalAddressArgs): Promise<PhysicalAddress> => {
593
+ const { physicalAddress } = args
594
+ const physicalAddressRepository: Repository<PhysicalAddressEntity> = (await this.dbConnection).getRepository(PhysicalAddressEntity)
595
+ const result: PhysicalAddressEntity | null = await physicalAddressRepository.findOne({
596
+ where: { id: physicalAddress.id },
597
+ })
598
+
599
+ if (!result) {
600
+ return Promise.reject(Error(`No physical address found for id: ${physicalAddress.id}`))
601
+ }
602
+
603
+ debug('Updating physical address', physicalAddress)
604
+ const updatedResult: PhysicalAddressEntity = await physicalAddressRepository.save(physicalAddress, { transaction: true })
605
+
606
+ return physicalAddressFrom(updatedResult)
607
+ }
608
+
609
+ removePhysicalAddress = async (args: RemovePhysicalAddressArgs): Promise<void> => {
610
+ const { physicalAddressId } = args
611
+ const physicalAddressRepository: Repository<PhysicalAddressEntity> = (await this.dbConnection).getRepository(PhysicalAddressEntity)
612
+ const physicalAddress: PhysicalAddressEntity | null = await physicalAddressRepository.findOne({
613
+ where: { id: physicalAddressId },
614
+ })
615
+
616
+ if (!physicalAddress) {
617
+ return Promise.reject(Error(`No physical address found for id: ${physicalAddressId}`))
618
+ }
619
+
620
+ debug('Removing physical address', physicalAddressId)
621
+
622
+ await physicalAddressRepository.delete(physicalAddressId)
623
+ }
624
+
625
+ private hasCorrectConnectionConfig(type: ConnectionType, config: NonPersistedConnectionConfig): boolean {
626
+ switch (type) {
627
+ case ConnectionType.OPENID_CONNECT:
628
+ return isOpenIdConfig(config)
629
+ case ConnectionType.SIOPv2:
630
+ return isDidAuthConfig(config)
631
+ default:
632
+ throw new Error('Connection type not supported')
633
+ }
634
+ }
635
+
636
+ private hasCorrectPartyType(type: PartyTypeType, contact: NonPersistedContact): boolean {
637
+ switch (type) {
638
+ case PartyTypeType.NATURAL_PERSON:
639
+ return isNaturalPerson(contact)
640
+ case PartyTypeType.ORGANIZATION:
641
+ return isOrganization(contact)
642
+ default:
643
+ throw new Error('Party type not supported')
644
+ }
645
+ }
646
+
647
+ private async deleteIdentities(identities: Array<IdentityEntity>): Promise<void> {
648
+ debug('Removing identities', identities)
649
+
650
+ const connection: DataSource = await this.dbConnection
651
+ const correlationIdentifierRepository: Repository<CorrelationIdentifierEntity> = connection.getRepository(CorrelationIdentifierEntity)
652
+ const baseConfigRepository: Repository<BaseConfigEntity> = connection.getRepository(BaseConfigEntity)
653
+ const connectionRepository: Repository<ConnectionEntity> = connection.getRepository(ConnectionEntity)
654
+ const identityMetadataItemRepository: Repository<IdentityMetadataItemEntity> = connection.getRepository(IdentityMetadataItemEntity)
655
+ const identityRepository: Repository<IdentityEntity> = connection.getRepository(IdentityEntity)
656
+
657
+ identities.map(async (identity: IdentityEntity): Promise<void> => {
658
+ await correlationIdentifierRepository
659
+ .delete(identity.identifier.id)
660
+ .catch((error) => Promise.reject(Error(`Unable to remove identity.identifier with id ${identity.identifier.id}. ${error}`)))
661
+
662
+ if (identity.connection) {
663
+ await baseConfigRepository.delete(identity.connection.config.id)
664
+ await connectionRepository
665
+ .delete(identity.connection.id)
666
+ .catch((error) => Promise.reject(Error(`Unable to remove identity.connection with id ${identity.connection?.id}. ${error}`)))
667
+ }
668
+
669
+ if (identity.metadata) {
670
+ identity.metadata.map(async (metadataItem: IdentityMetadataItemEntity): Promise<void> => {
671
+ await identityMetadataItemRepository
672
+ .delete(metadataItem.id)
673
+ .catch((error) => Promise.reject(Error(`Unable to remove identity.metadataItem with id ${metadataItem.id}. ${error}`)))
674
+ })
675
+ }
676
+
677
+ await identityRepository
678
+ .delete(identity.id)
679
+ .catch((error) => Promise.reject(Error(`Unable to remove identity with id ${identity.id}. ${error}`)))
680
+ })
681
+ }
682
+
683
+ private async deleteElectronicAddresses(electronicAddresses: Array<ElectronicAddressEntity>): Promise<void> {
684
+ debug('Removing electronic addresses', electronicAddresses)
685
+
686
+ const electronicAddressRepository: Repository<ElectronicAddressEntity> = (await this.dbConnection).getRepository(ElectronicAddressEntity)
687
+ electronicAddresses.map(async (electronicAddress: ElectronicAddressEntity): Promise<void> => {
688
+ await electronicAddressRepository
689
+ .delete(electronicAddress.id)
690
+ .catch((error) => Promise.reject(Error(`Unable to remove electronic address with id ${electronicAddress.id}. ${error}`)))
691
+ })
692
+ }
693
+
694
+ private async deletePhysicalAddresses(physicalAddresses: Array<PhysicalAddressEntity>): Promise<void> {
695
+ debug('Removing physical addresses', physicalAddresses)
696
+
697
+ const physicalAddressRepository: Repository<PhysicalAddressEntity> = (await this.dbConnection).getRepository(PhysicalAddressEntity)
698
+ physicalAddresses.map(async (physicalAddress: PhysicalAddressEntity): Promise<void> => {
699
+ await physicalAddressRepository
700
+ .delete(physicalAddress.id)
701
+ .catch((error) => Promise.reject(Error(`Unable to remove physical address with id ${physicalAddress.id}. ${error}`)))
702
+ })
703
+ }
704
+
705
+ private async assertRelationshipSides(leftId: string, rightId: string): Promise<void> {
706
+ const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
707
+ const leftParty: PartyEntity | null = await partyRepository.findOne({
708
+ where: { id: leftId },
709
+ })
710
+
711
+ if (!leftParty) {
712
+ return Promise.reject(Error(`No party found for left side of the relationship, party id: ${leftId}`))
713
+ }
714
+
715
+ const rightParty: PartyEntity | null = await partyRepository.findOne({
716
+ where: { id: rightId },
717
+ })
718
+
719
+ if (!rightParty) {
720
+ return Promise.reject(Error(`No party found for right side of the relationship, party id: ${rightId}`))
721
+ }
722
+ }
723
+ }