@sphereon/ssi-sdk.data-store 0.18.2-next.3 → 0.18.2-unstable.10
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/LICENSE +201 -201
- package/README.md +77 -77
- package/dist/contact/AbstractContactStore.d.ts +23 -23
- package/dist/contact/AbstractContactStore.js +6 -6
- package/dist/contact/ContactStore.d.ts +32 -32
- package/dist/contact/ContactStore.js +365 -365
- package/dist/entities/contact/BaseConfigEntity.d.ts +6 -6
- package/dist/entities/contact/BaseConfigEntity.js +33 -33
- package/dist/entities/contact/BaseContactEntity.d.ts +9 -9
- package/dist/entities/contact/BaseContactEntity.js +53 -53
- package/dist/entities/contact/ConnectionEntity.d.ts +10 -10
- package/dist/entities/contact/ConnectionEntity.js +46 -46
- package/dist/entities/contact/ContactEntity.d.ts +16 -0
- package/dist/entities/contact/ContactEntity.d.ts.map +1 -0
- package/dist/entities/contact/ContactEntity.js +164 -0
- package/dist/entities/contact/ContactEntity.js.map +1 -0
- package/dist/entities/contact/CorrelationIdentifierEntity.d.ts +10 -10
- package/dist/entities/contact/CorrelationIdentifierEntity.js +71 -71
- package/dist/entities/contact/DidAuthConfigEntity.d.ts +6 -6
- package/dist/entities/contact/DidAuthConfigEntity.js +32 -32
- package/dist/entities/contact/ElectronicAddressEntity.d.ts +13 -13
- package/dist/entities/contact/ElectronicAddressEntity.js +91 -91
- package/dist/entities/contact/IdentityEntity.d.ts +20 -20
- package/dist/entities/contact/IdentityEntity.js +128 -128
- package/dist/entities/contact/IdentityMetadataItemEntity.d.ts +9 -9
- package/dist/entities/contact/IdentityMetadataItemEntity.js +68 -68
- package/dist/entities/contact/NaturalPersonEntity.d.ts +8 -8
- package/dist/entities/contact/NaturalPersonEntity.js +71 -71
- package/dist/entities/contact/OpenIdConfigEntity.d.ts +10 -10
- package/dist/entities/contact/OpenIdConfigEntity.js +48 -48
- package/dist/entities/contact/OrganizationEntity.d.ts +8 -8
- package/dist/entities/contact/OrganizationEntity.js +66 -66
- package/dist/entities/contact/PartyEntity.d.ts +19 -19
- package/dist/entities/contact/PartyEntity.js +131 -131
- package/dist/entities/contact/PartyRelationshipEntity.d.ts +12 -12
- package/dist/entities/contact/PartyRelationshipEntity.js +94 -94
- package/dist/entities/contact/PartyTypeEntity.d.ts +14 -14
- package/dist/entities/contact/PartyTypeEntity.js +103 -103
- package/dist/entities/eventLogger/AuditEventEntity.d.ts +26 -26
- package/dist/entities/eventLogger/AuditEventEntity.js +123 -123
- package/dist/entities/issuanceBranding/BackgroundAttributesEntity.d.ts +10 -10
- package/dist/entities/issuanceBranding/BackgroundAttributesEntity.js +73 -73
- package/dist/entities/issuanceBranding/BaseLocaleBrandingEntity.d.ts +17 -17
- package/dist/entities/issuanceBranding/BaseLocaleBrandingEntity.js +119 -119
- package/dist/entities/issuanceBranding/CredentialBrandingEntity.d.ts +14 -14
- package/dist/entities/issuanceBranding/CredentialBrandingEntity.js +101 -101
- package/dist/entities/issuanceBranding/CredentialLocaleBrandingEntity.d.ts +8 -8
- package/dist/entities/issuanceBranding/CredentialLocaleBrandingEntity.js +48 -48
- package/dist/entities/issuanceBranding/ImageAttributesEntity.d.ts +13 -13
- package/dist/entities/issuanceBranding/ImageAttributesEntity.js +91 -91
- package/dist/entities/issuanceBranding/ImageDimensionsEntity.d.ts +8 -8
- package/dist/entities/issuanceBranding/ImageDimensionsEntity.js +38 -38
- package/dist/entities/issuanceBranding/IssuerBrandingEntity.d.ts +13 -13
- package/dist/entities/issuanceBranding/IssuerBrandingEntity.js +94 -94
- package/dist/entities/issuanceBranding/IssuerLocaleBrandingEntity.d.ts +8 -8
- package/dist/entities/issuanceBranding/IssuerLocaleBrandingEntity.js +48 -48
- package/dist/entities/issuanceBranding/TextAttributesEntity.d.ts +8 -8
- package/dist/entities/issuanceBranding/TextAttributesEntity.js +61 -61
- package/dist/entities/statusList2021/StatusList2021Entity.d.ts +18 -18
- package/dist/entities/statusList2021/StatusList2021Entity.js +114 -114
- package/dist/entities/statusList2021/StatusList2021EntryEntity.d.ts +10 -10
- package/dist/entities/statusList2021/StatusList2021EntryEntity.js +50 -50
- package/dist/entities/validators/index.d.ts +6 -6
- package/dist/entities/validators/index.js +26 -26
- package/dist/eventLogger/AbstractEventLoggerStore.d.ts +6 -6
- package/dist/eventLogger/AbstractEventLoggerStore.js +6 -6
- package/dist/eventLogger/EventLoggerStore.d.ts +12 -12
- package/dist/eventLogger/EventLoggerStore.js +43 -43
- package/dist/index.d.ts +41 -41
- package/dist/index.js +127 -127
- package/dist/issuanceBranding/AbstractIssuanceBrandingStore.d.ts +19 -19
- package/dist/issuanceBranding/AbstractIssuanceBrandingStore.js +6 -6
- package/dist/issuanceBranding/IssuanceBrandingStore.d.ts +30 -30
- package/dist/issuanceBranding/IssuanceBrandingStore.js +374 -374
- package/dist/migrations/generic/1-CreateContacts.d.ts +6 -6
- package/dist/migrations/generic/1-CreateContacts.js +77 -77
- package/dist/migrations/generic/1-CreateIssuanceBranding.d.ts +7 -0
- package/dist/migrations/generic/1-CreateIssuanceBranding.d.ts.map +1 -0
- package/dist/migrations/generic/1-CreateIssuanceBranding.js +96 -0
- package/dist/migrations/generic/1-CreateIssuanceBranding.js.map +1 -0
- package/dist/migrations/generic/2-CreateContacts.d.ts +7 -0
- package/dist/migrations/generic/2-CreateContacts.d.ts.map +1 -0
- package/dist/migrations/generic/2-CreateContacts.js +78 -0
- package/dist/migrations/generic/2-CreateContacts.js.map +1 -0
- package/dist/migrations/generic/2-CreateIssuanceBranding.d.ts +6 -6
- package/dist/migrations/generic/2-CreateIssuanceBranding.js +77 -77
- package/dist/migrations/generic/3-CreateContacts.d.ts +6 -6
- package/dist/migrations/generic/3-CreateContacts.js +77 -77
- package/dist/migrations/generic/3-CreateStatusList.d.ts +7 -0
- package/dist/migrations/generic/3-CreateStatusList.d.ts.map +1 -0
- package/dist/migrations/generic/3-CreateStatusList.js +72 -0
- package/dist/migrations/generic/3-CreateStatusList.js.map +1 -0
- package/dist/migrations/generic/4-CreateStatusList.d.ts +6 -6
- package/dist/migrations/generic/4-CreateStatusList.js +71 -71
- package/dist/migrations/generic/5-CreateAuditEvents.d.ts +6 -6
- package/dist/migrations/generic/5-CreateAuditEvents.js +77 -77
- package/dist/migrations/generic/index.d.ts +16 -16
- package/dist/migrations/generic/index.js +27 -27
- package/dist/migrations/index.d.ts +1 -1
- package/dist/migrations/index.js +5 -5
- package/dist/migrations/internal-migrations-ormconfig.d.ts +6 -6
- package/dist/migrations/internal-migrations-ormconfig.js +26 -26
- package/dist/migrations/postgres/1659463079428-CreateContacts.d.ts +6 -6
- package/dist/migrations/postgres/1659463079428-CreateContacts.js +56 -56
- package/dist/migrations/postgres/1685628974232-CreateIssuanceBranding.d.ts +6 -6
- package/dist/migrations/postgres/1685628974232-CreateIssuanceBranding.js +68 -68
- package/dist/migrations/postgres/1690925872592-CreateContacts.d.ts +6 -6
- package/dist/migrations/postgres/1690925872592-CreateContacts.js +68 -68
- package/dist/migrations/postgres/1693866470001-CreateStatusList.d.ts +6 -6
- package/dist/migrations/postgres/1693866470001-CreateStatusList.js +31 -31
- package/dist/migrations/postgres/1701634812183-CreateAuditEvents.d.ts +6 -6
- package/dist/migrations/postgres/1701634812183-CreateAuditEvents.js +42 -42
- package/dist/migrations/postgres/CreateStatusList1693866470001-CreateStatusList.d.ts +7 -0
- package/dist/migrations/postgres/CreateStatusList1693866470001-CreateStatusList.d.ts.map +1 -0
- package/dist/migrations/postgres/CreateStatusList1693866470001-CreateStatusList.js +32 -0
- package/dist/migrations/postgres/CreateStatusList1693866470001-CreateStatusList.js.map +1 -0
- package/dist/migrations/postgres/uuid.d.ts +3 -0
- package/dist/migrations/postgres/uuid.d.ts.map +1 -0
- package/dist/migrations/postgres/uuid.js +25 -0
- package/dist/migrations/postgres/uuid.js.map +1 -0
- package/dist/migrations/sqlite/1659463069549-CreateContacts.d.ts +6 -6
- package/dist/migrations/sqlite/1659463069549-CreateContacts.js +78 -78
- package/dist/migrations/sqlite/1685628973231-CreateIssuanceBranding.d.ts +6 -6
- package/dist/migrations/sqlite/1685628973231-CreateIssuanceBranding.js +88 -88
- package/dist/migrations/sqlite/1690925872693-CreateContacts.d.ts +6 -6
- package/dist/migrations/sqlite/1690925872693-CreateContacts.js +107 -107
- package/dist/migrations/sqlite/1693866470000-CreateStatusList.d.ts +6 -6
- package/dist/migrations/sqlite/1693866470000-CreateStatusList.js +31 -31
- package/dist/migrations/sqlite/1701634819487-CreateAuditEvents.d.ts +6 -6
- package/dist/migrations/sqlite/1701634819487-CreateAuditEvents.js +28 -28
- package/dist/statusList/IStatusListStore.d.ts +19 -19
- package/dist/statusList/IStatusListStore.js +2 -2
- package/dist/statusList/StatusListStore.d.ts +35 -35
- package/dist/statusList/StatusListStore.js +212 -212
- package/dist/types/contact/IAbstractContactStore.d.ts +74 -74
- package/dist/types/contact/IAbstractContactStore.js +2 -2
- package/dist/types/contact/contact.d.ts +173 -173
- package/dist/types/contact/contact.js +25 -25
- package/dist/types/eventLogger/IAbstractEventLoggerStore.d.ts +9 -9
- package/dist/types/eventLogger/IAbstractEventLoggerStore.js +2 -2
- package/dist/types/eventLogger/eventLogger.d.ts +2 -2
- package/dist/types/eventLogger/eventLogger.js +2 -2
- package/dist/types/index.d.ts +9 -9
- package/dist/types/index.js +25 -25
- package/dist/types/issuanceBranding/IAbstractIssuanceBrandingStore.d.ts +58 -58
- package/dist/types/issuanceBranding/IAbstractIssuanceBrandingStore.js +2 -2
- package/dist/types/issuanceBranding/issuanceBranding.d.ts +114 -114
- package/dist/types/issuanceBranding/issuanceBranding.js +2 -2
- package/dist/types/statusList/IAbstractStatusListStore.d.ts +37 -37
- package/dist/types/statusList/IAbstractStatusListStore.js +2 -2
- package/dist/types/statusList/statusList.d.ts +24 -24
- package/dist/types/statusList/statusList.js +2 -2
- package/dist/types/validation/validation.d.ts +3 -3
- package/dist/types/validation/validation.js +2 -2
- package/dist/utils/ValidatorUtils.d.ts +3 -3
- package/dist/utils/ValidatorUtils.js +12 -12
- package/dist/utils/contact/MappingUtils.d.ts +46 -46
- package/dist/utils/contact/MappingUtils.js +290 -290
- package/package.json +4 -4
- package/src/__tests__/eventLogger.entities.test.ts +73 -73
- package/src/__tests__/eventLogger.store.test.ts +136 -136
- package/src/contact/ContactStore.ts +501 -501
- package/src/entities/eventLogger/AuditEventEntity.ts +99 -99
- package/src/entities/statusList2021/StatusList2021Entity.ts +96 -96
- package/src/eventLogger/AbstractEventLoggerStore.ts +7 -7
- package/src/eventLogger/EventLoggerStore.ts +62 -62
- package/src/index.ts +113 -113
- package/src/migrations/generic/1-CreateContacts.ts +66 -66
- package/src/migrations/generic/2-CreateIssuanceBranding.ts +64 -64
- package/src/migrations/generic/3-CreateContacts.ts +66 -66
- package/src/migrations/generic/4-CreateStatusList.ts +54 -54
- package/src/migrations/generic/5-CreateAuditEvents.ts +66 -66
- package/src/migrations/generic/index.ts +27 -27
- package/src/migrations/postgres/1693866470001-CreateStatusList.ts +24 -24
- package/src/migrations/postgres/1701634812183-CreateAuditEvents.ts +33 -33
- package/src/migrations/sqlite/1690925872693-CreateContacts.ts +163 -163
- package/src/migrations/sqlite/1701634819487-CreateAuditEvents.ts +15 -15
- package/src/types/eventLogger/IAbstractEventLoggerStore.ts +12 -12
- package/src/types/eventLogger/eventLogger.ts +3 -3
- package/src/types/index.ts +9 -9
|
@@ -1,501 +1,501 @@
|
|
|
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 {
|
|
15
|
-
identityEntityFrom,
|
|
16
|
-
identityFrom,
|
|
17
|
-
isDidAuthConfig,
|
|
18
|
-
isNaturalPerson,
|
|
19
|
-
isOpenIdConfig,
|
|
20
|
-
isOrganization,
|
|
21
|
-
partyEntityFrom,
|
|
22
|
-
partyFrom,
|
|
23
|
-
partyRelationshipEntityFrom,
|
|
24
|
-
partyRelationshipFrom,
|
|
25
|
-
partyTypeEntityFrom,
|
|
26
|
-
partyTypeFrom,
|
|
27
|
-
} from '../utils/contact/MappingUtils'
|
|
28
|
-
import {
|
|
29
|
-
AddIdentityArgs,
|
|
30
|
-
GetIdentityArgs,
|
|
31
|
-
GetIdentitiesArgs,
|
|
32
|
-
GetPartyArgs,
|
|
33
|
-
RemoveIdentityArgs,
|
|
34
|
-
UpdateIdentityArgs,
|
|
35
|
-
GetPartiesArgs,
|
|
36
|
-
AddPartyArgs,
|
|
37
|
-
UpdatePartyArgs,
|
|
38
|
-
RemovePartyArgs,
|
|
39
|
-
NonPersistedConnectionConfig,
|
|
40
|
-
ConnectionTypeEnum,
|
|
41
|
-
CorrelationIdentifierEnum,
|
|
42
|
-
Party,
|
|
43
|
-
Identity,
|
|
44
|
-
RemoveRelationshipArgs,
|
|
45
|
-
PartyRelationship,
|
|
46
|
-
AddRelationshipArgs,
|
|
47
|
-
GetRelationshipArgs,
|
|
48
|
-
AddPartyTypeArgs,
|
|
49
|
-
PartyType,
|
|
50
|
-
GetPartyTypeArgs,
|
|
51
|
-
GetPartyTypesArgs,
|
|
52
|
-
UpdatePartyTypeArgs,
|
|
53
|
-
RemovePartyTypeArgs,
|
|
54
|
-
GetRelationshipsArgs,
|
|
55
|
-
UpdateRelationshipArgs,
|
|
56
|
-
PartyTypeEnum,
|
|
57
|
-
NonPersistedContact,
|
|
58
|
-
} from '../types'
|
|
59
|
-
|
|
60
|
-
const debug: Debug.Debugger = Debug('sphereon:ssi-sdk:contact-store')
|
|
61
|
-
|
|
62
|
-
export class ContactStore extends AbstractContactStore {
|
|
63
|
-
private readonly dbConnection: OrPromise<DataSource>
|
|
64
|
-
|
|
65
|
-
constructor(dbConnection: OrPromise<DataSource>) {
|
|
66
|
-
super()
|
|
67
|
-
this.dbConnection = dbConnection
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
getParty = async ({ partyId }: GetPartyArgs): Promise<Party> => {
|
|
71
|
-
const result: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
|
|
72
|
-
where: { id: partyId },
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
if (!result) {
|
|
76
|
-
return Promise.reject(Error(`No party found for id: ${partyId}`))
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return partyFrom(result)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
getParties = async (args?: GetPartiesArgs): Promise<Array<Party>> => {
|
|
83
|
-
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
84
|
-
const initialResult: Array<PartyEntity> = await partyRepository.find({
|
|
85
|
-
...(args?.filter && { where: args?.filter }),
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
const result: Array<PartyEntity> = await partyRepository.find({
|
|
89
|
-
where: {
|
|
90
|
-
id: In(initialResult.map((party: PartyEntity) => party.id)),
|
|
91
|
-
},
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
return result.map((party: PartyEntity) => partyFrom(party))
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
addParty = async (args: AddPartyArgs): Promise<Party> => {
|
|
98
|
-
const { identities, contact, partyType } = args
|
|
99
|
-
|
|
100
|
-
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
101
|
-
|
|
102
|
-
if (!this.hasCorrectPartyType(partyType.type, contact)) {
|
|
103
|
-
return Promise.reject(Error(`Party type ${partyType.type}, does not match for provided contact`))
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
for (const identity of identities ?? []) {
|
|
107
|
-
if (identity.identifier.type === CorrelationIdentifierEnum.URL) {
|
|
108
|
-
if (!identity.connection) {
|
|
109
|
-
return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierEnum.URL} should contain a connection`))
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
|
|
113
|
-
return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const partyEntity: PartyEntity = partyEntityFrom(args)
|
|
119
|
-
debug('Adding party', args)
|
|
120
|
-
const createdResult: PartyEntity = await partyRepository.save(partyEntity)
|
|
121
|
-
|
|
122
|
-
return partyFrom(createdResult)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
updateParty = async ({ party }: UpdatePartyArgs): Promise<Party> => {
|
|
126
|
-
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
127
|
-
const result: PartyEntity | null = await partyRepository.findOne({
|
|
128
|
-
where: { id: party.id },
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
if (!result) {
|
|
132
|
-
return Promise.reject(Error(`No party found for id: ${party.id}`))
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const updatedParty = {
|
|
136
|
-
...party,
|
|
137
|
-
identities: result.identities,
|
|
138
|
-
type: result.partyType,
|
|
139
|
-
relationships: result.relationships,
|
|
140
|
-
electronicAddresses: result.electronicAddresses,
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
debug('Updating party', party)
|
|
144
|
-
const updatedResult: PartyEntity = await partyRepository.save(updatedParty, { transaction: true })
|
|
145
|
-
|
|
146
|
-
return partyFrom(updatedResult)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
removeParty = async ({ partyId }: RemovePartyArgs): Promise<void> => {
|
|
150
|
-
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
151
|
-
debug('Removing party', partyId)
|
|
152
|
-
partyRepository
|
|
153
|
-
.findOneById(partyId)
|
|
154
|
-
.then(async (party: PartyEntity | null): Promise<void> => {
|
|
155
|
-
if (!party) {
|
|
156
|
-
await Promise.reject(Error(`Unable to find the party with id to remove: ${partyId}`))
|
|
157
|
-
} else {
|
|
158
|
-
await this.deleteIdentities(party.identities)
|
|
159
|
-
|
|
160
|
-
await partyRepository
|
|
161
|
-
.delete({ id: partyId })
|
|
162
|
-
.catch((error) => Promise.reject(Error(`Unable to remove party with id: ${partyId}. ${error}`)))
|
|
163
|
-
|
|
164
|
-
const partyContactRepository: Repository<BaseContactEntity> = (await this.dbConnection).getRepository(BaseContactEntity)
|
|
165
|
-
await partyContactRepository
|
|
166
|
-
.delete({ id: party.contact.id })
|
|
167
|
-
.catch((error) => Promise.reject(Error(`Unable to remove party contact with id: ${party.contact.id}. ${error}`)))
|
|
168
|
-
}
|
|
169
|
-
})
|
|
170
|
-
.catch((error) => Promise.reject(Error(`Unable to remove party with id: ${partyId}. ${error}`)))
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
getIdentity = async ({ identityId }: GetIdentityArgs): Promise<Identity> => {
|
|
174
|
-
const result: IdentityEntity | null = await (await this.dbConnection).getRepository(IdentityEntity).findOne({
|
|
175
|
-
where: { id: identityId },
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
if (!result) {
|
|
179
|
-
return Promise.reject(Error(`No identity found for id: ${identityId}`))
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return identityFrom(result)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
getIdentities = async (args?: GetIdentitiesArgs): Promise<Array<Identity>> => {
|
|
186
|
-
const identityRepository: Repository<IdentityEntity> = (await this.dbConnection).getRepository(IdentityEntity)
|
|
187
|
-
const initialResult: Array<IdentityEntity> = await identityRepository.find({
|
|
188
|
-
...(args?.filter && { where: args?.filter }),
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
const result: Array<IdentityEntity> = await identityRepository.find({
|
|
192
|
-
where: {
|
|
193
|
-
id: In(initialResult.map((identity: IdentityEntity) => identity.id)),
|
|
194
|
-
},
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
return result.map((identity: IdentityEntity) => identityFrom(identity))
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
addIdentity = async ({ identity, partyId }: AddIdentityArgs): Promise<Identity> => {
|
|
201
|
-
const party: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
|
|
202
|
-
where: { id: partyId },
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
if (!party) {
|
|
206
|
-
return Promise.reject(Error(`No party found for id: ${partyId}`))
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (identity.identifier.type === CorrelationIdentifierEnum.URL) {
|
|
210
|
-
if (!identity.connection) {
|
|
211
|
-
return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierEnum.URL} should contain a connection`))
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
|
|
215
|
-
return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const identityEntity: IdentityEntity = identityEntityFrom(identity)
|
|
220
|
-
identityEntity.party = party
|
|
221
|
-
debug('Adding identity', identity)
|
|
222
|
-
const result: IdentityEntity = await (await this.dbConnection).getRepository(IdentityEntity).save(identityEntity, {
|
|
223
|
-
transaction: true,
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
return identityFrom(result)
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
updateIdentity = async ({ identity }: UpdateIdentityArgs): Promise<Identity> => {
|
|
230
|
-
const identityRepository: Repository<IdentityEntity> = (await this.dbConnection).getRepository(IdentityEntity)
|
|
231
|
-
const result: IdentityEntity | null = await identityRepository.findOne({
|
|
232
|
-
where: { id: identity.id },
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
if (!result) {
|
|
236
|
-
return Promise.reject(Error(`No identity found for id: ${identity.id}`))
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (identity.identifier.type === CorrelationIdentifierEnum.URL) {
|
|
240
|
-
if (!identity.connection) {
|
|
241
|
-
return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierEnum.URL} should contain a connection`))
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
|
|
245
|
-
return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
debug('Updating identity', identity)
|
|
250
|
-
const updatedResult: IdentityEntity = await identityRepository.save(identity, { transaction: true })
|
|
251
|
-
|
|
252
|
-
return identityFrom(updatedResult)
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
removeIdentity = async ({ identityId }: RemoveIdentityArgs): Promise<void> => {
|
|
256
|
-
const identity: IdentityEntity | null = await (await this.dbConnection).getRepository(IdentityEntity).findOne({
|
|
257
|
-
where: { id: identityId },
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
if (!identity) {
|
|
261
|
-
return Promise.reject(Error(`No identity found for id: ${identityId}`))
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
debug('Removing identity', identityId)
|
|
265
|
-
|
|
266
|
-
await this.deleteIdentities([identity])
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
addRelationship = async ({ leftId, rightId }: AddRelationshipArgs): Promise<PartyRelationship> => {
|
|
270
|
-
return this.assertRelationshipSides(leftId, rightId).then(async (): Promise<PartyRelationship> => {
|
|
271
|
-
const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
|
|
272
|
-
leftId,
|
|
273
|
-
rightId,
|
|
274
|
-
})
|
|
275
|
-
debug('Adding party relationship', relationship)
|
|
276
|
-
|
|
277
|
-
const createdResult: PartyRelationshipEntity = await (await this.dbConnection).getRepository(PartyRelationshipEntity).save(relationship)
|
|
278
|
-
|
|
279
|
-
return partyRelationshipFrom(createdResult)
|
|
280
|
-
})
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
getRelationship = async ({ relationshipId }: GetRelationshipArgs): Promise<PartyRelationship> => {
|
|
284
|
-
const result: PartyRelationshipEntity | null = await (await this.dbConnection).getRepository(PartyRelationshipEntity).findOne({
|
|
285
|
-
where: { id: relationshipId },
|
|
286
|
-
})
|
|
287
|
-
|
|
288
|
-
if (!result) {
|
|
289
|
-
return Promise.reject(Error(`No relationship found for id: ${relationshipId}`))
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
return partyRelationshipFrom(result)
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
getRelationships = async (args?: GetRelationshipsArgs): Promise<Array<PartyRelationship>> => {
|
|
296
|
-
const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
|
|
297
|
-
const initialResult: Array<PartyRelationshipEntity> = await partyRelationshipRepository.find({
|
|
298
|
-
...(args?.filter && { where: args?.filter }),
|
|
299
|
-
})
|
|
300
|
-
|
|
301
|
-
const result: Array<PartyRelationshipEntity> = await partyRelationshipRepository.find({
|
|
302
|
-
where: {
|
|
303
|
-
id: In(initialResult.map((partyRelationship: PartyRelationshipEntity) => partyRelationship.id)),
|
|
304
|
-
},
|
|
305
|
-
})
|
|
306
|
-
|
|
307
|
-
return result.map((partyRelationship: PartyRelationshipEntity) => partyRelationshipFrom(partyRelationship))
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
updateRelationship = async ({ relationship }: UpdateRelationshipArgs): Promise<PartyRelationship> => {
|
|
311
|
-
const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
|
|
312
|
-
const result: PartyRelationshipEntity | null = await partyRelationshipRepository.findOne({
|
|
313
|
-
where: { id: relationship.id },
|
|
314
|
-
})
|
|
315
|
-
|
|
316
|
-
if (!result) {
|
|
317
|
-
return Promise.reject(Error(`No party relationship found for id: ${relationship.id}`))
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
return this.assertRelationshipSides(relationship.leftId, relationship.rightId).then(async (): Promise<PartyRelationship> => {
|
|
321
|
-
debug('Updating party relationship', relationship)
|
|
322
|
-
const updatedResult: PartyRelationshipEntity = await partyRelationshipRepository.save(relationship, { transaction: true })
|
|
323
|
-
|
|
324
|
-
return partyRelationshipFrom(updatedResult)
|
|
325
|
-
})
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
removeRelationship = async ({ relationshipId }: RemoveRelationshipArgs): Promise<void> => {
|
|
329
|
-
const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
|
|
330
|
-
const relationship: PartyRelationshipEntity | null = await partyRelationshipRepository.findOne({
|
|
331
|
-
where: { id: relationshipId },
|
|
332
|
-
})
|
|
333
|
-
|
|
334
|
-
if (!relationship) {
|
|
335
|
-
return Promise.reject(Error(`No relationship found for id: ${relationshipId}`))
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
debug('Removing relationship', relationshipId)
|
|
339
|
-
|
|
340
|
-
await partyRelationshipRepository.delete(relationshipId)
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
addPartyType = async (args: AddPartyTypeArgs): Promise<PartyType> => {
|
|
344
|
-
const partyEntity: PartyTypeEntity = partyTypeEntityFrom(args)
|
|
345
|
-
debug('Adding party type', args)
|
|
346
|
-
const createdResult: PartyTypeEntity = await (await this.dbConnection).getRepository(PartyTypeEntity).save(partyEntity)
|
|
347
|
-
|
|
348
|
-
return partyTypeFrom(createdResult)
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
getPartyType = async ({ partyTypeId }: GetPartyTypeArgs): Promise<PartyType> => {
|
|
352
|
-
const result: PartyTypeEntity | null = await (await this.dbConnection).getRepository(PartyTypeEntity).findOne({
|
|
353
|
-
where: { id: partyTypeId },
|
|
354
|
-
})
|
|
355
|
-
|
|
356
|
-
if (!result) {
|
|
357
|
-
return Promise.reject(Error(`No party type found for id: ${partyTypeId}`))
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
return partyTypeFrom(result)
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
getPartyTypes = async (args?: GetPartyTypesArgs): Promise<Array<PartyType>> => {
|
|
364
|
-
const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
|
|
365
|
-
const initialResult: Array<PartyTypeEntity> = await partyTypeRepository.find({
|
|
366
|
-
...(args?.filter && { where: args?.filter }),
|
|
367
|
-
})
|
|
368
|
-
|
|
369
|
-
const result: Array<PartyTypeEntity> = await partyTypeRepository.find({
|
|
370
|
-
where: {
|
|
371
|
-
id: In(initialResult.map((partyType: PartyTypeEntity) => partyType.id)),
|
|
372
|
-
},
|
|
373
|
-
})
|
|
374
|
-
|
|
375
|
-
return result.map((partyType: PartyTypeEntity) => partyTypeFrom(partyType))
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
updatePartyType = async ({ partyType }: UpdatePartyTypeArgs): Promise<PartyType> => {
|
|
379
|
-
const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
|
|
380
|
-
const result: PartyTypeEntity | null = await partyTypeRepository.findOne({
|
|
381
|
-
where: { id: partyType.id },
|
|
382
|
-
})
|
|
383
|
-
|
|
384
|
-
if (!result) {
|
|
385
|
-
return Promise.reject(Error(`No party type found for id: ${partyType.id}`))
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
debug('Updating party type', partyType)
|
|
389
|
-
const updatedResult: PartyTypeEntity = await partyTypeRepository.save(partyType, { transaction: true })
|
|
390
|
-
|
|
391
|
-
return partyTypeFrom(updatedResult)
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
removePartyType = async ({ partyTypeId }: RemovePartyTypeArgs): Promise<void> => {
|
|
395
|
-
const parties: Array<PartyEntity> = await (await this.dbConnection).getRepository(PartyEntity).find({
|
|
396
|
-
where: {
|
|
397
|
-
partyType: {
|
|
398
|
-
id: partyTypeId,
|
|
399
|
-
},
|
|
400
|
-
},
|
|
401
|
-
})
|
|
402
|
-
|
|
403
|
-
if (parties.length > 0) {
|
|
404
|
-
return Promise.reject(Error(`Unable to remove party type with id: ${partyTypeId}. Party type is in use`))
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
|
|
408
|
-
const partyType: PartyTypeEntity | null = await partyTypeRepository.findOne({
|
|
409
|
-
where: { id: partyTypeId },
|
|
410
|
-
})
|
|
411
|
-
|
|
412
|
-
if (!partyType) {
|
|
413
|
-
return Promise.reject(Error(`No party type found for id: ${partyTypeId}`))
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
debug('Removing party type', partyTypeId)
|
|
417
|
-
|
|
418
|
-
await partyTypeRepository.delete(partyTypeId)
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
private hasCorrectConnectionConfig(type: ConnectionTypeEnum, config: NonPersistedConnectionConfig): boolean {
|
|
422
|
-
switch (type) {
|
|
423
|
-
case ConnectionTypeEnum.OPENID_CONNECT:
|
|
424
|
-
return isOpenIdConfig(config)
|
|
425
|
-
case ConnectionTypeEnum.SIOPv2:
|
|
426
|
-
return isDidAuthConfig(config)
|
|
427
|
-
default:
|
|
428
|
-
throw new Error('Connection type not supported')
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
private hasCorrectPartyType(type: PartyTypeEnum, contact: NonPersistedContact): boolean {
|
|
433
|
-
switch (type) {
|
|
434
|
-
case PartyTypeEnum.NATURAL_PERSON:
|
|
435
|
-
return isNaturalPerson(contact)
|
|
436
|
-
case PartyTypeEnum.ORGANIZATION:
|
|
437
|
-
return isOrganization(contact)
|
|
438
|
-
default:
|
|
439
|
-
throw new Error('Party type not supported')
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
private async deleteIdentities(identities: Array<IdentityEntity>): Promise<void> {
|
|
444
|
-
debug('Removing identities', identities)
|
|
445
|
-
|
|
446
|
-
identities.map(async (identity: IdentityEntity): Promise<void> => {
|
|
447
|
-
await (
|
|
448
|
-
await this.dbConnection
|
|
449
|
-
)
|
|
450
|
-
.getRepository(CorrelationIdentifierEntity)
|
|
451
|
-
.delete(identity.identifier.id)
|
|
452
|
-
.catch((error) => Promise.reject(Error(`Unable to remove identity.identifier with id: ${identity.identifier.id}. ${error}`)))
|
|
453
|
-
|
|
454
|
-
if (identity.connection) {
|
|
455
|
-
await (await this.dbConnection).getRepository(BaseConfigEntity).delete(identity.connection.config.id)
|
|
456
|
-
|
|
457
|
-
await (
|
|
458
|
-
await this.dbConnection
|
|
459
|
-
)
|
|
460
|
-
.getRepository(ConnectionEntity)
|
|
461
|
-
.delete(identity.connection.id)
|
|
462
|
-
.catch((error) => Promise.reject(Error(`Unable to remove identity.connection with id. ${error}`)))
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
if (identity.metadata) {
|
|
466
|
-
identity.metadata.map(async (metadataItem: IdentityMetadataItemEntity): Promise<void> => {
|
|
467
|
-
await (
|
|
468
|
-
await this.dbConnection
|
|
469
|
-
)
|
|
470
|
-
.getRepository(IdentityMetadataItemEntity)
|
|
471
|
-
.delete(metadataItem.id)
|
|
472
|
-
.catch((error) => Promise.reject(Error(`Unable to remove metadataItem.id with id ${metadataItem.id}. ${error}`)))
|
|
473
|
-
})
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
;(await this.dbConnection)
|
|
477
|
-
.getRepository(IdentityEntity)
|
|
478
|
-
.delete(identity.id)
|
|
479
|
-
.catch((error) => Promise.reject(Error(`Unable to remove metadataItem.id with id ${identity.id}. ${error}`)))
|
|
480
|
-
})
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
private async assertRelationshipSides(leftId: string, rightId: string): Promise<void> {
|
|
484
|
-
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
485
|
-
const leftParty: PartyEntity | null = await partyRepository.findOne({
|
|
486
|
-
where: { id: leftId },
|
|
487
|
-
})
|
|
488
|
-
|
|
489
|
-
if (!leftParty) {
|
|
490
|
-
return Promise.reject(Error(`No party found for left side of the relationship, party id: ${leftId}`))
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
const rightParty: PartyEntity | null = await partyRepository.findOne({
|
|
494
|
-
where: { id: rightId },
|
|
495
|
-
})
|
|
496
|
-
|
|
497
|
-
if (!rightParty) {
|
|
498
|
-
return Promise.reject(Error(`No party found for right side of the relationship, party id: ${rightId}`))
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
}
|
|
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 {
|
|
15
|
+
identityEntityFrom,
|
|
16
|
+
identityFrom,
|
|
17
|
+
isDidAuthConfig,
|
|
18
|
+
isNaturalPerson,
|
|
19
|
+
isOpenIdConfig,
|
|
20
|
+
isOrganization,
|
|
21
|
+
partyEntityFrom,
|
|
22
|
+
partyFrom,
|
|
23
|
+
partyRelationshipEntityFrom,
|
|
24
|
+
partyRelationshipFrom,
|
|
25
|
+
partyTypeEntityFrom,
|
|
26
|
+
partyTypeFrom,
|
|
27
|
+
} from '../utils/contact/MappingUtils'
|
|
28
|
+
import {
|
|
29
|
+
AddIdentityArgs,
|
|
30
|
+
GetIdentityArgs,
|
|
31
|
+
GetIdentitiesArgs,
|
|
32
|
+
GetPartyArgs,
|
|
33
|
+
RemoveIdentityArgs,
|
|
34
|
+
UpdateIdentityArgs,
|
|
35
|
+
GetPartiesArgs,
|
|
36
|
+
AddPartyArgs,
|
|
37
|
+
UpdatePartyArgs,
|
|
38
|
+
RemovePartyArgs,
|
|
39
|
+
NonPersistedConnectionConfig,
|
|
40
|
+
ConnectionTypeEnum,
|
|
41
|
+
CorrelationIdentifierEnum,
|
|
42
|
+
Party,
|
|
43
|
+
Identity,
|
|
44
|
+
RemoveRelationshipArgs,
|
|
45
|
+
PartyRelationship,
|
|
46
|
+
AddRelationshipArgs,
|
|
47
|
+
GetRelationshipArgs,
|
|
48
|
+
AddPartyTypeArgs,
|
|
49
|
+
PartyType,
|
|
50
|
+
GetPartyTypeArgs,
|
|
51
|
+
GetPartyTypesArgs,
|
|
52
|
+
UpdatePartyTypeArgs,
|
|
53
|
+
RemovePartyTypeArgs,
|
|
54
|
+
GetRelationshipsArgs,
|
|
55
|
+
UpdateRelationshipArgs,
|
|
56
|
+
PartyTypeEnum,
|
|
57
|
+
NonPersistedContact,
|
|
58
|
+
} from '../types'
|
|
59
|
+
|
|
60
|
+
const debug: Debug.Debugger = Debug('sphereon:ssi-sdk:contact-store')
|
|
61
|
+
|
|
62
|
+
export class ContactStore extends AbstractContactStore {
|
|
63
|
+
private readonly dbConnection: OrPromise<DataSource>
|
|
64
|
+
|
|
65
|
+
constructor(dbConnection: OrPromise<DataSource>) {
|
|
66
|
+
super()
|
|
67
|
+
this.dbConnection = dbConnection
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getParty = async ({ partyId }: GetPartyArgs): Promise<Party> => {
|
|
71
|
+
const result: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
|
|
72
|
+
where: { id: partyId },
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
if (!result) {
|
|
76
|
+
return Promise.reject(Error(`No party found for id: ${partyId}`))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return partyFrom(result)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getParties = async (args?: GetPartiesArgs): Promise<Array<Party>> => {
|
|
83
|
+
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
84
|
+
const initialResult: Array<PartyEntity> = await partyRepository.find({
|
|
85
|
+
...(args?.filter && { where: args?.filter }),
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const result: Array<PartyEntity> = await partyRepository.find({
|
|
89
|
+
where: {
|
|
90
|
+
id: In(initialResult.map((party: PartyEntity) => party.id)),
|
|
91
|
+
},
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
return result.map((party: PartyEntity) => partyFrom(party))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
addParty = async (args: AddPartyArgs): Promise<Party> => {
|
|
98
|
+
const { identities, contact, partyType } = args
|
|
99
|
+
|
|
100
|
+
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
101
|
+
|
|
102
|
+
if (!this.hasCorrectPartyType(partyType.type, contact)) {
|
|
103
|
+
return Promise.reject(Error(`Party type ${partyType.type}, does not match for provided contact`))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
for (const identity of identities ?? []) {
|
|
107
|
+
if (identity.identifier.type === CorrelationIdentifierEnum.URL) {
|
|
108
|
+
if (!identity.connection) {
|
|
109
|
+
return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierEnum.URL} should contain a connection`))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
|
|
113
|
+
return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const partyEntity: PartyEntity = partyEntityFrom(args)
|
|
119
|
+
debug('Adding party', args)
|
|
120
|
+
const createdResult: PartyEntity = await partyRepository.save(partyEntity)
|
|
121
|
+
|
|
122
|
+
return partyFrom(createdResult)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
updateParty = async ({ party }: UpdatePartyArgs): Promise<Party> => {
|
|
126
|
+
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
127
|
+
const result: PartyEntity | null = await partyRepository.findOne({
|
|
128
|
+
where: { id: party.id },
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
if (!result) {
|
|
132
|
+
return Promise.reject(Error(`No party found for id: ${party.id}`))
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const updatedParty = {
|
|
136
|
+
...party,
|
|
137
|
+
identities: result.identities,
|
|
138
|
+
type: result.partyType,
|
|
139
|
+
relationships: result.relationships,
|
|
140
|
+
electronicAddresses: result.electronicAddresses,
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
debug('Updating party', party)
|
|
144
|
+
const updatedResult: PartyEntity = await partyRepository.save(updatedParty, { transaction: true })
|
|
145
|
+
|
|
146
|
+
return partyFrom(updatedResult)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
removeParty = async ({ partyId }: RemovePartyArgs): Promise<void> => {
|
|
150
|
+
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
151
|
+
debug('Removing party', partyId)
|
|
152
|
+
partyRepository
|
|
153
|
+
.findOneById(partyId)
|
|
154
|
+
.then(async (party: PartyEntity | null): Promise<void> => {
|
|
155
|
+
if (!party) {
|
|
156
|
+
await Promise.reject(Error(`Unable to find the party with id to remove: ${partyId}`))
|
|
157
|
+
} else {
|
|
158
|
+
await this.deleteIdentities(party.identities)
|
|
159
|
+
|
|
160
|
+
await partyRepository
|
|
161
|
+
.delete({ id: partyId })
|
|
162
|
+
.catch((error) => Promise.reject(Error(`Unable to remove party with id: ${partyId}. ${error}`)))
|
|
163
|
+
|
|
164
|
+
const partyContactRepository: Repository<BaseContactEntity> = (await this.dbConnection).getRepository(BaseContactEntity)
|
|
165
|
+
await partyContactRepository
|
|
166
|
+
.delete({ id: party.contact.id })
|
|
167
|
+
.catch((error) => Promise.reject(Error(`Unable to remove party contact with id: ${party.contact.id}. ${error}`)))
|
|
168
|
+
}
|
|
169
|
+
})
|
|
170
|
+
.catch((error) => Promise.reject(Error(`Unable to remove party with id: ${partyId}. ${error}`)))
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
getIdentity = async ({ identityId }: GetIdentityArgs): Promise<Identity> => {
|
|
174
|
+
const result: IdentityEntity | null = await (await this.dbConnection).getRepository(IdentityEntity).findOne({
|
|
175
|
+
where: { id: identityId },
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
if (!result) {
|
|
179
|
+
return Promise.reject(Error(`No identity found for id: ${identityId}`))
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return identityFrom(result)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
getIdentities = async (args?: GetIdentitiesArgs): Promise<Array<Identity>> => {
|
|
186
|
+
const identityRepository: Repository<IdentityEntity> = (await this.dbConnection).getRepository(IdentityEntity)
|
|
187
|
+
const initialResult: Array<IdentityEntity> = await identityRepository.find({
|
|
188
|
+
...(args?.filter && { where: args?.filter }),
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
const result: Array<IdentityEntity> = await identityRepository.find({
|
|
192
|
+
where: {
|
|
193
|
+
id: In(initialResult.map((identity: IdentityEntity) => identity.id)),
|
|
194
|
+
},
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
return result.map((identity: IdentityEntity) => identityFrom(identity))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
addIdentity = async ({ identity, partyId }: AddIdentityArgs): Promise<Identity> => {
|
|
201
|
+
const party: PartyEntity | null = await (await this.dbConnection).getRepository(PartyEntity).findOne({
|
|
202
|
+
where: { id: partyId },
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
if (!party) {
|
|
206
|
+
return Promise.reject(Error(`No party found for id: ${partyId}`))
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (identity.identifier.type === CorrelationIdentifierEnum.URL) {
|
|
210
|
+
if (!identity.connection) {
|
|
211
|
+
return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierEnum.URL} should contain a connection`))
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
|
|
215
|
+
return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const identityEntity: IdentityEntity = identityEntityFrom(identity)
|
|
220
|
+
identityEntity.party = party
|
|
221
|
+
debug('Adding identity', identity)
|
|
222
|
+
const result: IdentityEntity = await (await this.dbConnection).getRepository(IdentityEntity).save(identityEntity, {
|
|
223
|
+
transaction: true,
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
return identityFrom(result)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
updateIdentity = async ({ identity }: UpdateIdentityArgs): Promise<Identity> => {
|
|
230
|
+
const identityRepository: Repository<IdentityEntity> = (await this.dbConnection).getRepository(IdentityEntity)
|
|
231
|
+
const result: IdentityEntity | null = await identityRepository.findOne({
|
|
232
|
+
where: { id: identity.id },
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
if (!result) {
|
|
236
|
+
return Promise.reject(Error(`No identity found for id: ${identity.id}`))
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (identity.identifier.type === CorrelationIdentifierEnum.URL) {
|
|
240
|
+
if (!identity.connection) {
|
|
241
|
+
return Promise.reject(Error(`Identity with correlation type ${CorrelationIdentifierEnum.URL} should contain a connection`))
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (!this.hasCorrectConnectionConfig(identity.connection.type, identity.connection.config)) {
|
|
245
|
+
return Promise.reject(Error(`Connection type ${identity.connection.type}, does not match for provided config`))
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
debug('Updating identity', identity)
|
|
250
|
+
const updatedResult: IdentityEntity = await identityRepository.save(identity, { transaction: true })
|
|
251
|
+
|
|
252
|
+
return identityFrom(updatedResult)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
removeIdentity = async ({ identityId }: RemoveIdentityArgs): Promise<void> => {
|
|
256
|
+
const identity: IdentityEntity | null = await (await this.dbConnection).getRepository(IdentityEntity).findOne({
|
|
257
|
+
where: { id: identityId },
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
if (!identity) {
|
|
261
|
+
return Promise.reject(Error(`No identity found for id: ${identityId}`))
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
debug('Removing identity', identityId)
|
|
265
|
+
|
|
266
|
+
await this.deleteIdentities([identity])
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
addRelationship = async ({ leftId, rightId }: AddRelationshipArgs): Promise<PartyRelationship> => {
|
|
270
|
+
return this.assertRelationshipSides(leftId, rightId).then(async (): Promise<PartyRelationship> => {
|
|
271
|
+
const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
|
|
272
|
+
leftId,
|
|
273
|
+
rightId,
|
|
274
|
+
})
|
|
275
|
+
debug('Adding party relationship', relationship)
|
|
276
|
+
|
|
277
|
+
const createdResult: PartyRelationshipEntity = await (await this.dbConnection).getRepository(PartyRelationshipEntity).save(relationship)
|
|
278
|
+
|
|
279
|
+
return partyRelationshipFrom(createdResult)
|
|
280
|
+
})
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
getRelationship = async ({ relationshipId }: GetRelationshipArgs): Promise<PartyRelationship> => {
|
|
284
|
+
const result: PartyRelationshipEntity | null = await (await this.dbConnection).getRepository(PartyRelationshipEntity).findOne({
|
|
285
|
+
where: { id: relationshipId },
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
if (!result) {
|
|
289
|
+
return Promise.reject(Error(`No relationship found for id: ${relationshipId}`))
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return partyRelationshipFrom(result)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
getRelationships = async (args?: GetRelationshipsArgs): Promise<Array<PartyRelationship>> => {
|
|
296
|
+
const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
|
|
297
|
+
const initialResult: Array<PartyRelationshipEntity> = await partyRelationshipRepository.find({
|
|
298
|
+
...(args?.filter && { where: args?.filter }),
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
const result: Array<PartyRelationshipEntity> = await partyRelationshipRepository.find({
|
|
302
|
+
where: {
|
|
303
|
+
id: In(initialResult.map((partyRelationship: PartyRelationshipEntity) => partyRelationship.id)),
|
|
304
|
+
},
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
return result.map((partyRelationship: PartyRelationshipEntity) => partyRelationshipFrom(partyRelationship))
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
updateRelationship = async ({ relationship }: UpdateRelationshipArgs): Promise<PartyRelationship> => {
|
|
311
|
+
const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
|
|
312
|
+
const result: PartyRelationshipEntity | null = await partyRelationshipRepository.findOne({
|
|
313
|
+
where: { id: relationship.id },
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
if (!result) {
|
|
317
|
+
return Promise.reject(Error(`No party relationship found for id: ${relationship.id}`))
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return this.assertRelationshipSides(relationship.leftId, relationship.rightId).then(async (): Promise<PartyRelationship> => {
|
|
321
|
+
debug('Updating party relationship', relationship)
|
|
322
|
+
const updatedResult: PartyRelationshipEntity = await partyRelationshipRepository.save(relationship, { transaction: true })
|
|
323
|
+
|
|
324
|
+
return partyRelationshipFrom(updatedResult)
|
|
325
|
+
})
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
removeRelationship = async ({ relationshipId }: RemoveRelationshipArgs): Promise<void> => {
|
|
329
|
+
const partyRelationshipRepository: Repository<PartyRelationshipEntity> = (await this.dbConnection).getRepository(PartyRelationshipEntity)
|
|
330
|
+
const relationship: PartyRelationshipEntity | null = await partyRelationshipRepository.findOne({
|
|
331
|
+
where: { id: relationshipId },
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
if (!relationship) {
|
|
335
|
+
return Promise.reject(Error(`No relationship found for id: ${relationshipId}`))
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
debug('Removing relationship', relationshipId)
|
|
339
|
+
|
|
340
|
+
await partyRelationshipRepository.delete(relationshipId)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
addPartyType = async (args: AddPartyTypeArgs): Promise<PartyType> => {
|
|
344
|
+
const partyEntity: PartyTypeEntity = partyTypeEntityFrom(args)
|
|
345
|
+
debug('Adding party type', args)
|
|
346
|
+
const createdResult: PartyTypeEntity = await (await this.dbConnection).getRepository(PartyTypeEntity).save(partyEntity)
|
|
347
|
+
|
|
348
|
+
return partyTypeFrom(createdResult)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
getPartyType = async ({ partyTypeId }: GetPartyTypeArgs): Promise<PartyType> => {
|
|
352
|
+
const result: PartyTypeEntity | null = await (await this.dbConnection).getRepository(PartyTypeEntity).findOne({
|
|
353
|
+
where: { id: partyTypeId },
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
if (!result) {
|
|
357
|
+
return Promise.reject(Error(`No party type found for id: ${partyTypeId}`))
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return partyTypeFrom(result)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
getPartyTypes = async (args?: GetPartyTypesArgs): Promise<Array<PartyType>> => {
|
|
364
|
+
const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
|
|
365
|
+
const initialResult: Array<PartyTypeEntity> = await partyTypeRepository.find({
|
|
366
|
+
...(args?.filter && { where: args?.filter }),
|
|
367
|
+
})
|
|
368
|
+
|
|
369
|
+
const result: Array<PartyTypeEntity> = await partyTypeRepository.find({
|
|
370
|
+
where: {
|
|
371
|
+
id: In(initialResult.map((partyType: PartyTypeEntity) => partyType.id)),
|
|
372
|
+
},
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
return result.map((partyType: PartyTypeEntity) => partyTypeFrom(partyType))
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
updatePartyType = async ({ partyType }: UpdatePartyTypeArgs): Promise<PartyType> => {
|
|
379
|
+
const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
|
|
380
|
+
const result: PartyTypeEntity | null = await partyTypeRepository.findOne({
|
|
381
|
+
where: { id: partyType.id },
|
|
382
|
+
})
|
|
383
|
+
|
|
384
|
+
if (!result) {
|
|
385
|
+
return Promise.reject(Error(`No party type found for id: ${partyType.id}`))
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
debug('Updating party type', partyType)
|
|
389
|
+
const updatedResult: PartyTypeEntity = await partyTypeRepository.save(partyType, { transaction: true })
|
|
390
|
+
|
|
391
|
+
return partyTypeFrom(updatedResult)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
removePartyType = async ({ partyTypeId }: RemovePartyTypeArgs): Promise<void> => {
|
|
395
|
+
const parties: Array<PartyEntity> = await (await this.dbConnection).getRepository(PartyEntity).find({
|
|
396
|
+
where: {
|
|
397
|
+
partyType: {
|
|
398
|
+
id: partyTypeId,
|
|
399
|
+
},
|
|
400
|
+
},
|
|
401
|
+
})
|
|
402
|
+
|
|
403
|
+
if (parties.length > 0) {
|
|
404
|
+
return Promise.reject(Error(`Unable to remove party type with id: ${partyTypeId}. Party type is in use`))
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const partyTypeRepository: Repository<PartyTypeEntity> = (await this.dbConnection).getRepository(PartyTypeEntity)
|
|
408
|
+
const partyType: PartyTypeEntity | null = await partyTypeRepository.findOne({
|
|
409
|
+
where: { id: partyTypeId },
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
if (!partyType) {
|
|
413
|
+
return Promise.reject(Error(`No party type found for id: ${partyTypeId}`))
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
debug('Removing party type', partyTypeId)
|
|
417
|
+
|
|
418
|
+
await partyTypeRepository.delete(partyTypeId)
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
private hasCorrectConnectionConfig(type: ConnectionTypeEnum, config: NonPersistedConnectionConfig): boolean {
|
|
422
|
+
switch (type) {
|
|
423
|
+
case ConnectionTypeEnum.OPENID_CONNECT:
|
|
424
|
+
return isOpenIdConfig(config)
|
|
425
|
+
case ConnectionTypeEnum.SIOPv2:
|
|
426
|
+
return isDidAuthConfig(config)
|
|
427
|
+
default:
|
|
428
|
+
throw new Error('Connection type not supported')
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private hasCorrectPartyType(type: PartyTypeEnum, contact: NonPersistedContact): boolean {
|
|
433
|
+
switch (type) {
|
|
434
|
+
case PartyTypeEnum.NATURAL_PERSON:
|
|
435
|
+
return isNaturalPerson(contact)
|
|
436
|
+
case PartyTypeEnum.ORGANIZATION:
|
|
437
|
+
return isOrganization(contact)
|
|
438
|
+
default:
|
|
439
|
+
throw new Error('Party type not supported')
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private async deleteIdentities(identities: Array<IdentityEntity>): Promise<void> {
|
|
444
|
+
debug('Removing identities', identities)
|
|
445
|
+
|
|
446
|
+
identities.map(async (identity: IdentityEntity): Promise<void> => {
|
|
447
|
+
await (
|
|
448
|
+
await this.dbConnection
|
|
449
|
+
)
|
|
450
|
+
.getRepository(CorrelationIdentifierEntity)
|
|
451
|
+
.delete(identity.identifier.id)
|
|
452
|
+
.catch((error) => Promise.reject(Error(`Unable to remove identity.identifier with id: ${identity.identifier.id}. ${error}`)))
|
|
453
|
+
|
|
454
|
+
if (identity.connection) {
|
|
455
|
+
await (await this.dbConnection).getRepository(BaseConfigEntity).delete(identity.connection.config.id)
|
|
456
|
+
|
|
457
|
+
await (
|
|
458
|
+
await this.dbConnection
|
|
459
|
+
)
|
|
460
|
+
.getRepository(ConnectionEntity)
|
|
461
|
+
.delete(identity.connection.id)
|
|
462
|
+
.catch((error) => Promise.reject(Error(`Unable to remove identity.connection with id. ${error}`)))
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (identity.metadata) {
|
|
466
|
+
identity.metadata.map(async (metadataItem: IdentityMetadataItemEntity): Promise<void> => {
|
|
467
|
+
await (
|
|
468
|
+
await this.dbConnection
|
|
469
|
+
)
|
|
470
|
+
.getRepository(IdentityMetadataItemEntity)
|
|
471
|
+
.delete(metadataItem.id)
|
|
472
|
+
.catch((error) => Promise.reject(Error(`Unable to remove metadataItem.id with id ${metadataItem.id}. ${error}`)))
|
|
473
|
+
})
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
;(await this.dbConnection)
|
|
477
|
+
.getRepository(IdentityEntity)
|
|
478
|
+
.delete(identity.id)
|
|
479
|
+
.catch((error) => Promise.reject(Error(`Unable to remove metadataItem.id with id ${identity.id}. ${error}`)))
|
|
480
|
+
})
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
private async assertRelationshipSides(leftId: string, rightId: string): Promise<void> {
|
|
484
|
+
const partyRepository: Repository<PartyEntity> = (await this.dbConnection).getRepository(PartyEntity)
|
|
485
|
+
const leftParty: PartyEntity | null = await partyRepository.findOne({
|
|
486
|
+
where: { id: leftId },
|
|
487
|
+
})
|
|
488
|
+
|
|
489
|
+
if (!leftParty) {
|
|
490
|
+
return Promise.reject(Error(`No party found for left side of the relationship, party id: ${leftId}`))
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const rightParty: PartyEntity | null = await partyRepository.findOne({
|
|
494
|
+
where: { id: rightId },
|
|
495
|
+
})
|
|
496
|
+
|
|
497
|
+
if (!rightParty) {
|
|
498
|
+
return Promise.reject(Error(`No party found for right side of the relationship, party id: ${rightId}`))
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|