@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,2542 +1,2542 @@
1
- import { DataSource, FindOptionsWhere } from 'typeorm'
2
- import { DataStoreContactEntities, DataStoreMigrations, PartyOrigin } from '../index'
3
- import { BaseContactEntity } from '../entities/contact/BaseContactEntity'
4
- import { ConnectionEntity } from '../entities/contact/ConnectionEntity'
5
- import { CorrelationIdentifierEntity } from '../entities/contact/CorrelationIdentifierEntity'
6
- import { DidAuthConfigEntity } from '../entities/contact/DidAuthConfigEntity'
7
- import { ElectronicAddressEntity } from '../entities/contact/ElectronicAddressEntity'
8
- import { IdentityEntity } from '../entities/contact/IdentityEntity'
9
- import { IdentityMetadataItemEntity } from '../entities/contact/IdentityMetadataItemEntity'
10
- import { NaturalPersonEntity } from '../entities/contact/NaturalPersonEntity'
11
- import { OpenIdConfigEntity } from '../entities/contact/OpenIdConfigEntity'
12
- import { OrganizationEntity } from '../entities/contact/OrganizationEntity'
13
- import { PartyEntity } from '../entities/contact/PartyEntity'
14
- import { PartyRelationshipEntity } from '../entities/contact/PartyRelationshipEntity'
15
- import { PartyTypeEntity } from '../entities/contact/PartyTypeEntity'
16
- import { PhysicalAddressEntity } from '../entities/contact/PhysicalAddressEntity'
17
- import {
18
- ConnectionType,
19
- CorrelationIdentifierType,
20
- IdentityRole,
21
- NaturalPerson,
22
- NonPersistedConnection,
23
- NonPersistedDidAuthConfig,
24
- NonPersistedElectronicAddress,
25
- NonPersistedIdentity,
26
- NonPersistedNaturalPerson,
27
- NonPersistedOpenIdConfig,
28
- NonPersistedOrganization,
29
- NonPersistedParty,
30
- NonPersistedPartyType,
31
- NonPersistedPhysicalAddress,
32
- Organization,
33
- PartyTypeType,
34
- } from '../types'
35
- import {
36
- connectionEntityFrom,
37
- didAuthConfigEntityFrom,
38
- electronicAddressEntityFrom,
39
- identityEntityFrom,
40
- naturalPersonEntityFrom,
41
- openIdConfigEntityFrom,
42
- organizationEntityFrom,
43
- partyEntityFrom,
44
- partyRelationshipEntityFrom,
45
- partyTypeEntityFrom,
46
- physicalAddressEntityFrom,
47
- } from '../utils/contact/MappingUtils'
48
-
49
- // TODO write test adding two contacts reusing the same contactType
50
-
51
- describe('Database entities tests', (): void => {
52
- let dbConnection: DataSource
53
-
54
- beforeEach(async (): Promise<void> => {
55
- dbConnection = await new DataSource({
56
- type: 'sqlite',
57
- database: ':memory:',
58
- logging: ['info'],
59
- migrationsRun: false,
60
- migrations: DataStoreMigrations,
61
- synchronize: false,
62
- entities: DataStoreContactEntities,
63
- }).initialize()
64
- await dbConnection.runMigrations()
65
- expect(await dbConnection.showMigrations()).toBeFalsy()
66
- })
67
-
68
- afterEach(async (): Promise<void> => {
69
- await (await dbConnection).destroy()
70
- })
71
-
72
- it('Should save person party to database', async (): Promise<void> => {
73
- const party: NonPersistedParty = {
74
- uri: 'example.com',
75
- partyType: {
76
- type: PartyTypeType.NATURAL_PERSON,
77
- origin: PartyOrigin.INTERNAL,
78
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
79
- name: 'example_name',
80
- },
81
- contact: {
82
- firstName: 'example_first_name',
83
- middleName: 'example_middle_name',
84
- lastName: 'example_last_name',
85
- displayName: 'example_display_name',
86
- },
87
- }
88
-
89
- const partyEntity: PartyEntity = partyEntityFrom(party)
90
- const savedParty: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
91
- transaction: true,
92
- })
93
-
94
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
95
- where: { id: savedParty.id },
96
- })
97
-
98
- expect(fromDb).toBeDefined()
99
- expect(fromDb?.identities?.length).toEqual(0)
100
- expect(fromDb?.uri).toEqual(party.uri)
101
- expect(fromDb?.partyType).toBeDefined()
102
- expect(fromDb?.partyType.type).toEqual(party.partyType.type)
103
- expect(fromDb?.partyType.origin).toEqual(party.partyType.origin)
104
- expect(fromDb?.partyType.tenantId).toEqual(party.partyType.tenantId)
105
- expect(fromDb?.partyType.name).toEqual(party.partyType.name)
106
- expect(fromDb?.contact).toBeDefined()
107
- expect((<NaturalPersonEntity>fromDb?.contact).firstName).toEqual((<NaturalPerson>party.contact).firstName)
108
- expect((<NaturalPersonEntity>fromDb?.contact).middleName).toEqual((<NaturalPerson>party.contact).middleName)
109
- expect((<NaturalPersonEntity>fromDb?.contact).lastName).toEqual((<NaturalPerson>party.contact).lastName)
110
- expect((<NaturalPersonEntity>fromDb?.contact).displayName).toEqual((<NaturalPerson>party.contact).displayName)
111
- })
112
-
113
- it('Should save organization party to database', async (): Promise<void> => {
114
- const party: NonPersistedParty = {
115
- uri: 'example.com',
116
- partyType: {
117
- type: PartyTypeType.ORGANIZATION,
118
- origin: PartyOrigin.INTERNAL,
119
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
120
- name: 'example_name',
121
- },
122
- contact: {
123
- legalName: 'example_legal_name',
124
- displayName: 'example_display_name',
125
- },
126
- }
127
-
128
- const partyEntity: PartyEntity = partyEntityFrom(party)
129
- const savedParty: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
130
- transaction: true,
131
- })
132
-
133
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
134
- where: { id: savedParty.id },
135
- })
136
-
137
- expect(fromDb).toBeDefined()
138
- expect(fromDb?.identities?.length).toEqual(0)
139
- expect(fromDb?.uri).toEqual(party.uri)
140
- expect(fromDb?.partyType).toBeDefined()
141
- expect(fromDb?.partyType.type).toEqual(party.partyType.type)
142
- expect(fromDb?.partyType.origin).toEqual(party.partyType.origin)
143
- expect(fromDb?.partyType.tenantId).toEqual(party.partyType.tenantId)
144
- expect(fromDb?.partyType.name).toEqual(party.partyType.name)
145
- expect(fromDb?.contact).toBeDefined()
146
- expect((<OrganizationEntity>fromDb?.contact).legalName).toEqual((<Organization>party.contact).legalName)
147
- expect((<OrganizationEntity>fromDb?.contact).displayName).toEqual((<Organization>party.contact).displayName)
148
- })
149
-
150
- it('Should result in party relationship for the owner side only', async (): Promise<void> => {
151
- const party1: NonPersistedParty = {
152
- uri: 'example1.com',
153
- partyType: {
154
- type: PartyTypeType.NATURAL_PERSON,
155
- origin: PartyOrigin.INTERNAL,
156
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
157
- name: 'example_name1',
158
- },
159
- contact: {
160
- firstName: 'example_first_name1',
161
- middleName: 'example_middle_name1',
162
- lastName: 'example_last_name1',
163
- displayName: 'example_display_name1',
164
- },
165
- }
166
-
167
- const partyEntity1: PartyEntity = partyEntityFrom(party1)
168
- const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
169
- transaction: true,
170
- })
171
-
172
- const party2: NonPersistedParty = {
173
- uri: 'example2.com',
174
- partyType: {
175
- type: PartyTypeType.NATURAL_PERSON,
176
- origin: PartyOrigin.INTERNAL,
177
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
178
- name: 'example_name2',
179
- },
180
- contact: {
181
- firstName: 'example_first_name2',
182
- middleName: 'example_middle_name2',
183
- lastName: 'example_last_name2',
184
- displayName: 'example_display_name2',
185
- },
186
- }
187
-
188
- const partyEntity2: PartyEntity = partyEntityFrom(party2)
189
- const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
190
- transaction: true,
191
- })
192
-
193
- const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
194
- leftId: savedParty1.id,
195
- rightId: savedParty2.id,
196
- })
197
-
198
- await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
199
- transaction: true,
200
- })
201
-
202
- const fromDb1: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
203
- where: { id: savedParty1.id },
204
- })
205
-
206
- const fromDb2: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
207
- where: { id: savedParty2.id },
208
- })
209
-
210
- expect(fromDb1).toBeDefined()
211
- expect(fromDb1?.relationships.length).toEqual(1)
212
- expect(fromDb2).toBeDefined()
213
- expect(fromDb2?.relationships.length).toEqual(0)
214
- })
215
-
216
- it('should throw error when saving person party with blank first name', async (): Promise<void> => {
217
- const party: NonPersistedParty = {
218
- uri: 'example.com',
219
- partyType: {
220
- type: PartyTypeType.NATURAL_PERSON,
221
- origin: PartyOrigin.INTERNAL,
222
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
223
- name: 'example_name',
224
- },
225
- contact: {
226
- firstName: '',
227
- middleName: 'example_middle_name1',
228
- lastName: 'example_last_name1',
229
- displayName: 'example_display_name1',
230
- },
231
- }
232
-
233
- const partyEntity: PartyEntity = partyEntityFrom(party)
234
-
235
- await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank first names are not allowed')
236
- })
237
-
238
- it('should throw error when saving person party with blank middle name', async (): Promise<void> => {
239
- const party: NonPersistedParty = {
240
- uri: 'example.com',
241
- partyType: {
242
- type: PartyTypeType.NATURAL_PERSON,
243
- origin: PartyOrigin.EXTERNAL,
244
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
245
- name: 'example_name',
246
- },
247
- contact: {
248
- firstName: 'example_first_name',
249
- middleName: '',
250
- lastName: 'example_last_name',
251
- displayName: 'example_display_name',
252
- },
253
- }
254
-
255
- const partyEntity: PartyEntity = partyEntityFrom(party)
256
-
257
- await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank middle names are not allowed')
258
- })
259
-
260
- it('should throw error when saving person party with blank last name', async (): Promise<void> => {
261
- const party: NonPersistedParty = {
262
- uri: 'example.com',
263
- partyType: {
264
- type: PartyTypeType.NATURAL_PERSON,
265
- origin: PartyOrigin.EXTERNAL,
266
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
267
- name: 'example_name',
268
- },
269
- contact: {
270
- firstName: 'example_first_name',
271
- middleName: 'example_middle_name',
272
- lastName: '',
273
- displayName: 'example_display_name',
274
- },
275
- }
276
-
277
- const partyEntity: PartyEntity = partyEntityFrom(party)
278
-
279
- await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank last names are not allowed')
280
- })
281
-
282
- it('should throw error when saving person party with blank display name', async (): Promise<void> => {
283
- const party: NonPersistedParty = {
284
- uri: 'example.com',
285
- partyType: {
286
- type: PartyTypeType.NATURAL_PERSON,
287
- origin: PartyOrigin.EXTERNAL,
288
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
289
- name: 'example_name',
290
- },
291
- contact: {
292
- firstName: 'example_first_name',
293
- middleName: 'example_middle_name',
294
- lastName: 'example_last_name',
295
- displayName: '',
296
- },
297
- }
298
-
299
- const partyEntity: PartyEntity = partyEntityFrom(party)
300
-
301
- await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank display names are not allowed')
302
- })
303
-
304
- it('should throw error when saving organization party with blank legal name', async (): Promise<void> => {
305
- const party: NonPersistedParty = {
306
- uri: 'example.com',
307
- partyType: {
308
- type: PartyTypeType.ORGANIZATION,
309
- origin: PartyOrigin.INTERNAL,
310
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
311
- name: 'example_name',
312
- },
313
- contact: {
314
- legalName: '',
315
- displayName: 'example_legal_name',
316
- },
317
- }
318
-
319
- const partyEntity: PartyEntity = partyEntityFrom(party)
320
-
321
- await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank legal names are not allowed')
322
- })
323
-
324
- it('should throw error when saving organization party with blank display name', async (): Promise<void> => {
325
- const party: NonPersistedParty = {
326
- uri: 'example.com',
327
- partyType: {
328
- type: PartyTypeType.ORGANIZATION,
329
- origin: PartyOrigin.INTERNAL,
330
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
331
- name: 'example_name',
332
- },
333
- contact: {
334
- legalName: 'example_first_name',
335
- displayName: '',
336
- },
337
- }
338
-
339
- const partyEntity: PartyEntity = partyEntityFrom(party)
340
-
341
- await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank display names are not allowed')
342
- })
343
-
344
- it('should throw error when saving party with blank party type name', async (): Promise<void> => {
345
- const party: NonPersistedParty = {
346
- uri: 'example.com',
347
- partyType: {
348
- type: PartyTypeType.NATURAL_PERSON,
349
- origin: PartyOrigin.EXTERNAL,
350
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
351
- name: '',
352
- },
353
- contact: {
354
- firstName: 'example_first_name',
355
- middleName: 'example_middle_name',
356
- lastName: 'example_last_name',
357
- displayName: 'example_display_name',
358
- },
359
- }
360
-
361
- const partyEntity: PartyEntity = partyEntityFrom(party)
362
-
363
- await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank names are not allowed')
364
- })
365
-
366
- it('should throw error when saving party with blank party type description', async (): Promise<void> => {
367
- const party: NonPersistedParty = {
368
- uri: 'example.com',
369
- partyType: {
370
- type: PartyTypeType.NATURAL_PERSON,
371
- origin: PartyOrigin.INTERNAL,
372
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
373
- name: 'example_name',
374
- description: '',
375
- },
376
- contact: {
377
- firstName: 'example_first_name',
378
- middleName: 'example_middle_name',
379
- lastName: 'example_last_name',
380
- displayName: 'example_display_name',
381
- },
382
- }
383
-
384
- const partyEntity: PartyEntity = partyEntityFrom(party)
385
-
386
- await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank descriptions are not allowed')
387
- })
388
-
389
- it('should throw error when saving party with blank party type tenant id', async (): Promise<void> => {
390
- const party: NonPersistedParty = {
391
- uri: 'example.com',
392
- partyType: {
393
- type: PartyTypeType.NATURAL_PERSON,
394
- origin: PartyOrigin.EXTERNAL,
395
- tenantId: '',
396
- name: 'example_name',
397
- },
398
- contact: {
399
- firstName: 'example_first_name',
400
- middleName: 'example_middle_name',
401
- lastName: 'example_last_name',
402
- displayName: 'example_display_name',
403
- },
404
- }
405
-
406
- const partyEntity: PartyEntity = partyEntityFrom(party)
407
-
408
- await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError("Blank tenant id's are not allowed")
409
- })
410
-
411
- it('Should enforce unique alias for an identity', async (): Promise<void> => {
412
- const alias = 'non_unique_alias'
413
- const identity1: NonPersistedIdentity = {
414
- alias,
415
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
416
- identifier: {
417
- type: CorrelationIdentifierType.DID,
418
- correlationId: 'unique_correlationId1',
419
- },
420
- }
421
- const identity1Entity: IdentityEntity = identityEntityFrom(identity1)
422
- await dbConnection.getRepository(IdentityEntity).save(identity1Entity)
423
-
424
- const identity2: NonPersistedIdentity = {
425
- alias: alias,
426
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
427
- identifier: {
428
- type: CorrelationIdentifierType.DID,
429
- correlationId: 'unique_correlationId2',
430
- },
431
- }
432
- const identity2Entity: IdentityEntity = identityEntityFrom(identity2)
433
- await expect(dbConnection.getRepository(IdentityEntity).save(identity2Entity)).rejects.toThrowError(
434
- 'SQLITE_CONSTRAINT: UNIQUE constraint failed: Identity.alias',
435
- )
436
- })
437
-
438
- it('Should enforce unique correlationId for a identity', async (): Promise<void> => {
439
- const correlationId = 'non_unique_correlationId'
440
- const identity1: NonPersistedIdentity = {
441
- alias: 'unique_alias1',
442
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
443
- identifier: {
444
- type: CorrelationIdentifierType.DID,
445
- correlationId,
446
- },
447
- }
448
- const identity1Entity: IdentityEntity = identityEntityFrom(identity1)
449
- await dbConnection.getRepository(IdentityEntity).save(identity1Entity)
450
-
451
- const identity2: NonPersistedIdentity = {
452
- alias: 'unique_alias2',
453
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
454
- identifier: {
455
- type: CorrelationIdentifierType.DID,
456
- correlationId,
457
- },
458
- }
459
- const identity2Entity: IdentityEntity = identityEntityFrom(identity2)
460
- await expect(dbConnection.getRepository(IdentityEntity).save(identity2Entity)).rejects.toThrowError(
461
- 'SQLITE_CONSTRAINT: UNIQUE constraint failed: CorrelationIdentifier.correlation_id',
462
- )
463
- })
464
-
465
- it('Should save identity to database', async (): Promise<void> => {
466
- const correlationId = 'example_did'
467
- const identity: NonPersistedIdentity = {
468
- alias: correlationId,
469
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
470
- identifier: {
471
- type: CorrelationIdentifierType.DID,
472
- correlationId,
473
- },
474
- }
475
-
476
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
477
-
478
- await dbConnection.getRepository(IdentityEntity).save(identityEntity)
479
-
480
- const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
481
- where: {
482
- identifier: {
483
- correlationId,
484
- },
485
- },
486
- })
487
-
488
- expect(fromDb).toBeDefined()
489
- expect(fromDb?.connection).toBeNull()
490
- expect(fromDb?.identifier).toBeDefined()
491
- expect(fromDb?.identifier.correlationId).toEqual(identity.identifier.correlationId)
492
- expect(fromDb?.identifier.type).toEqual(identity.identifier.type)
493
- })
494
-
495
- it('should throw error when saving identity with blank alias', async (): Promise<void> => {
496
- const identity: NonPersistedIdentity = {
497
- alias: '',
498
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
499
- identifier: {
500
- type: CorrelationIdentifierType.DID,
501
- correlationId: 'example_did',
502
- },
503
- }
504
-
505
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
506
-
507
- await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank aliases are not allowed')
508
- })
509
-
510
- it('should throw error when saving identity with blank correlation id', async (): Promise<void> => {
511
- const identity: NonPersistedIdentity = {
512
- alias: 'example_did',
513
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
514
- identifier: {
515
- type: CorrelationIdentifierType.DID,
516
- correlationId: '',
517
- },
518
- }
519
-
520
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
521
-
522
- await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank correlation ids are not allowed')
523
- })
524
-
525
- it('should throw error when saving identity with blank metadata label', async (): Promise<void> => {
526
- const correlationId = 'example_did'
527
- const identity: NonPersistedIdentity = {
528
- alias: correlationId,
529
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
530
- identifier: {
531
- type: CorrelationIdentifierType.DID,
532
- correlationId,
533
- },
534
- metadata: [
535
- {
536
- label: '',
537
- value: 'example_value',
538
- },
539
- ],
540
- }
541
-
542
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
543
-
544
- await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank metadata labels are not allowed')
545
- })
546
-
547
- it('should throw error when saving identity with blank metadata value', async (): Promise<void> => {
548
- const correlationId = 'example_did'
549
- const identity: NonPersistedIdentity = {
550
- alias: correlationId,
551
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
552
- identifier: {
553
- type: CorrelationIdentifierType.DID,
554
- correlationId,
555
- },
556
- metadata: [
557
- {
558
- label: 'example_label',
559
- value: '',
560
- },
561
- ],
562
- }
563
-
564
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
565
-
566
- await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank metadata values are not allowed')
567
- })
568
-
569
- it('Should save identity with openid connection to database', async (): Promise<void> => {
570
- const correlationId = 'example.com'
571
- const identity: NonPersistedIdentity = {
572
- alias: correlationId,
573
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
574
- identifier: {
575
- type: CorrelationIdentifierType.URL,
576
- correlationId,
577
- },
578
- connection: {
579
- type: ConnectionType.OPENID_CONNECT,
580
- config: {
581
- clientId: '138d7bf8-c930-4c6e-b928-97d3a4928b01',
582
- clientSecret: '03b3955f-d020-4f2a-8a27-4e452d4e27a0',
583
- scopes: ['auth'],
584
- issuer: 'https://example.com/app-test',
585
- redirectUrl: 'app:/callback',
586
- dangerouslyAllowInsecureHttpRequests: true,
587
- clientAuthMethod: <const>'post',
588
- },
589
- },
590
- }
591
-
592
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
593
-
594
- await dbConnection.getRepository(IdentityEntity).save(identityEntity)
595
-
596
- const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
597
- where: {
598
- identifier: {
599
- correlationId,
600
- },
601
- },
602
- })
603
-
604
- expect(fromDb).toBeDefined()
605
- expect(fromDb?.connection).toBeDefined()
606
- expect(fromDb?.identifier).toBeDefined()
607
- expect(fromDb?.identifier.correlationId).toEqual(identity.identifier.correlationId)
608
- expect(fromDb?.identifier.type).toEqual(identity.identifier.type)
609
- expect(fromDb?.connection?.type).toEqual(identity.connection?.type)
610
- expect(fromDb?.connection?.config).toBeDefined()
611
- expect((<OpenIdConfigEntity>fromDb?.connection?.config).clientId).toEqual((<NonPersistedOpenIdConfig>identity.connection?.config).clientId)
612
- })
613
-
614
- it('Should save identity with didauth connection to database', async (): Promise<void> => {
615
- const correlationId = 'example.com'
616
- const identity: NonPersistedIdentity = {
617
- alias: correlationId,
618
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
619
- identifier: {
620
- type: CorrelationIdentifierType.URL,
621
- correlationId,
622
- },
623
- connection: {
624
- type: ConnectionType.SIOPv2,
625
- config: {
626
- identifier: {
627
- did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
628
- provider: 'test_provider',
629
- keys: [],
630
- services: [],
631
- },
632
- redirectUrl: 'https://example.com',
633
- stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
634
- sessionId: 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
635
- },
636
- },
637
- }
638
-
639
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
640
-
641
- await dbConnection.getRepository(IdentityEntity).save(identityEntity)
642
-
643
- const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
644
- where: {
645
- identifier: {
646
- correlationId,
647
- },
648
- },
649
- })
650
-
651
- expect(fromDb).toBeDefined()
652
- expect(fromDb?.connection).toBeDefined()
653
- expect(fromDb?.identifier).toBeDefined()
654
- expect(fromDb?.identifier.correlationId).toEqual(identity.identifier.correlationId)
655
- expect(fromDb?.identifier.type).toEqual(identity.identifier.type)
656
- expect(fromDb?.connection?.type).toEqual(identity.connection?.type)
657
- expect(fromDb?.connection?.config).toBeDefined()
658
- expect((<DidAuthConfigEntity>fromDb?.connection?.config).identifier).toEqual(
659
- (<NonPersistedDidAuthConfig>identity.connection?.config).identifier.did,
660
- )
661
- })
662
-
663
- it('Should save connection with openid config to database', async (): Promise<void> => {
664
- const connection: NonPersistedConnection = {
665
- type: ConnectionType.OPENID_CONNECT,
666
- config: {
667
- clientId: '138d7bf8-c930-4c6e-b928-97d3a4928b01',
668
- clientSecret: '03b3955f-d020-4f2a-8a27-4e452d4e27a0',
669
- scopes: ['auth'],
670
- issuer: 'https://example.com/app-test',
671
- redirectUrl: 'app:/callback',
672
- dangerouslyAllowInsecureHttpRequests: true,
673
- clientAuthMethod: <const>'post',
674
- },
675
- }
676
- const connectionEntity: ConnectionEntity = connectionEntityFrom(connection)
677
- await dbConnection.getRepository(ConnectionEntity).save(connectionEntity, {
678
- transaction: true,
679
- })
680
-
681
- const fromDb: ConnectionEntity | null = await dbConnection.getRepository(ConnectionEntity).findOne({
682
- where: { type: connection.type },
683
- })
684
-
685
- expect(fromDb).toBeDefined()
686
-
687
- const fromDbConfig: OpenIdConfigEntity | null = await dbConnection.getRepository(OpenIdConfigEntity).findOne({
688
- where: { id: fromDb?.id },
689
- })
690
-
691
- expect(fromDbConfig).toBeDefined()
692
- expect(fromDb?.type).toEqual(connection.type)
693
- expect(fromDb?.config).toBeDefined()
694
- expect((<OpenIdConfigEntity>fromDb?.config).clientId).toEqual((<NonPersistedOpenIdConfig>connection.config).clientId)
695
- })
696
-
697
- it('Should save connection with didauth config to database', async (): Promise<void> => {
698
- const connection: NonPersistedConnection = {
699
- type: ConnectionType.SIOPv2,
700
- config: {
701
- identifier: {
702
- did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
703
- provider: 'test_provider',
704
- keys: [],
705
- services: [],
706
- },
707
- redirectUrl: 'https://example.com',
708
- stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
709
- sessionId: 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
710
- },
711
- }
712
- const connectionEntity: ConnectionEntity = connectionEntityFrom(connection)
713
- await dbConnection.getRepository(ConnectionEntity).save(connectionEntity, {
714
- transaction: true,
715
- })
716
-
717
- const fromDb: ConnectionEntity | null = await dbConnection.getRepository(ConnectionEntity).findOne({
718
- where: { type: connection.type },
719
- })
720
-
721
- expect(fromDb).toBeDefined()
722
-
723
- const fromDbConfig: DidAuthConfigEntity | null = await dbConnection.getRepository(DidAuthConfigEntity).findOne({
724
- where: { id: fromDb?.id },
725
- })
726
-
727
- expect(fromDbConfig).toBeDefined()
728
- expect(fromDb?.type).toEqual(connection.type)
729
- expect(fromDb?.config).toBeDefined()
730
- expect((<DidAuthConfigEntity>fromDb?.config).identifier).toEqual((<NonPersistedDidAuthConfig>connection.config).identifier.did)
731
- })
732
-
733
- it('Should save openid config to database', async (): Promise<void> => {
734
- const clientId = '138d7bf8-c930-4c6e-b928-97d3a4928b01'
735
- const config: NonPersistedOpenIdConfig = {
736
- clientId,
737
- clientSecret: '03b3955f-d020-4f2a-8a27-4e452d4e27a0',
738
- scopes: ['auth'],
739
- issuer: 'https://example.com/app-test',
740
- redirectUrl: 'app:/callback',
741
- dangerouslyAllowInsecureHttpRequests: true,
742
- clientAuthMethod: <const>'post',
743
- }
744
-
745
- const configEntity: OpenIdConfigEntity = openIdConfigEntityFrom(config)
746
- await dbConnection.getRepository(OpenIdConfigEntity).save(configEntity, {
747
- transaction: true,
748
- })
749
-
750
- const fromDb: OpenIdConfigEntity | null = await dbConnection.getRepository(OpenIdConfigEntity).findOne({
751
- where: { clientId: config.clientId },
752
- })
753
-
754
- expect(fromDb).toBeDefined()
755
- expect((<OpenIdConfigEntity>fromDb).clientId).toEqual(config.clientId)
756
- })
757
-
758
- it('Should save didauth config to database', async (): Promise<void> => {
759
- const sessionId = 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01'
760
- const config: NonPersistedDidAuthConfig = {
761
- identifier: {
762
- did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
763
- provider: 'test_provider',
764
- keys: [],
765
- services: [],
766
- },
767
- redirectUrl: 'https://example.com',
768
- stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
769
- sessionId,
770
- }
771
-
772
- const configEntity: DidAuthConfigEntity = didAuthConfigEntityFrom(config)
773
- await dbConnection.getRepository(DidAuthConfigEntity).save(configEntity, {
774
- transaction: true,
775
- })
776
-
777
- const fromDb: DidAuthConfigEntity | null = await dbConnection.getRepository(DidAuthConfigEntity).findOne({
778
- where: { sessionId: config.sessionId },
779
- })
780
-
781
- expect(fromDb).toBeDefined()
782
- expect((<DidAuthConfigEntity>fromDb).identifier).toEqual(config.identifier.did)
783
- })
784
-
785
- it('Should delete party and all child relations', async (): Promise<void> => {
786
- const party1: NonPersistedParty = {
787
- uri: 'example.com',
788
- partyType: {
789
- type: PartyTypeType.NATURAL_PERSON,
790
- origin: PartyOrigin.INTERNAL,
791
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
792
- name: 'example_name1',
793
- },
794
- contact: {
795
- firstName: 'example_first_name1',
796
- middleName: 'example_middle_name1',
797
- lastName: 'example_last_name1',
798
- displayName: 'example_display_name1',
799
- },
800
- }
801
-
802
- const partyEntity1: PartyEntity = partyEntityFrom(party1)
803
- const savedParty1: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity1)
804
-
805
- expect(savedParty1).toBeDefined()
806
-
807
- const party2: NonPersistedParty = {
808
- uri: 'example.com',
809
- partyType: {
810
- type: PartyTypeType.NATURAL_PERSON,
811
- origin: PartyOrigin.INTERNAL,
812
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
813
- name: 'example_name2',
814
- },
815
- contact: {
816
- firstName: 'example_first_name2',
817
- middleName: 'example_middle_name2',
818
- lastName: 'example_last_name2',
819
- displayName: 'example_display_name2',
820
- },
821
- }
822
-
823
- const partyEntity2: PartyEntity = partyEntityFrom(party2)
824
- const savedParty2: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity2)
825
-
826
- expect(savedParty2).toBeDefined()
827
-
828
- const correlationId = 'relation_example.com'
829
- const identity: NonPersistedIdentity = {
830
- alias: correlationId,
831
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
832
- identifier: {
833
- type: CorrelationIdentifierType.URL,
834
- correlationId,
835
- },
836
- connection: {
837
- type: ConnectionType.OPENID_CONNECT,
838
- config: {
839
- clientId: '138d7bf8-c930-4c6e-b928-97d3a4928b01',
840
- clientSecret: '03b3955f-d020-4f2a-8a27-4e452d4e27a0',
841
- scopes: ['auth'],
842
- issuer: 'https://example.com/app-test',
843
- redirectUrl: 'app:/callback',
844
- dangerouslyAllowInsecureHttpRequests: true,
845
- clientAuthMethod: <const>'post',
846
- },
847
- },
848
- metadata: [
849
- {
850
- label: 'example_label',
851
- value: 'example_value',
852
- },
853
- ],
854
- }
855
-
856
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
857
- identityEntity.party = savedParty1
858
-
859
- const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
860
-
861
- expect(savedIdentity).toBeDefined()
862
-
863
- const electronicAddress: NonPersistedElectronicAddress = {
864
- type: 'email',
865
- electronicAddress: 'example_electronic_address',
866
- }
867
- const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
868
- electronicAddressEntity.party = savedParty1
869
-
870
- const savedElectronicAddress: ElectronicAddressEntity | null = await dbConnection
871
- .getRepository(ElectronicAddressEntity)
872
- .save(electronicAddressEntity)
873
-
874
- expect(savedElectronicAddress).toBeDefined()
875
-
876
- const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
877
- leftId: savedParty1.id,
878
- rightId: savedParty2.id,
879
- })
880
-
881
- const savedRelationship: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
882
- transaction: true,
883
- })
884
-
885
- expect(savedRelationship).toBeDefined()
886
-
887
- expect(
888
- await dbConnection.getRepository(PartyEntity).findOne({
889
- where: { id: savedParty1.id },
890
- }),
891
- ).toBeDefined()
892
-
893
- await dbConnection.getRepository(PartyEntity).delete({ id: savedParty1.id })
894
-
895
- // check party
896
- await expect(
897
- await dbConnection.getRepository(PartyEntity).findOne({
898
- where: { id: savedParty1.id },
899
- }),
900
- ).toBeNull()
901
-
902
- // check identity
903
- expect(
904
- await dbConnection.getRepository(IdentityEntity).findOne({
905
- where: { id: savedParty1.id },
906
- }),
907
- ).toBeNull()
908
-
909
- // check identity identifier
910
- expect(
911
- await dbConnection.getRepository(CorrelationIdentifierEntity).findOne({
912
- where: { id: savedIdentity.identifier.id },
913
- }),
914
- ).toBeNull()
915
-
916
- // check identity connection
917
- expect(
918
- await dbConnection.getRepository(ConnectionEntity).findOne({
919
- where: { id: savedIdentity.connection!.id },
920
- }),
921
- ).toBeNull()
922
-
923
- // check connection config
924
- expect(
925
- await dbConnection.getRepository(OpenIdConfigEntity).findOne({
926
- where: { id: savedIdentity.connection!.config.id },
927
- }),
928
- ).toBeNull()
929
-
930
- // check identity metadata
931
- expect(
932
- await dbConnection.getRepository(IdentityMetadataItemEntity).findOne({
933
- where: { id: savedIdentity.metadata![0].id },
934
- }),
935
- ).toBeNull()
936
-
937
- // check electronic address
938
- expect(
939
- await dbConnection.getRepository(ElectronicAddressEntity).findOne({
940
- where: { id: savedParty1.id },
941
- }),
942
- ).toBeNull()
943
-
944
- // check contact
945
- expect(
946
- await dbConnection.getRepository(BaseContactEntity).findOne({
947
- where: { id: savedParty1.contact.id },
948
- }),
949
- ).toBeNull()
950
-
951
- // check party type
952
- expect(
953
- await dbConnection.getRepository(PartyTypeEntity).findOne({
954
- where: { id: savedParty1.partyType.id },
955
- }),
956
- ).toBeDefined()
957
-
958
- // check relation
959
- expect(
960
- await dbConnection.getRepository(PartyRelationshipEntity).findOne({
961
- where: { id: savedRelationship.id },
962
- }),
963
- ).toBeNull()
964
- })
965
-
966
- it('Should delete identity and all child relations', async (): Promise<void> => {
967
- const party: NonPersistedParty = {
968
- uri: 'example.com',
969
- partyType: {
970
- type: PartyTypeType.NATURAL_PERSON,
971
- origin: PartyOrigin.EXTERNAL,
972
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
973
- name: 'example_name',
974
- },
975
- contact: {
976
- firstName: 'example_first_name',
977
- middleName: 'example_middle_name',
978
- lastName: 'example_last_name',
979
- displayName: 'example_display_name',
980
- },
981
- }
982
-
983
- const partyEntity: PartyEntity = partyEntityFrom(party)
984
- const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
985
-
986
- expect(savedParty).toBeDefined()
987
-
988
- const correlationId = 'relation_example.com'
989
- const identity: NonPersistedIdentity = {
990
- alias: correlationId,
991
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
992
- identifier: {
993
- type: CorrelationIdentifierType.URL,
994
- correlationId,
995
- },
996
- connection: {
997
- type: ConnectionType.SIOPv2,
998
- config: {
999
- identifier: {
1000
- did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
1001
- provider: 'test_provider',
1002
- keys: [],
1003
- services: [],
1004
- },
1005
- redirectUrl: 'https://example.com',
1006
- stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
1007
- sessionId: 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
1008
- },
1009
- },
1010
- metadata: [
1011
- {
1012
- label: 'example_label',
1013
- value: 'example_value',
1014
- },
1015
- ],
1016
- }
1017
-
1018
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
1019
- identityEntity.party = savedParty
1020
-
1021
- const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1022
-
1023
- expect(
1024
- await dbConnection.getRepository(PartyEntity).findOne({
1025
- where: { id: savedParty.id },
1026
- }),
1027
- ).toBeDefined()
1028
-
1029
- await dbConnection.getRepository(IdentityEntity).delete({ id: savedIdentity.id })
1030
-
1031
- // check identity
1032
- expect(
1033
- await dbConnection.getRepository(IdentityEntity).findOne({
1034
- where: { alias: correlationId },
1035
- }),
1036
- ).toBeNull()
1037
-
1038
- // check identity identifier
1039
- expect(
1040
- await dbConnection.getRepository(CorrelationIdentifierEntity).findOne({
1041
- where: { id: savedIdentity.identifier.id },
1042
- }),
1043
- ).toBeNull()
1044
-
1045
- // check identity connection
1046
- expect(
1047
- await dbConnection.getRepository(ConnectionEntity).findOne({
1048
- where: { id: savedIdentity.connection!.id },
1049
- }),
1050
- ).toBeNull()
1051
-
1052
- // check connection config
1053
- expect(
1054
- await dbConnection.getRepository(OpenIdConfigEntity).findOne({
1055
- where: { id: savedIdentity.connection!.config.id },
1056
- }),
1057
- ).toBeNull()
1058
-
1059
- // check identity metadata
1060
- expect(
1061
- await dbConnection.getRepository(IdentityMetadataItemEntity).findOne({
1062
- where: { id: savedIdentity.metadata![0].id },
1063
- }),
1064
- ).toBeNull()
1065
- })
1066
-
1067
- it('Should not delete party when deleting identity', async (): Promise<void> => {
1068
- const party: NonPersistedParty = {
1069
- uri: 'example.com',
1070
- partyType: {
1071
- type: PartyTypeType.NATURAL_PERSON,
1072
- origin: PartyOrigin.EXTERNAL,
1073
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1074
- name: 'example_name',
1075
- },
1076
- contact: {
1077
- firstName: 'example_first_name',
1078
- middleName: 'example_middle_name',
1079
- lastName: 'example_last_name',
1080
- displayName: 'example_display_name',
1081
- },
1082
- }
1083
-
1084
- const partyEntity: PartyEntity = partyEntityFrom(party)
1085
- const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1086
-
1087
- expect(savedParty).toBeDefined()
1088
-
1089
- const correlationId = 'relation_example.com'
1090
- const identity: NonPersistedIdentity = {
1091
- alias: correlationId,
1092
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
1093
- identifier: {
1094
- type: CorrelationIdentifierType.URL,
1095
- correlationId,
1096
- },
1097
- connection: {
1098
- type: ConnectionType.SIOPv2,
1099
- config: {
1100
- identifier: {
1101
- did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
1102
- provider: 'test_provider',
1103
- keys: [],
1104
- services: [],
1105
- },
1106
- redirectUrl: 'https://example.com',
1107
- stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
1108
- sessionId: 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
1109
- },
1110
- },
1111
- metadata: [
1112
- {
1113
- label: 'example_label',
1114
- value: 'example_value',
1115
- },
1116
- ],
1117
- }
1118
-
1119
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
1120
- identityEntity.party = savedParty
1121
-
1122
- const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1123
-
1124
- expect(savedIdentity).toBeDefined()
1125
-
1126
- await dbConnection.getRepository(IdentityEntity).delete({ id: savedIdentity.id })
1127
-
1128
- // check identity
1129
- expect(
1130
- await dbConnection.getRepository(IdentityEntity).findOne({
1131
- where: { id: savedIdentity.id },
1132
- }),
1133
- ).toBeNull()
1134
-
1135
- // check party
1136
- expect(
1137
- await dbConnection.getRepository(PartyEntity).findOne({
1138
- where: { id: savedParty.id },
1139
- }),
1140
- ).toBeDefined()
1141
- })
1142
-
1143
- it('Should set creation date when saving party', async (): Promise<void> => {
1144
- const party: NonPersistedParty = {
1145
- uri: 'example.com',
1146
- partyType: {
1147
- type: PartyTypeType.NATURAL_PERSON,
1148
- origin: PartyOrigin.INTERNAL,
1149
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1150
- name: 'example_name',
1151
- },
1152
- contact: {
1153
- firstName: 'example_first_name',
1154
- middleName: 'example_middle_name',
1155
- lastName: 'example_last_name',
1156
- displayName: 'example_display_name',
1157
- },
1158
- }
1159
-
1160
- const partyEntity: PartyEntity = partyEntityFrom(party)
1161
- const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1162
-
1163
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1164
- where: { id: savedParty.id },
1165
- })
1166
-
1167
- expect(fromDb).toBeDefined()
1168
- expect(fromDb?.createdAt).toBeDefined()
1169
- })
1170
-
1171
- it('Should not update creation date when updating party', async (): Promise<void> => {
1172
- const party: NonPersistedParty = {
1173
- uri: 'example.com',
1174
- partyType: {
1175
- type: PartyTypeType.NATURAL_PERSON,
1176
- origin: PartyOrigin.INTERNAL,
1177
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1178
- name: 'example_name',
1179
- },
1180
- contact: {
1181
- firstName: 'example_first_name',
1182
- middleName: 'example_middle_name',
1183
- lastName: 'example_last_name',
1184
- displayName: 'example_display_name',
1185
- },
1186
- }
1187
-
1188
- const partyEntity: PartyEntity = partyEntityFrom(party)
1189
- const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1190
-
1191
- expect(savedParty).toBeDefined()
1192
-
1193
- const newContactFirstName = 'new_first_name'
1194
- await dbConnection.getRepository(PartyEntity).save({
1195
- ...savedParty,
1196
- contact: {
1197
- ...savedParty.contact,
1198
- firstName: newContactFirstName,
1199
- },
1200
- })
1201
-
1202
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1203
- where: { id: savedParty.id },
1204
- })
1205
-
1206
- expect(fromDb).toBeDefined()
1207
- expect((<NaturalPersonEntity>fromDb?.contact).firstName).toEqual(newContactFirstName)
1208
- expect(fromDb?.createdAt).toEqual(savedParty?.createdAt)
1209
- })
1210
-
1211
- it('Should set creation date when saving identity', async (): Promise<void> => {
1212
- const correlationId = 'example_did'
1213
- const identity: NonPersistedIdentity = {
1214
- alias: correlationId,
1215
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
1216
- identifier: {
1217
- type: CorrelationIdentifierType.DID,
1218
- correlationId,
1219
- },
1220
- }
1221
-
1222
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
1223
- await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1224
-
1225
- const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
1226
- where: {
1227
- identifier: {
1228
- correlationId,
1229
- },
1230
- },
1231
- })
1232
-
1233
- expect(fromDb).toBeDefined()
1234
- expect(fromDb?.createdAt).toBeDefined()
1235
- })
1236
-
1237
- it('Should not update creation date when saving identity', async (): Promise<void> => {
1238
- const correlationId = 'example_did'
1239
- const identity: NonPersistedIdentity = {
1240
- alias: correlationId,
1241
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
1242
- identifier: {
1243
- type: CorrelationIdentifierType.DID,
1244
- correlationId,
1245
- },
1246
- }
1247
-
1248
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
1249
- const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1250
- const newCorrelationId = 'new_example_did'
1251
- await dbConnection
1252
- .getRepository(IdentityEntity)
1253
- .save({ ...savedIdentity, identifier: { ...savedIdentity.identifier, correlationId: newCorrelationId } })
1254
-
1255
- const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
1256
- where: {
1257
- identifier: {
1258
- correlationId: newCorrelationId,
1259
- },
1260
- },
1261
- })
1262
-
1263
- expect(fromDb).toBeDefined()
1264
- expect(fromDb?.createdAt).toEqual(savedIdentity?.createdAt)
1265
- })
1266
-
1267
- it('Should set last updated date when saving party', async (): Promise<void> => {
1268
- const party: NonPersistedParty = {
1269
- uri: 'example.com',
1270
- partyType: {
1271
- type: PartyTypeType.NATURAL_PERSON,
1272
- origin: PartyOrigin.INTERNAL,
1273
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1274
- name: 'example_name',
1275
- },
1276
- contact: {
1277
- firstName: 'example_first_name',
1278
- middleName: 'example_middle_name',
1279
- lastName: 'example_last_name',
1280
- displayName: 'example_display_name',
1281
- },
1282
- }
1283
-
1284
- const partyEntity: PartyEntity = partyEntityFrom(party)
1285
- const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1286
-
1287
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1288
- where: { id: savedParty.id },
1289
- })
1290
-
1291
- expect(fromDb).toBeDefined()
1292
- expect(fromDb?.lastUpdatedAt).toBeDefined()
1293
- })
1294
-
1295
- it('Should update last updated date when updating party', async (): Promise<void> => {
1296
- const party: NonPersistedParty = {
1297
- uri: 'example.com',
1298
- partyType: {
1299
- type: PartyTypeType.NATURAL_PERSON,
1300
- origin: PartyOrigin.EXTERNAL,
1301
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1302
- name: 'example_name',
1303
- },
1304
- contact: {
1305
- firstName: 'example_first_name',
1306
- middleName: 'example_middle_name',
1307
- lastName: 'example_last_name',
1308
- displayName: 'example_display_name',
1309
- },
1310
- }
1311
-
1312
- const partyEntity: PartyEntity = partyEntityFrom(party)
1313
- const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1314
- expect(savedParty).toBeDefined()
1315
-
1316
- // waiting here to get a different timestamp
1317
- await new Promise((resolve) => setTimeout(resolve, 2000))
1318
-
1319
- const newContactFirstName = 'new_first_name'
1320
- await dbConnection.getRepository(PartyEntity).save({
1321
- ...savedParty,
1322
- // FIXME there is still an issue when updating nested objects, the parent does not update
1323
- // https://github.com/typeorm/typeorm/issues/5378
1324
- uri: 'new uri', // TODO remove this to trigger the bug
1325
- contact: {
1326
- ...savedParty.contact,
1327
- firstName: newContactFirstName,
1328
- },
1329
- })
1330
-
1331
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1332
- where: { id: savedParty.id },
1333
- })
1334
-
1335
- expect(fromDb).toBeDefined()
1336
- expect((<NaturalPersonEntity>fromDb?.contact).firstName).toEqual(newContactFirstName)
1337
- expect(fromDb?.lastUpdatedAt).not.toEqual(savedParty?.lastUpdatedAt)
1338
- })
1339
-
1340
- it('Should set last updated date when saving party type', async (): Promise<void> => {
1341
- const partyType: NonPersistedPartyType = {
1342
- type: PartyTypeType.NATURAL_PERSON,
1343
- origin: PartyOrigin.EXTERNAL,
1344
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1345
- name: 'example_name',
1346
- }
1347
-
1348
- const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
1349
- const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
1350
-
1351
- const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
1352
- where: { id: savedPartyType.id },
1353
- })
1354
-
1355
- expect(fromDb).toBeDefined()
1356
- expect(fromDb?.lastUpdatedAt).toBeDefined()
1357
- })
1358
-
1359
- it('Should set last creation date when saving party type', async (): Promise<void> => {
1360
- const partyType: NonPersistedPartyType = {
1361
- type: PartyTypeType.NATURAL_PERSON,
1362
- origin: PartyOrigin.INTERNAL,
1363
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1364
- name: 'example_name',
1365
- }
1366
-
1367
- const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
1368
- const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
1369
-
1370
- const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
1371
- where: { id: savedPartyType.id },
1372
- })
1373
-
1374
- expect(fromDb).toBeDefined()
1375
- expect(fromDb?.createdAt).toBeDefined()
1376
- })
1377
-
1378
- it('Should set last updated date when saving identity', async (): Promise<void> => {
1379
- const correlationId = 'example_did'
1380
- const identity: NonPersistedIdentity = {
1381
- alias: correlationId,
1382
- roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
1383
- identifier: {
1384
- type: CorrelationIdentifierType.DID,
1385
- correlationId,
1386
- },
1387
- }
1388
-
1389
- const identityEntity: IdentityEntity = identityEntityFrom(identity)
1390
- await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1391
-
1392
- const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
1393
- where: {
1394
- identifier: {
1395
- correlationId,
1396
- },
1397
- },
1398
- })
1399
-
1400
- expect(fromDb).toBeDefined()
1401
- expect(fromDb?.lastUpdatedAt).toBeDefined()
1402
- })
1403
-
1404
- it('Should enforce unique type and tenant id combination when saving party type', async (): Promise<void> => {
1405
- const tenantId = 'non_unique_value'
1406
- const name = 'non_unique_value'
1407
- const partyType1: NonPersistedPartyType = {
1408
- type: PartyTypeType.NATURAL_PERSON,
1409
- origin: PartyOrigin.EXTERNAL,
1410
- tenantId,
1411
- name,
1412
- }
1413
-
1414
- const partyTypeEntity1: PartyTypeEntity = partyTypeEntityFrom(partyType1)
1415
- const savedPartyType1: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity1)
1416
-
1417
- expect(savedPartyType1).toBeDefined()
1418
-
1419
- const partyType2: NonPersistedPartyType = {
1420
- type: PartyTypeType.NATURAL_PERSON,
1421
- origin: PartyOrigin.INTERNAL,
1422
- tenantId,
1423
- name,
1424
- }
1425
-
1426
- const partyTypeEntity2: PartyTypeEntity = partyTypeEntityFrom(partyType2)
1427
- await expect(dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity2)).rejects.toThrowError(
1428
- 'SQLITE_CONSTRAINT: UNIQUE constraint failed: PartyType.type, PartyType.tenant_id',
1429
- )
1430
- })
1431
-
1432
- it('Should enforce unique name when saving party type', async (): Promise<void> => {
1433
- const name = 'non_unique_value'
1434
- const partyType1: NonPersistedPartyType = {
1435
- type: PartyTypeType.NATURAL_PERSON,
1436
- origin: PartyOrigin.INTERNAL,
1437
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1438
- name,
1439
- }
1440
-
1441
- const partyTypeEntity1: PartyTypeEntity = partyTypeEntityFrom(partyType1)
1442
- const savedPartyType1: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity1)
1443
-
1444
- expect(savedPartyType1).toBeDefined()
1445
-
1446
- const partyType2: NonPersistedPartyType = {
1447
- type: PartyTypeType.NATURAL_PERSON,
1448
- origin: PartyOrigin.INTERNAL,
1449
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1450
- name,
1451
- }
1452
-
1453
- const partyTypeEntity2: PartyTypeEntity = partyTypeEntityFrom(partyType2)
1454
- await expect(dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity2)).rejects.toThrowError(
1455
- 'SQLITE_CONSTRAINT: UNIQUE constraint failed: PartyType.name',
1456
- )
1457
- })
1458
-
1459
- it('Should enforce unique legal name when saving organization', async (): Promise<void> => {
1460
- const legalName = 'non_unique_value'
1461
- const organization1: NonPersistedOrganization = {
1462
- legalName,
1463
- displayName: 'example_display_name',
1464
- }
1465
-
1466
- const organizationEntity1: OrganizationEntity = organizationEntityFrom(organization1)
1467
- const savedOrganization1: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity1, {
1468
- transaction: true,
1469
- })
1470
-
1471
- expect(savedOrganization1).toBeDefined()
1472
-
1473
- const organization2: NonPersistedOrganization = {
1474
- legalName,
1475
- displayName: 'example_display_name',
1476
- }
1477
-
1478
- const organizationEntity2: OrganizationEntity = organizationEntityFrom(organization2)
1479
- await expect(dbConnection.getRepository(OrganizationEntity).save(organizationEntity2)).rejects.toThrowError(
1480
- 'SQLITE_CONSTRAINT: UNIQUE constraint failed: BaseContact.legal_name',
1481
- )
1482
- })
1483
-
1484
- it('Should enforce unique legal name when saving organization', async (): Promise<void> => {
1485
- const legalName = 'example_legal_name'
1486
- const organization1: NonPersistedOrganization = {
1487
- legalName,
1488
- displayName: 'example_display_name',
1489
- }
1490
-
1491
- const organizationEntity1: OrganizationEntity = organizationEntityFrom(organization1)
1492
- const savedOrganization1: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity1, {
1493
- transaction: true,
1494
- })
1495
-
1496
- expect(savedOrganization1).toBeDefined()
1497
-
1498
- const organization2: NonPersistedOrganization = {
1499
- legalName,
1500
- displayName: 'example_display_name',
1501
- }
1502
-
1503
- const organizationEntity2: OrganizationEntity = organizationEntityFrom(organization2)
1504
- await expect(dbConnection.getRepository(OrganizationEntity).save(organizationEntity2)).rejects.toThrowError(
1505
- 'SQLITE_CONSTRAINT: UNIQUE constraint failed: BaseContact.legal_name',
1506
- )
1507
- })
1508
-
1509
- it('Should save party relationship to database', async (): Promise<void> => {
1510
- const party1: NonPersistedParty = {
1511
- uri: 'example1.com',
1512
- partyType: {
1513
- type: PartyTypeType.NATURAL_PERSON,
1514
- origin: PartyOrigin.INTERNAL,
1515
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1516
- name: 'example_name1',
1517
- },
1518
- contact: {
1519
- firstName: 'example_first_name1',
1520
- middleName: 'example_middle_name1',
1521
- lastName: 'example_last_name1',
1522
- displayName: 'example_display_name1',
1523
- },
1524
- }
1525
-
1526
- const partyEntity1: PartyEntity = partyEntityFrom(party1)
1527
- const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1528
- transaction: true,
1529
- })
1530
-
1531
- expect(savedParty1).toBeDefined()
1532
-
1533
- const party2: NonPersistedParty = {
1534
- uri: 'example2.com',
1535
- partyType: {
1536
- type: PartyTypeType.NATURAL_PERSON,
1537
- origin: PartyOrigin.INTERNAL,
1538
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1539
- name: 'example_name2',
1540
- },
1541
- contact: {
1542
- firstName: 'example_first_name2',
1543
- middleName: 'example_middle_name2',
1544
- lastName: 'example_last_name2',
1545
- displayName: 'example_display_name2',
1546
- },
1547
- }
1548
-
1549
- const partyEntity2: PartyEntity = partyEntityFrom(party2)
1550
- const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1551
- transaction: true,
1552
- })
1553
-
1554
- expect(savedParty2).toBeDefined()
1555
-
1556
- const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
1557
- leftId: savedParty1.id,
1558
- rightId: savedParty2.id,
1559
- })
1560
-
1561
- await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
1562
- transaction: true,
1563
- })
1564
-
1565
- // TODO check the relation field
1566
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1567
- where: { id: partyEntity1.id },
1568
- })
1569
-
1570
- expect(fromDb).toBeDefined()
1571
- })
1572
-
1573
- it('Should set last updated date when saving party relationship', async (): Promise<void> => {
1574
- const party1: NonPersistedParty = {
1575
- uri: 'example1.com',
1576
- partyType: {
1577
- type: PartyTypeType.NATURAL_PERSON,
1578
- origin: PartyOrigin.INTERNAL,
1579
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1580
- name: 'example_name1',
1581
- },
1582
- contact: {
1583
- firstName: 'example_first_name1',
1584
- middleName: 'example_middle_name1',
1585
- lastName: 'example_last_name1',
1586
- displayName: 'example_display_name1',
1587
- },
1588
- }
1589
-
1590
- const partyEntity1: PartyEntity = partyEntityFrom(party1)
1591
- const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1592
- transaction: true,
1593
- })
1594
-
1595
- const party2: NonPersistedParty = {
1596
- uri: 'example2.com',
1597
- partyType: {
1598
- type: PartyTypeType.NATURAL_PERSON,
1599
- origin: PartyOrigin.INTERNAL,
1600
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1601
- name: 'example_name2',
1602
- },
1603
- contact: {
1604
- firstName: 'example_first_name2',
1605
- middleName: 'example_middle_name2',
1606
- lastName: 'example_last_name2',
1607
- displayName: 'example_display_name2',
1608
- },
1609
- }
1610
-
1611
- const partyEntity2: PartyEntity = partyEntityFrom(party2)
1612
- const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1613
- transaction: true,
1614
- })
1615
-
1616
- const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
1617
- leftId: savedParty1.id,
1618
- rightId: savedParty2.id,
1619
- })
1620
-
1621
- await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
1622
- transaction: true,
1623
- })
1624
-
1625
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1626
- where: { id: partyEntity1.id },
1627
- })
1628
-
1629
- expect(fromDb).toBeDefined()
1630
- expect(fromDb?.lastUpdatedAt).toBeDefined()
1631
- })
1632
-
1633
- it('Should set creation date when saving party relationship', async (): Promise<void> => {
1634
- const party1: NonPersistedParty = {
1635
- uri: 'example1.com',
1636
- partyType: {
1637
- type: PartyTypeType.NATURAL_PERSON,
1638
- origin: PartyOrigin.INTERNAL,
1639
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1640
- name: 'example_name1',
1641
- },
1642
- contact: {
1643
- firstName: 'example_first_name1',
1644
- middleName: 'example_middle_name1',
1645
- lastName: 'example_last_name1',
1646
- displayName: 'example_display_name1',
1647
- },
1648
- }
1649
-
1650
- const partyEntity1: PartyEntity = partyEntityFrom(party1)
1651
- const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1652
- transaction: true,
1653
- })
1654
-
1655
- const party2: NonPersistedParty = {
1656
- uri: 'example2.com',
1657
- partyType: {
1658
- type: PartyTypeType.NATURAL_PERSON,
1659
- origin: PartyOrigin.INTERNAL,
1660
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1661
- name: 'example_name2',
1662
- },
1663
- contact: {
1664
- firstName: 'example_first_name2',
1665
- middleName: 'example_middle_name2',
1666
- lastName: 'example_last_name2',
1667
- displayName: 'example_display_name2',
1668
- },
1669
- }
1670
-
1671
- const partyEntity2: PartyEntity = partyEntityFrom(party2)
1672
- const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1673
- transaction: true,
1674
- })
1675
-
1676
- const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
1677
- leftId: savedParty1.id,
1678
- rightId: savedParty2.id,
1679
- })
1680
-
1681
- await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
1682
- transaction: true,
1683
- })
1684
-
1685
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1686
- where: { id: partyEntity1.id },
1687
- })
1688
-
1689
- expect(fromDb).toBeDefined()
1690
- expect(fromDb?.createdAt).toBeDefined()
1691
- })
1692
-
1693
- it('Should save bidirectional party relationships to database', async (): Promise<void> => {
1694
- const party1: NonPersistedParty = {
1695
- uri: 'example1.com',
1696
- partyType: {
1697
- type: PartyTypeType.NATURAL_PERSON,
1698
- origin: PartyOrigin.INTERNAL,
1699
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1700
- name: 'example_name1',
1701
- },
1702
- contact: {
1703
- firstName: 'example_first_name1',
1704
- middleName: 'example_middle_name1',
1705
- lastName: 'example_last_name1',
1706
- displayName: 'example_display_name1',
1707
- },
1708
- }
1709
-
1710
- const partyEntity1: PartyEntity = partyEntityFrom(party1)
1711
- const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1712
- transaction: true,
1713
- })
1714
-
1715
- expect(savedParty1).toBeDefined()
1716
-
1717
- const party2: NonPersistedParty = {
1718
- uri: 'example2.com',
1719
- partyType: {
1720
- type: PartyTypeType.NATURAL_PERSON,
1721
- origin: PartyOrigin.INTERNAL,
1722
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1723
- name: 'example_name2',
1724
- },
1725
- contact: {
1726
- firstName: 'example_first_name2',
1727
- middleName: 'example_middle_name2',
1728
- lastName: 'example_last_name2',
1729
- displayName: 'example_display_name2',
1730
- },
1731
- }
1732
-
1733
- const partyEntity2: PartyEntity = partyEntityFrom(party2)
1734
- const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1735
- transaction: true,
1736
- })
1737
-
1738
- expect(savedParty2).toBeDefined()
1739
-
1740
- const relationship1: PartyRelationshipEntity = partyRelationshipEntityFrom({
1741
- leftId: savedParty1.id,
1742
- rightId: savedParty2.id,
1743
- })
1744
-
1745
- const savedRelationship1: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship1, {
1746
- transaction: true,
1747
- })
1748
-
1749
- expect(savedRelationship1).toBeDefined()
1750
-
1751
- const relationship2: PartyRelationshipEntity = partyRelationshipEntityFrom({
1752
- leftId: savedParty2.id,
1753
- rightId: savedParty1.id,
1754
- })
1755
-
1756
- const savedRelationship2: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship2, {
1757
- transaction: true,
1758
- })
1759
-
1760
- expect(savedRelationship2).toBeDefined()
1761
-
1762
- const fromDb: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).findOne({
1763
- where: { id: savedRelationship2.id },
1764
- })
1765
-
1766
- expect(fromDb).toBeDefined()
1767
- })
1768
-
1769
- it('Should enforce unique owner combination for party relationship', async (): Promise<void> => {
1770
- const party1: NonPersistedParty = {
1771
- uri: 'example1.com',
1772
- partyType: {
1773
- type: PartyTypeType.NATURAL_PERSON,
1774
- origin: PartyOrigin.INTERNAL,
1775
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1776
- name: 'example_name1',
1777
- },
1778
- contact: {
1779
- firstName: 'example_first_name1',
1780
- middleName: 'example_middle_name1',
1781
- lastName: 'example_last_name1',
1782
- displayName: 'example_display_name1',
1783
- },
1784
- }
1785
-
1786
- const partyEntity1: PartyEntity = partyEntityFrom(party1)
1787
- const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1788
- transaction: true,
1789
- })
1790
-
1791
- expect(savedParty1).toBeDefined()
1792
-
1793
- const party2: NonPersistedParty = {
1794
- uri: 'example2.com',
1795
- partyType: {
1796
- type: PartyTypeType.NATURAL_PERSON,
1797
- origin: PartyOrigin.INTERNAL,
1798
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1799
- name: 'example_name2',
1800
- },
1801
- contact: {
1802
- firstName: 'example_first_name2',
1803
- middleName: 'example_middle_name2',
1804
- lastName: 'example_last_name2',
1805
- displayName: 'example_display_name2',
1806
- },
1807
- }
1808
-
1809
- const partyEntity2: PartyEntity = partyEntityFrom(party2)
1810
- const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1811
- transaction: true,
1812
- })
1813
-
1814
- expect(savedParty2).toBeDefined()
1815
-
1816
- const relationship1: PartyRelationshipEntity = partyRelationshipEntityFrom({
1817
- leftId: savedParty1.id,
1818
- rightId: savedParty2.id,
1819
- })
1820
-
1821
- const savedRelationship1: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship1, {
1822
- transaction: true,
1823
- })
1824
-
1825
- expect(savedRelationship1).toBeDefined()
1826
-
1827
- const relationship2: PartyRelationshipEntity = partyRelationshipEntityFrom({
1828
- leftId: savedParty1.id,
1829
- rightId: savedParty2.id,
1830
- })
1831
-
1832
- await expect(dbConnection.getRepository(PartyRelationshipEntity).save(relationship2)).rejects.toThrowError(
1833
- 'SQLITE_CONSTRAINT: UNIQUE constraint failed: PartyRelationship.left_id, PartyRelationship.right_id',
1834
- )
1835
- })
1836
-
1837
- it('Should save party type to database', async (): Promise<void> => {
1838
- const partyType: NonPersistedPartyType = {
1839
- type: PartyTypeType.NATURAL_PERSON,
1840
- origin: PartyOrigin.INTERNAL,
1841
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1842
- name: 'example_name',
1843
- }
1844
-
1845
- const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
1846
- const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
1847
-
1848
- const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
1849
- where: { id: savedPartyType.id },
1850
- })
1851
-
1852
- expect(fromDb).toBeDefined()
1853
- })
1854
-
1855
- it('Should save person to database', async (): Promise<void> => {
1856
- const person: NonPersistedNaturalPerson = {
1857
- firstName: 'example_first_name',
1858
- lastName: 'lastName2',
1859
- displayName: 'displayName',
1860
- }
1861
-
1862
- const personEntity: NaturalPersonEntity = naturalPersonEntityFrom(person)
1863
- const savedPerson: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).save(personEntity, {
1864
- transaction: true,
1865
- })
1866
-
1867
- const fromDb: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).findOne({
1868
- where: { id: savedPerson.id },
1869
- })
1870
-
1871
- expect(fromDb).toBeDefined()
1872
- })
1873
-
1874
- it('Should set last updated date when saving person', async (): Promise<void> => {
1875
- const person: NonPersistedNaturalPerson = {
1876
- firstName: 'example_first_name',
1877
- lastName: 'lastName2',
1878
- displayName: 'displayName',
1879
- }
1880
-
1881
- const personEntity: NaturalPersonEntity = naturalPersonEntityFrom(person)
1882
- const savedPerson: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).save(personEntity, {
1883
- transaction: true,
1884
- })
1885
-
1886
- const fromDb: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).findOne({
1887
- where: { id: savedPerson.id },
1888
- })
1889
-
1890
- expect(fromDb).toBeDefined()
1891
- expect(fromDb?.lastUpdatedAt).toBeDefined()
1892
- })
1893
-
1894
- it('Should set creation date when saving person', async (): Promise<void> => {
1895
- const person: NonPersistedNaturalPerson = {
1896
- firstName: 'example_first_name',
1897
- lastName: 'lastName2',
1898
- displayName: 'displayName',
1899
- }
1900
-
1901
- const personEntity: NaturalPersonEntity = naturalPersonEntityFrom(person)
1902
- const savedPerson: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).save(personEntity, {
1903
- transaction: true,
1904
- })
1905
-
1906
- const fromDb: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).findOne({
1907
- where: { id: savedPerson.id },
1908
- })
1909
-
1910
- expect(fromDb).toBeDefined()
1911
- expect(fromDb?.createdAt).toBeDefined()
1912
- })
1913
-
1914
- it('Should save organization to database', async (): Promise<void> => {
1915
- const organization: NonPersistedOrganization = {
1916
- legalName: 'example_legal_name',
1917
- displayName: 'example_display_name',
1918
- }
1919
-
1920
- const organizationEntity: OrganizationEntity = organizationEntityFrom(organization)
1921
- const savedOrganization: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity, {
1922
- transaction: true,
1923
- })
1924
-
1925
- const fromDb: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).findOne({
1926
- where: { id: savedOrganization.id },
1927
- })
1928
-
1929
- expect(fromDb).toBeDefined()
1930
- })
1931
-
1932
- it('Should set last updated date when saving organization', async (): Promise<void> => {
1933
- const organization: NonPersistedOrganization = {
1934
- legalName: 'example_legal_name',
1935
- displayName: 'example_display_name',
1936
- }
1937
-
1938
- const organizationEntity: OrganizationEntity = organizationEntityFrom(organization)
1939
- const savedOrganization: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity, {
1940
- transaction: true,
1941
- })
1942
-
1943
- const fromDb: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).findOne({
1944
- where: { id: savedOrganization.id },
1945
- })
1946
-
1947
- expect(fromDb).toBeDefined()
1948
- expect(fromDb?.lastUpdatedAt).toBeDefined()
1949
- })
1950
-
1951
- it('Should set creation date when saving organization', async (): Promise<void> => {
1952
- const organization: NonPersistedOrganization = {
1953
- legalName: 'example_legal_name',
1954
- displayName: 'example_display_name',
1955
- }
1956
-
1957
- const organizationEntity: OrganizationEntity = organizationEntityFrom(organization)
1958
- const savedOrganization: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity, {
1959
- transaction: true,
1960
- })
1961
-
1962
- const fromDb: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).findOne({
1963
- where: { id: savedOrganization.id },
1964
- })
1965
-
1966
- expect(fromDb).toBeDefined()
1967
- expect(fromDb?.createdAt).toBeDefined()
1968
- })
1969
-
1970
- it('Should get party based on person information', async (): Promise<void> => {
1971
- const firstName = 'example_first_name'
1972
- const party: NonPersistedParty = {
1973
- uri: 'example.com',
1974
- partyType: {
1975
- type: PartyTypeType.NATURAL_PERSON,
1976
- origin: PartyOrigin.INTERNAL,
1977
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1978
- name: 'example_name',
1979
- },
1980
- contact: {
1981
- firstName,
1982
- middleName: 'example_middle_name',
1983
- lastName: 'example_last_name',
1984
- displayName: 'example_display_name',
1985
- },
1986
- }
1987
-
1988
- const partyEntity: PartyEntity = partyEntityFrom(party)
1989
- await dbConnection.getRepository(PartyEntity).save(partyEntity, {
1990
- transaction: true,
1991
- })
1992
-
1993
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1994
- where: {
1995
- contact: {
1996
- firstName,
1997
- } as FindOptionsWhere<BaseContactEntity>, //NaturalPersonEntity | OrganizationEntity
1998
- },
1999
- })
2000
-
2001
- expect(fromDb).toBeDefined()
2002
- })
2003
-
2004
- it('Should get party based on organization information', async (): Promise<void> => {
2005
- const legalName = 'example_legal_name'
2006
- const party: NonPersistedParty = {
2007
- uri: 'example.com',
2008
- partyType: {
2009
- type: PartyTypeType.ORGANIZATION,
2010
- origin: PartyOrigin.INTERNAL,
2011
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2012
- name: 'example_name',
2013
- },
2014
- contact: {
2015
- legalName,
2016
- displayName: 'example_display_name',
2017
- },
2018
- }
2019
-
2020
- const partyEntity: PartyEntity = partyEntityFrom(party)
2021
- await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2022
- transaction: true,
2023
- })
2024
-
2025
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
2026
- where: {
2027
- contact: {
2028
- legalName,
2029
- } as FindOptionsWhere<BaseContactEntity>, //NaturalPersonEntity | OrganizationEntity
2030
- },
2031
- })
2032
-
2033
- expect(fromDb).toBeDefined()
2034
- })
2035
-
2036
- it("Should enforce unique party id's for relationship sides", async (): Promise<void> => {
2037
- const party: NonPersistedParty = {
2038
- uri: 'example.com',
2039
- partyType: {
2040
- type: PartyTypeType.NATURAL_PERSON,
2041
- origin: PartyOrigin.INTERNAL,
2042
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2043
- name: 'example_name',
2044
- },
2045
- contact: {
2046
- firstName: 'example_first_name',
2047
- middleName: 'example_middle_name',
2048
- lastName: 'example_last_name',
2049
- displayName: 'example_display_name',
2050
- },
2051
- }
2052
-
2053
- const partyEntity: PartyEntity = partyEntityFrom(party)
2054
- const savedParty: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2055
- transaction: true,
2056
- })
2057
-
2058
- expect(savedParty).toBeDefined()
2059
-
2060
- const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
2061
- leftId: savedParty.id,
2062
- rightId: savedParty.id,
2063
- })
2064
-
2065
- await expect(dbConnection.getRepository(PartyRelationshipEntity).save(relationship)).rejects.toThrowError(
2066
- 'Cannot use the same id for both sides of the relationship',
2067
- )
2068
- })
2069
-
2070
- it('Should delete party relationship', async (): Promise<void> => {
2071
- const party1: NonPersistedParty = {
2072
- uri: 'example1.com',
2073
- partyType: {
2074
- type: PartyTypeType.NATURAL_PERSON,
2075
- origin: PartyOrigin.INTERNAL,
2076
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2077
- name: 'example_name1',
2078
- },
2079
- contact: {
2080
- firstName: 'example_first_name1',
2081
- middleName: 'example_middle_name1',
2082
- lastName: 'example_last_name1',
2083
- displayName: 'example_display_name1',
2084
- },
2085
- }
2086
-
2087
- const partyEntity1: PartyEntity = partyEntityFrom(party1)
2088
- const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
2089
- transaction: true,
2090
- })
2091
-
2092
- expect(savedParty1).toBeDefined()
2093
-
2094
- const party2: NonPersistedParty = {
2095
- uri: 'example2.com',
2096
- partyType: {
2097
- type: PartyTypeType.NATURAL_PERSON,
2098
- origin: PartyOrigin.INTERNAL,
2099
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
2100
- name: 'example_name2',
2101
- },
2102
- contact: {
2103
- firstName: 'example_first_name2',
2104
- middleName: 'example_middle_name2',
2105
- lastName: 'example_last_name2',
2106
- displayName: 'example_display_name2',
2107
- },
2108
- }
2109
-
2110
- const partyEntity2: PartyEntity = partyEntityFrom(party2)
2111
- const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
2112
- transaction: true,
2113
- })
2114
-
2115
- expect(savedParty2).toBeDefined()
2116
-
2117
- const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
2118
- leftId: savedParty1.id,
2119
- rightId: savedParty2.id,
2120
- })
2121
-
2122
- const savedRelationship: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
2123
- transaction: true,
2124
- })
2125
-
2126
- expect(savedRelationship).toBeDefined()
2127
-
2128
- await dbConnection.getRepository(PartyRelationshipEntity).delete({ id: savedRelationship.id })
2129
-
2130
- await expect(
2131
- await dbConnection.getRepository(PartyRelationshipEntity).findOne({
2132
- where: { id: savedRelationship.id },
2133
- }),
2134
- ).toBeNull()
2135
- })
2136
-
2137
- it('Should delete party type', async (): Promise<void> => {
2138
- const partyType: NonPersistedPartyType = {
2139
- type: PartyTypeType.NATURAL_PERSON,
2140
- origin: PartyOrigin.INTERNAL,
2141
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2142
- name: 'example_name',
2143
- }
2144
-
2145
- const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
2146
- const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
2147
-
2148
- expect(savedPartyType).toBeDefined()
2149
-
2150
- await dbConnection.getRepository(PartyTypeEntity).delete({ id: savedPartyType.id })
2151
-
2152
- await expect(
2153
- await dbConnection.getRepository(PartyTypeEntity).findOne({
2154
- where: { id: savedPartyType.id },
2155
- }),
2156
- ).toBeNull()
2157
- })
2158
-
2159
- it('Should not be able to remove party type when used by parties', async (): Promise<void> => {
2160
- const party: NonPersistedParty = {
2161
- uri: 'example.com',
2162
- partyType: {
2163
- type: PartyTypeType.NATURAL_PERSON,
2164
- origin: PartyOrigin.INTERNAL,
2165
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2166
- name: 'example_name',
2167
- },
2168
- contact: {
2169
- firstName: 'example_first_name',
2170
- middleName: 'example_middle_name',
2171
- lastName: 'example_last_name',
2172
- displayName: 'example_display_name',
2173
- },
2174
- }
2175
-
2176
- const partyEntity: PartyEntity = partyEntityFrom(party)
2177
- const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2178
- transaction: true,
2179
- })
2180
-
2181
- expect(savedParty).toBeDefined()
2182
-
2183
- await expect(dbConnection.getRepository(PartyTypeEntity).delete({ id: savedParty.partyType.id })).rejects.toThrowError(
2184
- 'FOREIGN KEY constraint failed',
2185
- )
2186
- })
2187
-
2188
- it('Should save party with existing party type', async (): Promise<void> => {
2189
- const partyType: NonPersistedPartyType = {
2190
- type: PartyTypeType.NATURAL_PERSON,
2191
- origin: PartyOrigin.INTERNAL,
2192
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2193
- name: 'example_name',
2194
- }
2195
-
2196
- const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
2197
- const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
2198
-
2199
- const party: NonPersistedParty = {
2200
- uri: 'example.com',
2201
- partyType: savedPartyType,
2202
- contact: {
2203
- firstName: 'example_first_name',
2204
- middleName: 'example_middle_name',
2205
- lastName: 'example_last_name',
2206
- displayName: 'example_display_name',
2207
- },
2208
- }
2209
-
2210
- const partyEntity: PartyEntity = partyEntityFrom(party)
2211
- partyEntity.partyType = savedPartyType
2212
- await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2213
- transaction: true,
2214
- })
2215
-
2216
- const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
2217
- where: { id: partyEntity.id },
2218
- })
2219
-
2220
- expect(fromDb).toBeDefined()
2221
- expect(fromDb?.partyType).toBeDefined()
2222
- expect(fromDb?.partyType.id).toEqual(savedPartyType.id)
2223
- expect(fromDb?.partyType.type).toEqual(savedPartyType.type)
2224
- expect(fromDb?.partyType.origin).toEqual(savedPartyType.origin)
2225
- expect(fromDb?.partyType.tenantId).toEqual(savedPartyType.tenantId)
2226
- expect(fromDb?.partyType.name).toEqual(savedPartyType.name)
2227
- })
2228
-
2229
- it('Should not update creation date when saving party type', async (): Promise<void> => {
2230
- const partyType: NonPersistedPartyType = {
2231
- type: PartyTypeType.NATURAL_PERSON,
2232
- origin: PartyOrigin.INTERNAL,
2233
- tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2234
- name: 'example_name',
2235
- }
2236
-
2237
- const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
2238
- const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
2239
- await dbConnection.getRepository(PartyTypeEntity).save({ ...savedPartyType, type: PartyTypeType.ORGANIZATION })
2240
-
2241
- const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
2242
- where: {
2243
- type: PartyTypeType.ORGANIZATION,
2244
- },
2245
- })
2246
-
2247
- expect(fromDb).toBeDefined()
2248
- expect(fromDb?.createdAt).toEqual(savedPartyType?.createdAt)
2249
- })
2250
-
2251
- it('Should save email electronic address to database', async (): Promise<void> => {
2252
- const electronicAddress: NonPersistedElectronicAddress = {
2253
- type: 'email',
2254
- electronicAddress: 'example_email_address',
2255
- }
2256
-
2257
- const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
2258
- const savedElectronicAddress: ElectronicAddressEntity = await dbConnection.getRepository(ElectronicAddressEntity).save(electronicAddressEntity, {
2259
- transaction: true,
2260
- })
2261
-
2262
- const fromDb: ElectronicAddressEntity | null = await dbConnection.getRepository(ElectronicAddressEntity).findOne({
2263
- where: { id: savedElectronicAddress.id },
2264
- })
2265
-
2266
- expect(fromDb).toBeDefined()
2267
- expect(fromDb?.type).toEqual(electronicAddress.type)
2268
- expect(fromDb?.electronicAddress).toEqual(electronicAddress.electronicAddress)
2269
- expect(fromDb?.createdAt).toBeDefined()
2270
- expect(fromDb?.lastUpdatedAt).toBeDefined()
2271
- })
2272
-
2273
- it('Should save phone electronic address to database', async (): Promise<void> => {
2274
- const electronicAddress: NonPersistedElectronicAddress = {
2275
- type: 'phone',
2276
- electronicAddress: 'example_phone_number',
2277
- }
2278
-
2279
- const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
2280
- const savedElectronicAddress: ElectronicAddressEntity = await dbConnection.getRepository(ElectronicAddressEntity).save(electronicAddressEntity, {
2281
- transaction: true,
2282
- })
2283
-
2284
- const fromDb: ElectronicAddressEntity | null = await dbConnection.getRepository(ElectronicAddressEntity).findOne({
2285
- where: { id: savedElectronicAddress.id },
2286
- })
2287
-
2288
- expect(fromDb).toBeDefined()
2289
- expect(fromDb?.type).toEqual(electronicAddress.type)
2290
- expect(fromDb?.electronicAddress).toEqual(electronicAddress.electronicAddress)
2291
- expect(fromDb?.createdAt).toBeDefined()
2292
- expect(fromDb?.lastUpdatedAt).toBeDefined()
2293
- })
2294
-
2295
- it('should throw error when saving electronic address with blank electronic address', async (): Promise<void> => {
2296
- const electronicAddress: NonPersistedElectronicAddress = {
2297
- type: 'email',
2298
- electronicAddress: '',
2299
- }
2300
-
2301
- const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
2302
-
2303
- await expect(dbConnection.getRepository(ElectronicAddressEntity).save(electronicAddressEntity)).rejects.toThrowError(
2304
- 'Blank electronic addresses are not allowed',
2305
- )
2306
- })
2307
-
2308
- it('Should save home physical address to database', async (): Promise<void> => {
2309
- const physicalAddress: NonPersistedPhysicalAddress = {
2310
- type: 'home',
2311
- streetName: 'example_street_name',
2312
- streetNumber: 'example_street_number',
2313
- buildingName: 'example_building_name',
2314
- postalCode: 'example_postal_code',
2315
- cityName: 'example_city_name',
2316
- provinceName: 'example_province_name',
2317
- countryCode: 'example_country_code',
2318
- }
2319
-
2320
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2321
- const savedPhysicalAddress: PhysicalAddressEntity = await dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity, {
2322
- transaction: true,
2323
- })
2324
-
2325
- const fromDb: PhysicalAddressEntity | null = await dbConnection.getRepository(PhysicalAddressEntity).findOne({
2326
- where: { id: savedPhysicalAddress.id },
2327
- })
2328
-
2329
- expect(fromDb).toBeDefined()
2330
- expect(fromDb?.type).toEqual(physicalAddress.type)
2331
- expect(fromDb?.streetName).toEqual(physicalAddress.streetName)
2332
- expect(fromDb?.streetNumber).toEqual(physicalAddress.streetNumber)
2333
- expect(fromDb?.buildingName).toEqual(physicalAddress.buildingName)
2334
- expect(fromDb?.postalCode).toEqual(physicalAddress.postalCode)
2335
- expect(fromDb?.cityName).toEqual(physicalAddress.cityName)
2336
- expect(fromDb?.provinceName).toEqual(physicalAddress.provinceName)
2337
- expect(fromDb?.countryCode).toEqual(physicalAddress.countryCode)
2338
- expect(fromDb?.createdAt).toBeDefined()
2339
- expect(fromDb?.lastUpdatedAt).toBeDefined()
2340
- })
2341
-
2342
- it('Should save visit physical address to database', async (): Promise<void> => {
2343
- const physicalAddress: NonPersistedPhysicalAddress = {
2344
- type: 'visit',
2345
- streetName: 'example_street_name',
2346
- streetNumber: 'example_street_number',
2347
- buildingName: 'example_building_name',
2348
- postalCode: 'example_postal_code',
2349
- cityName: 'example_city_name',
2350
- provinceName: 'example_province_name',
2351
- countryCode: 'example_country_code',
2352
- }
2353
-
2354
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2355
- const savedPhysicalAddress: PhysicalAddressEntity = await dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity, {
2356
- transaction: true,
2357
- })
2358
-
2359
- const fromDb: PhysicalAddressEntity | null = await dbConnection.getRepository(PhysicalAddressEntity).findOne({
2360
- where: { id: savedPhysicalAddress.id },
2361
- })
2362
-
2363
- expect(fromDb).toBeDefined()
2364
- expect(fromDb?.type).toEqual(physicalAddress.type)
2365
- expect(fromDb?.streetName).toEqual(physicalAddress.streetName)
2366
- expect(fromDb?.streetNumber).toEqual(physicalAddress.streetNumber)
2367
- expect(fromDb?.buildingName).toEqual(physicalAddress.buildingName)
2368
- expect(fromDb?.postalCode).toEqual(physicalAddress.postalCode)
2369
- expect(fromDb?.cityName).toEqual(physicalAddress.cityName)
2370
- expect(fromDb?.provinceName).toEqual(physicalAddress.provinceName)
2371
- expect(fromDb?.countryCode).toEqual(physicalAddress.countryCode)
2372
- expect(fromDb?.createdAt).toBeDefined()
2373
- expect(fromDb?.lastUpdatedAt).toBeDefined()
2374
- })
2375
-
2376
- it('Should save postal physical address to database', async (): Promise<void> => {
2377
- const physicalAddress: NonPersistedPhysicalAddress = {
2378
- type: 'postal',
2379
- streetName: 'example_street_name',
2380
- streetNumber: 'example_street_number',
2381
- buildingName: 'example_building_name',
2382
- postalCode: 'example_postal_code',
2383
- cityName: 'example_city_name',
2384
- provinceName: 'example_province_name',
2385
- countryCode: 'example_country_code',
2386
- }
2387
-
2388
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2389
- const savedPhysicalAddress: PhysicalAddressEntity = await dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity, {
2390
- transaction: true,
2391
- })
2392
-
2393
- const fromDb: PhysicalAddressEntity | null = await dbConnection.getRepository(PhysicalAddressEntity).findOne({
2394
- where: { id: savedPhysicalAddress.id },
2395
- })
2396
-
2397
- expect(fromDb).toBeDefined()
2398
- expect(fromDb?.type).toEqual(physicalAddress.type)
2399
- expect(fromDb?.streetName).toEqual(physicalAddress.streetName)
2400
- expect(fromDb?.streetNumber).toEqual(physicalAddress.streetNumber)
2401
- expect(fromDb?.buildingName).toEqual(physicalAddress.buildingName)
2402
- expect(fromDb?.postalCode).toEqual(physicalAddress.postalCode)
2403
- expect(fromDb?.cityName).toEqual(physicalAddress.cityName)
2404
- expect(fromDb?.provinceName).toEqual(physicalAddress.provinceName)
2405
- expect(fromDb?.countryCode).toEqual(physicalAddress.countryCode)
2406
- expect(fromDb?.createdAt).toBeDefined()
2407
- expect(fromDb?.lastUpdatedAt).toBeDefined()
2408
- })
2409
-
2410
- it('should throw error when saving physical address with blank street name', async (): Promise<void> => {
2411
- const physicalAddress: NonPersistedPhysicalAddress = {
2412
- type: 'home',
2413
- streetName: '',
2414
- streetNumber: 'example_street_number',
2415
- buildingName: 'example_building_name',
2416
- postalCode: 'example_postal_code',
2417
- cityName: 'example_city_name',
2418
- provinceName: 'example_province_name',
2419
- countryCode: 'example_country_code',
2420
- }
2421
-
2422
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2423
-
2424
- await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2425
- 'Blank street names are not allowed',
2426
- )
2427
- })
2428
-
2429
- it('should throw error when saving physical address with blank street number', async (): Promise<void> => {
2430
- const physicalAddress: NonPersistedPhysicalAddress = {
2431
- type: 'home',
2432
- streetName: 'example_street_name',
2433
- streetNumber: '',
2434
- buildingName: 'example_building_name',
2435
- postalCode: 'example_postal_code',
2436
- cityName: 'example_city_name',
2437
- provinceName: 'example_province_name',
2438
- countryCode: 'example_country_code',
2439
- }
2440
-
2441
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2442
-
2443
- await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2444
- 'Blank street numbers are not allowed',
2445
- )
2446
- })
2447
-
2448
- it('should throw error when saving physical address with blank building name', async (): Promise<void> => {
2449
- const physicalAddress: NonPersistedPhysicalAddress = {
2450
- type: 'home',
2451
- streetName: 'example_street_name',
2452
- streetNumber: 'example_street_number',
2453
- buildingName: '',
2454
- postalCode: 'example_postal_code',
2455
- cityName: 'example_city_name',
2456
- provinceName: 'example_province_name',
2457
- countryCode: 'example_country_code',
2458
- }
2459
-
2460
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2461
-
2462
- await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2463
- 'Blank building names are not allowed',
2464
- )
2465
- })
2466
-
2467
- it('should throw error when saving physical address with blank postal code', async (): Promise<void> => {
2468
- const physicalAddress: NonPersistedPhysicalAddress = {
2469
- type: 'home',
2470
- streetName: 'example_street_name',
2471
- streetNumber: 'example_street_number',
2472
- buildingName: 'example_building_name',
2473
- postalCode: '',
2474
- cityName: 'example_city_name',
2475
- provinceName: 'example_province_name',
2476
- countryCode: 'example_country_code',
2477
- }
2478
-
2479
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2480
-
2481
- await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2482
- 'Blank postal codes are not allowed',
2483
- )
2484
- })
2485
-
2486
- it('should throw error when saving physical address with blank city name', async (): Promise<void> => {
2487
- const physicalAddress: NonPersistedPhysicalAddress = {
2488
- type: 'home',
2489
- streetName: 'example_street_name',
2490
- streetNumber: 'example_street_number',
2491
- buildingName: 'example_building_name',
2492
- postalCode: 'example_postal_code',
2493
- cityName: '',
2494
- provinceName: 'example_province_name',
2495
- countryCode: 'example_country_code',
2496
- }
2497
-
2498
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2499
-
2500
- await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2501
- 'Blank city names are not allowed',
2502
- )
2503
- })
2504
-
2505
- it('should throw error when saving physical address with blank province name', async (): Promise<void> => {
2506
- const physicalAddress: NonPersistedPhysicalAddress = {
2507
- type: 'home',
2508
- streetName: 'example_street_name',
2509
- streetNumber: 'example_street_number',
2510
- buildingName: 'example_building_name',
2511
- postalCode: 'example_postal_code',
2512
- cityName: 'example_city_name',
2513
- provinceName: '',
2514
- countryCode: 'example_country_code',
2515
- }
2516
-
2517
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2518
-
2519
- await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2520
- 'Blank province names are not allowed',
2521
- )
2522
- })
2523
-
2524
- it('should throw error when saving physical address with blank country code', async (): Promise<void> => {
2525
- const physicalAddress: NonPersistedPhysicalAddress = {
2526
- type: 'home',
2527
- streetName: 'example_street_name',
2528
- streetNumber: 'example_street_number',
2529
- buildingName: 'example_building_name',
2530
- postalCode: 'example_postal_code',
2531
- cityName: 'example_city_name',
2532
- provinceName: 'example_province_name',
2533
- countryCode: '',
2534
- }
2535
-
2536
- const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2537
-
2538
- await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2539
- 'Blank country codes are not allowed',
2540
- )
2541
- })
2542
- })
1
+ import { DataSource, FindOptionsWhere } from 'typeorm'
2
+ import { DataStoreContactEntities, DataStoreMigrations, PartyOrigin } from '../index'
3
+ import { BaseContactEntity } from '../entities/contact/BaseContactEntity'
4
+ import { ConnectionEntity } from '../entities/contact/ConnectionEntity'
5
+ import { CorrelationIdentifierEntity } from '../entities/contact/CorrelationIdentifierEntity'
6
+ import { DidAuthConfigEntity } from '../entities/contact/DidAuthConfigEntity'
7
+ import { ElectronicAddressEntity } from '../entities/contact/ElectronicAddressEntity'
8
+ import { IdentityEntity } from '../entities/contact/IdentityEntity'
9
+ import { IdentityMetadataItemEntity } from '../entities/contact/IdentityMetadataItemEntity'
10
+ import { NaturalPersonEntity } from '../entities/contact/NaturalPersonEntity'
11
+ import { OpenIdConfigEntity } from '../entities/contact/OpenIdConfigEntity'
12
+ import { OrganizationEntity } from '../entities/contact/OrganizationEntity'
13
+ import { PartyEntity } from '../entities/contact/PartyEntity'
14
+ import { PartyRelationshipEntity } from '../entities/contact/PartyRelationshipEntity'
15
+ import { PartyTypeEntity } from '../entities/contact/PartyTypeEntity'
16
+ import { PhysicalAddressEntity } from '../entities/contact/PhysicalAddressEntity'
17
+ import {
18
+ ConnectionType,
19
+ CorrelationIdentifierType,
20
+ IdentityRole,
21
+ NaturalPerson,
22
+ NonPersistedConnection,
23
+ NonPersistedDidAuthConfig,
24
+ NonPersistedElectronicAddress,
25
+ NonPersistedIdentity,
26
+ NonPersistedNaturalPerson,
27
+ NonPersistedOpenIdConfig,
28
+ NonPersistedOrganization,
29
+ NonPersistedParty,
30
+ NonPersistedPartyType,
31
+ NonPersistedPhysicalAddress,
32
+ Organization,
33
+ PartyTypeType,
34
+ } from '../types'
35
+ import {
36
+ connectionEntityFrom,
37
+ didAuthConfigEntityFrom,
38
+ electronicAddressEntityFrom,
39
+ identityEntityFrom,
40
+ naturalPersonEntityFrom,
41
+ openIdConfigEntityFrom,
42
+ organizationEntityFrom,
43
+ partyEntityFrom,
44
+ partyRelationshipEntityFrom,
45
+ partyTypeEntityFrom,
46
+ physicalAddressEntityFrom,
47
+ } from '../utils/contact/MappingUtils'
48
+
49
+ // TODO write test adding two contacts reusing the same contactType
50
+
51
+ describe('Database entities tests', (): void => {
52
+ let dbConnection: DataSource
53
+
54
+ beforeEach(async (): Promise<void> => {
55
+ dbConnection = await new DataSource({
56
+ type: 'sqlite',
57
+ database: ':memory:',
58
+ logging: ['info'],
59
+ migrationsRun: false,
60
+ migrations: DataStoreMigrations,
61
+ synchronize: false,
62
+ entities: DataStoreContactEntities,
63
+ }).initialize()
64
+ await dbConnection.runMigrations()
65
+ expect(await dbConnection.showMigrations()).toBeFalsy()
66
+ })
67
+
68
+ afterEach(async (): Promise<void> => {
69
+ await (await dbConnection).destroy()
70
+ })
71
+
72
+ it('Should save person party to database', async (): Promise<void> => {
73
+ const party: NonPersistedParty = {
74
+ uri: 'example.com',
75
+ partyType: {
76
+ type: PartyTypeType.NATURAL_PERSON,
77
+ origin: PartyOrigin.INTERNAL,
78
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
79
+ name: 'example_name',
80
+ },
81
+ contact: {
82
+ firstName: 'example_first_name',
83
+ middleName: 'example_middle_name',
84
+ lastName: 'example_last_name',
85
+ displayName: 'example_display_name',
86
+ },
87
+ }
88
+
89
+ const partyEntity: PartyEntity = partyEntityFrom(party)
90
+ const savedParty: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
91
+ transaction: true,
92
+ })
93
+
94
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
95
+ where: { id: savedParty.id },
96
+ })
97
+
98
+ expect(fromDb).toBeDefined()
99
+ expect(fromDb?.identities?.length).toEqual(0)
100
+ expect(fromDb?.uri).toEqual(party.uri)
101
+ expect(fromDb?.partyType).toBeDefined()
102
+ expect(fromDb?.partyType.type).toEqual(party.partyType.type)
103
+ expect(fromDb?.partyType.origin).toEqual(party.partyType.origin)
104
+ expect(fromDb?.partyType.tenantId).toEqual(party.partyType.tenantId)
105
+ expect(fromDb?.partyType.name).toEqual(party.partyType.name)
106
+ expect(fromDb?.contact).toBeDefined()
107
+ expect((<NaturalPersonEntity>fromDb?.contact).firstName).toEqual((<NaturalPerson>party.contact).firstName)
108
+ expect((<NaturalPersonEntity>fromDb?.contact).middleName).toEqual((<NaturalPerson>party.contact).middleName)
109
+ expect((<NaturalPersonEntity>fromDb?.contact).lastName).toEqual((<NaturalPerson>party.contact).lastName)
110
+ expect((<NaturalPersonEntity>fromDb?.contact).displayName).toEqual((<NaturalPerson>party.contact).displayName)
111
+ })
112
+
113
+ it('Should save organization party to database', async (): Promise<void> => {
114
+ const party: NonPersistedParty = {
115
+ uri: 'example.com',
116
+ partyType: {
117
+ type: PartyTypeType.ORGANIZATION,
118
+ origin: PartyOrigin.INTERNAL,
119
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
120
+ name: 'example_name',
121
+ },
122
+ contact: {
123
+ legalName: 'example_legal_name',
124
+ displayName: 'example_display_name',
125
+ },
126
+ }
127
+
128
+ const partyEntity: PartyEntity = partyEntityFrom(party)
129
+ const savedParty: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
130
+ transaction: true,
131
+ })
132
+
133
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
134
+ where: { id: savedParty.id },
135
+ })
136
+
137
+ expect(fromDb).toBeDefined()
138
+ expect(fromDb?.identities?.length).toEqual(0)
139
+ expect(fromDb?.uri).toEqual(party.uri)
140
+ expect(fromDb?.partyType).toBeDefined()
141
+ expect(fromDb?.partyType.type).toEqual(party.partyType.type)
142
+ expect(fromDb?.partyType.origin).toEqual(party.partyType.origin)
143
+ expect(fromDb?.partyType.tenantId).toEqual(party.partyType.tenantId)
144
+ expect(fromDb?.partyType.name).toEqual(party.partyType.name)
145
+ expect(fromDb?.contact).toBeDefined()
146
+ expect((<OrganizationEntity>fromDb?.contact).legalName).toEqual((<Organization>party.contact).legalName)
147
+ expect((<OrganizationEntity>fromDb?.contact).displayName).toEqual((<Organization>party.contact).displayName)
148
+ })
149
+
150
+ it('Should result in party relationship for the owner side only', async (): Promise<void> => {
151
+ const party1: NonPersistedParty = {
152
+ uri: 'example1.com',
153
+ partyType: {
154
+ type: PartyTypeType.NATURAL_PERSON,
155
+ origin: PartyOrigin.INTERNAL,
156
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
157
+ name: 'example_name1',
158
+ },
159
+ contact: {
160
+ firstName: 'example_first_name1',
161
+ middleName: 'example_middle_name1',
162
+ lastName: 'example_last_name1',
163
+ displayName: 'example_display_name1',
164
+ },
165
+ }
166
+
167
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
168
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
169
+ transaction: true,
170
+ })
171
+
172
+ const party2: NonPersistedParty = {
173
+ uri: 'example2.com',
174
+ partyType: {
175
+ type: PartyTypeType.NATURAL_PERSON,
176
+ origin: PartyOrigin.INTERNAL,
177
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
178
+ name: 'example_name2',
179
+ },
180
+ contact: {
181
+ firstName: 'example_first_name2',
182
+ middleName: 'example_middle_name2',
183
+ lastName: 'example_last_name2',
184
+ displayName: 'example_display_name2',
185
+ },
186
+ }
187
+
188
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
189
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
190
+ transaction: true,
191
+ })
192
+
193
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
194
+ leftId: savedParty1.id,
195
+ rightId: savedParty2.id,
196
+ })
197
+
198
+ await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
199
+ transaction: true,
200
+ })
201
+
202
+ const fromDb1: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
203
+ where: { id: savedParty1.id },
204
+ })
205
+
206
+ const fromDb2: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
207
+ where: { id: savedParty2.id },
208
+ })
209
+
210
+ expect(fromDb1).toBeDefined()
211
+ expect(fromDb1?.relationships.length).toEqual(1)
212
+ expect(fromDb2).toBeDefined()
213
+ expect(fromDb2?.relationships.length).toEqual(0)
214
+ })
215
+
216
+ it('should throw error when saving person party with blank first name', async (): Promise<void> => {
217
+ const party: NonPersistedParty = {
218
+ uri: 'example.com',
219
+ partyType: {
220
+ type: PartyTypeType.NATURAL_PERSON,
221
+ origin: PartyOrigin.INTERNAL,
222
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
223
+ name: 'example_name',
224
+ },
225
+ contact: {
226
+ firstName: '',
227
+ middleName: 'example_middle_name1',
228
+ lastName: 'example_last_name1',
229
+ displayName: 'example_display_name1',
230
+ },
231
+ }
232
+
233
+ const partyEntity: PartyEntity = partyEntityFrom(party)
234
+
235
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank first names are not allowed')
236
+ })
237
+
238
+ it('should throw error when saving person party with blank middle name', async (): Promise<void> => {
239
+ const party: NonPersistedParty = {
240
+ uri: 'example.com',
241
+ partyType: {
242
+ type: PartyTypeType.NATURAL_PERSON,
243
+ origin: PartyOrigin.EXTERNAL,
244
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
245
+ name: 'example_name',
246
+ },
247
+ contact: {
248
+ firstName: 'example_first_name',
249
+ middleName: '',
250
+ lastName: 'example_last_name',
251
+ displayName: 'example_display_name',
252
+ },
253
+ }
254
+
255
+ const partyEntity: PartyEntity = partyEntityFrom(party)
256
+
257
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank middle names are not allowed')
258
+ })
259
+
260
+ it('should throw error when saving person party with blank last name', async (): Promise<void> => {
261
+ const party: NonPersistedParty = {
262
+ uri: 'example.com',
263
+ partyType: {
264
+ type: PartyTypeType.NATURAL_PERSON,
265
+ origin: PartyOrigin.EXTERNAL,
266
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
267
+ name: 'example_name',
268
+ },
269
+ contact: {
270
+ firstName: 'example_first_name',
271
+ middleName: 'example_middle_name',
272
+ lastName: '',
273
+ displayName: 'example_display_name',
274
+ },
275
+ }
276
+
277
+ const partyEntity: PartyEntity = partyEntityFrom(party)
278
+
279
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank last names are not allowed')
280
+ })
281
+
282
+ it('should throw error when saving person party with blank display name', async (): Promise<void> => {
283
+ const party: NonPersistedParty = {
284
+ uri: 'example.com',
285
+ partyType: {
286
+ type: PartyTypeType.NATURAL_PERSON,
287
+ origin: PartyOrigin.EXTERNAL,
288
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
289
+ name: 'example_name',
290
+ },
291
+ contact: {
292
+ firstName: 'example_first_name',
293
+ middleName: 'example_middle_name',
294
+ lastName: 'example_last_name',
295
+ displayName: '',
296
+ },
297
+ }
298
+
299
+ const partyEntity: PartyEntity = partyEntityFrom(party)
300
+
301
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank display names are not allowed')
302
+ })
303
+
304
+ it('should throw error when saving organization party with blank legal name', async (): Promise<void> => {
305
+ const party: NonPersistedParty = {
306
+ uri: 'example.com',
307
+ partyType: {
308
+ type: PartyTypeType.ORGANIZATION,
309
+ origin: PartyOrigin.INTERNAL,
310
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
311
+ name: 'example_name',
312
+ },
313
+ contact: {
314
+ legalName: '',
315
+ displayName: 'example_legal_name',
316
+ },
317
+ }
318
+
319
+ const partyEntity: PartyEntity = partyEntityFrom(party)
320
+
321
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank legal names are not allowed')
322
+ })
323
+
324
+ it('should throw error when saving organization party with blank display name', async (): Promise<void> => {
325
+ const party: NonPersistedParty = {
326
+ uri: 'example.com',
327
+ partyType: {
328
+ type: PartyTypeType.ORGANIZATION,
329
+ origin: PartyOrigin.INTERNAL,
330
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
331
+ name: 'example_name',
332
+ },
333
+ contact: {
334
+ legalName: 'example_first_name',
335
+ displayName: '',
336
+ },
337
+ }
338
+
339
+ const partyEntity: PartyEntity = partyEntityFrom(party)
340
+
341
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank display names are not allowed')
342
+ })
343
+
344
+ it('should throw error when saving party with blank party type name', async (): Promise<void> => {
345
+ const party: NonPersistedParty = {
346
+ uri: 'example.com',
347
+ partyType: {
348
+ type: PartyTypeType.NATURAL_PERSON,
349
+ origin: PartyOrigin.EXTERNAL,
350
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
351
+ name: '',
352
+ },
353
+ contact: {
354
+ firstName: 'example_first_name',
355
+ middleName: 'example_middle_name',
356
+ lastName: 'example_last_name',
357
+ displayName: 'example_display_name',
358
+ },
359
+ }
360
+
361
+ const partyEntity: PartyEntity = partyEntityFrom(party)
362
+
363
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank names are not allowed')
364
+ })
365
+
366
+ it('should throw error when saving party with blank party type description', async (): Promise<void> => {
367
+ const party: NonPersistedParty = {
368
+ uri: 'example.com',
369
+ partyType: {
370
+ type: PartyTypeType.NATURAL_PERSON,
371
+ origin: PartyOrigin.INTERNAL,
372
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
373
+ name: 'example_name',
374
+ description: '',
375
+ },
376
+ contact: {
377
+ firstName: 'example_first_name',
378
+ middleName: 'example_middle_name',
379
+ lastName: 'example_last_name',
380
+ displayName: 'example_display_name',
381
+ },
382
+ }
383
+
384
+ const partyEntity: PartyEntity = partyEntityFrom(party)
385
+
386
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank descriptions are not allowed')
387
+ })
388
+
389
+ it('should throw error when saving party with blank party type tenant id', async (): Promise<void> => {
390
+ const party: NonPersistedParty = {
391
+ uri: 'example.com',
392
+ partyType: {
393
+ type: PartyTypeType.NATURAL_PERSON,
394
+ origin: PartyOrigin.EXTERNAL,
395
+ tenantId: '',
396
+ name: 'example_name',
397
+ },
398
+ contact: {
399
+ firstName: 'example_first_name',
400
+ middleName: 'example_middle_name',
401
+ lastName: 'example_last_name',
402
+ displayName: 'example_display_name',
403
+ },
404
+ }
405
+
406
+ const partyEntity: PartyEntity = partyEntityFrom(party)
407
+
408
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError("Blank tenant id's are not allowed")
409
+ })
410
+
411
+ it('Should enforce unique alias for an identity', async (): Promise<void> => {
412
+ const alias = 'non_unique_alias'
413
+ const identity1: NonPersistedIdentity = {
414
+ alias,
415
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
416
+ identifier: {
417
+ type: CorrelationIdentifierType.DID,
418
+ correlationId: 'unique_correlationId1',
419
+ },
420
+ }
421
+ const identity1Entity: IdentityEntity = identityEntityFrom(identity1)
422
+ await dbConnection.getRepository(IdentityEntity).save(identity1Entity)
423
+
424
+ const identity2: NonPersistedIdentity = {
425
+ alias: alias,
426
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
427
+ identifier: {
428
+ type: CorrelationIdentifierType.DID,
429
+ correlationId: 'unique_correlationId2',
430
+ },
431
+ }
432
+ const identity2Entity: IdentityEntity = identityEntityFrom(identity2)
433
+ await expect(dbConnection.getRepository(IdentityEntity).save(identity2Entity)).rejects.toThrowError(
434
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: Identity.alias',
435
+ )
436
+ })
437
+
438
+ it('Should enforce unique correlationId for a identity', async (): Promise<void> => {
439
+ const correlationId = 'non_unique_correlationId'
440
+ const identity1: NonPersistedIdentity = {
441
+ alias: 'unique_alias1',
442
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
443
+ identifier: {
444
+ type: CorrelationIdentifierType.DID,
445
+ correlationId,
446
+ },
447
+ }
448
+ const identity1Entity: IdentityEntity = identityEntityFrom(identity1)
449
+ await dbConnection.getRepository(IdentityEntity).save(identity1Entity)
450
+
451
+ const identity2: NonPersistedIdentity = {
452
+ alias: 'unique_alias2',
453
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
454
+ identifier: {
455
+ type: CorrelationIdentifierType.DID,
456
+ correlationId,
457
+ },
458
+ }
459
+ const identity2Entity: IdentityEntity = identityEntityFrom(identity2)
460
+ await expect(dbConnection.getRepository(IdentityEntity).save(identity2Entity)).rejects.toThrowError(
461
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: CorrelationIdentifier.correlation_id',
462
+ )
463
+ })
464
+
465
+ it('Should save identity to database', async (): Promise<void> => {
466
+ const correlationId = 'example_did'
467
+ const identity: NonPersistedIdentity = {
468
+ alias: correlationId,
469
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
470
+ identifier: {
471
+ type: CorrelationIdentifierType.DID,
472
+ correlationId,
473
+ },
474
+ }
475
+
476
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
477
+
478
+ await dbConnection.getRepository(IdentityEntity).save(identityEntity)
479
+
480
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
481
+ where: {
482
+ identifier: {
483
+ correlationId,
484
+ },
485
+ },
486
+ })
487
+
488
+ expect(fromDb).toBeDefined()
489
+ expect(fromDb?.connection).toBeNull()
490
+ expect(fromDb?.identifier).toBeDefined()
491
+ expect(fromDb?.identifier.correlationId).toEqual(identity.identifier.correlationId)
492
+ expect(fromDb?.identifier.type).toEqual(identity.identifier.type)
493
+ })
494
+
495
+ it('should throw error when saving identity with blank alias', async (): Promise<void> => {
496
+ const identity: NonPersistedIdentity = {
497
+ alias: '',
498
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
499
+ identifier: {
500
+ type: CorrelationIdentifierType.DID,
501
+ correlationId: 'example_did',
502
+ },
503
+ }
504
+
505
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
506
+
507
+ await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank aliases are not allowed')
508
+ })
509
+
510
+ it('should throw error when saving identity with blank correlation id', async (): Promise<void> => {
511
+ const identity: NonPersistedIdentity = {
512
+ alias: 'example_did',
513
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
514
+ identifier: {
515
+ type: CorrelationIdentifierType.DID,
516
+ correlationId: '',
517
+ },
518
+ }
519
+
520
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
521
+
522
+ await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank correlation ids are not allowed')
523
+ })
524
+
525
+ it('should throw error when saving identity with blank metadata label', async (): Promise<void> => {
526
+ const correlationId = 'example_did'
527
+ const identity: NonPersistedIdentity = {
528
+ alias: correlationId,
529
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
530
+ identifier: {
531
+ type: CorrelationIdentifierType.DID,
532
+ correlationId,
533
+ },
534
+ metadata: [
535
+ {
536
+ label: '',
537
+ value: 'example_value',
538
+ },
539
+ ],
540
+ }
541
+
542
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
543
+
544
+ await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank metadata labels are not allowed')
545
+ })
546
+
547
+ it('should throw error when saving identity with blank metadata value', async (): Promise<void> => {
548
+ const correlationId = 'example_did'
549
+ const identity: NonPersistedIdentity = {
550
+ alias: correlationId,
551
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
552
+ identifier: {
553
+ type: CorrelationIdentifierType.DID,
554
+ correlationId,
555
+ },
556
+ metadata: [
557
+ {
558
+ label: 'example_label',
559
+ value: '',
560
+ },
561
+ ],
562
+ }
563
+
564
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
565
+
566
+ await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank metadata values are not allowed')
567
+ })
568
+
569
+ it('Should save identity with openid connection to database', async (): Promise<void> => {
570
+ const correlationId = 'example.com'
571
+ const identity: NonPersistedIdentity = {
572
+ alias: correlationId,
573
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
574
+ identifier: {
575
+ type: CorrelationIdentifierType.URL,
576
+ correlationId,
577
+ },
578
+ connection: {
579
+ type: ConnectionType.OPENID_CONNECT,
580
+ config: {
581
+ clientId: '138d7bf8-c930-4c6e-b928-97d3a4928b01',
582
+ clientSecret: '03b3955f-d020-4f2a-8a27-4e452d4e27a0',
583
+ scopes: ['auth'],
584
+ issuer: 'https://example.com/app-test',
585
+ redirectUrl: 'app:/callback',
586
+ dangerouslyAllowInsecureHttpRequests: true,
587
+ clientAuthMethod: <const>'post',
588
+ },
589
+ },
590
+ }
591
+
592
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
593
+
594
+ await dbConnection.getRepository(IdentityEntity).save(identityEntity)
595
+
596
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
597
+ where: {
598
+ identifier: {
599
+ correlationId,
600
+ },
601
+ },
602
+ })
603
+
604
+ expect(fromDb).toBeDefined()
605
+ expect(fromDb?.connection).toBeDefined()
606
+ expect(fromDb?.identifier).toBeDefined()
607
+ expect(fromDb?.identifier.correlationId).toEqual(identity.identifier.correlationId)
608
+ expect(fromDb?.identifier.type).toEqual(identity.identifier.type)
609
+ expect(fromDb?.connection?.type).toEqual(identity.connection?.type)
610
+ expect(fromDb?.connection?.config).toBeDefined()
611
+ expect((<OpenIdConfigEntity>fromDb?.connection?.config).clientId).toEqual((<NonPersistedOpenIdConfig>identity.connection?.config).clientId)
612
+ })
613
+
614
+ it('Should save identity with didauth connection to database', async (): Promise<void> => {
615
+ const correlationId = 'example.com'
616
+ const identity: NonPersistedIdentity = {
617
+ alias: correlationId,
618
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
619
+ identifier: {
620
+ type: CorrelationIdentifierType.URL,
621
+ correlationId,
622
+ },
623
+ connection: {
624
+ type: ConnectionType.SIOPv2,
625
+ config: {
626
+ identifier: {
627
+ did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
628
+ provider: 'test_provider',
629
+ keys: [],
630
+ services: [],
631
+ },
632
+ redirectUrl: 'https://example.com',
633
+ stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
634
+ sessionId: 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
635
+ },
636
+ },
637
+ }
638
+
639
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
640
+
641
+ await dbConnection.getRepository(IdentityEntity).save(identityEntity)
642
+
643
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
644
+ where: {
645
+ identifier: {
646
+ correlationId,
647
+ },
648
+ },
649
+ })
650
+
651
+ expect(fromDb).toBeDefined()
652
+ expect(fromDb?.connection).toBeDefined()
653
+ expect(fromDb?.identifier).toBeDefined()
654
+ expect(fromDb?.identifier.correlationId).toEqual(identity.identifier.correlationId)
655
+ expect(fromDb?.identifier.type).toEqual(identity.identifier.type)
656
+ expect(fromDb?.connection?.type).toEqual(identity.connection?.type)
657
+ expect(fromDb?.connection?.config).toBeDefined()
658
+ expect((<DidAuthConfigEntity>fromDb?.connection?.config).identifier).toEqual(
659
+ (<NonPersistedDidAuthConfig>identity.connection?.config).identifier.did,
660
+ )
661
+ })
662
+
663
+ it('Should save connection with openid config to database', async (): Promise<void> => {
664
+ const connection: NonPersistedConnection = {
665
+ type: ConnectionType.OPENID_CONNECT,
666
+ config: {
667
+ clientId: '138d7bf8-c930-4c6e-b928-97d3a4928b01',
668
+ clientSecret: '03b3955f-d020-4f2a-8a27-4e452d4e27a0',
669
+ scopes: ['auth'],
670
+ issuer: 'https://example.com/app-test',
671
+ redirectUrl: 'app:/callback',
672
+ dangerouslyAllowInsecureHttpRequests: true,
673
+ clientAuthMethod: <const>'post',
674
+ },
675
+ }
676
+ const connectionEntity: ConnectionEntity = connectionEntityFrom(connection)
677
+ await dbConnection.getRepository(ConnectionEntity).save(connectionEntity, {
678
+ transaction: true,
679
+ })
680
+
681
+ const fromDb: ConnectionEntity | null = await dbConnection.getRepository(ConnectionEntity).findOne({
682
+ where: { type: connection.type },
683
+ })
684
+
685
+ expect(fromDb).toBeDefined()
686
+
687
+ const fromDbConfig: OpenIdConfigEntity | null = await dbConnection.getRepository(OpenIdConfigEntity).findOne({
688
+ where: { id: fromDb?.id },
689
+ })
690
+
691
+ expect(fromDbConfig).toBeDefined()
692
+ expect(fromDb?.type).toEqual(connection.type)
693
+ expect(fromDb?.config).toBeDefined()
694
+ expect((<OpenIdConfigEntity>fromDb?.config).clientId).toEqual((<NonPersistedOpenIdConfig>connection.config).clientId)
695
+ })
696
+
697
+ it('Should save connection with didauth config to database', async (): Promise<void> => {
698
+ const connection: NonPersistedConnection = {
699
+ type: ConnectionType.SIOPv2,
700
+ config: {
701
+ identifier: {
702
+ did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
703
+ provider: 'test_provider',
704
+ keys: [],
705
+ services: [],
706
+ },
707
+ redirectUrl: 'https://example.com',
708
+ stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
709
+ sessionId: 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
710
+ },
711
+ }
712
+ const connectionEntity: ConnectionEntity = connectionEntityFrom(connection)
713
+ await dbConnection.getRepository(ConnectionEntity).save(connectionEntity, {
714
+ transaction: true,
715
+ })
716
+
717
+ const fromDb: ConnectionEntity | null = await dbConnection.getRepository(ConnectionEntity).findOne({
718
+ where: { type: connection.type },
719
+ })
720
+
721
+ expect(fromDb).toBeDefined()
722
+
723
+ const fromDbConfig: DidAuthConfigEntity | null = await dbConnection.getRepository(DidAuthConfigEntity).findOne({
724
+ where: { id: fromDb?.id },
725
+ })
726
+
727
+ expect(fromDbConfig).toBeDefined()
728
+ expect(fromDb?.type).toEqual(connection.type)
729
+ expect(fromDb?.config).toBeDefined()
730
+ expect((<DidAuthConfigEntity>fromDb?.config).identifier).toEqual((<NonPersistedDidAuthConfig>connection.config).identifier.did)
731
+ })
732
+
733
+ it('Should save openid config to database', async (): Promise<void> => {
734
+ const clientId = '138d7bf8-c930-4c6e-b928-97d3a4928b01'
735
+ const config: NonPersistedOpenIdConfig = {
736
+ clientId,
737
+ clientSecret: '03b3955f-d020-4f2a-8a27-4e452d4e27a0',
738
+ scopes: ['auth'],
739
+ issuer: 'https://example.com/app-test',
740
+ redirectUrl: 'app:/callback',
741
+ dangerouslyAllowInsecureHttpRequests: true,
742
+ clientAuthMethod: <const>'post',
743
+ }
744
+
745
+ const configEntity: OpenIdConfigEntity = openIdConfigEntityFrom(config)
746
+ await dbConnection.getRepository(OpenIdConfigEntity).save(configEntity, {
747
+ transaction: true,
748
+ })
749
+
750
+ const fromDb: OpenIdConfigEntity | null = await dbConnection.getRepository(OpenIdConfigEntity).findOne({
751
+ where: { clientId: config.clientId },
752
+ })
753
+
754
+ expect(fromDb).toBeDefined()
755
+ expect((<OpenIdConfigEntity>fromDb).clientId).toEqual(config.clientId)
756
+ })
757
+
758
+ it('Should save didauth config to database', async (): Promise<void> => {
759
+ const sessionId = 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01'
760
+ const config: NonPersistedDidAuthConfig = {
761
+ identifier: {
762
+ did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
763
+ provider: 'test_provider',
764
+ keys: [],
765
+ services: [],
766
+ },
767
+ redirectUrl: 'https://example.com',
768
+ stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
769
+ sessionId,
770
+ }
771
+
772
+ const configEntity: DidAuthConfigEntity = didAuthConfigEntityFrom(config)
773
+ await dbConnection.getRepository(DidAuthConfigEntity).save(configEntity, {
774
+ transaction: true,
775
+ })
776
+
777
+ const fromDb: DidAuthConfigEntity | null = await dbConnection.getRepository(DidAuthConfigEntity).findOne({
778
+ where: { sessionId: config.sessionId },
779
+ })
780
+
781
+ expect(fromDb).toBeDefined()
782
+ expect((<DidAuthConfigEntity>fromDb).identifier).toEqual(config.identifier.did)
783
+ })
784
+
785
+ it('Should delete party and all child relations', async (): Promise<void> => {
786
+ const party1: NonPersistedParty = {
787
+ uri: 'example.com',
788
+ partyType: {
789
+ type: PartyTypeType.NATURAL_PERSON,
790
+ origin: PartyOrigin.INTERNAL,
791
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
792
+ name: 'example_name1',
793
+ },
794
+ contact: {
795
+ firstName: 'example_first_name1',
796
+ middleName: 'example_middle_name1',
797
+ lastName: 'example_last_name1',
798
+ displayName: 'example_display_name1',
799
+ },
800
+ }
801
+
802
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
803
+ const savedParty1: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity1)
804
+
805
+ expect(savedParty1).toBeDefined()
806
+
807
+ const party2: NonPersistedParty = {
808
+ uri: 'example.com',
809
+ partyType: {
810
+ type: PartyTypeType.NATURAL_PERSON,
811
+ origin: PartyOrigin.INTERNAL,
812
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
813
+ name: 'example_name2',
814
+ },
815
+ contact: {
816
+ firstName: 'example_first_name2',
817
+ middleName: 'example_middle_name2',
818
+ lastName: 'example_last_name2',
819
+ displayName: 'example_display_name2',
820
+ },
821
+ }
822
+
823
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
824
+ const savedParty2: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity2)
825
+
826
+ expect(savedParty2).toBeDefined()
827
+
828
+ const correlationId = 'relation_example.com'
829
+ const identity: NonPersistedIdentity = {
830
+ alias: correlationId,
831
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
832
+ identifier: {
833
+ type: CorrelationIdentifierType.URL,
834
+ correlationId,
835
+ },
836
+ connection: {
837
+ type: ConnectionType.OPENID_CONNECT,
838
+ config: {
839
+ clientId: '138d7bf8-c930-4c6e-b928-97d3a4928b01',
840
+ clientSecret: '03b3955f-d020-4f2a-8a27-4e452d4e27a0',
841
+ scopes: ['auth'],
842
+ issuer: 'https://example.com/app-test',
843
+ redirectUrl: 'app:/callback',
844
+ dangerouslyAllowInsecureHttpRequests: true,
845
+ clientAuthMethod: <const>'post',
846
+ },
847
+ },
848
+ metadata: [
849
+ {
850
+ label: 'example_label',
851
+ value: 'example_value',
852
+ },
853
+ ],
854
+ }
855
+
856
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
857
+ identityEntity.party = savedParty1
858
+
859
+ const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
860
+
861
+ expect(savedIdentity).toBeDefined()
862
+
863
+ const electronicAddress: NonPersistedElectronicAddress = {
864
+ type: 'email',
865
+ electronicAddress: 'example_electronic_address',
866
+ }
867
+ const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
868
+ electronicAddressEntity.party = savedParty1
869
+
870
+ const savedElectronicAddress: ElectronicAddressEntity | null = await dbConnection
871
+ .getRepository(ElectronicAddressEntity)
872
+ .save(electronicAddressEntity)
873
+
874
+ expect(savedElectronicAddress).toBeDefined()
875
+
876
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
877
+ leftId: savedParty1.id,
878
+ rightId: savedParty2.id,
879
+ })
880
+
881
+ const savedRelationship: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
882
+ transaction: true,
883
+ })
884
+
885
+ expect(savedRelationship).toBeDefined()
886
+
887
+ expect(
888
+ await dbConnection.getRepository(PartyEntity).findOne({
889
+ where: { id: savedParty1.id },
890
+ }),
891
+ ).toBeDefined()
892
+
893
+ await dbConnection.getRepository(PartyEntity).delete({ id: savedParty1.id })
894
+
895
+ // check party
896
+ await expect(
897
+ await dbConnection.getRepository(PartyEntity).findOne({
898
+ where: { id: savedParty1.id },
899
+ }),
900
+ ).toBeNull()
901
+
902
+ // check identity
903
+ expect(
904
+ await dbConnection.getRepository(IdentityEntity).findOne({
905
+ where: { id: savedParty1.id },
906
+ }),
907
+ ).toBeNull()
908
+
909
+ // check identity identifier
910
+ expect(
911
+ await dbConnection.getRepository(CorrelationIdentifierEntity).findOne({
912
+ where: { id: savedIdentity.identifier.id },
913
+ }),
914
+ ).toBeNull()
915
+
916
+ // check identity connection
917
+ expect(
918
+ await dbConnection.getRepository(ConnectionEntity).findOne({
919
+ where: { id: savedIdentity.connection!.id },
920
+ }),
921
+ ).toBeNull()
922
+
923
+ // check connection config
924
+ expect(
925
+ await dbConnection.getRepository(OpenIdConfigEntity).findOne({
926
+ where: { id: savedIdentity.connection!.config.id },
927
+ }),
928
+ ).toBeNull()
929
+
930
+ // check identity metadata
931
+ expect(
932
+ await dbConnection.getRepository(IdentityMetadataItemEntity).findOne({
933
+ where: { id: savedIdentity.metadata![0].id },
934
+ }),
935
+ ).toBeNull()
936
+
937
+ // check electronic address
938
+ expect(
939
+ await dbConnection.getRepository(ElectronicAddressEntity).findOne({
940
+ where: { id: savedParty1.id },
941
+ }),
942
+ ).toBeNull()
943
+
944
+ // check contact
945
+ expect(
946
+ await dbConnection.getRepository(BaseContactEntity).findOne({
947
+ where: { id: savedParty1.contact.id },
948
+ }),
949
+ ).toBeNull()
950
+
951
+ // check party type
952
+ expect(
953
+ await dbConnection.getRepository(PartyTypeEntity).findOne({
954
+ where: { id: savedParty1.partyType.id },
955
+ }),
956
+ ).toBeDefined()
957
+
958
+ // check relation
959
+ expect(
960
+ await dbConnection.getRepository(PartyRelationshipEntity).findOne({
961
+ where: { id: savedRelationship.id },
962
+ }),
963
+ ).toBeNull()
964
+ })
965
+
966
+ it('Should delete identity and all child relations', async (): Promise<void> => {
967
+ const party: NonPersistedParty = {
968
+ uri: 'example.com',
969
+ partyType: {
970
+ type: PartyTypeType.NATURAL_PERSON,
971
+ origin: PartyOrigin.EXTERNAL,
972
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
973
+ name: 'example_name',
974
+ },
975
+ contact: {
976
+ firstName: 'example_first_name',
977
+ middleName: 'example_middle_name',
978
+ lastName: 'example_last_name',
979
+ displayName: 'example_display_name',
980
+ },
981
+ }
982
+
983
+ const partyEntity: PartyEntity = partyEntityFrom(party)
984
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
985
+
986
+ expect(savedParty).toBeDefined()
987
+
988
+ const correlationId = 'relation_example.com'
989
+ const identity: NonPersistedIdentity = {
990
+ alias: correlationId,
991
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
992
+ identifier: {
993
+ type: CorrelationIdentifierType.URL,
994
+ correlationId,
995
+ },
996
+ connection: {
997
+ type: ConnectionType.SIOPv2,
998
+ config: {
999
+ identifier: {
1000
+ did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
1001
+ provider: 'test_provider',
1002
+ keys: [],
1003
+ services: [],
1004
+ },
1005
+ redirectUrl: 'https://example.com',
1006
+ stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
1007
+ sessionId: 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
1008
+ },
1009
+ },
1010
+ metadata: [
1011
+ {
1012
+ label: 'example_label',
1013
+ value: 'example_value',
1014
+ },
1015
+ ],
1016
+ }
1017
+
1018
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
1019
+ identityEntity.party = savedParty
1020
+
1021
+ const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1022
+
1023
+ expect(
1024
+ await dbConnection.getRepository(PartyEntity).findOne({
1025
+ where: { id: savedParty.id },
1026
+ }),
1027
+ ).toBeDefined()
1028
+
1029
+ await dbConnection.getRepository(IdentityEntity).delete({ id: savedIdentity.id })
1030
+
1031
+ // check identity
1032
+ expect(
1033
+ await dbConnection.getRepository(IdentityEntity).findOne({
1034
+ where: { alias: correlationId },
1035
+ }),
1036
+ ).toBeNull()
1037
+
1038
+ // check identity identifier
1039
+ expect(
1040
+ await dbConnection.getRepository(CorrelationIdentifierEntity).findOne({
1041
+ where: { id: savedIdentity.identifier.id },
1042
+ }),
1043
+ ).toBeNull()
1044
+
1045
+ // check identity connection
1046
+ expect(
1047
+ await dbConnection.getRepository(ConnectionEntity).findOne({
1048
+ where: { id: savedIdentity.connection!.id },
1049
+ }),
1050
+ ).toBeNull()
1051
+
1052
+ // check connection config
1053
+ expect(
1054
+ await dbConnection.getRepository(OpenIdConfigEntity).findOne({
1055
+ where: { id: savedIdentity.connection!.config.id },
1056
+ }),
1057
+ ).toBeNull()
1058
+
1059
+ // check identity metadata
1060
+ expect(
1061
+ await dbConnection.getRepository(IdentityMetadataItemEntity).findOne({
1062
+ where: { id: savedIdentity.metadata![0].id },
1063
+ }),
1064
+ ).toBeNull()
1065
+ })
1066
+
1067
+ it('Should not delete party when deleting identity', async (): Promise<void> => {
1068
+ const party: NonPersistedParty = {
1069
+ uri: 'example.com',
1070
+ partyType: {
1071
+ type: PartyTypeType.NATURAL_PERSON,
1072
+ origin: PartyOrigin.EXTERNAL,
1073
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1074
+ name: 'example_name',
1075
+ },
1076
+ contact: {
1077
+ firstName: 'example_first_name',
1078
+ middleName: 'example_middle_name',
1079
+ lastName: 'example_last_name',
1080
+ displayName: 'example_display_name',
1081
+ },
1082
+ }
1083
+
1084
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1085
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1086
+
1087
+ expect(savedParty).toBeDefined()
1088
+
1089
+ const correlationId = 'relation_example.com'
1090
+ const identity: NonPersistedIdentity = {
1091
+ alias: correlationId,
1092
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
1093
+ identifier: {
1094
+ type: CorrelationIdentifierType.URL,
1095
+ correlationId,
1096
+ },
1097
+ connection: {
1098
+ type: ConnectionType.SIOPv2,
1099
+ config: {
1100
+ identifier: {
1101
+ did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
1102
+ provider: 'test_provider',
1103
+ keys: [],
1104
+ services: [],
1105
+ },
1106
+ redirectUrl: 'https://example.com',
1107
+ stateId: 'e91f3510-5ce9-42ee-83b7-fa68ff323d27',
1108
+ sessionId: 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
1109
+ },
1110
+ },
1111
+ metadata: [
1112
+ {
1113
+ label: 'example_label',
1114
+ value: 'example_value',
1115
+ },
1116
+ ],
1117
+ }
1118
+
1119
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
1120
+ identityEntity.party = savedParty
1121
+
1122
+ const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1123
+
1124
+ expect(savedIdentity).toBeDefined()
1125
+
1126
+ await dbConnection.getRepository(IdentityEntity).delete({ id: savedIdentity.id })
1127
+
1128
+ // check identity
1129
+ expect(
1130
+ await dbConnection.getRepository(IdentityEntity).findOne({
1131
+ where: { id: savedIdentity.id },
1132
+ }),
1133
+ ).toBeNull()
1134
+
1135
+ // check party
1136
+ expect(
1137
+ await dbConnection.getRepository(PartyEntity).findOne({
1138
+ where: { id: savedParty.id },
1139
+ }),
1140
+ ).toBeDefined()
1141
+ })
1142
+
1143
+ it('Should set creation date when saving party', async (): Promise<void> => {
1144
+ const party: NonPersistedParty = {
1145
+ uri: 'example.com',
1146
+ partyType: {
1147
+ type: PartyTypeType.NATURAL_PERSON,
1148
+ origin: PartyOrigin.INTERNAL,
1149
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1150
+ name: 'example_name',
1151
+ },
1152
+ contact: {
1153
+ firstName: 'example_first_name',
1154
+ middleName: 'example_middle_name',
1155
+ lastName: 'example_last_name',
1156
+ displayName: 'example_display_name',
1157
+ },
1158
+ }
1159
+
1160
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1161
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1162
+
1163
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1164
+ where: { id: savedParty.id },
1165
+ })
1166
+
1167
+ expect(fromDb).toBeDefined()
1168
+ expect(fromDb?.createdAt).toBeDefined()
1169
+ })
1170
+
1171
+ it('Should not update creation date when updating party', async (): Promise<void> => {
1172
+ const party: NonPersistedParty = {
1173
+ uri: 'example.com',
1174
+ partyType: {
1175
+ type: PartyTypeType.NATURAL_PERSON,
1176
+ origin: PartyOrigin.INTERNAL,
1177
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1178
+ name: 'example_name',
1179
+ },
1180
+ contact: {
1181
+ firstName: 'example_first_name',
1182
+ middleName: 'example_middle_name',
1183
+ lastName: 'example_last_name',
1184
+ displayName: 'example_display_name',
1185
+ },
1186
+ }
1187
+
1188
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1189
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1190
+
1191
+ expect(savedParty).toBeDefined()
1192
+
1193
+ const newContactFirstName = 'new_first_name'
1194
+ await dbConnection.getRepository(PartyEntity).save({
1195
+ ...savedParty,
1196
+ contact: {
1197
+ ...savedParty.contact,
1198
+ firstName: newContactFirstName,
1199
+ },
1200
+ })
1201
+
1202
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1203
+ where: { id: savedParty.id },
1204
+ })
1205
+
1206
+ expect(fromDb).toBeDefined()
1207
+ expect((<NaturalPersonEntity>fromDb?.contact).firstName).toEqual(newContactFirstName)
1208
+ expect(fromDb?.createdAt).toEqual(savedParty?.createdAt)
1209
+ })
1210
+
1211
+ it('Should set creation date when saving identity', async (): Promise<void> => {
1212
+ const correlationId = 'example_did'
1213
+ const identity: NonPersistedIdentity = {
1214
+ alias: correlationId,
1215
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
1216
+ identifier: {
1217
+ type: CorrelationIdentifierType.DID,
1218
+ correlationId,
1219
+ },
1220
+ }
1221
+
1222
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
1223
+ await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1224
+
1225
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
1226
+ where: {
1227
+ identifier: {
1228
+ correlationId,
1229
+ },
1230
+ },
1231
+ })
1232
+
1233
+ expect(fromDb).toBeDefined()
1234
+ expect(fromDb?.createdAt).toBeDefined()
1235
+ })
1236
+
1237
+ it('Should not update creation date when saving identity', async (): Promise<void> => {
1238
+ const correlationId = 'example_did'
1239
+ const identity: NonPersistedIdentity = {
1240
+ alias: correlationId,
1241
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
1242
+ identifier: {
1243
+ type: CorrelationIdentifierType.DID,
1244
+ correlationId,
1245
+ },
1246
+ }
1247
+
1248
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
1249
+ const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1250
+ const newCorrelationId = 'new_example_did'
1251
+ await dbConnection
1252
+ .getRepository(IdentityEntity)
1253
+ .save({ ...savedIdentity, identifier: { ...savedIdentity.identifier, correlationId: newCorrelationId } })
1254
+
1255
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
1256
+ where: {
1257
+ identifier: {
1258
+ correlationId: newCorrelationId,
1259
+ },
1260
+ },
1261
+ })
1262
+
1263
+ expect(fromDb).toBeDefined()
1264
+ expect(fromDb?.createdAt).toEqual(savedIdentity?.createdAt)
1265
+ })
1266
+
1267
+ it('Should set last updated date when saving party', async (): Promise<void> => {
1268
+ const party: NonPersistedParty = {
1269
+ uri: 'example.com',
1270
+ partyType: {
1271
+ type: PartyTypeType.NATURAL_PERSON,
1272
+ origin: PartyOrigin.INTERNAL,
1273
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1274
+ name: 'example_name',
1275
+ },
1276
+ contact: {
1277
+ firstName: 'example_first_name',
1278
+ middleName: 'example_middle_name',
1279
+ lastName: 'example_last_name',
1280
+ displayName: 'example_display_name',
1281
+ },
1282
+ }
1283
+
1284
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1285
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1286
+
1287
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1288
+ where: { id: savedParty.id },
1289
+ })
1290
+
1291
+ expect(fromDb).toBeDefined()
1292
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1293
+ })
1294
+
1295
+ it('Should update last updated date when updating party', async (): Promise<void> => {
1296
+ const party: NonPersistedParty = {
1297
+ uri: 'example.com',
1298
+ partyType: {
1299
+ type: PartyTypeType.NATURAL_PERSON,
1300
+ origin: PartyOrigin.EXTERNAL,
1301
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1302
+ name: 'example_name',
1303
+ },
1304
+ contact: {
1305
+ firstName: 'example_first_name',
1306
+ middleName: 'example_middle_name',
1307
+ lastName: 'example_last_name',
1308
+ displayName: 'example_display_name',
1309
+ },
1310
+ }
1311
+
1312
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1313
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1314
+ expect(savedParty).toBeDefined()
1315
+
1316
+ // waiting here to get a different timestamp
1317
+ await new Promise((resolve) => setTimeout(resolve, 2000))
1318
+
1319
+ const newContactFirstName = 'new_first_name'
1320
+ await dbConnection.getRepository(PartyEntity).save({
1321
+ ...savedParty,
1322
+ // FIXME there is still an issue when updating nested objects, the parent does not update
1323
+ // https://github.com/typeorm/typeorm/issues/5378
1324
+ uri: 'new uri', // TODO remove this to trigger the bug
1325
+ contact: {
1326
+ ...savedParty.contact,
1327
+ firstName: newContactFirstName,
1328
+ },
1329
+ })
1330
+
1331
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1332
+ where: { id: savedParty.id },
1333
+ })
1334
+
1335
+ expect(fromDb).toBeDefined()
1336
+ expect((<NaturalPersonEntity>fromDb?.contact).firstName).toEqual(newContactFirstName)
1337
+ expect(fromDb?.lastUpdatedAt).not.toEqual(savedParty?.lastUpdatedAt)
1338
+ })
1339
+
1340
+ it('Should set last updated date when saving party type', async (): Promise<void> => {
1341
+ const partyType: NonPersistedPartyType = {
1342
+ type: PartyTypeType.NATURAL_PERSON,
1343
+ origin: PartyOrigin.EXTERNAL,
1344
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1345
+ name: 'example_name',
1346
+ }
1347
+
1348
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
1349
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
1350
+
1351
+ const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
1352
+ where: { id: savedPartyType.id },
1353
+ })
1354
+
1355
+ expect(fromDb).toBeDefined()
1356
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1357
+ })
1358
+
1359
+ it('Should set last creation date when saving party type', async (): Promise<void> => {
1360
+ const partyType: NonPersistedPartyType = {
1361
+ type: PartyTypeType.NATURAL_PERSON,
1362
+ origin: PartyOrigin.INTERNAL,
1363
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1364
+ name: 'example_name',
1365
+ }
1366
+
1367
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
1368
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
1369
+
1370
+ const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
1371
+ where: { id: savedPartyType.id },
1372
+ })
1373
+
1374
+ expect(fromDb).toBeDefined()
1375
+ expect(fromDb?.createdAt).toBeDefined()
1376
+ })
1377
+
1378
+ it('Should set last updated date when saving identity', async (): Promise<void> => {
1379
+ const correlationId = 'example_did'
1380
+ const identity: NonPersistedIdentity = {
1381
+ alias: correlationId,
1382
+ roles: [IdentityRole.ISSUER, IdentityRole.VERIFIER],
1383
+ identifier: {
1384
+ type: CorrelationIdentifierType.DID,
1385
+ correlationId,
1386
+ },
1387
+ }
1388
+
1389
+ const identityEntity: IdentityEntity = identityEntityFrom(identity)
1390
+ await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1391
+
1392
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
1393
+ where: {
1394
+ identifier: {
1395
+ correlationId,
1396
+ },
1397
+ },
1398
+ })
1399
+
1400
+ expect(fromDb).toBeDefined()
1401
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1402
+ })
1403
+
1404
+ it('Should enforce unique type and tenant id combination when saving party type', async (): Promise<void> => {
1405
+ const tenantId = 'non_unique_value'
1406
+ const name = 'non_unique_value'
1407
+ const partyType1: NonPersistedPartyType = {
1408
+ type: PartyTypeType.NATURAL_PERSON,
1409
+ origin: PartyOrigin.EXTERNAL,
1410
+ tenantId,
1411
+ name,
1412
+ }
1413
+
1414
+ const partyTypeEntity1: PartyTypeEntity = partyTypeEntityFrom(partyType1)
1415
+ const savedPartyType1: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity1)
1416
+
1417
+ expect(savedPartyType1).toBeDefined()
1418
+
1419
+ const partyType2: NonPersistedPartyType = {
1420
+ type: PartyTypeType.NATURAL_PERSON,
1421
+ origin: PartyOrigin.INTERNAL,
1422
+ tenantId,
1423
+ name,
1424
+ }
1425
+
1426
+ const partyTypeEntity2: PartyTypeEntity = partyTypeEntityFrom(partyType2)
1427
+ await expect(dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity2)).rejects.toThrowError(
1428
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: PartyType.type, PartyType.tenant_id',
1429
+ )
1430
+ })
1431
+
1432
+ it('Should enforce unique name when saving party type', async (): Promise<void> => {
1433
+ const name = 'non_unique_value'
1434
+ const partyType1: NonPersistedPartyType = {
1435
+ type: PartyTypeType.NATURAL_PERSON,
1436
+ origin: PartyOrigin.INTERNAL,
1437
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1438
+ name,
1439
+ }
1440
+
1441
+ const partyTypeEntity1: PartyTypeEntity = partyTypeEntityFrom(partyType1)
1442
+ const savedPartyType1: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity1)
1443
+
1444
+ expect(savedPartyType1).toBeDefined()
1445
+
1446
+ const partyType2: NonPersistedPartyType = {
1447
+ type: PartyTypeType.NATURAL_PERSON,
1448
+ origin: PartyOrigin.INTERNAL,
1449
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1450
+ name,
1451
+ }
1452
+
1453
+ const partyTypeEntity2: PartyTypeEntity = partyTypeEntityFrom(partyType2)
1454
+ await expect(dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity2)).rejects.toThrowError(
1455
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: PartyType.name',
1456
+ )
1457
+ })
1458
+
1459
+ it('Should enforce unique legal name when saving organization', async (): Promise<void> => {
1460
+ const legalName = 'non_unique_value'
1461
+ const organization1: NonPersistedOrganization = {
1462
+ legalName,
1463
+ displayName: 'example_display_name',
1464
+ }
1465
+
1466
+ const organizationEntity1: OrganizationEntity = organizationEntityFrom(organization1)
1467
+ const savedOrganization1: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity1, {
1468
+ transaction: true,
1469
+ })
1470
+
1471
+ expect(savedOrganization1).toBeDefined()
1472
+
1473
+ const organization2: NonPersistedOrganization = {
1474
+ legalName,
1475
+ displayName: 'example_display_name',
1476
+ }
1477
+
1478
+ const organizationEntity2: OrganizationEntity = organizationEntityFrom(organization2)
1479
+ await expect(dbConnection.getRepository(OrganizationEntity).save(organizationEntity2)).rejects.toThrowError(
1480
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: BaseContact.legal_name',
1481
+ )
1482
+ })
1483
+
1484
+ it('Should enforce unique legal name when saving organization', async (): Promise<void> => {
1485
+ const legalName = 'example_legal_name'
1486
+ const organization1: NonPersistedOrganization = {
1487
+ legalName,
1488
+ displayName: 'example_display_name',
1489
+ }
1490
+
1491
+ const organizationEntity1: OrganizationEntity = organizationEntityFrom(organization1)
1492
+ const savedOrganization1: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity1, {
1493
+ transaction: true,
1494
+ })
1495
+
1496
+ expect(savedOrganization1).toBeDefined()
1497
+
1498
+ const organization2: NonPersistedOrganization = {
1499
+ legalName,
1500
+ displayName: 'example_display_name',
1501
+ }
1502
+
1503
+ const organizationEntity2: OrganizationEntity = organizationEntityFrom(organization2)
1504
+ await expect(dbConnection.getRepository(OrganizationEntity).save(organizationEntity2)).rejects.toThrowError(
1505
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: BaseContact.legal_name',
1506
+ )
1507
+ })
1508
+
1509
+ it('Should save party relationship to database', async (): Promise<void> => {
1510
+ const party1: NonPersistedParty = {
1511
+ uri: 'example1.com',
1512
+ partyType: {
1513
+ type: PartyTypeType.NATURAL_PERSON,
1514
+ origin: PartyOrigin.INTERNAL,
1515
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1516
+ name: 'example_name1',
1517
+ },
1518
+ contact: {
1519
+ firstName: 'example_first_name1',
1520
+ middleName: 'example_middle_name1',
1521
+ lastName: 'example_last_name1',
1522
+ displayName: 'example_display_name1',
1523
+ },
1524
+ }
1525
+
1526
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1527
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1528
+ transaction: true,
1529
+ })
1530
+
1531
+ expect(savedParty1).toBeDefined()
1532
+
1533
+ const party2: NonPersistedParty = {
1534
+ uri: 'example2.com',
1535
+ partyType: {
1536
+ type: PartyTypeType.NATURAL_PERSON,
1537
+ origin: PartyOrigin.INTERNAL,
1538
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1539
+ name: 'example_name2',
1540
+ },
1541
+ contact: {
1542
+ firstName: 'example_first_name2',
1543
+ middleName: 'example_middle_name2',
1544
+ lastName: 'example_last_name2',
1545
+ displayName: 'example_display_name2',
1546
+ },
1547
+ }
1548
+
1549
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1550
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1551
+ transaction: true,
1552
+ })
1553
+
1554
+ expect(savedParty2).toBeDefined()
1555
+
1556
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
1557
+ leftId: savedParty1.id,
1558
+ rightId: savedParty2.id,
1559
+ })
1560
+
1561
+ await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
1562
+ transaction: true,
1563
+ })
1564
+
1565
+ // TODO check the relation field
1566
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1567
+ where: { id: partyEntity1.id },
1568
+ })
1569
+
1570
+ expect(fromDb).toBeDefined()
1571
+ })
1572
+
1573
+ it('Should set last updated date when saving party relationship', async (): Promise<void> => {
1574
+ const party1: NonPersistedParty = {
1575
+ uri: 'example1.com',
1576
+ partyType: {
1577
+ type: PartyTypeType.NATURAL_PERSON,
1578
+ origin: PartyOrigin.INTERNAL,
1579
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1580
+ name: 'example_name1',
1581
+ },
1582
+ contact: {
1583
+ firstName: 'example_first_name1',
1584
+ middleName: 'example_middle_name1',
1585
+ lastName: 'example_last_name1',
1586
+ displayName: 'example_display_name1',
1587
+ },
1588
+ }
1589
+
1590
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1591
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1592
+ transaction: true,
1593
+ })
1594
+
1595
+ const party2: NonPersistedParty = {
1596
+ uri: 'example2.com',
1597
+ partyType: {
1598
+ type: PartyTypeType.NATURAL_PERSON,
1599
+ origin: PartyOrigin.INTERNAL,
1600
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1601
+ name: 'example_name2',
1602
+ },
1603
+ contact: {
1604
+ firstName: 'example_first_name2',
1605
+ middleName: 'example_middle_name2',
1606
+ lastName: 'example_last_name2',
1607
+ displayName: 'example_display_name2',
1608
+ },
1609
+ }
1610
+
1611
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1612
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1613
+ transaction: true,
1614
+ })
1615
+
1616
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
1617
+ leftId: savedParty1.id,
1618
+ rightId: savedParty2.id,
1619
+ })
1620
+
1621
+ await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
1622
+ transaction: true,
1623
+ })
1624
+
1625
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1626
+ where: { id: partyEntity1.id },
1627
+ })
1628
+
1629
+ expect(fromDb).toBeDefined()
1630
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1631
+ })
1632
+
1633
+ it('Should set creation date when saving party relationship', async (): Promise<void> => {
1634
+ const party1: NonPersistedParty = {
1635
+ uri: 'example1.com',
1636
+ partyType: {
1637
+ type: PartyTypeType.NATURAL_PERSON,
1638
+ origin: PartyOrigin.INTERNAL,
1639
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1640
+ name: 'example_name1',
1641
+ },
1642
+ contact: {
1643
+ firstName: 'example_first_name1',
1644
+ middleName: 'example_middle_name1',
1645
+ lastName: 'example_last_name1',
1646
+ displayName: 'example_display_name1',
1647
+ },
1648
+ }
1649
+
1650
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1651
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1652
+ transaction: true,
1653
+ })
1654
+
1655
+ const party2: NonPersistedParty = {
1656
+ uri: 'example2.com',
1657
+ partyType: {
1658
+ type: PartyTypeType.NATURAL_PERSON,
1659
+ origin: PartyOrigin.INTERNAL,
1660
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1661
+ name: 'example_name2',
1662
+ },
1663
+ contact: {
1664
+ firstName: 'example_first_name2',
1665
+ middleName: 'example_middle_name2',
1666
+ lastName: 'example_last_name2',
1667
+ displayName: 'example_display_name2',
1668
+ },
1669
+ }
1670
+
1671
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1672
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1673
+ transaction: true,
1674
+ })
1675
+
1676
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
1677
+ leftId: savedParty1.id,
1678
+ rightId: savedParty2.id,
1679
+ })
1680
+
1681
+ await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
1682
+ transaction: true,
1683
+ })
1684
+
1685
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1686
+ where: { id: partyEntity1.id },
1687
+ })
1688
+
1689
+ expect(fromDb).toBeDefined()
1690
+ expect(fromDb?.createdAt).toBeDefined()
1691
+ })
1692
+
1693
+ it('Should save bidirectional party relationships to database', async (): Promise<void> => {
1694
+ const party1: NonPersistedParty = {
1695
+ uri: 'example1.com',
1696
+ partyType: {
1697
+ type: PartyTypeType.NATURAL_PERSON,
1698
+ origin: PartyOrigin.INTERNAL,
1699
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1700
+ name: 'example_name1',
1701
+ },
1702
+ contact: {
1703
+ firstName: 'example_first_name1',
1704
+ middleName: 'example_middle_name1',
1705
+ lastName: 'example_last_name1',
1706
+ displayName: 'example_display_name1',
1707
+ },
1708
+ }
1709
+
1710
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1711
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1712
+ transaction: true,
1713
+ })
1714
+
1715
+ expect(savedParty1).toBeDefined()
1716
+
1717
+ const party2: NonPersistedParty = {
1718
+ uri: 'example2.com',
1719
+ partyType: {
1720
+ type: PartyTypeType.NATURAL_PERSON,
1721
+ origin: PartyOrigin.INTERNAL,
1722
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1723
+ name: 'example_name2',
1724
+ },
1725
+ contact: {
1726
+ firstName: 'example_first_name2',
1727
+ middleName: 'example_middle_name2',
1728
+ lastName: 'example_last_name2',
1729
+ displayName: 'example_display_name2',
1730
+ },
1731
+ }
1732
+
1733
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1734
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1735
+ transaction: true,
1736
+ })
1737
+
1738
+ expect(savedParty2).toBeDefined()
1739
+
1740
+ const relationship1: PartyRelationshipEntity = partyRelationshipEntityFrom({
1741
+ leftId: savedParty1.id,
1742
+ rightId: savedParty2.id,
1743
+ })
1744
+
1745
+ const savedRelationship1: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship1, {
1746
+ transaction: true,
1747
+ })
1748
+
1749
+ expect(savedRelationship1).toBeDefined()
1750
+
1751
+ const relationship2: PartyRelationshipEntity = partyRelationshipEntityFrom({
1752
+ leftId: savedParty2.id,
1753
+ rightId: savedParty1.id,
1754
+ })
1755
+
1756
+ const savedRelationship2: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship2, {
1757
+ transaction: true,
1758
+ })
1759
+
1760
+ expect(savedRelationship2).toBeDefined()
1761
+
1762
+ const fromDb: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).findOne({
1763
+ where: { id: savedRelationship2.id },
1764
+ })
1765
+
1766
+ expect(fromDb).toBeDefined()
1767
+ })
1768
+
1769
+ it('Should enforce unique owner combination for party relationship', async (): Promise<void> => {
1770
+ const party1: NonPersistedParty = {
1771
+ uri: 'example1.com',
1772
+ partyType: {
1773
+ type: PartyTypeType.NATURAL_PERSON,
1774
+ origin: PartyOrigin.INTERNAL,
1775
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1776
+ name: 'example_name1',
1777
+ },
1778
+ contact: {
1779
+ firstName: 'example_first_name1',
1780
+ middleName: 'example_middle_name1',
1781
+ lastName: 'example_last_name1',
1782
+ displayName: 'example_display_name1',
1783
+ },
1784
+ }
1785
+
1786
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1787
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1788
+ transaction: true,
1789
+ })
1790
+
1791
+ expect(savedParty1).toBeDefined()
1792
+
1793
+ const party2: NonPersistedParty = {
1794
+ uri: 'example2.com',
1795
+ partyType: {
1796
+ type: PartyTypeType.NATURAL_PERSON,
1797
+ origin: PartyOrigin.INTERNAL,
1798
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1799
+ name: 'example_name2',
1800
+ },
1801
+ contact: {
1802
+ firstName: 'example_first_name2',
1803
+ middleName: 'example_middle_name2',
1804
+ lastName: 'example_last_name2',
1805
+ displayName: 'example_display_name2',
1806
+ },
1807
+ }
1808
+
1809
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1810
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1811
+ transaction: true,
1812
+ })
1813
+
1814
+ expect(savedParty2).toBeDefined()
1815
+
1816
+ const relationship1: PartyRelationshipEntity = partyRelationshipEntityFrom({
1817
+ leftId: savedParty1.id,
1818
+ rightId: savedParty2.id,
1819
+ })
1820
+
1821
+ const savedRelationship1: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship1, {
1822
+ transaction: true,
1823
+ })
1824
+
1825
+ expect(savedRelationship1).toBeDefined()
1826
+
1827
+ const relationship2: PartyRelationshipEntity = partyRelationshipEntityFrom({
1828
+ leftId: savedParty1.id,
1829
+ rightId: savedParty2.id,
1830
+ })
1831
+
1832
+ await expect(dbConnection.getRepository(PartyRelationshipEntity).save(relationship2)).rejects.toThrowError(
1833
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: PartyRelationship.left_id, PartyRelationship.right_id',
1834
+ )
1835
+ })
1836
+
1837
+ it('Should save party type to database', async (): Promise<void> => {
1838
+ const partyType: NonPersistedPartyType = {
1839
+ type: PartyTypeType.NATURAL_PERSON,
1840
+ origin: PartyOrigin.INTERNAL,
1841
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1842
+ name: 'example_name',
1843
+ }
1844
+
1845
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
1846
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
1847
+
1848
+ const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
1849
+ where: { id: savedPartyType.id },
1850
+ })
1851
+
1852
+ expect(fromDb).toBeDefined()
1853
+ })
1854
+
1855
+ it('Should save person to database', async (): Promise<void> => {
1856
+ const person: NonPersistedNaturalPerson = {
1857
+ firstName: 'example_first_name',
1858
+ lastName: 'lastName2',
1859
+ displayName: 'displayName',
1860
+ }
1861
+
1862
+ const personEntity: NaturalPersonEntity = naturalPersonEntityFrom(person)
1863
+ const savedPerson: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).save(personEntity, {
1864
+ transaction: true,
1865
+ })
1866
+
1867
+ const fromDb: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).findOne({
1868
+ where: { id: savedPerson.id },
1869
+ })
1870
+
1871
+ expect(fromDb).toBeDefined()
1872
+ })
1873
+
1874
+ it('Should set last updated date when saving person', async (): Promise<void> => {
1875
+ const person: NonPersistedNaturalPerson = {
1876
+ firstName: 'example_first_name',
1877
+ lastName: 'lastName2',
1878
+ displayName: 'displayName',
1879
+ }
1880
+
1881
+ const personEntity: NaturalPersonEntity = naturalPersonEntityFrom(person)
1882
+ const savedPerson: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).save(personEntity, {
1883
+ transaction: true,
1884
+ })
1885
+
1886
+ const fromDb: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).findOne({
1887
+ where: { id: savedPerson.id },
1888
+ })
1889
+
1890
+ expect(fromDb).toBeDefined()
1891
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1892
+ })
1893
+
1894
+ it('Should set creation date when saving person', async (): Promise<void> => {
1895
+ const person: NonPersistedNaturalPerson = {
1896
+ firstName: 'example_first_name',
1897
+ lastName: 'lastName2',
1898
+ displayName: 'displayName',
1899
+ }
1900
+
1901
+ const personEntity: NaturalPersonEntity = naturalPersonEntityFrom(person)
1902
+ const savedPerson: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).save(personEntity, {
1903
+ transaction: true,
1904
+ })
1905
+
1906
+ const fromDb: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).findOne({
1907
+ where: { id: savedPerson.id },
1908
+ })
1909
+
1910
+ expect(fromDb).toBeDefined()
1911
+ expect(fromDb?.createdAt).toBeDefined()
1912
+ })
1913
+
1914
+ it('Should save organization to database', async (): Promise<void> => {
1915
+ const organization: NonPersistedOrganization = {
1916
+ legalName: 'example_legal_name',
1917
+ displayName: 'example_display_name',
1918
+ }
1919
+
1920
+ const organizationEntity: OrganizationEntity = organizationEntityFrom(organization)
1921
+ const savedOrganization: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity, {
1922
+ transaction: true,
1923
+ })
1924
+
1925
+ const fromDb: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).findOne({
1926
+ where: { id: savedOrganization.id },
1927
+ })
1928
+
1929
+ expect(fromDb).toBeDefined()
1930
+ })
1931
+
1932
+ it('Should set last updated date when saving organization', async (): Promise<void> => {
1933
+ const organization: NonPersistedOrganization = {
1934
+ legalName: 'example_legal_name',
1935
+ displayName: 'example_display_name',
1936
+ }
1937
+
1938
+ const organizationEntity: OrganizationEntity = organizationEntityFrom(organization)
1939
+ const savedOrganization: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity, {
1940
+ transaction: true,
1941
+ })
1942
+
1943
+ const fromDb: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).findOne({
1944
+ where: { id: savedOrganization.id },
1945
+ })
1946
+
1947
+ expect(fromDb).toBeDefined()
1948
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1949
+ })
1950
+
1951
+ it('Should set creation date when saving organization', async (): Promise<void> => {
1952
+ const organization: NonPersistedOrganization = {
1953
+ legalName: 'example_legal_name',
1954
+ displayName: 'example_display_name',
1955
+ }
1956
+
1957
+ const organizationEntity: OrganizationEntity = organizationEntityFrom(organization)
1958
+ const savedOrganization: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity, {
1959
+ transaction: true,
1960
+ })
1961
+
1962
+ const fromDb: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).findOne({
1963
+ where: { id: savedOrganization.id },
1964
+ })
1965
+
1966
+ expect(fromDb).toBeDefined()
1967
+ expect(fromDb?.createdAt).toBeDefined()
1968
+ })
1969
+
1970
+ it('Should get party based on person information', async (): Promise<void> => {
1971
+ const firstName = 'example_first_name'
1972
+ const party: NonPersistedParty = {
1973
+ uri: 'example.com',
1974
+ partyType: {
1975
+ type: PartyTypeType.NATURAL_PERSON,
1976
+ origin: PartyOrigin.INTERNAL,
1977
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1978
+ name: 'example_name',
1979
+ },
1980
+ contact: {
1981
+ firstName,
1982
+ middleName: 'example_middle_name',
1983
+ lastName: 'example_last_name',
1984
+ displayName: 'example_display_name',
1985
+ },
1986
+ }
1987
+
1988
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1989
+ await dbConnection.getRepository(PartyEntity).save(partyEntity, {
1990
+ transaction: true,
1991
+ })
1992
+
1993
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1994
+ where: {
1995
+ contact: {
1996
+ firstName,
1997
+ } as FindOptionsWhere<BaseContactEntity>, //NaturalPersonEntity | OrganizationEntity
1998
+ },
1999
+ })
2000
+
2001
+ expect(fromDb).toBeDefined()
2002
+ })
2003
+
2004
+ it('Should get party based on organization information', async (): Promise<void> => {
2005
+ const legalName = 'example_legal_name'
2006
+ const party: NonPersistedParty = {
2007
+ uri: 'example.com',
2008
+ partyType: {
2009
+ type: PartyTypeType.ORGANIZATION,
2010
+ origin: PartyOrigin.INTERNAL,
2011
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2012
+ name: 'example_name',
2013
+ },
2014
+ contact: {
2015
+ legalName,
2016
+ displayName: 'example_display_name',
2017
+ },
2018
+ }
2019
+
2020
+ const partyEntity: PartyEntity = partyEntityFrom(party)
2021
+ await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2022
+ transaction: true,
2023
+ })
2024
+
2025
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
2026
+ where: {
2027
+ contact: {
2028
+ legalName,
2029
+ } as FindOptionsWhere<BaseContactEntity>, //NaturalPersonEntity | OrganizationEntity
2030
+ },
2031
+ })
2032
+
2033
+ expect(fromDb).toBeDefined()
2034
+ })
2035
+
2036
+ it("Should enforce unique party id's for relationship sides", async (): Promise<void> => {
2037
+ const party: NonPersistedParty = {
2038
+ uri: 'example.com',
2039
+ partyType: {
2040
+ type: PartyTypeType.NATURAL_PERSON,
2041
+ origin: PartyOrigin.INTERNAL,
2042
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2043
+ name: 'example_name',
2044
+ },
2045
+ contact: {
2046
+ firstName: 'example_first_name',
2047
+ middleName: 'example_middle_name',
2048
+ lastName: 'example_last_name',
2049
+ displayName: 'example_display_name',
2050
+ },
2051
+ }
2052
+
2053
+ const partyEntity: PartyEntity = partyEntityFrom(party)
2054
+ const savedParty: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2055
+ transaction: true,
2056
+ })
2057
+
2058
+ expect(savedParty).toBeDefined()
2059
+
2060
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
2061
+ leftId: savedParty.id,
2062
+ rightId: savedParty.id,
2063
+ })
2064
+
2065
+ await expect(dbConnection.getRepository(PartyRelationshipEntity).save(relationship)).rejects.toThrowError(
2066
+ 'Cannot use the same id for both sides of the relationship',
2067
+ )
2068
+ })
2069
+
2070
+ it('Should delete party relationship', async (): Promise<void> => {
2071
+ const party1: NonPersistedParty = {
2072
+ uri: 'example1.com',
2073
+ partyType: {
2074
+ type: PartyTypeType.NATURAL_PERSON,
2075
+ origin: PartyOrigin.INTERNAL,
2076
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2077
+ name: 'example_name1',
2078
+ },
2079
+ contact: {
2080
+ firstName: 'example_first_name1',
2081
+ middleName: 'example_middle_name1',
2082
+ lastName: 'example_last_name1',
2083
+ displayName: 'example_display_name1',
2084
+ },
2085
+ }
2086
+
2087
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
2088
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
2089
+ transaction: true,
2090
+ })
2091
+
2092
+ expect(savedParty1).toBeDefined()
2093
+
2094
+ const party2: NonPersistedParty = {
2095
+ uri: 'example2.com',
2096
+ partyType: {
2097
+ type: PartyTypeType.NATURAL_PERSON,
2098
+ origin: PartyOrigin.INTERNAL,
2099
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
2100
+ name: 'example_name2',
2101
+ },
2102
+ contact: {
2103
+ firstName: 'example_first_name2',
2104
+ middleName: 'example_middle_name2',
2105
+ lastName: 'example_last_name2',
2106
+ displayName: 'example_display_name2',
2107
+ },
2108
+ }
2109
+
2110
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
2111
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
2112
+ transaction: true,
2113
+ })
2114
+
2115
+ expect(savedParty2).toBeDefined()
2116
+
2117
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
2118
+ leftId: savedParty1.id,
2119
+ rightId: savedParty2.id,
2120
+ })
2121
+
2122
+ const savedRelationship: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
2123
+ transaction: true,
2124
+ })
2125
+
2126
+ expect(savedRelationship).toBeDefined()
2127
+
2128
+ await dbConnection.getRepository(PartyRelationshipEntity).delete({ id: savedRelationship.id })
2129
+
2130
+ await expect(
2131
+ await dbConnection.getRepository(PartyRelationshipEntity).findOne({
2132
+ where: { id: savedRelationship.id },
2133
+ }),
2134
+ ).toBeNull()
2135
+ })
2136
+
2137
+ it('Should delete party type', async (): Promise<void> => {
2138
+ const partyType: NonPersistedPartyType = {
2139
+ type: PartyTypeType.NATURAL_PERSON,
2140
+ origin: PartyOrigin.INTERNAL,
2141
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2142
+ name: 'example_name',
2143
+ }
2144
+
2145
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
2146
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
2147
+
2148
+ expect(savedPartyType).toBeDefined()
2149
+
2150
+ await dbConnection.getRepository(PartyTypeEntity).delete({ id: savedPartyType.id })
2151
+
2152
+ await expect(
2153
+ await dbConnection.getRepository(PartyTypeEntity).findOne({
2154
+ where: { id: savedPartyType.id },
2155
+ }),
2156
+ ).toBeNull()
2157
+ })
2158
+
2159
+ it('Should not be able to remove party type when used by parties', async (): Promise<void> => {
2160
+ const party: NonPersistedParty = {
2161
+ uri: 'example.com',
2162
+ partyType: {
2163
+ type: PartyTypeType.NATURAL_PERSON,
2164
+ origin: PartyOrigin.INTERNAL,
2165
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2166
+ name: 'example_name',
2167
+ },
2168
+ contact: {
2169
+ firstName: 'example_first_name',
2170
+ middleName: 'example_middle_name',
2171
+ lastName: 'example_last_name',
2172
+ displayName: 'example_display_name',
2173
+ },
2174
+ }
2175
+
2176
+ const partyEntity: PartyEntity = partyEntityFrom(party)
2177
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2178
+ transaction: true,
2179
+ })
2180
+
2181
+ expect(savedParty).toBeDefined()
2182
+
2183
+ await expect(dbConnection.getRepository(PartyTypeEntity).delete({ id: savedParty.partyType.id })).rejects.toThrowError(
2184
+ 'FOREIGN KEY constraint failed',
2185
+ )
2186
+ })
2187
+
2188
+ it('Should save party with existing party type', async (): Promise<void> => {
2189
+ const partyType: NonPersistedPartyType = {
2190
+ type: PartyTypeType.NATURAL_PERSON,
2191
+ origin: PartyOrigin.INTERNAL,
2192
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2193
+ name: 'example_name',
2194
+ }
2195
+
2196
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
2197
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
2198
+
2199
+ const party: NonPersistedParty = {
2200
+ uri: 'example.com',
2201
+ partyType: savedPartyType,
2202
+ contact: {
2203
+ firstName: 'example_first_name',
2204
+ middleName: 'example_middle_name',
2205
+ lastName: 'example_last_name',
2206
+ displayName: 'example_display_name',
2207
+ },
2208
+ }
2209
+
2210
+ const partyEntity: PartyEntity = partyEntityFrom(party)
2211
+ partyEntity.partyType = savedPartyType
2212
+ await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2213
+ transaction: true,
2214
+ })
2215
+
2216
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
2217
+ where: { id: partyEntity.id },
2218
+ })
2219
+
2220
+ expect(fromDb).toBeDefined()
2221
+ expect(fromDb?.partyType).toBeDefined()
2222
+ expect(fromDb?.partyType.id).toEqual(savedPartyType.id)
2223
+ expect(fromDb?.partyType.type).toEqual(savedPartyType.type)
2224
+ expect(fromDb?.partyType.origin).toEqual(savedPartyType.origin)
2225
+ expect(fromDb?.partyType.tenantId).toEqual(savedPartyType.tenantId)
2226
+ expect(fromDb?.partyType.name).toEqual(savedPartyType.name)
2227
+ })
2228
+
2229
+ it('Should not update creation date when saving party type', async (): Promise<void> => {
2230
+ const partyType: NonPersistedPartyType = {
2231
+ type: PartyTypeType.NATURAL_PERSON,
2232
+ origin: PartyOrigin.INTERNAL,
2233
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2234
+ name: 'example_name',
2235
+ }
2236
+
2237
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
2238
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
2239
+ await dbConnection.getRepository(PartyTypeEntity).save({ ...savedPartyType, type: PartyTypeType.ORGANIZATION })
2240
+
2241
+ const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
2242
+ where: {
2243
+ type: PartyTypeType.ORGANIZATION,
2244
+ },
2245
+ })
2246
+
2247
+ expect(fromDb).toBeDefined()
2248
+ expect(fromDb?.createdAt).toEqual(savedPartyType?.createdAt)
2249
+ })
2250
+
2251
+ it('Should save email electronic address to database', async (): Promise<void> => {
2252
+ const electronicAddress: NonPersistedElectronicAddress = {
2253
+ type: 'email',
2254
+ electronicAddress: 'example_email_address',
2255
+ }
2256
+
2257
+ const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
2258
+ const savedElectronicAddress: ElectronicAddressEntity = await dbConnection.getRepository(ElectronicAddressEntity).save(electronicAddressEntity, {
2259
+ transaction: true,
2260
+ })
2261
+
2262
+ const fromDb: ElectronicAddressEntity | null = await dbConnection.getRepository(ElectronicAddressEntity).findOne({
2263
+ where: { id: savedElectronicAddress.id },
2264
+ })
2265
+
2266
+ expect(fromDb).toBeDefined()
2267
+ expect(fromDb?.type).toEqual(electronicAddress.type)
2268
+ expect(fromDb?.electronicAddress).toEqual(electronicAddress.electronicAddress)
2269
+ expect(fromDb?.createdAt).toBeDefined()
2270
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
2271
+ })
2272
+
2273
+ it('Should save phone electronic address to database', async (): Promise<void> => {
2274
+ const electronicAddress: NonPersistedElectronicAddress = {
2275
+ type: 'phone',
2276
+ electronicAddress: 'example_phone_number',
2277
+ }
2278
+
2279
+ const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
2280
+ const savedElectronicAddress: ElectronicAddressEntity = await dbConnection.getRepository(ElectronicAddressEntity).save(electronicAddressEntity, {
2281
+ transaction: true,
2282
+ })
2283
+
2284
+ const fromDb: ElectronicAddressEntity | null = await dbConnection.getRepository(ElectronicAddressEntity).findOne({
2285
+ where: { id: savedElectronicAddress.id },
2286
+ })
2287
+
2288
+ expect(fromDb).toBeDefined()
2289
+ expect(fromDb?.type).toEqual(electronicAddress.type)
2290
+ expect(fromDb?.electronicAddress).toEqual(electronicAddress.electronicAddress)
2291
+ expect(fromDb?.createdAt).toBeDefined()
2292
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
2293
+ })
2294
+
2295
+ it('should throw error when saving electronic address with blank electronic address', async (): Promise<void> => {
2296
+ const electronicAddress: NonPersistedElectronicAddress = {
2297
+ type: 'email',
2298
+ electronicAddress: '',
2299
+ }
2300
+
2301
+ const electronicAddressEntity: ElectronicAddressEntity = electronicAddressEntityFrom(electronicAddress)
2302
+
2303
+ await expect(dbConnection.getRepository(ElectronicAddressEntity).save(electronicAddressEntity)).rejects.toThrowError(
2304
+ 'Blank electronic addresses are not allowed',
2305
+ )
2306
+ })
2307
+
2308
+ it('Should save home physical address to database', async (): Promise<void> => {
2309
+ const physicalAddress: NonPersistedPhysicalAddress = {
2310
+ type: 'home',
2311
+ streetName: 'example_street_name',
2312
+ streetNumber: 'example_street_number',
2313
+ buildingName: 'example_building_name',
2314
+ postalCode: 'example_postal_code',
2315
+ cityName: 'example_city_name',
2316
+ provinceName: 'example_province_name',
2317
+ countryCode: 'example_country_code',
2318
+ }
2319
+
2320
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2321
+ const savedPhysicalAddress: PhysicalAddressEntity = await dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity, {
2322
+ transaction: true,
2323
+ })
2324
+
2325
+ const fromDb: PhysicalAddressEntity | null = await dbConnection.getRepository(PhysicalAddressEntity).findOne({
2326
+ where: { id: savedPhysicalAddress.id },
2327
+ })
2328
+
2329
+ expect(fromDb).toBeDefined()
2330
+ expect(fromDb?.type).toEqual(physicalAddress.type)
2331
+ expect(fromDb?.streetName).toEqual(physicalAddress.streetName)
2332
+ expect(fromDb?.streetNumber).toEqual(physicalAddress.streetNumber)
2333
+ expect(fromDb?.buildingName).toEqual(physicalAddress.buildingName)
2334
+ expect(fromDb?.postalCode).toEqual(physicalAddress.postalCode)
2335
+ expect(fromDb?.cityName).toEqual(physicalAddress.cityName)
2336
+ expect(fromDb?.provinceName).toEqual(physicalAddress.provinceName)
2337
+ expect(fromDb?.countryCode).toEqual(physicalAddress.countryCode)
2338
+ expect(fromDb?.createdAt).toBeDefined()
2339
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
2340
+ })
2341
+
2342
+ it('Should save visit physical address to database', async (): Promise<void> => {
2343
+ const physicalAddress: NonPersistedPhysicalAddress = {
2344
+ type: 'visit',
2345
+ streetName: 'example_street_name',
2346
+ streetNumber: 'example_street_number',
2347
+ buildingName: 'example_building_name',
2348
+ postalCode: 'example_postal_code',
2349
+ cityName: 'example_city_name',
2350
+ provinceName: 'example_province_name',
2351
+ countryCode: 'example_country_code',
2352
+ }
2353
+
2354
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2355
+ const savedPhysicalAddress: PhysicalAddressEntity = await dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity, {
2356
+ transaction: true,
2357
+ })
2358
+
2359
+ const fromDb: PhysicalAddressEntity | null = await dbConnection.getRepository(PhysicalAddressEntity).findOne({
2360
+ where: { id: savedPhysicalAddress.id },
2361
+ })
2362
+
2363
+ expect(fromDb).toBeDefined()
2364
+ expect(fromDb?.type).toEqual(physicalAddress.type)
2365
+ expect(fromDb?.streetName).toEqual(physicalAddress.streetName)
2366
+ expect(fromDb?.streetNumber).toEqual(physicalAddress.streetNumber)
2367
+ expect(fromDb?.buildingName).toEqual(physicalAddress.buildingName)
2368
+ expect(fromDb?.postalCode).toEqual(physicalAddress.postalCode)
2369
+ expect(fromDb?.cityName).toEqual(physicalAddress.cityName)
2370
+ expect(fromDb?.provinceName).toEqual(physicalAddress.provinceName)
2371
+ expect(fromDb?.countryCode).toEqual(physicalAddress.countryCode)
2372
+ expect(fromDb?.createdAt).toBeDefined()
2373
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
2374
+ })
2375
+
2376
+ it('Should save postal physical address to database', async (): Promise<void> => {
2377
+ const physicalAddress: NonPersistedPhysicalAddress = {
2378
+ type: 'postal',
2379
+ streetName: 'example_street_name',
2380
+ streetNumber: 'example_street_number',
2381
+ buildingName: 'example_building_name',
2382
+ postalCode: 'example_postal_code',
2383
+ cityName: 'example_city_name',
2384
+ provinceName: 'example_province_name',
2385
+ countryCode: 'example_country_code',
2386
+ }
2387
+
2388
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2389
+ const savedPhysicalAddress: PhysicalAddressEntity = await dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity, {
2390
+ transaction: true,
2391
+ })
2392
+
2393
+ const fromDb: PhysicalAddressEntity | null = await dbConnection.getRepository(PhysicalAddressEntity).findOne({
2394
+ where: { id: savedPhysicalAddress.id },
2395
+ })
2396
+
2397
+ expect(fromDb).toBeDefined()
2398
+ expect(fromDb?.type).toEqual(physicalAddress.type)
2399
+ expect(fromDb?.streetName).toEqual(physicalAddress.streetName)
2400
+ expect(fromDb?.streetNumber).toEqual(physicalAddress.streetNumber)
2401
+ expect(fromDb?.buildingName).toEqual(physicalAddress.buildingName)
2402
+ expect(fromDb?.postalCode).toEqual(physicalAddress.postalCode)
2403
+ expect(fromDb?.cityName).toEqual(physicalAddress.cityName)
2404
+ expect(fromDb?.provinceName).toEqual(physicalAddress.provinceName)
2405
+ expect(fromDb?.countryCode).toEqual(physicalAddress.countryCode)
2406
+ expect(fromDb?.createdAt).toBeDefined()
2407
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
2408
+ })
2409
+
2410
+ it('should throw error when saving physical address with blank street name', async (): Promise<void> => {
2411
+ const physicalAddress: NonPersistedPhysicalAddress = {
2412
+ type: 'home',
2413
+ streetName: '',
2414
+ streetNumber: 'example_street_number',
2415
+ buildingName: 'example_building_name',
2416
+ postalCode: 'example_postal_code',
2417
+ cityName: 'example_city_name',
2418
+ provinceName: 'example_province_name',
2419
+ countryCode: 'example_country_code',
2420
+ }
2421
+
2422
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2423
+
2424
+ await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2425
+ 'Blank street names are not allowed',
2426
+ )
2427
+ })
2428
+
2429
+ it('should throw error when saving physical address with blank street number', async (): Promise<void> => {
2430
+ const physicalAddress: NonPersistedPhysicalAddress = {
2431
+ type: 'home',
2432
+ streetName: 'example_street_name',
2433
+ streetNumber: '',
2434
+ buildingName: 'example_building_name',
2435
+ postalCode: 'example_postal_code',
2436
+ cityName: 'example_city_name',
2437
+ provinceName: 'example_province_name',
2438
+ countryCode: 'example_country_code',
2439
+ }
2440
+
2441
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2442
+
2443
+ await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2444
+ 'Blank street numbers are not allowed',
2445
+ )
2446
+ })
2447
+
2448
+ it('should throw error when saving physical address with blank building name', async (): Promise<void> => {
2449
+ const physicalAddress: NonPersistedPhysicalAddress = {
2450
+ type: 'home',
2451
+ streetName: 'example_street_name',
2452
+ streetNumber: 'example_street_number',
2453
+ buildingName: '',
2454
+ postalCode: 'example_postal_code',
2455
+ cityName: 'example_city_name',
2456
+ provinceName: 'example_province_name',
2457
+ countryCode: 'example_country_code',
2458
+ }
2459
+
2460
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2461
+
2462
+ await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2463
+ 'Blank building names are not allowed',
2464
+ )
2465
+ })
2466
+
2467
+ it('should throw error when saving physical address with blank postal code', async (): Promise<void> => {
2468
+ const physicalAddress: NonPersistedPhysicalAddress = {
2469
+ type: 'home',
2470
+ streetName: 'example_street_name',
2471
+ streetNumber: 'example_street_number',
2472
+ buildingName: 'example_building_name',
2473
+ postalCode: '',
2474
+ cityName: 'example_city_name',
2475
+ provinceName: 'example_province_name',
2476
+ countryCode: 'example_country_code',
2477
+ }
2478
+
2479
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2480
+
2481
+ await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2482
+ 'Blank postal codes are not allowed',
2483
+ )
2484
+ })
2485
+
2486
+ it('should throw error when saving physical address with blank city name', async (): Promise<void> => {
2487
+ const physicalAddress: NonPersistedPhysicalAddress = {
2488
+ type: 'home',
2489
+ streetName: 'example_street_name',
2490
+ streetNumber: 'example_street_number',
2491
+ buildingName: 'example_building_name',
2492
+ postalCode: 'example_postal_code',
2493
+ cityName: '',
2494
+ provinceName: 'example_province_name',
2495
+ countryCode: 'example_country_code',
2496
+ }
2497
+
2498
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2499
+
2500
+ await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2501
+ 'Blank city names are not allowed',
2502
+ )
2503
+ })
2504
+
2505
+ it('should throw error when saving physical address with blank province name', async (): Promise<void> => {
2506
+ const physicalAddress: NonPersistedPhysicalAddress = {
2507
+ type: 'home',
2508
+ streetName: 'example_street_name',
2509
+ streetNumber: 'example_street_number',
2510
+ buildingName: 'example_building_name',
2511
+ postalCode: 'example_postal_code',
2512
+ cityName: 'example_city_name',
2513
+ provinceName: '',
2514
+ countryCode: 'example_country_code',
2515
+ }
2516
+
2517
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2518
+
2519
+ await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2520
+ 'Blank province names are not allowed',
2521
+ )
2522
+ })
2523
+
2524
+ it('should throw error when saving physical address with blank country code', async (): Promise<void> => {
2525
+ const physicalAddress: NonPersistedPhysicalAddress = {
2526
+ type: 'home',
2527
+ streetName: 'example_street_name',
2528
+ streetNumber: 'example_street_number',
2529
+ buildingName: 'example_building_name',
2530
+ postalCode: 'example_postal_code',
2531
+ cityName: 'example_city_name',
2532
+ provinceName: 'example_province_name',
2533
+ countryCode: '',
2534
+ }
2535
+
2536
+ const physicalAddressEntity: PhysicalAddressEntity = physicalAddressEntityFrom(physicalAddress)
2537
+
2538
+ await expect(dbConnection.getRepository(PhysicalAddressEntity).save(physicalAddressEntity)).rejects.toThrowError(
2539
+ 'Blank country codes are not allowed',
2540
+ )
2541
+ })
2542
+ })