@sphereon/ssi-sdk.data-store 0.15.2-next.5 → 0.15.2-next.77

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 (213) hide show
  1. package/dist/contact/AbstractContactStore.d.ts +21 -11
  2. package/dist/contact/AbstractContactStore.d.ts.map +1 -1
  3. package/dist/contact/AbstractContactStore.js.map +1 -1
  4. package/dist/contact/ContactStore.d.ts +25 -21
  5. package/dist/contact/ContactStore.d.ts.map +1 -1
  6. package/dist/contact/ContactStore.js +216 -136
  7. package/dist/contact/ContactStore.js.map +1 -1
  8. package/dist/entities/contact/BaseConfigEntity.d.ts +2 -0
  9. package/dist/entities/contact/BaseConfigEntity.d.ts.map +1 -1
  10. package/dist/entities/contact/BaseConfigEntity.js +10 -2
  11. package/dist/entities/contact/BaseConfigEntity.js.map +1 -1
  12. package/dist/entities/contact/BaseContactEntity.d.ts +10 -0
  13. package/dist/entities/contact/BaseContactEntity.d.ts.map +1 -0
  14. package/dist/entities/contact/BaseContactEntity.js +54 -0
  15. package/dist/entities/contact/BaseContactEntity.js.map +1 -0
  16. package/dist/entities/contact/ConnectionEntity.d.ts +1 -2
  17. package/dist/entities/contact/ConnectionEntity.d.ts.map +1 -1
  18. package/dist/entities/contact/ConnectionEntity.js +2 -21
  19. package/dist/entities/contact/ConnectionEntity.js.map +1 -1
  20. package/dist/entities/contact/CorrelationIdentifierEntity.d.ts +2 -3
  21. package/dist/entities/contact/CorrelationIdentifierEntity.d.ts.map +1 -1
  22. package/dist/entities/contact/CorrelationIdentifierEntity.js +8 -11
  23. package/dist/entities/contact/CorrelationIdentifierEntity.js.map +1 -1
  24. package/dist/entities/contact/DidAuthConfigEntity.d.ts +0 -4
  25. package/dist/entities/contact/DidAuthConfigEntity.d.ts.map +1 -1
  26. package/dist/entities/contact/DidAuthConfigEntity.js +1 -17
  27. package/dist/entities/contact/DidAuthConfigEntity.js.map +1 -1
  28. package/dist/entities/contact/ElectronicAddressEntity.d.ts +14 -0
  29. package/dist/entities/contact/ElectronicAddressEntity.d.ts.map +1 -0
  30. package/dist/entities/contact/ElectronicAddressEntity.js +92 -0
  31. package/dist/entities/contact/ElectronicAddressEntity.js.map +1 -0
  32. package/dist/entities/contact/IdentityEntity.d.ts +5 -6
  33. package/dist/entities/contact/IdentityEntity.d.ts.map +1 -1
  34. package/dist/entities/contact/IdentityEntity.js +15 -20
  35. package/dist/entities/contact/IdentityEntity.js.map +1 -1
  36. package/dist/entities/contact/IdentityMetadataItemEntity.d.ts +1 -3
  37. package/dist/entities/contact/IdentityMetadataItemEntity.d.ts.map +1 -1
  38. package/dist/entities/contact/IdentityMetadataItemEntity.js +7 -10
  39. package/dist/entities/contact/IdentityMetadataItemEntity.js.map +1 -1
  40. package/dist/entities/contact/NaturalPersonEntity.d.ts +9 -0
  41. package/dist/entities/contact/NaturalPersonEntity.d.ts.map +1 -0
  42. package/dist/entities/contact/NaturalPersonEntity.js +72 -0
  43. package/dist/entities/contact/NaturalPersonEntity.js.map +1 -0
  44. package/dist/entities/contact/OpenIdConfigEntity.d.ts +0 -4
  45. package/dist/entities/contact/OpenIdConfigEntity.d.ts.map +1 -1
  46. package/dist/entities/contact/OpenIdConfigEntity.js +1 -22
  47. package/dist/entities/contact/OpenIdConfigEntity.js.map +1 -1
  48. package/dist/entities/contact/OrganizationEntity.d.ts +9 -0
  49. package/dist/entities/contact/OrganizationEntity.d.ts.map +1 -0
  50. package/dist/entities/contact/OrganizationEntity.js +67 -0
  51. package/dist/entities/contact/OrganizationEntity.js.map +1 -0
  52. package/dist/entities/contact/PartyEntity.d.ts +21 -0
  53. package/dist/entities/contact/PartyEntity.d.ts.map +1 -0
  54. package/dist/entities/contact/PartyEntity.js +155 -0
  55. package/dist/entities/contact/PartyEntity.js.map +1 -0
  56. package/dist/entities/contact/PartyRelationshipEntity.d.ts +13 -0
  57. package/dist/entities/contact/PartyRelationshipEntity.d.ts.map +1 -0
  58. package/dist/entities/contact/PartyRelationshipEntity.js +95 -0
  59. package/dist/entities/contact/PartyRelationshipEntity.js.map +1 -0
  60. package/dist/entities/contact/PartyTypeEntity.d.ts +15 -0
  61. package/dist/entities/contact/PartyTypeEntity.d.ts.map +1 -0
  62. package/dist/entities/contact/{ContactEntity.js → PartyTypeEntity.js} +35 -36
  63. package/dist/entities/contact/PartyTypeEntity.js.map +1 -0
  64. package/dist/entities/statusList2021/StatusList2021Entity.d.ts +19 -0
  65. package/dist/entities/statusList2021/StatusList2021Entity.d.ts.map +1 -0
  66. package/dist/entities/statusList2021/StatusList2021Entity.js +115 -0
  67. package/dist/entities/statusList2021/StatusList2021Entity.js.map +1 -0
  68. package/dist/entities/statusList2021/StatusList2021EntryEntity.d.ts +11 -0
  69. package/dist/entities/statusList2021/StatusList2021EntryEntity.d.ts.map +1 -0
  70. package/dist/entities/statusList2021/StatusList2021EntryEntity.js +51 -0
  71. package/dist/entities/statusList2021/StatusList2021EntryEntity.js.map +1 -0
  72. package/dist/index.d.ts +20 -9
  73. package/dist/index.d.ts.map +1 -1
  74. package/dist/index.js +27 -11
  75. package/dist/index.js.map +1 -1
  76. package/dist/migrations/generic/1-CreateContacts.d.ts.map +1 -1
  77. package/dist/migrations/generic/1-CreateContacts.js +36 -32
  78. package/dist/migrations/generic/1-CreateContacts.js.map +1 -1
  79. package/dist/migrations/generic/2-CreateContacts.d.ts +7 -0
  80. package/dist/migrations/generic/2-CreateContacts.d.ts.map +1 -0
  81. package/dist/migrations/generic/2-CreateContacts.js +76 -0
  82. package/dist/migrations/generic/2-CreateContacts.js.map +1 -0
  83. package/dist/migrations/generic/{1-CreateIssuanceBranding.d.ts → 2-CreateIssuanceBranding.d.ts} +1 -1
  84. package/dist/migrations/generic/2-CreateIssuanceBranding.d.ts.map +1 -0
  85. package/dist/migrations/generic/2-CreateIssuanceBranding.js +76 -0
  86. package/dist/migrations/generic/2-CreateIssuanceBranding.js.map +1 -0
  87. package/dist/migrations/generic/3-CreateStatusList.d.ts +7 -0
  88. package/dist/migrations/generic/3-CreateStatusList.d.ts.map +1 -0
  89. package/dist/migrations/generic/{1-CreateIssuanceBranding.js → 3-CreateStatusList.js} +11 -11
  90. package/dist/migrations/generic/3-CreateStatusList.js.map +1 -0
  91. package/dist/migrations/generic/index.d.ts +5 -0
  92. package/dist/migrations/generic/index.d.ts.map +1 -1
  93. package/dist/migrations/generic/index.js +10 -3
  94. package/dist/migrations/generic/index.js.map +1 -1
  95. package/dist/migrations/internal-migrations-ormconfig.d.ts +2 -1
  96. package/dist/migrations/internal-migrations-ormconfig.d.ts.map +1 -1
  97. package/dist/migrations/internal-migrations-ormconfig.js +0 -2
  98. package/dist/migrations/internal-migrations-ormconfig.js.map +1 -1
  99. package/dist/migrations/postgres/1659463079428-CreateContacts.d.ts.map +1 -1
  100. package/dist/migrations/postgres/1659463079428-CreateContacts.js +4 -2
  101. package/dist/migrations/postgres/1659463079428-CreateContacts.js.map +1 -1
  102. package/dist/migrations/postgres/1685628974232-CreateIssuanceBranding.d.ts.map +1 -1
  103. package/dist/migrations/postgres/1685628974232-CreateIssuanceBranding.js +2 -0
  104. package/dist/migrations/postgres/1685628974232-CreateIssuanceBranding.js.map +1 -1
  105. package/dist/migrations/postgres/1690925872592-CreateContacts.d.ts +7 -0
  106. package/dist/migrations/postgres/1690925872592-CreateContacts.d.ts.map +1 -0
  107. package/dist/migrations/postgres/1690925872592-CreateContacts.js +69 -0
  108. package/dist/migrations/postgres/1690925872592-CreateContacts.js.map +1 -0
  109. package/dist/migrations/postgres/CreateStatusList1693866470001-CreateStatusList.d.ts +7 -0
  110. package/dist/migrations/postgres/CreateStatusList1693866470001-CreateStatusList.d.ts.map +1 -0
  111. package/dist/migrations/postgres/CreateStatusList1693866470001-CreateStatusList.js +32 -0
  112. package/dist/migrations/postgres/CreateStatusList1693866470001-CreateStatusList.js.map +1 -0
  113. package/dist/migrations/postgres/uuid.d.ts +3 -0
  114. package/dist/migrations/postgres/uuid.d.ts.map +1 -0
  115. package/dist/migrations/postgres/uuid.js +25 -0
  116. package/dist/migrations/postgres/uuid.js.map +1 -0
  117. package/dist/migrations/sqlite/1690925872693-CreateContacts.d.ts +7 -0
  118. package/dist/migrations/sqlite/1690925872693-CreateContacts.d.ts.map +1 -0
  119. package/dist/migrations/sqlite/1690925872693-CreateContacts.js +108 -0
  120. package/dist/migrations/sqlite/1690925872693-CreateContacts.js.map +1 -0
  121. package/dist/migrations/sqlite/1693866470000-CreateStatusList.d.ts +7 -0
  122. package/dist/migrations/sqlite/1693866470000-CreateStatusList.d.ts.map +1 -0
  123. package/dist/migrations/sqlite/1693866470000-CreateStatusList.js +32 -0
  124. package/dist/migrations/sqlite/1693866470000-CreateStatusList.js.map +1 -0
  125. package/dist/statusList/IStatusListStore.d.ts +20 -0
  126. package/dist/statusList/IStatusListStore.d.ts.map +1 -0
  127. package/dist/statusList/IStatusListStore.js +3 -0
  128. package/dist/statusList/IStatusListStore.js.map +1 -0
  129. package/dist/statusList/StatusListStore.d.ts +36 -0
  130. package/dist/statusList/StatusListStore.d.ts.map +1 -0
  131. package/dist/statusList/StatusListStore.js +213 -0
  132. package/dist/statusList/StatusListStore.js.map +1 -0
  133. package/dist/types/contact/IAbstractContactStore.d.ts +70 -36
  134. package/dist/types/contact/IAbstractContactStore.d.ts.map +1 -1
  135. package/dist/types/contact/contact.d.ts +139 -56
  136. package/dist/types/contact/contact.d.ts.map +1 -1
  137. package/dist/types/contact/contact.js +6 -1
  138. package/dist/types/contact/contact.js.map +1 -1
  139. package/dist/types/index.d.ts +3 -0
  140. package/dist/types/index.d.ts.map +1 -1
  141. package/dist/types/index.js +3 -0
  142. package/dist/types/index.js.map +1 -1
  143. package/dist/types/statusList/IAbstractStatusListStore.d.ts +38 -0
  144. package/dist/types/statusList/IAbstractStatusListStore.d.ts.map +1 -0
  145. package/dist/types/statusList/IAbstractStatusListStore.js +3 -0
  146. package/dist/types/statusList/IAbstractStatusListStore.js.map +1 -0
  147. package/dist/types/statusList/statusList.d.ts +25 -0
  148. package/dist/types/statusList/statusList.d.ts.map +1 -0
  149. package/dist/types/statusList/statusList.js +3 -0
  150. package/dist/types/statusList/statusList.js.map +1 -0
  151. package/dist/types/validation/validation.d.ts +4 -0
  152. package/dist/types/validation/validation.d.ts.map +1 -0
  153. package/dist/types/validation/validation.js +3 -0
  154. package/dist/types/validation/validation.js.map +1 -0
  155. package/dist/utils/ValidatorUtils.d.ts +4 -0
  156. package/dist/utils/ValidatorUtils.d.ts.map +1 -0
  157. package/dist/utils/ValidatorUtils.js +13 -0
  158. package/dist/utils/ValidatorUtils.js.map +1 -0
  159. package/dist/utils/contact/MappingUtils.d.ts +47 -0
  160. package/dist/utils/contact/MappingUtils.d.ts.map +1 -0
  161. package/dist/utils/contact/MappingUtils.js +291 -0
  162. package/dist/utils/contact/MappingUtils.js.map +1 -0
  163. package/package.json +5 -4
  164. package/src/__tests__/contact.entities.test.ts +1457 -195
  165. package/src/__tests__/contact.store.test.ts +1302 -266
  166. package/src/contact/AbstractContactStore.ts +44 -22
  167. package/src/contact/ContactStore.ts +328 -204
  168. package/src/entities/contact/BaseConfigEntity.ts +10 -2
  169. package/src/entities/contact/BaseContactEntity.ts +30 -0
  170. package/src/entities/contact/ConnectionEntity.ts +4 -23
  171. package/src/entities/contact/CorrelationIdentifierEntity.ts +9 -13
  172. package/src/entities/contact/DidAuthConfigEntity.ts +1 -18
  173. package/src/entities/contact/ElectronicAddressEntity.ts +60 -0
  174. package/src/entities/contact/IdentityEntity.ts +18 -25
  175. package/src/entities/contact/IdentityMetadataItemEntity.ts +8 -12
  176. package/src/entities/contact/NaturalPersonEntity.ts +38 -0
  177. package/src/entities/contact/OpenIdConfigEntity.ts +1 -23
  178. package/src/entities/contact/OrganizationEntity.ts +34 -0
  179. package/src/entities/contact/PartyEntity.ts +118 -0
  180. package/src/entities/contact/PartyRelationshipEntity.ts +61 -0
  181. package/src/entities/contact/PartyTypeEntity.ts +59 -0
  182. package/src/entities/statusList2021/StatusList2021Entity.ts +95 -0
  183. package/src/entities/statusList2021/StatusList2021EntryEntity.ts +29 -0
  184. package/src/index.ts +36 -17
  185. package/src/migrations/generic/1-CreateContacts.ts +46 -36
  186. package/src/migrations/generic/2-CreateContacts.ts +64 -0
  187. package/src/migrations/generic/2-CreateIssuanceBranding.ts +62 -0
  188. package/src/migrations/generic/{1-CreateIssuanceBranding.ts → 3-CreateStatusList.ts} +9 -9
  189. package/src/migrations/generic/index.ts +11 -2
  190. package/src/migrations/internal-migrations-ormconfig.ts +2 -4
  191. package/src/migrations/postgres/1659463079428-CreateContacts.ts +4 -2
  192. package/src/migrations/postgres/1685628974232-CreateIssuanceBranding.ts +2 -0
  193. package/src/migrations/postgres/1690925872592-CreateContacts.ts +94 -0
  194. package/src/migrations/postgres/CreateStatusList1693866470001-CreateStatusList.ts +24 -0
  195. package/src/migrations/postgres/uuid.ts +12 -0
  196. package/src/migrations/sqlite/1690925872693-CreateContacts.ts +163 -0
  197. package/src/migrations/sqlite/1693866470000-CreateStatusList.ts +24 -0
  198. package/src/statusList/IStatusListStore.ts +44 -0
  199. package/src/statusList/StatusListStore.ts +237 -0
  200. package/src/types/contact/IAbstractContactStore.ts +84 -27
  201. package/src/types/contact/contact.ts +144 -50
  202. package/src/types/index.ts +4 -0
  203. package/src/types/statusList/IAbstractStatusListStore.ts +47 -0
  204. package/src/types/statusList/statusList.ts +39 -0
  205. package/src/types/validation/validation.ts +3 -0
  206. package/src/utils/ValidatorUtils.ts +10 -0
  207. package/src/utils/contact/MappingUtils.ts +344 -0
  208. package/dist/entities/contact/ContactEntity.d.ts +0 -16
  209. package/dist/entities/contact/ContactEntity.d.ts.map +0 -1
  210. package/dist/entities/contact/ContactEntity.js.map +0 -1
  211. package/dist/migrations/generic/1-CreateIssuanceBranding.d.ts.map +0 -1
  212. package/dist/migrations/generic/1-CreateIssuanceBranding.js.map +0 -1
  213. package/src/entities/contact/ContactEntity.ts +0 -74
@@ -1,24 +1,45 @@
1
- import { DataSource } from 'typeorm'
2
-
1
+ import { DataSource, FindOptionsWhere } from 'typeorm'
2
+
3
+ import { DataStoreContactEntities, DataStoreMigrations } from '../index'
4
+ import { NaturalPersonEntity } from '../entities/contact/NaturalPersonEntity'
5
+ import { OrganizationEntity } from '../entities/contact/OrganizationEntity'
6
+ import { PartyRelationshipEntity } from '../entities/contact/PartyRelationshipEntity'
7
+ import { PartyTypeEntity } from '../entities/contact/PartyTypeEntity'
8
+ import { PartyEntity } from '../entities/contact/PartyEntity'
9
+ import { IdentityEntity } from '../entities/contact/IdentityEntity'
10
+ import { OpenIdConfigEntity } from '../entities/contact/OpenIdConfigEntity'
11
+ import { DidAuthConfigEntity } from '../entities/contact/DidAuthConfigEntity'
12
+ import { ConnectionEntity } from '../entities/contact/ConnectionEntity'
13
+ import { CorrelationIdentifierEntity } from '../entities/contact/CorrelationIdentifierEntity'
14
+ import { IdentityMetadataItemEntity } from '../entities/contact/IdentityMetadataItemEntity'
15
+ import { BaseContactEntity } from '../entities/contact/BaseContactEntity'
3
16
  import {
17
+ NonPersistedParty,
18
+ PartyTypeEnum,
19
+ NaturalPerson,
20
+ Organization,
21
+ IdentityRoleEnum,
4
22
  CorrelationIdentifierEnum,
5
- DataStoreContactEntities,
6
- DataStoreMigrations,
7
- contactEntityFrom,
23
+ ConnectionTypeEnum,
24
+ NonPersistedPartyType,
25
+ NonPersistedOrganization,
26
+ NonPersistedNaturalPerson,
27
+ NonPersistedConnection,
28
+ NonPersistedIdentity,
29
+ NonPersistedDidAuthConfig,
30
+ NonPersistedOpenIdConfig,
31
+ } from '../types'
32
+ import {
8
33
  connectionEntityFrom,
9
- identityEntityFrom,
10
34
  didAuthConfigEntityFrom,
35
+ identityEntityFrom,
36
+ naturalPersonEntityFrom,
11
37
  openIdConfigEntityFrom,
12
- ContactEntity,
13
- IdentityEntity,
14
- ConnectionTypeEnum,
15
- OpenIdConfigEntity,
16
- DidAuthConfigEntity,
17
- ConnectionEntity,
18
- CorrelationIdentifierEntity,
19
- IdentityMetadataItemEntity,
20
- IdentityRoleEnum,
21
- } from '../index'
38
+ organizationEntityFrom,
39
+ partyEntityFrom,
40
+ partyRelationshipEntityFrom,
41
+ partyTypeEntityFrom,
42
+ } from '../utils/contact/MappingUtils'
22
43
 
23
44
  describe('Database entities tests', (): void => {
24
45
  let dbConnection: DataSource
@@ -27,7 +48,7 @@ describe('Database entities tests', (): void => {
27
48
  dbConnection = await new DataSource({
28
49
  type: 'sqlite',
29
50
  database: ':memory:',
30
- //logging: 'all',
51
+ logging: 'all',
31
52
  migrationsRun: false,
32
53
  migrations: DataStoreMigrations,
33
54
  synchronize: false,
@@ -41,98 +62,333 @@ describe('Database entities tests', (): void => {
41
62
  await (await dbConnection).destroy()
42
63
  })
43
64
 
44
- it('Should save contact to database', async (): Promise<void> => {
45
- const contact = {
46
- name: 'test_name',
47
- alias: 'test_alias',
65
+ it('Should save person party to database', async (): Promise<void> => {
66
+ const party: NonPersistedParty = {
48
67
  uri: 'example.com',
68
+ partyType: {
69
+ type: PartyTypeEnum.NATURAL_PERSON,
70
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
71
+ name: 'example_name',
72
+ },
73
+ contact: {
74
+ firstName: 'example_first_name',
75
+ middleName: 'example_middle_name',
76
+ lastName: 'example_last_name',
77
+ displayName: 'example_display_name',
78
+ },
49
79
  }
50
80
 
51
- const contactEntity: ContactEntity = contactEntityFrom(contact)
52
- await dbConnection.getRepository(ContactEntity).save(contactEntity)
81
+ const partyEntity: PartyEntity = partyEntityFrom(party)
82
+ await dbConnection.getRepository(PartyEntity).save(partyEntity, {
83
+ transaction: true,
84
+ })
53
85
 
54
- const fromDb = await dbConnection.getRepository(ContactEntity).findOne({
55
- where: { name: contact.name },
86
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
87
+ where: { id: partyEntity.id },
56
88
  })
57
89
 
58
90
  expect(fromDb).toBeDefined()
59
91
  expect(fromDb?.identities?.length).toEqual(0)
60
- expect(fromDb?.name).toEqual(contact.name)
61
- expect(fromDb?.alias).toEqual(contact.alias)
62
- expect(fromDb?.uri).toEqual(contact.uri)
92
+ expect(fromDb?.uri).toEqual(party.uri)
93
+ expect(fromDb?.partyType).toBeDefined()
94
+ expect(fromDb?.partyType.type).toEqual(party.partyType.type)
95
+ expect(fromDb?.partyType.tenantId).toEqual(party.partyType.tenantId)
96
+ expect(fromDb?.partyType.name).toEqual(party.partyType.name)
97
+ expect(fromDb?.contact).toBeDefined()
98
+ expect((<NaturalPersonEntity>fromDb?.contact).firstName).toEqual((<NaturalPerson>party.contact).firstName)
99
+ expect((<NaturalPersonEntity>fromDb?.contact).middleName).toEqual((<NaturalPerson>party.contact).middleName)
100
+ expect((<NaturalPersonEntity>fromDb?.contact).lastName).toEqual((<NaturalPerson>party.contact).lastName)
101
+ expect((<NaturalPersonEntity>fromDb?.contact).displayName).toEqual((<NaturalPerson>party.contact).displayName)
63
102
  })
64
103
 
65
- it('should throw error when saving contact with blank name', async (): Promise<void> => {
66
- const contact = {
67
- name: '',
68
- alias: 'test_alias',
104
+ it('Should save organization party to database', async (): Promise<void> => {
105
+ const party: NonPersistedParty = {
69
106
  uri: 'example.com',
107
+ partyType: {
108
+ type: PartyTypeEnum.ORGANIZATION,
109
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
110
+ name: 'example_name',
111
+ },
112
+ contact: {
113
+ legalName: 'example_legal_name',
114
+ displayName: 'example_display_name',
115
+ },
70
116
  }
71
117
 
72
- const contactEntity: ContactEntity = contactEntityFrom(contact)
118
+ const partyEntity: PartyEntity = partyEntityFrom(party)
119
+ await dbConnection.getRepository(PartyEntity).save(partyEntity, {
120
+ transaction: true,
121
+ })
122
+
123
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
124
+ where: { id: partyEntity.id },
125
+ })
73
126
 
74
- await expect(dbConnection.getRepository(ContactEntity).save(contactEntity)).rejects.toThrow('Blank names are not allowed')
127
+ expect(fromDb).toBeDefined()
128
+ expect(fromDb?.identities?.length).toEqual(0)
129
+ expect(fromDb?.uri).toEqual(party.uri)
130
+ expect(fromDb?.partyType).toBeDefined()
131
+ expect(fromDb?.partyType.type).toEqual(party.partyType.type)
132
+ expect(fromDb?.partyType.tenantId).toEqual(party.partyType.tenantId)
133
+ expect(fromDb?.partyType.name).toEqual(party.partyType.name)
134
+ expect(fromDb?.contact).toBeDefined()
135
+ expect((<OrganizationEntity>fromDb?.contact).legalName).toEqual((<Organization>party.contact).legalName)
136
+ expect((<OrganizationEntity>fromDb?.contact).displayName).toEqual((<Organization>party.contact).displayName)
75
137
  })
76
138
 
77
- it('should throw error when saving contact with blank alias', async (): Promise<void> => {
78
- const contact = {
79
- name: 'test_name',
80
- alias: '',
139
+ it('Should result in party relationship for the owner side only', async (): Promise<void> => {
140
+ const party1: NonPersistedParty = {
141
+ uri: 'example1.com',
142
+ partyType: {
143
+ type: PartyTypeEnum.NATURAL_PERSON,
144
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
145
+ name: 'example_name1',
146
+ },
147
+ contact: {
148
+ firstName: 'example_first_name1',
149
+ middleName: 'example_middle_name1',
150
+ lastName: 'example_last_name1',
151
+ displayName: 'example_display_name1',
152
+ },
153
+ }
154
+
155
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
156
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
157
+ transaction: true,
158
+ })
159
+
160
+ const party2: NonPersistedParty = {
161
+ uri: 'example2.com',
162
+ partyType: {
163
+ type: PartyTypeEnum.NATURAL_PERSON,
164
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
165
+ name: 'example_name2',
166
+ },
167
+ contact: {
168
+ firstName: 'example_first_name2',
169
+ middleName: 'example_middle_name2',
170
+ lastName: 'example_last_name2',
171
+ displayName: 'example_display_name2',
172
+ },
173
+ }
174
+
175
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
176
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
177
+ transaction: true,
178
+ })
179
+
180
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
181
+ leftId: savedParty1.id,
182
+ rightId: savedParty2.id,
183
+ })
184
+
185
+ await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
186
+ transaction: true,
187
+ })
188
+
189
+ const fromDb1: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
190
+ where: { id: savedParty1.id },
191
+ })
192
+
193
+ const fromDb2: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
194
+ where: { id: savedParty2.id },
195
+ })
196
+
197
+ expect(fromDb1).toBeDefined()
198
+ expect(fromDb1?.relationships.length).toEqual(1)
199
+ expect(fromDb2).toBeDefined()
200
+ expect(fromDb2?.relationships.length).toEqual(0)
201
+ })
202
+
203
+ it('should throw error when saving person party with blank first name', async (): Promise<void> => {
204
+ const party: NonPersistedParty = {
81
205
  uri: 'example.com',
206
+ partyType: {
207
+ type: PartyTypeEnum.NATURAL_PERSON,
208
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
209
+ name: 'example_name',
210
+ },
211
+ contact: {
212
+ firstName: '',
213
+ middleName: 'example_middle_name1',
214
+ lastName: 'example_last_name1',
215
+ displayName: 'example_display_name1',
216
+ },
82
217
  }
83
218
 
84
- const contactEntity: ContactEntity = contactEntityFrom(contact)
219
+ const partyEntity: PartyEntity = partyEntityFrom(party)
85
220
 
86
- await expect(dbConnection.getRepository(ContactEntity).save(contactEntity)).rejects.toThrow('Blank aliases are not allowed')
221
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank first names are not allowed')
87
222
  })
88
223
 
89
- it('Should enforce unique name for a contact', async (): Promise<void> => {
90
- const contactName = 'non_unique_name'
91
- const contact1 = {
92
- name: contactName,
93
- alias: 'unique_alias1',
224
+ it('should throw error when saving person party with blank middle name', async (): Promise<void> => {
225
+ const party: NonPersistedParty = {
94
226
  uri: 'example.com',
227
+ partyType: {
228
+ type: PartyTypeEnum.NATURAL_PERSON,
229
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
230
+ name: 'example_name',
231
+ },
232
+ contact: {
233
+ firstName: 'example_first_name',
234
+ middleName: '',
235
+ lastName: 'example_last_name',
236
+ displayName: 'example_display_name',
237
+ },
95
238
  }
96
- const contact1Entity: ContactEntity = contactEntityFrom(contact1)
97
- await dbConnection.getRepository(ContactEntity).save(contact1Entity)
98
239
 
99
- const contact2 = {
100
- name: contactName,
101
- alias: 'unique_alias2',
240
+ const partyEntity: PartyEntity = partyEntityFrom(party)
241
+
242
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank middle names are not allowed')
243
+ })
244
+
245
+ it('should throw error when saving person party with blank last name', async (): Promise<void> => {
246
+ const party: NonPersistedParty = {
102
247
  uri: 'example.com',
248
+ partyType: {
249
+ type: PartyTypeEnum.NATURAL_PERSON,
250
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
251
+ name: 'example_name',
252
+ },
253
+ contact: {
254
+ firstName: 'example_first_name',
255
+ middleName: 'example_middle_name',
256
+ lastName: '',
257
+ displayName: 'example_display_name',
258
+ },
103
259
  }
104
- const contact2Entity: ContactEntity = contactEntityFrom(contact2)
105
260
 
106
- await expect(dbConnection.getRepository(ContactEntity).save(contact2Entity)).rejects.toThrowError(
107
- 'SQLITE_CONSTRAINT: UNIQUE constraint failed: Contact.name'
108
- )
261
+ const partyEntity: PartyEntity = partyEntityFrom(party)
262
+
263
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank last names are not allowed')
109
264
  })
110
265
 
111
- it('Should enforce unique alias for a contact', async (): Promise<void> => {
112
- const alias = 'non_unique_alias'
113
- const contact1 = {
114
- name: 'unique_name1',
115
- alias,
266
+ it('should throw error when saving person party with blank display name', async (): Promise<void> => {
267
+ const party: NonPersistedParty = {
116
268
  uri: 'example.com',
269
+ partyType: {
270
+ type: PartyTypeEnum.NATURAL_PERSON,
271
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
272
+ name: 'example_name',
273
+ },
274
+ contact: {
275
+ firstName: 'example_first_name',
276
+ middleName: 'example_middle_name',
277
+ lastName: 'example_last_name',
278
+ displayName: '',
279
+ },
117
280
  }
118
- const contact1Entity: ContactEntity = contactEntityFrom(contact1)
119
- await dbConnection.getRepository(ContactEntity).save(contact1Entity)
120
281
 
121
- const contact2 = {
122
- name: 'unique_name2',
123
- alias,
282
+ const partyEntity: PartyEntity = partyEntityFrom(party)
283
+
284
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank display names are not allowed')
285
+ })
286
+
287
+ it('should throw error when saving organization party with blank legal name', async (): Promise<void> => {
288
+ const party: NonPersistedParty = {
124
289
  uri: 'example.com',
290
+ partyType: {
291
+ type: PartyTypeEnum.ORGANIZATION,
292
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
293
+ name: 'example_name',
294
+ },
295
+ contact: {
296
+ legalName: '',
297
+ displayName: 'example_legal_name',
298
+ },
125
299
  }
126
- const contact2Entity: ContactEntity = contactEntityFrom(contact2)
127
300
 
128
- await expect(dbConnection.getRepository(ContactEntity).save(contact2Entity)).rejects.toThrowError(
129
- 'SQLITE_CONSTRAINT: UNIQUE constraint failed: Contact.alias'
130
- )
301
+ const partyEntity: PartyEntity = partyEntityFrom(party)
302
+
303
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank legal names are not allowed')
304
+ })
305
+
306
+ it('should throw error when saving organization party with blank display name', async (): Promise<void> => {
307
+ const party: NonPersistedParty = {
308
+ uri: 'example.com',
309
+ partyType: {
310
+ type: PartyTypeEnum.ORGANIZATION,
311
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
312
+ name: 'example_name',
313
+ },
314
+ contact: {
315
+ legalName: 'example_first_name',
316
+ displayName: '',
317
+ },
318
+ }
319
+
320
+ const partyEntity: PartyEntity = partyEntityFrom(party)
321
+
322
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank display names are not allowed')
323
+ })
324
+
325
+ it('should throw error when saving party with blank party type name', async (): Promise<void> => {
326
+ const party: NonPersistedParty = {
327
+ uri: 'example.com',
328
+ partyType: {
329
+ type: PartyTypeEnum.NATURAL_PERSON,
330
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
331
+ name: '',
332
+ },
333
+ contact: {
334
+ firstName: 'example_first_name',
335
+ middleName: 'example_middle_name',
336
+ lastName: 'example_last_name',
337
+ displayName: 'example_display_name',
338
+ },
339
+ }
340
+
341
+ const partyEntity: PartyEntity = partyEntityFrom(party)
342
+
343
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank names are not allowed')
344
+ })
345
+
346
+ it('should throw error when saving party with blank party type description', async (): Promise<void> => {
347
+ const party: NonPersistedParty = {
348
+ uri: 'example.com',
349
+ partyType: {
350
+ type: PartyTypeEnum.NATURAL_PERSON,
351
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
352
+ name: 'example_name',
353
+ description: '',
354
+ },
355
+ contact: {
356
+ firstName: 'example_first_name',
357
+ middleName: 'example_middle_name',
358
+ lastName: 'example_last_name',
359
+ displayName: 'example_display_name',
360
+ },
361
+ }
362
+
363
+ const partyEntity: PartyEntity = partyEntityFrom(party)
364
+
365
+ await expect(dbConnection.getRepository(PartyEntity).save(partyEntity)).rejects.toThrowError('Blank descriptions are not allowed')
366
+ })
367
+
368
+ it('should throw error when saving party with blank party type tenant id', async (): Promise<void> => {
369
+ const party: NonPersistedParty = {
370
+ uri: 'example.com',
371
+ partyType: {
372
+ type: PartyTypeEnum.NATURAL_PERSON,
373
+ tenantId: '',
374
+ name: 'example_name',
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 tenant id's are not allowed")
131
387
  })
132
388
 
133
389
  it('Should enforce unique alias for an identity', async (): Promise<void> => {
134
390
  const alias = 'non_unique_alias'
135
- const identity1 = {
391
+ const identity1: NonPersistedIdentity = {
136
392
  alias,
137
393
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
138
394
  identifier: {
@@ -143,7 +399,7 @@ describe('Database entities tests', (): void => {
143
399
  const identity1Entity: IdentityEntity = identityEntityFrom(identity1)
144
400
  await dbConnection.getRepository(IdentityEntity).save(identity1Entity)
145
401
 
146
- const identity2 = {
402
+ const identity2: NonPersistedIdentity = {
147
403
  alias: alias,
148
404
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
149
405
  identifier: {
@@ -159,7 +415,7 @@ describe('Database entities tests', (): void => {
159
415
 
160
416
  it('Should enforce unique correlationId for a identity', async (): Promise<void> => {
161
417
  const correlationId = 'non_unique_correlationId'
162
- const identity1 = {
418
+ const identity1: NonPersistedIdentity = {
163
419
  alias: 'unique_alias1',
164
420
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
165
421
  identifier: {
@@ -170,7 +426,7 @@ describe('Database entities tests', (): void => {
170
426
  const identity1Entity: IdentityEntity = identityEntityFrom(identity1)
171
427
  await dbConnection.getRepository(IdentityEntity).save(identity1Entity)
172
428
 
173
- const identity2 = {
429
+ const identity2: NonPersistedIdentity = {
174
430
  alias: 'unique_alias2',
175
431
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
176
432
  identifier: {
@@ -186,7 +442,7 @@ describe('Database entities tests', (): void => {
186
442
 
187
443
  it('Should save identity to database', async (): Promise<void> => {
188
444
  const correlationId = 'example_did'
189
- const identity = {
445
+ const identity: NonPersistedIdentity = {
190
446
  alias: correlationId,
191
447
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
192
448
  identifier: {
@@ -199,7 +455,7 @@ describe('Database entities tests', (): void => {
199
455
 
200
456
  await dbConnection.getRepository(IdentityEntity).save(identityEntity)
201
457
 
202
- const fromDb = await dbConnection.getRepository(IdentityEntity).findOne({
458
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
203
459
  where: {
204
460
  identifier: {
205
461
  correlationId,
@@ -215,7 +471,7 @@ describe('Database entities tests', (): void => {
215
471
  })
216
472
 
217
473
  it('should throw error when saving identity with blank alias', async (): Promise<void> => {
218
- const identity = {
474
+ const identity: NonPersistedIdentity = {
219
475
  alias: '',
220
476
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
221
477
  identifier: {
@@ -226,11 +482,11 @@ describe('Database entities tests', (): void => {
226
482
 
227
483
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
228
484
 
229
- await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrow('Blank aliases are not allowed')
485
+ await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank aliases are not allowed')
230
486
  })
231
487
 
232
488
  it('should throw error when saving identity with blank correlation id', async (): Promise<void> => {
233
- const identity = {
489
+ const identity: NonPersistedIdentity = {
234
490
  alias: 'example_did',
235
491
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
236
492
  identifier: {
@@ -241,12 +497,12 @@ describe('Database entities tests', (): void => {
241
497
 
242
498
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
243
499
 
244
- await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrow('Blank correlation ids are not allowed')
500
+ await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank correlation ids are not allowed')
245
501
  })
246
502
 
247
503
  it('should throw error when saving identity with blank metadata label', async (): Promise<void> => {
248
504
  const correlationId = 'example_did'
249
- const identity = {
505
+ const identity: NonPersistedIdentity = {
250
506
  alias: correlationId,
251
507
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
252
508
  identifier: {
@@ -263,12 +519,12 @@ describe('Database entities tests', (): void => {
263
519
 
264
520
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
265
521
 
266
- await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrow('Blank metadata labels are not allowed')
522
+ await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank metadata labels are not allowed')
267
523
  })
268
524
 
269
525
  it('should throw error when saving identity with blank metadata value', async (): Promise<void> => {
270
526
  const correlationId = 'example_did'
271
- const identity = {
527
+ const identity: NonPersistedIdentity = {
272
528
  alias: correlationId,
273
529
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
274
530
  identifier: {
@@ -285,12 +541,12 @@ describe('Database entities tests', (): void => {
285
541
 
286
542
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
287
543
 
288
- await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrow('Blank metadata values are not allowed')
544
+ await expect(dbConnection.getRepository(IdentityEntity).save(identityEntity)).rejects.toThrowError('Blank metadata values are not allowed')
289
545
  })
290
546
 
291
547
  it('Should save identity with openid connection to database', async (): Promise<void> => {
292
548
  const correlationId = 'example.com'
293
- const identity = {
549
+ const identity: NonPersistedIdentity = {
294
550
  alias: correlationId,
295
551
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
296
552
  identifier: {
@@ -306,7 +562,7 @@ describe('Database entities tests', (): void => {
306
562
  issuer: 'https://example.com/app-test',
307
563
  redirectUrl: 'app:/callback',
308
564
  dangerouslyAllowInsecureHttpRequests: true,
309
- clientAuthMethod: 'post' as const,
565
+ clientAuthMethod: <const>'post',
310
566
  },
311
567
  },
312
568
  }
@@ -315,7 +571,7 @@ describe('Database entities tests', (): void => {
315
571
 
316
572
  await dbConnection.getRepository(IdentityEntity).save(identityEntity)
317
573
 
318
- const fromDb = await dbConnection.getRepository(IdentityEntity).findOne({
574
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
319
575
  where: {
320
576
  identifier: {
321
577
  correlationId,
@@ -328,14 +584,14 @@ describe('Database entities tests', (): void => {
328
584
  expect(fromDb?.identifier).toBeDefined()
329
585
  expect(fromDb?.identifier.correlationId).toEqual(identity.identifier.correlationId)
330
586
  expect(fromDb?.identifier.type).toEqual(identity.identifier.type)
331
- expect(fromDb?.connection?.type).toEqual(identity.connection.type)
587
+ expect(fromDb?.connection?.type).toEqual(identity.connection?.type)
332
588
  expect(fromDb?.connection?.config).toBeDefined()
333
- expect((fromDb?.connection?.config as OpenIdConfigEntity).clientId).toEqual(identity.connection.config.clientId)
589
+ expect((<OpenIdConfigEntity>fromDb?.connection?.config).clientId).toEqual((<NonPersistedOpenIdConfig>identity.connection?.config).clientId)
334
590
  })
335
591
 
336
592
  it('Should save identity with didauth connection to database', async (): Promise<void> => {
337
593
  const correlationId = 'example.com'
338
- const identity = {
594
+ const identity: NonPersistedIdentity = {
339
595
  alias: correlationId,
340
596
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
341
597
  identifier: {
@@ -362,7 +618,7 @@ describe('Database entities tests', (): void => {
362
618
 
363
619
  await dbConnection.getRepository(IdentityEntity).save(identityEntity)
364
620
 
365
- const fromDb = await dbConnection.getRepository(IdentityEntity).findOne({
621
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
366
622
  where: {
367
623
  identifier: {
368
624
  correlationId,
@@ -375,13 +631,15 @@ describe('Database entities tests', (): void => {
375
631
  expect(fromDb?.identifier).toBeDefined()
376
632
  expect(fromDb?.identifier.correlationId).toEqual(identity.identifier.correlationId)
377
633
  expect(fromDb?.identifier.type).toEqual(identity.identifier.type)
378
- expect(fromDb?.connection?.type).toEqual(identity.connection.type)
634
+ expect(fromDb?.connection?.type).toEqual(identity.connection?.type)
379
635
  expect(fromDb?.connection?.config).toBeDefined()
380
- expect((fromDb?.connection?.config as DidAuthConfigEntity).identifier).toEqual(identity.connection.config.identifier.did)
636
+ expect((<DidAuthConfigEntity>fromDb?.connection?.config).identifier).toEqual(
637
+ (<NonPersistedDidAuthConfig>identity.connection?.config).identifier.did
638
+ )
381
639
  })
382
640
 
383
641
  it('Should save connection with openid config to database', async (): Promise<void> => {
384
- const connection = {
642
+ const connection: NonPersistedConnection = {
385
643
  type: ConnectionTypeEnum.OPENID_CONNECT,
386
644
  config: {
387
645
  clientId: '138d7bf8-c930-4c6e-b928-97d3a4928b01',
@@ -390,32 +648,32 @@ describe('Database entities tests', (): void => {
390
648
  issuer: 'https://example.com/app-test',
391
649
  redirectUrl: 'app:/callback',
392
650
  dangerouslyAllowInsecureHttpRequests: true,
393
- clientAuthMethod: 'post' as const,
651
+ clientAuthMethod: <const>'post',
394
652
  },
395
653
  }
396
- const connectionEntity = connectionEntityFrom(connection)
654
+ const connectionEntity: ConnectionEntity = connectionEntityFrom(connection)
397
655
  await dbConnection.getRepository(ConnectionEntity).save(connectionEntity, {
398
656
  transaction: true,
399
657
  })
400
658
 
401
- const fromDb = await dbConnection.getRepository(ConnectionEntity).findOne({
659
+ const fromDb: ConnectionEntity | null = await dbConnection.getRepository(ConnectionEntity).findOne({
402
660
  where: { type: connection.type },
403
661
  })
404
662
 
405
663
  expect(fromDb).toBeDefined()
406
664
 
407
- const fromDbConfig = await dbConnection.getRepository(OpenIdConfigEntity).findOne({
665
+ const fromDbConfig: OpenIdConfigEntity | null = await dbConnection.getRepository(OpenIdConfigEntity).findOne({
408
666
  where: { id: fromDb?.id },
409
667
  })
410
668
 
411
669
  expect(fromDbConfig).toBeDefined()
412
670
  expect(fromDb?.type).toEqual(connection.type)
413
671
  expect(fromDb?.config).toBeDefined()
414
- expect((fromDb?.config as OpenIdConfigEntity).clientId).toEqual(connection.config.clientId)
672
+ expect((<OpenIdConfigEntity>fromDb?.config).clientId).toEqual((<NonPersistedOpenIdConfig>connection.config).clientId)
415
673
  })
416
674
 
417
675
  it('Should save connection with didauth config to database', async (): Promise<void> => {
418
- const connection = {
676
+ const connection: NonPersistedConnection = {
419
677
  type: ConnectionTypeEnum.SIOPv2,
420
678
  config: {
421
679
  identifier: {
@@ -429,55 +687,55 @@ describe('Database entities tests', (): void => {
429
687
  sessionId: 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
430
688
  },
431
689
  }
432
- const connectionEntity = connectionEntityFrom(connection)
690
+ const connectionEntity: ConnectionEntity = connectionEntityFrom(connection)
433
691
  await dbConnection.getRepository(ConnectionEntity).save(connectionEntity, {
434
692
  transaction: true,
435
693
  })
436
694
 
437
- const fromDb = await dbConnection.getRepository(ConnectionEntity).findOne({
695
+ const fromDb: ConnectionEntity | null = await dbConnection.getRepository(ConnectionEntity).findOne({
438
696
  where: { type: connection.type },
439
697
  })
440
698
 
441
699
  expect(fromDb).toBeDefined()
442
700
 
443
- const fromDbConfig = await dbConnection.getRepository(DidAuthConfigEntity).findOne({
701
+ const fromDbConfig: DidAuthConfigEntity | null = await dbConnection.getRepository(DidAuthConfigEntity).findOne({
444
702
  where: { id: fromDb?.id },
445
703
  })
446
704
 
447
705
  expect(fromDbConfig).toBeDefined()
448
706
  expect(fromDb?.type).toEqual(connection.type)
449
707
  expect(fromDb?.config).toBeDefined()
450
- expect((fromDb?.config as DidAuthConfigEntity).identifier).toEqual(connection.config.identifier.did)
708
+ expect((<DidAuthConfigEntity>fromDb?.config).identifier).toEqual((<NonPersistedDidAuthConfig>connection.config).identifier.did)
451
709
  })
452
710
 
453
711
  it('Should save openid config to database', async (): Promise<void> => {
454
712
  const clientId = '138d7bf8-c930-4c6e-b928-97d3a4928b01'
455
- const config = {
713
+ const config: NonPersistedOpenIdConfig = {
456
714
  clientId,
457
715
  clientSecret: '03b3955f-d020-4f2a-8a27-4e452d4e27a0',
458
716
  scopes: ['auth'],
459
717
  issuer: 'https://example.com/app-test',
460
718
  redirectUrl: 'app:/callback',
461
719
  dangerouslyAllowInsecureHttpRequests: true,
462
- clientAuthMethod: 'post' as const,
720
+ clientAuthMethod: <const>'post',
463
721
  }
464
722
 
465
- const configEntity = openIdConfigEntityFrom(config)
723
+ const configEntity: OpenIdConfigEntity = openIdConfigEntityFrom(config)
466
724
  await dbConnection.getRepository(OpenIdConfigEntity).save(configEntity, {
467
725
  transaction: true,
468
726
  })
469
727
 
470
- const fromDb = await dbConnection.getRepository(OpenIdConfigEntity).findOne({
728
+ const fromDb: OpenIdConfigEntity | null = await dbConnection.getRepository(OpenIdConfigEntity).findOne({
471
729
  where: { clientId: config.clientId },
472
730
  })
473
731
 
474
732
  expect(fromDb).toBeDefined()
475
- expect((fromDb as OpenIdConfigEntity).clientId).toEqual(config.clientId)
733
+ expect((<OpenIdConfigEntity>fromDb).clientId).toEqual(config.clientId)
476
734
  })
477
735
 
478
736
  it('Should save didauth config to database', async (): Promise<void> => {
479
737
  const sessionId = 'https://example.com/did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01'
480
- const config = {
738
+ const config: NonPersistedDidAuthConfig = {
481
739
  identifier: {
482
740
  did: 'did:test:138d7bf8-c930-4c6e-b928-97d3a4928b01',
483
741
  provider: 'test_provider',
@@ -489,31 +747,62 @@ describe('Database entities tests', (): void => {
489
747
  sessionId,
490
748
  }
491
749
 
492
- const configEntity = didAuthConfigEntityFrom(config)
750
+ const configEntity: DidAuthConfigEntity = didAuthConfigEntityFrom(config)
493
751
  await dbConnection.getRepository(DidAuthConfigEntity).save(configEntity, {
494
752
  transaction: true,
495
753
  })
496
754
 
497
- const fromDb = await dbConnection.getRepository(DidAuthConfigEntity).findOne({
755
+ const fromDb: DidAuthConfigEntity | null = await dbConnection.getRepository(DidAuthConfigEntity).findOne({
498
756
  where: { sessionId: config.sessionId },
499
757
  })
500
758
 
501
759
  expect(fromDb).toBeDefined()
502
- expect((fromDb as DidAuthConfigEntity).identifier).toEqual(config.identifier.did)
760
+ expect((<DidAuthConfigEntity>fromDb).identifier).toEqual(config.identifier.did)
503
761
  })
504
762
 
505
- it('Should delete contact and all child relations', async (): Promise<void> => {
506
- const contact = {
507
- name: 'relation_test_name',
508
- alias: 'relation_test_alias',
763
+ it('Should delete party and all child relations', async (): Promise<void> => {
764
+ const party1: NonPersistedParty = {
509
765
  uri: 'example.com',
766
+ partyType: {
767
+ type: PartyTypeEnum.NATURAL_PERSON,
768
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
769
+ name: 'example_name1',
770
+ },
771
+ contact: {
772
+ firstName: 'example_first_name1',
773
+ middleName: 'example_middle_name1',
774
+ lastName: 'example_last_name1',
775
+ displayName: 'example_display_name1',
776
+ },
777
+ }
778
+
779
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
780
+ const savedParty1: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity1)
781
+
782
+ expect(savedParty1).toBeDefined()
783
+
784
+ const party2: NonPersistedParty = {
785
+ uri: 'example.com',
786
+ partyType: {
787
+ type: PartyTypeEnum.NATURAL_PERSON,
788
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
789
+ name: 'example_name2',
790
+ },
791
+ contact: {
792
+ firstName: 'example_first_name2',
793
+ middleName: 'example_middle_name2',
794
+ lastName: 'example_last_name2',
795
+ displayName: 'example_display_name2',
796
+ },
510
797
  }
511
798
 
512
- const contactEntity: ContactEntity = contactEntityFrom(contact)
513
- const savedContact = await dbConnection.getRepository(ContactEntity).save(contactEntity)
799
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
800
+ const savedParty2: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity2)
801
+
802
+ expect(savedParty2).toBeDefined()
514
803
 
515
804
  const correlationId = 'relation_example.com'
516
- const identity = {
805
+ const identity: NonPersistedIdentity = {
517
806
  alias: correlationId,
518
807
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
519
808
  identifier: {
@@ -529,7 +818,7 @@ describe('Database entities tests', (): void => {
529
818
  issuer: 'https://example.com/app-test',
530
819
  redirectUrl: 'app:/callback',
531
820
  dangerouslyAllowInsecureHttpRequests: true,
532
- clientAuthMethod: 'post' as const,
821
+ clientAuthMethod: <const>'post',
533
822
  },
534
823
  },
535
824
  metadata: [
@@ -541,29 +830,42 @@ describe('Database entities tests', (): void => {
541
830
  }
542
831
 
543
832
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
544
- identityEntity.contact = savedContact
833
+ identityEntity.party = savedParty1
834
+
835
+ const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
836
+
837
+ expect(savedIdentity).toBeDefined()
838
+
839
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
840
+ leftId: savedParty1.id,
841
+ rightId: savedParty2.id,
842
+ })
843
+
844
+ const savedRelationship: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
845
+ transaction: true,
846
+ })
545
847
 
546
- const savedIdentity = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
848
+ expect(savedRelationship).toBeDefined()
547
849
 
548
850
  expect(
549
- await dbConnection.getRepository(ContactEntity).findOne({
550
- where: { name: contact.name },
851
+ await dbConnection.getRepository(PartyEntity).findOne({
852
+ where: { id: savedParty1.id },
551
853
  })
552
854
  ).toBeDefined()
553
855
 
554
- await dbConnection.getRepository(ContactEntity).delete({ id: savedContact.id })
856
+ await dbConnection.getRepository(PartyEntity).delete({ id: savedParty1.id })
555
857
 
556
- // check contact
858
+ // check party
557
859
  await expect(
558
- await dbConnection.getRepository(ContactEntity).findOne({
559
- where: { name: contact.name },
860
+ await dbConnection.getRepository(PartyEntity).findOne({
861
+ where: { id: savedParty1.id },
560
862
  })
561
863
  ).toBeNull()
562
864
 
563
865
  // check identity
564
866
  expect(
565
867
  await dbConnection.getRepository(IdentityEntity).findOne({
566
- where: { alias: correlationId },
868
+ where: { id: savedParty1.id },
567
869
  })
568
870
  ).toBeNull()
569
871
 
@@ -594,20 +896,52 @@ describe('Database entities tests', (): void => {
594
896
  where: { id: savedIdentity.metadata![0].id },
595
897
  })
596
898
  ).toBeNull()
899
+
900
+ // check contact
901
+ expect(
902
+ await dbConnection.getRepository(BaseContactEntity).findOne({
903
+ where: { id: savedParty1.contact.id },
904
+ })
905
+ ).toBeNull()
906
+
907
+ // check party type
908
+ expect(
909
+ await dbConnection.getRepository(PartyTypeEntity).findOne({
910
+ where: { id: savedParty1.partyType.id },
911
+ })
912
+ ).toBeDefined()
913
+
914
+ // check relation
915
+ expect(
916
+ await dbConnection.getRepository(PartyRelationshipEntity).findOne({
917
+ where: { id: savedRelationship.id },
918
+ })
919
+ ).toBeNull()
597
920
  })
598
921
 
599
922
  it('Should delete identity and all child relations', async (): Promise<void> => {
600
- const contact = {
601
- name: 'relation_test_name',
602
- alias: 'relation_test_alias',
923
+ const party: NonPersistedParty = {
603
924
  uri: 'example.com',
925
+ partyType: {
926
+ type: PartyTypeEnum.NATURAL_PERSON,
927
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
928
+ name: 'example_name',
929
+ },
930
+ contact: {
931
+ firstName: 'example_first_name',
932
+ middleName: 'example_middle_name',
933
+ lastName: 'example_last_name',
934
+ displayName: 'example_display_name',
935
+ },
604
936
  }
605
937
 
606
- const contactEntity: ContactEntity = contactEntityFrom(contact)
607
- const savedContact = await dbConnection.getRepository(ContactEntity).save(contactEntity)
938
+ const partyEntity: PartyEntity = partyEntityFrom(party)
939
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
940
+
941
+ expect(savedParty).toBeDefined()
608
942
 
609
943
  const correlationId = 'relation_example.com'
610
- const identity = {
944
+ const identity: NonPersistedIdentity = {
611
945
  alias: correlationId,
612
946
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
613
947
  identifier: {
@@ -637,13 +971,13 @@ describe('Database entities tests', (): void => {
637
971
  }
638
972
 
639
973
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
640
- identityEntity.contact = savedContact
974
+ identityEntity.party = savedParty
641
975
 
642
- const savedIdentity = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
976
+ const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
643
977
 
644
978
  expect(
645
- await dbConnection.getRepository(ContactEntity).findOne({
646
- where: { name: contact.name },
979
+ await dbConnection.getRepository(PartyEntity).findOne({
980
+ where: { id: savedParty.id },
647
981
  })
648
982
  ).toBeDefined()
649
983
 
@@ -685,18 +1019,29 @@ describe('Database entities tests', (): void => {
685
1019
  ).toBeNull()
686
1020
  })
687
1021
 
688
- it('Should not delete contact when deleting identity', async (): Promise<void> => {
689
- const contact = {
690
- name: 'relation_test_name',
691
- alias: 'relation_test_alias',
1022
+ it('Should not delete party when deleting identity', async (): Promise<void> => {
1023
+ const party: NonPersistedParty = {
692
1024
  uri: 'example.com',
1025
+ partyType: {
1026
+ type: PartyTypeEnum.NATURAL_PERSON,
1027
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1028
+ name: 'example_name',
1029
+ },
1030
+ contact: {
1031
+ firstName: 'example_first_name',
1032
+ middleName: 'example_middle_name',
1033
+ lastName: 'example_last_name',
1034
+ displayName: 'example_display_name',
1035
+ },
693
1036
  }
694
1037
 
695
- const contactEntity: ContactEntity = contactEntityFrom(contact)
696
- const savedContact = await dbConnection.getRepository(ContactEntity).save(contactEntity)
1038
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1039
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1040
+
1041
+ expect(savedParty).toBeDefined()
697
1042
 
698
1043
  const correlationId = 'relation_example.com'
699
- const identity = {
1044
+ const identity: NonPersistedIdentity = {
700
1045
  alias: correlationId,
701
1046
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
702
1047
  identifier: {
@@ -726,9 +1071,11 @@ describe('Database entities tests', (): void => {
726
1071
  }
727
1072
 
728
1073
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
729
- identityEntity.contact = savedContact
1074
+ identityEntity.party = savedParty
730
1075
 
731
- const savedIdentity = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1076
+ const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1077
+
1078
+ expect(savedIdentity).toBeDefined()
732
1079
 
733
1080
  await dbConnection.getRepository(IdentityEntity).delete({ id: savedIdentity.id })
734
1081
 
@@ -739,55 +1086,83 @@ describe('Database entities tests', (): void => {
739
1086
  })
740
1087
  ).toBeNull()
741
1088
 
742
- // check contact
1089
+ // check party
743
1090
  expect(
744
- await dbConnection.getRepository(ContactEntity).findOne({
745
- where: { name: contact.name },
1091
+ await dbConnection.getRepository(PartyEntity).findOne({
1092
+ where: { id: savedParty.id },
746
1093
  })
747
1094
  ).toBeDefined()
748
1095
  })
749
1096
 
750
- it('Should set creation date when saving contact', async (): Promise<void> => {
751
- const contact = {
752
- name: 'test_name',
753
- alias: 'test_alias',
1097
+ it('Should set creation date when saving party', async (): Promise<void> => {
1098
+ const party: NonPersistedParty = {
754
1099
  uri: 'example.com',
1100
+ partyType: {
1101
+ type: PartyTypeEnum.NATURAL_PERSON,
1102
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1103
+ name: 'example_name',
1104
+ },
1105
+ contact: {
1106
+ firstName: 'example_first_name',
1107
+ middleName: 'example_middle_name',
1108
+ lastName: 'example_last_name',
1109
+ displayName: 'example_display_name',
1110
+ },
755
1111
  }
756
1112
 
757
- const contactEntity: ContactEntity = contactEntityFrom(contact)
758
- await dbConnection.getRepository(ContactEntity).save(contactEntity)
1113
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1114
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
759
1115
 
760
- const fromDb = await dbConnection.getRepository(ContactEntity).findOne({
761
- where: { name: contact.name },
1116
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1117
+ where: { id: savedParty.id },
762
1118
  })
763
1119
 
764
1120
  expect(fromDb).toBeDefined()
765
1121
  expect(fromDb?.createdAt).toBeDefined()
766
1122
  })
767
1123
 
768
- it('Should not update creation date when updating contact', async (): Promise<void> => {
769
- const contact = {
770
- name: 'test_name',
771
- alias: 'test_alias',
1124
+ it('Should not update creation date when updating party', async (): Promise<void> => {
1125
+ const party: NonPersistedParty = {
772
1126
  uri: 'example.com',
1127
+ partyType: {
1128
+ type: PartyTypeEnum.NATURAL_PERSON,
1129
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1130
+ name: 'example_name',
1131
+ },
1132
+ contact: {
1133
+ firstName: 'example_first_name',
1134
+ middleName: 'example_middle_name',
1135
+ lastName: 'example_last_name',
1136
+ displayName: 'example_display_name',
1137
+ },
773
1138
  }
774
1139
 
775
- const contactEntity: ContactEntity = contactEntityFrom(contact)
776
- const savedContact = await dbConnection.getRepository(ContactEntity).save(contactEntity)
777
- const newContactName = 'new_name'
778
- await dbConnection.getRepository(ContactEntity).save({ ...savedContact, name: newContactName })
1140
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1141
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1142
+
1143
+ expect(savedParty).toBeDefined()
1144
+
1145
+ const newContactFirstName = 'new_first_name'
1146
+ await dbConnection.getRepository(PartyEntity).save({
1147
+ ...savedParty,
1148
+ contact: {
1149
+ ...savedParty.contact,
1150
+ firstName: newContactFirstName,
1151
+ },
1152
+ })
779
1153
 
780
- const fromDb = await dbConnection.getRepository(ContactEntity).findOne({
781
- where: { id: savedContact.id },
1154
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1155
+ where: { id: savedParty.id },
782
1156
  })
783
1157
 
784
1158
  expect(fromDb).toBeDefined()
785
- expect(fromDb?.createdAt).toEqual(savedContact?.createdAt)
1159
+ expect((<NaturalPersonEntity>fromDb?.contact).firstName).toEqual(newContactFirstName)
1160
+ expect(fromDb?.createdAt).toEqual(savedParty?.createdAt)
786
1161
  })
787
1162
 
788
1163
  it('Should set creation date when saving identity', async (): Promise<void> => {
789
1164
  const correlationId = 'example_did'
790
- const identity = {
1165
+ const identity: NonPersistedIdentity = {
791
1166
  alias: correlationId,
792
1167
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
793
1168
  identifier: {
@@ -799,7 +1174,7 @@ describe('Database entities tests', (): void => {
799
1174
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
800
1175
  await dbConnection.getRepository(IdentityEntity).save(identityEntity)
801
1176
 
802
- const fromDb = await dbConnection.getRepository(IdentityEntity).findOne({
1177
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
803
1178
  where: {
804
1179
  identifier: {
805
1180
  correlationId,
@@ -813,7 +1188,7 @@ describe('Database entities tests', (): void => {
813
1188
 
814
1189
  it('Should not update creation date when saving identity', async (): Promise<void> => {
815
1190
  const correlationId = 'example_did'
816
- const identity = {
1191
+ const identity: NonPersistedIdentity = {
817
1192
  alias: correlationId,
818
1193
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
819
1194
  identifier: {
@@ -823,13 +1198,13 @@ describe('Database entities tests', (): void => {
823
1198
  }
824
1199
 
825
1200
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
826
- const savedIdentity = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
1201
+ const savedIdentity: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).save(identityEntity)
827
1202
  const newCorrelationId = 'new_example_did'
828
1203
  await dbConnection
829
1204
  .getRepository(IdentityEntity)
830
1205
  .save({ ...savedIdentity, identifier: { ...savedIdentity.identifier, correlationId: newCorrelationId } })
831
1206
 
832
- const fromDb = await dbConnection.getRepository(IdentityEntity).findOne({
1207
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
833
1208
  where: {
834
1209
  identifier: {
835
1210
  correlationId: newCorrelationId,
@@ -841,51 +1216,116 @@ describe('Database entities tests', (): void => {
841
1216
  expect(fromDb?.createdAt).toEqual(savedIdentity?.createdAt)
842
1217
  })
843
1218
 
844
- it('Should set last updated date when saving contact', async (): Promise<void> => {
845
- const contact = {
846
- name: 'test_name',
847
- alias: 'test_alias',
1219
+ it('Should set last updated date when saving party', async (): Promise<void> => {
1220
+ const party: NonPersistedParty = {
848
1221
  uri: 'example.com',
1222
+ partyType: {
1223
+ type: PartyTypeEnum.NATURAL_PERSON,
1224
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1225
+ name: 'example_name',
1226
+ },
1227
+ contact: {
1228
+ firstName: 'example_first_name',
1229
+ middleName: 'example_middle_name',
1230
+ lastName: 'example_last_name',
1231
+ displayName: 'example_display_name',
1232
+ },
849
1233
  }
850
1234
 
851
- const contactEntity: ContactEntity = contactEntityFrom(contact)
852
- await dbConnection.getRepository(ContactEntity).save(contactEntity)
1235
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1236
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
853
1237
 
854
- const fromDb = await dbConnection.getRepository(ContactEntity).findOne({
855
- where: { name: contact.name },
1238
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1239
+ where: { id: savedParty.id },
856
1240
  })
857
1241
 
858
1242
  expect(fromDb).toBeDefined()
859
1243
  expect(fromDb?.lastUpdatedAt).toBeDefined()
860
1244
  })
861
1245
 
862
- it('Should update last updated date when updating contact', async (): Promise<void> => {
863
- const contact = {
864
- name: 'test_name',
865
- alias: 'test_alias',
1246
+ it('Should update last updated date when updating party', async (): Promise<void> => {
1247
+ const party: NonPersistedParty = {
866
1248
  uri: 'example.com',
1249
+ partyType: {
1250
+ type: PartyTypeEnum.NATURAL_PERSON,
1251
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1252
+ name: 'example_name',
1253
+ },
1254
+ contact: {
1255
+ firstName: 'example_first_name',
1256
+ middleName: 'example_middle_name',
1257
+ lastName: 'example_last_name',
1258
+ displayName: 'example_display_name',
1259
+ },
867
1260
  }
868
1261
 
869
- const contactEntity: ContactEntity = contactEntityFrom(contact)
870
- const savedContact = await dbConnection.getRepository(ContactEntity).save(contactEntity)
1262
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1263
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity)
1264
+ expect(savedParty).toBeDefined()
871
1265
 
872
1266
  // waiting here to get a different timestamp
873
1267
  await new Promise((resolve) => setTimeout(resolve, 2000))
874
1268
 
875
- const newContactName = 'new_name'
876
- await dbConnection.getRepository(ContactEntity).save({ ...savedContact, name: newContactName })
1269
+ const newContactFirstName = 'new_first_name'
1270
+ await dbConnection.getRepository(PartyEntity).save({
1271
+ ...savedParty,
1272
+ // FIXME there is still an issue when updating nested objects, the parent does not update
1273
+ // https://github.com/typeorm/typeorm/issues/5378
1274
+ uri: 'new uri', // TODO remove this to trigger the bug
1275
+ contact: {
1276
+ ...savedParty.contact,
1277
+ firstName: newContactFirstName,
1278
+ },
1279
+ })
1280
+
1281
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1282
+ where: { id: savedParty.id },
1283
+ })
1284
+
1285
+ expect(fromDb).toBeDefined()
1286
+ expect((<NaturalPersonEntity>fromDb?.contact).firstName).toEqual(newContactFirstName)
1287
+ expect(fromDb?.lastUpdatedAt).not.toEqual(savedParty?.lastUpdatedAt)
1288
+ })
1289
+
1290
+ it('Should set last updated date when saving party type', async (): Promise<void> => {
1291
+ const partyType: NonPersistedPartyType = {
1292
+ type: PartyTypeEnum.NATURAL_PERSON,
1293
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1294
+ name: 'example_name',
1295
+ }
1296
+
1297
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
1298
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
1299
+
1300
+ const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
1301
+ where: { id: savedPartyType.id },
1302
+ })
1303
+
1304
+ expect(fromDb).toBeDefined()
1305
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1306
+ })
1307
+
1308
+ it('Should set last creation date when saving party type', async (): Promise<void> => {
1309
+ const partyType: NonPersistedPartyType = {
1310
+ type: PartyTypeEnum.NATURAL_PERSON,
1311
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1312
+ name: 'example_name',
1313
+ }
1314
+
1315
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
1316
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
877
1317
 
878
- const fromDb = await dbConnection.getRepository(ContactEntity).findOne({
879
- where: { id: savedContact.id },
1318
+ const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
1319
+ where: { id: savedPartyType.id },
880
1320
  })
881
1321
 
882
1322
  expect(fromDb).toBeDefined()
883
- expect(fromDb?.lastUpdatedAt).not.toEqual(savedContact?.lastUpdatedAt)
1323
+ expect(fromDb?.createdAt).toBeDefined()
884
1324
  })
885
1325
 
886
1326
  it('Should set last updated date when saving identity', async (): Promise<void> => {
887
1327
  const correlationId = 'example_did'
888
- const identity = {
1328
+ const identity: NonPersistedIdentity = {
889
1329
  alias: correlationId,
890
1330
  roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
891
1331
  identifier: {
@@ -897,7 +1337,7 @@ describe('Database entities tests', (): void => {
897
1337
  const identityEntity: IdentityEntity = identityEntityFrom(identity)
898
1338
  await dbConnection.getRepository(IdentityEntity).save(identityEntity)
899
1339
 
900
- const fromDb = await dbConnection.getRepository(IdentityEntity).findOne({
1340
+ const fromDb: IdentityEntity | null = await dbConnection.getRepository(IdentityEntity).findOne({
901
1341
  where: {
902
1342
  identifier: {
903
1343
  correlationId,
@@ -908,4 +1348,826 @@ describe('Database entities tests', (): void => {
908
1348
  expect(fromDb).toBeDefined()
909
1349
  expect(fromDb?.lastUpdatedAt).toBeDefined()
910
1350
  })
1351
+
1352
+ it('Should enforce unique type and tenant id combination when saving party type', async (): Promise<void> => {
1353
+ const tenantId = 'non_unique_value'
1354
+ const name = 'non_unique_value'
1355
+ const partyType1: NonPersistedPartyType = {
1356
+ type: PartyTypeEnum.NATURAL_PERSON,
1357
+ tenantId,
1358
+ name,
1359
+ }
1360
+
1361
+ const partyTypeEntity1: PartyTypeEntity = partyTypeEntityFrom(partyType1)
1362
+ const savedPartyType1: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity1)
1363
+
1364
+ expect(savedPartyType1).toBeDefined()
1365
+
1366
+ const partyType2: NonPersistedPartyType = {
1367
+ type: PartyTypeEnum.NATURAL_PERSON,
1368
+ tenantId,
1369
+ name,
1370
+ }
1371
+
1372
+ const partyTypeEntity2: PartyTypeEntity = partyTypeEntityFrom(partyType2)
1373
+ await expect(dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity2)).rejects.toThrowError(
1374
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: PartyType.type, PartyType.tenant_id'
1375
+ )
1376
+ })
1377
+
1378
+ it('Should enforce unique name when saving party type', async (): Promise<void> => {
1379
+ const name = 'non_unique_value'
1380
+ const partyType1: NonPersistedPartyType = {
1381
+ type: PartyTypeEnum.NATURAL_PERSON,
1382
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1383
+ name,
1384
+ }
1385
+
1386
+ const partyTypeEntity1: PartyTypeEntity = partyTypeEntityFrom(partyType1)
1387
+ const savedPartyType1: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity1)
1388
+
1389
+ expect(savedPartyType1).toBeDefined()
1390
+
1391
+ const partyType2: NonPersistedPartyType = {
1392
+ type: PartyTypeEnum.NATURAL_PERSON,
1393
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1394
+ name,
1395
+ }
1396
+
1397
+ const partyTypeEntity2: PartyTypeEntity = partyTypeEntityFrom(partyType2)
1398
+ await expect(dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity2)).rejects.toThrowError(
1399
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: PartyType.name'
1400
+ )
1401
+ })
1402
+
1403
+ it('Should enforce unique legal name when saving organization', async (): Promise<void> => {
1404
+ const legalName = 'non_unique_value'
1405
+ const organization1: NonPersistedOrganization = {
1406
+ legalName,
1407
+ displayName: 'example_display_name',
1408
+ }
1409
+
1410
+ const organizationEntity1: OrganizationEntity = organizationEntityFrom(organization1)
1411
+ const savedOrganization1: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity1, {
1412
+ transaction: true,
1413
+ })
1414
+
1415
+ expect(savedOrganization1).toBeDefined()
1416
+
1417
+ const organization2: NonPersistedOrganization = {
1418
+ legalName,
1419
+ displayName: 'example_display_name',
1420
+ }
1421
+
1422
+ const organizationEntity2: OrganizationEntity = organizationEntityFrom(organization2)
1423
+ await expect(dbConnection.getRepository(OrganizationEntity).save(organizationEntity2)).rejects.toThrowError(
1424
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: BaseContact.legal_name'
1425
+ )
1426
+ })
1427
+
1428
+ it('Should enforce unique legal name when saving organization', async (): Promise<void> => {
1429
+ const legalName = 'example_legal_name'
1430
+ const organization1: NonPersistedOrganization = {
1431
+ legalName,
1432
+ displayName: 'example_display_name',
1433
+ }
1434
+
1435
+ const organizationEntity1: OrganizationEntity = organizationEntityFrom(organization1)
1436
+ const savedOrganization1: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity1, {
1437
+ transaction: true,
1438
+ })
1439
+
1440
+ expect(savedOrganization1).toBeDefined()
1441
+
1442
+ const organization2: NonPersistedOrganization = {
1443
+ legalName,
1444
+ displayName: 'example_display_name',
1445
+ }
1446
+
1447
+ const organizationEntity2: OrganizationEntity = organizationEntityFrom(organization2)
1448
+ await expect(dbConnection.getRepository(OrganizationEntity).save(organizationEntity2)).rejects.toThrowError(
1449
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: BaseContact.legal_name'
1450
+ )
1451
+ })
1452
+
1453
+ it('Should save party relationship to database', async (): Promise<void> => {
1454
+ const party1: NonPersistedParty = {
1455
+ uri: 'example1.com',
1456
+ partyType: {
1457
+ type: PartyTypeEnum.NATURAL_PERSON,
1458
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1459
+ name: 'example_name1',
1460
+ },
1461
+ contact: {
1462
+ firstName: 'example_first_name1',
1463
+ middleName: 'example_middle_name1',
1464
+ lastName: 'example_last_name1',
1465
+ displayName: 'example_display_name1',
1466
+ },
1467
+ }
1468
+
1469
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1470
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1471
+ transaction: true,
1472
+ })
1473
+
1474
+ expect(savedParty1).toBeDefined()
1475
+
1476
+ const party2: NonPersistedParty = {
1477
+ uri: 'example2.com',
1478
+ partyType: {
1479
+ type: PartyTypeEnum.NATURAL_PERSON,
1480
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1481
+ name: 'example_name2',
1482
+ },
1483
+ contact: {
1484
+ firstName: 'example_first_name2',
1485
+ middleName: 'example_middle_name2',
1486
+ lastName: 'example_last_name2',
1487
+ displayName: 'example_display_name2',
1488
+ },
1489
+ }
1490
+
1491
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1492
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1493
+ transaction: true,
1494
+ })
1495
+
1496
+ expect(savedParty2).toBeDefined()
1497
+
1498
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
1499
+ leftId: savedParty1.id,
1500
+ rightId: savedParty2.id,
1501
+ })
1502
+
1503
+ await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
1504
+ transaction: true,
1505
+ })
1506
+
1507
+ // TODO check the relation field
1508
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1509
+ where: { id: partyEntity1.id },
1510
+ })
1511
+
1512
+ expect(fromDb).toBeDefined()
1513
+ })
1514
+
1515
+ it('Should set last updated date when saving party relationship', async (): Promise<void> => {
1516
+ const party1: NonPersistedParty = {
1517
+ uri: 'example1.com',
1518
+ partyType: {
1519
+ type: PartyTypeEnum.NATURAL_PERSON,
1520
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1521
+ name: 'example_name1',
1522
+ },
1523
+ contact: {
1524
+ firstName: 'example_first_name1',
1525
+ middleName: 'example_middle_name1',
1526
+ lastName: 'example_last_name1',
1527
+ displayName: 'example_display_name1',
1528
+ },
1529
+ }
1530
+
1531
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1532
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1533
+ transaction: true,
1534
+ })
1535
+
1536
+ const party2: NonPersistedParty = {
1537
+ uri: 'example2.com',
1538
+ partyType: {
1539
+ type: PartyTypeEnum.NATURAL_PERSON,
1540
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1541
+ name: 'example_name2',
1542
+ },
1543
+ contact: {
1544
+ firstName: 'example_first_name2',
1545
+ middleName: 'example_middle_name2',
1546
+ lastName: 'example_last_name2',
1547
+ displayName: 'example_display_name2',
1548
+ },
1549
+ }
1550
+
1551
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1552
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1553
+ transaction: true,
1554
+ })
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
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1566
+ where: { id: partyEntity1.id },
1567
+ })
1568
+
1569
+ expect(fromDb).toBeDefined()
1570
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1571
+ })
1572
+
1573
+ it('Should set creation date when saving party relationship', async (): Promise<void> => {
1574
+ const party1: NonPersistedParty = {
1575
+ uri: 'example1.com',
1576
+ partyType: {
1577
+ type: PartyTypeEnum.NATURAL_PERSON,
1578
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1579
+ name: 'example_name1',
1580
+ },
1581
+ contact: {
1582
+ firstName: 'example_first_name1',
1583
+ middleName: 'example_middle_name1',
1584
+ lastName: 'example_last_name1',
1585
+ displayName: 'example_display_name1',
1586
+ },
1587
+ }
1588
+
1589
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1590
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1591
+ transaction: true,
1592
+ })
1593
+
1594
+ const party2: NonPersistedParty = {
1595
+ uri: 'example2.com',
1596
+ partyType: {
1597
+ type: PartyTypeEnum.NATURAL_PERSON,
1598
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1599
+ name: 'example_name2',
1600
+ },
1601
+ contact: {
1602
+ firstName: 'example_first_name2',
1603
+ middleName: 'example_middle_name2',
1604
+ lastName: 'example_last_name2',
1605
+ displayName: 'example_display_name2',
1606
+ },
1607
+ }
1608
+
1609
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1610
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1611
+ transaction: true,
1612
+ })
1613
+
1614
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
1615
+ leftId: savedParty1.id,
1616
+ rightId: savedParty2.id,
1617
+ })
1618
+
1619
+ await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
1620
+ transaction: true,
1621
+ })
1622
+
1623
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1624
+ where: { id: partyEntity1.id },
1625
+ })
1626
+
1627
+ expect(fromDb).toBeDefined()
1628
+ expect(fromDb?.createdAt).toBeDefined()
1629
+ })
1630
+
1631
+ it('Should save bidirectional party relationships to database', async (): Promise<void> => {
1632
+ const party1: NonPersistedParty = {
1633
+ uri: 'example1.com',
1634
+ partyType: {
1635
+ type: PartyTypeEnum.NATURAL_PERSON,
1636
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1637
+ name: 'example_name1',
1638
+ },
1639
+ contact: {
1640
+ firstName: 'example_first_name1',
1641
+ middleName: 'example_middle_name1',
1642
+ lastName: 'example_last_name1',
1643
+ displayName: 'example_display_name1',
1644
+ },
1645
+ }
1646
+
1647
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1648
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1649
+ transaction: true,
1650
+ })
1651
+
1652
+ expect(savedParty1).toBeDefined()
1653
+
1654
+ const party2: NonPersistedParty = {
1655
+ uri: 'example2.com',
1656
+ partyType: {
1657
+ type: PartyTypeEnum.NATURAL_PERSON,
1658
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1659
+ name: 'example_name2',
1660
+ },
1661
+ contact: {
1662
+ firstName: 'example_first_name2',
1663
+ middleName: 'example_middle_name2',
1664
+ lastName: 'example_last_name2',
1665
+ displayName: 'example_display_name2',
1666
+ },
1667
+ }
1668
+
1669
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1670
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1671
+ transaction: true,
1672
+ })
1673
+
1674
+ expect(savedParty2).toBeDefined()
1675
+
1676
+ const relationship1: PartyRelationshipEntity = partyRelationshipEntityFrom({
1677
+ leftId: savedParty1.id,
1678
+ rightId: savedParty2.id,
1679
+ })
1680
+
1681
+ const savedRelationship1: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship1, {
1682
+ transaction: true,
1683
+ })
1684
+
1685
+ expect(savedRelationship1).toBeDefined()
1686
+
1687
+ const relationship2: PartyRelationshipEntity = partyRelationshipEntityFrom({
1688
+ leftId: savedParty2.id,
1689
+ rightId: savedParty1.id,
1690
+ })
1691
+
1692
+ const savedRelationship2: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship2, {
1693
+ transaction: true,
1694
+ })
1695
+
1696
+ expect(savedRelationship2).toBeDefined()
1697
+
1698
+ const fromDb: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).findOne({
1699
+ where: { id: savedRelationship2.id },
1700
+ })
1701
+
1702
+ expect(fromDb).toBeDefined()
1703
+ })
1704
+
1705
+ it('Should enforce unique owner combination for party relationship', async (): Promise<void> => {
1706
+ const party1: NonPersistedParty = {
1707
+ uri: 'example1.com',
1708
+ partyType: {
1709
+ type: PartyTypeEnum.NATURAL_PERSON,
1710
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1711
+ name: 'example_name1',
1712
+ },
1713
+ contact: {
1714
+ firstName: 'example_first_name1',
1715
+ middleName: 'example_middle_name1',
1716
+ lastName: 'example_last_name1',
1717
+ displayName: 'example_display_name1',
1718
+ },
1719
+ }
1720
+
1721
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
1722
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
1723
+ transaction: true,
1724
+ })
1725
+
1726
+ expect(savedParty1).toBeDefined()
1727
+
1728
+ const party2: NonPersistedParty = {
1729
+ uri: 'example2.com',
1730
+ partyType: {
1731
+ type: PartyTypeEnum.NATURAL_PERSON,
1732
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
1733
+ name: 'example_name2',
1734
+ },
1735
+ contact: {
1736
+ firstName: 'example_first_name2',
1737
+ middleName: 'example_middle_name2',
1738
+ lastName: 'example_last_name2',
1739
+ displayName: 'example_display_name2',
1740
+ },
1741
+ }
1742
+
1743
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
1744
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
1745
+ transaction: true,
1746
+ })
1747
+
1748
+ expect(savedParty2).toBeDefined()
1749
+
1750
+ const relationship1: PartyRelationshipEntity = partyRelationshipEntityFrom({
1751
+ leftId: savedParty1.id,
1752
+ rightId: savedParty2.id,
1753
+ })
1754
+
1755
+ const savedRelationship1: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship1, {
1756
+ transaction: true,
1757
+ })
1758
+
1759
+ expect(savedRelationship1).toBeDefined()
1760
+
1761
+ const relationship2: PartyRelationshipEntity = partyRelationshipEntityFrom({
1762
+ leftId: savedParty1.id,
1763
+ rightId: savedParty2.id,
1764
+ })
1765
+
1766
+ await expect(dbConnection.getRepository(PartyRelationshipEntity).save(relationship2)).rejects.toThrowError(
1767
+ 'SQLITE_CONSTRAINT: UNIQUE constraint failed: PartyRelationship.left_id, PartyRelationship.right_id'
1768
+ )
1769
+ })
1770
+
1771
+ it('Should save party type to database', async (): Promise<void> => {
1772
+ const partyType: NonPersistedPartyType = {
1773
+ type: PartyTypeEnum.NATURAL_PERSON,
1774
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1775
+ name: 'example_name',
1776
+ }
1777
+
1778
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
1779
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
1780
+
1781
+ const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
1782
+ where: { id: savedPartyType.id },
1783
+ })
1784
+
1785
+ expect(fromDb).toBeDefined()
1786
+ })
1787
+
1788
+ it('Should save person to database', async (): Promise<void> => {
1789
+ const person: NonPersistedNaturalPerson = {
1790
+ firstName: 'example_first_name',
1791
+ lastName: 'lastName2',
1792
+ displayName: 'displayName',
1793
+ }
1794
+
1795
+ const personEntity: NaturalPersonEntity = naturalPersonEntityFrom(person)
1796
+ const savedPerson: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).save(personEntity, {
1797
+ transaction: true,
1798
+ })
1799
+
1800
+ const fromDb: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).findOne({
1801
+ where: { id: savedPerson.id },
1802
+ })
1803
+
1804
+ expect(fromDb).toBeDefined()
1805
+ })
1806
+
1807
+ it('Should set last updated date when saving person', async (): Promise<void> => {
1808
+ const person: NonPersistedNaturalPerson = {
1809
+ firstName: 'example_first_name',
1810
+ lastName: 'lastName2',
1811
+ displayName: 'displayName',
1812
+ }
1813
+
1814
+ const personEntity: NaturalPersonEntity = naturalPersonEntityFrom(person)
1815
+ const savedPerson: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).save(personEntity, {
1816
+ transaction: true,
1817
+ })
1818
+
1819
+ const fromDb: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).findOne({
1820
+ where: { id: savedPerson.id },
1821
+ })
1822
+
1823
+ expect(fromDb).toBeDefined()
1824
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1825
+ })
1826
+
1827
+ it('Should set creation date when saving person', async (): Promise<void> => {
1828
+ const person: NonPersistedNaturalPerson = {
1829
+ firstName: 'example_first_name',
1830
+ lastName: 'lastName2',
1831
+ displayName: 'displayName',
1832
+ }
1833
+
1834
+ const personEntity: NaturalPersonEntity = naturalPersonEntityFrom(person)
1835
+ const savedPerson: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).save(personEntity, {
1836
+ transaction: true,
1837
+ })
1838
+
1839
+ const fromDb: NaturalPersonEntity | null = await dbConnection.getRepository(NaturalPersonEntity).findOne({
1840
+ where: { id: savedPerson.id },
1841
+ })
1842
+
1843
+ expect(fromDb).toBeDefined()
1844
+ expect(fromDb?.createdAt).toBeDefined()
1845
+ })
1846
+
1847
+ it('Should save organization to database', async (): Promise<void> => {
1848
+ const organization: NonPersistedOrganization = {
1849
+ legalName: 'example_legal_name',
1850
+ displayName: 'example_display_name',
1851
+ }
1852
+
1853
+ const organizationEntity: OrganizationEntity = organizationEntityFrom(organization)
1854
+ const savedOrganization: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity, {
1855
+ transaction: true,
1856
+ })
1857
+
1858
+ const fromDb: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).findOne({
1859
+ where: { id: savedOrganization.id },
1860
+ })
1861
+
1862
+ expect(fromDb).toBeDefined()
1863
+ })
1864
+
1865
+ it('Should set last updated date when saving organization', async (): Promise<void> => {
1866
+ const organization: NonPersistedOrganization = {
1867
+ legalName: 'example_legal_name',
1868
+ displayName: 'example_display_name',
1869
+ }
1870
+
1871
+ const organizationEntity: OrganizationEntity = organizationEntityFrom(organization)
1872
+ const savedOrganization: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity, {
1873
+ transaction: true,
1874
+ })
1875
+
1876
+ const fromDb: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).findOne({
1877
+ where: { id: savedOrganization.id },
1878
+ })
1879
+
1880
+ expect(fromDb).toBeDefined()
1881
+ expect(fromDb?.lastUpdatedAt).toBeDefined()
1882
+ })
1883
+
1884
+ it('Should set creation date when saving organization', async (): Promise<void> => {
1885
+ const organization: NonPersistedOrganization = {
1886
+ legalName: 'example_legal_name',
1887
+ displayName: 'example_display_name',
1888
+ }
1889
+
1890
+ const organizationEntity: OrganizationEntity = organizationEntityFrom(organization)
1891
+ const savedOrganization: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).save(organizationEntity, {
1892
+ transaction: true,
1893
+ })
1894
+
1895
+ const fromDb: OrganizationEntity | null = await dbConnection.getRepository(OrganizationEntity).findOne({
1896
+ where: { id: savedOrganization.id },
1897
+ })
1898
+
1899
+ expect(fromDb).toBeDefined()
1900
+ expect(fromDb?.createdAt).toBeDefined()
1901
+ })
1902
+
1903
+ it('Should get party based on person information', async (): Promise<void> => {
1904
+ const firstName = 'example_first_name'
1905
+ const party: NonPersistedParty = {
1906
+ uri: 'example.com',
1907
+ partyType: {
1908
+ type: PartyTypeEnum.NATURAL_PERSON,
1909
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1910
+ name: 'example_name',
1911
+ },
1912
+ contact: {
1913
+ firstName,
1914
+ middleName: 'example_middle_name',
1915
+ lastName: 'example_last_name',
1916
+ displayName: 'example_display_name',
1917
+ },
1918
+ }
1919
+
1920
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1921
+ await dbConnection.getRepository(PartyEntity).save(partyEntity, {
1922
+ transaction: true,
1923
+ })
1924
+
1925
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1926
+ where: {
1927
+ contact: {
1928
+ firstName,
1929
+ } as FindOptionsWhere<BaseContactEntity>, //NaturalPersonEntity | OrganizationEntity
1930
+ },
1931
+ })
1932
+
1933
+ expect(fromDb).toBeDefined()
1934
+ })
1935
+
1936
+ it('Should get party based on organization information', async (): Promise<void> => {
1937
+ const legalName = 'example_legal_name'
1938
+ const party: NonPersistedParty = {
1939
+ uri: 'example.com',
1940
+ partyType: {
1941
+ type: PartyTypeEnum.ORGANIZATION,
1942
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1943
+ name: 'example_name',
1944
+ },
1945
+ contact: {
1946
+ legalName,
1947
+ displayName: 'example_display_name',
1948
+ },
1949
+ }
1950
+
1951
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1952
+ await dbConnection.getRepository(PartyEntity).save(partyEntity, {
1953
+ transaction: true,
1954
+ })
1955
+
1956
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
1957
+ where: {
1958
+ contact: {
1959
+ legalName,
1960
+ } as FindOptionsWhere<BaseContactEntity>, //NaturalPersonEntity | OrganizationEntity
1961
+ },
1962
+ })
1963
+
1964
+ expect(fromDb).toBeDefined()
1965
+ })
1966
+
1967
+ it("Should enforce unique party id's for relationship sides", async (): Promise<void> => {
1968
+ const party: NonPersistedParty = {
1969
+ uri: 'example.com',
1970
+ partyType: {
1971
+ type: PartyTypeEnum.NATURAL_PERSON,
1972
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
1973
+ name: 'example_name',
1974
+ },
1975
+ contact: {
1976
+ firstName: 'example_first_name',
1977
+ middleName: 'example_middle_name',
1978
+ lastName: 'example_last_name',
1979
+ displayName: 'example_display_name',
1980
+ },
1981
+ }
1982
+
1983
+ const partyEntity: PartyEntity = partyEntityFrom(party)
1984
+ const savedParty: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
1985
+ transaction: true,
1986
+ })
1987
+
1988
+ expect(savedParty).toBeDefined()
1989
+
1990
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
1991
+ leftId: savedParty.id,
1992
+ rightId: savedParty.id,
1993
+ })
1994
+
1995
+ await expect(dbConnection.getRepository(PartyRelationshipEntity).save(relationship)).rejects.toThrowError(
1996
+ 'Cannot use the same id for both sides of the relationship'
1997
+ )
1998
+ })
1999
+
2000
+ it('Should delete party relationship', async (): Promise<void> => {
2001
+ const party1: NonPersistedParty = {
2002
+ uri: 'example1.com',
2003
+ partyType: {
2004
+ type: PartyTypeEnum.NATURAL_PERSON,
2005
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2006
+ name: 'example_name1',
2007
+ },
2008
+ contact: {
2009
+ firstName: 'example_first_name1',
2010
+ middleName: 'example_middle_name1',
2011
+ lastName: 'example_last_name1',
2012
+ displayName: 'example_display_name1',
2013
+ },
2014
+ }
2015
+
2016
+ const partyEntity1: PartyEntity = partyEntityFrom(party1)
2017
+ const savedParty1: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity1, {
2018
+ transaction: true,
2019
+ })
2020
+
2021
+ expect(savedParty1).toBeDefined()
2022
+
2023
+ const party2: NonPersistedParty = {
2024
+ uri: 'example2.com',
2025
+ partyType: {
2026
+ type: PartyTypeEnum.NATURAL_PERSON,
2027
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d288',
2028
+ name: 'example_name2',
2029
+ },
2030
+ contact: {
2031
+ firstName: 'example_first_name2',
2032
+ middleName: 'example_middle_name2',
2033
+ lastName: 'example_last_name2',
2034
+ displayName: 'example_display_name2',
2035
+ },
2036
+ }
2037
+
2038
+ const partyEntity2: PartyEntity = partyEntityFrom(party2)
2039
+ const savedParty2: PartyEntity = await dbConnection.getRepository(PartyEntity).save(partyEntity2, {
2040
+ transaction: true,
2041
+ })
2042
+
2043
+ expect(savedParty2).toBeDefined()
2044
+
2045
+ const relationship: PartyRelationshipEntity = partyRelationshipEntityFrom({
2046
+ leftId: savedParty1.id,
2047
+ rightId: savedParty2.id,
2048
+ })
2049
+
2050
+ const savedRelationship: PartyRelationshipEntity | null = await dbConnection.getRepository(PartyRelationshipEntity).save(relationship, {
2051
+ transaction: true,
2052
+ })
2053
+
2054
+ expect(savedRelationship).toBeDefined()
2055
+
2056
+ await dbConnection.getRepository(PartyRelationshipEntity).delete({ id: savedRelationship.id })
2057
+
2058
+ await expect(
2059
+ await dbConnection.getRepository(PartyRelationshipEntity).findOne({
2060
+ where: { id: savedRelationship.id },
2061
+ })
2062
+ ).toBeNull()
2063
+ })
2064
+
2065
+ it('Should delete party type', async (): Promise<void> => {
2066
+ const partyType: NonPersistedPartyType = {
2067
+ type: PartyTypeEnum.NATURAL_PERSON,
2068
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2069
+ name: 'example_name',
2070
+ }
2071
+
2072
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
2073
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
2074
+
2075
+ expect(savedPartyType).toBeDefined()
2076
+
2077
+ await dbConnection.getRepository(PartyTypeEntity).delete({ id: savedPartyType.id })
2078
+
2079
+ await expect(
2080
+ await dbConnection.getRepository(PartyTypeEntity).findOne({
2081
+ where: { id: savedPartyType.id },
2082
+ })
2083
+ ).toBeNull()
2084
+ })
2085
+
2086
+ it('Should not be able to remove party type when used by parties', async (): Promise<void> => {
2087
+ const party: NonPersistedParty = {
2088
+ uri: 'example.com',
2089
+ partyType: {
2090
+ type: PartyTypeEnum.NATURAL_PERSON,
2091
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2092
+ name: 'example_name',
2093
+ },
2094
+ contact: {
2095
+ firstName: 'example_first_name',
2096
+ middleName: 'example_middle_name',
2097
+ lastName: 'example_last_name',
2098
+ displayName: 'example_display_name',
2099
+ },
2100
+ }
2101
+
2102
+ const partyEntity: PartyEntity = partyEntityFrom(party)
2103
+ const savedParty: PartyEntity | null = await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2104
+ transaction: true,
2105
+ })
2106
+
2107
+ expect(savedParty).toBeDefined()
2108
+
2109
+ await expect(dbConnection.getRepository(PartyTypeEntity).delete({ id: savedParty.partyType.id })).rejects.toThrowError(
2110
+ 'FOREIGN KEY constraint failed'
2111
+ )
2112
+ })
2113
+
2114
+ it('Should save party with existing party type', async (): Promise<void> => {
2115
+ const partyType: NonPersistedPartyType = {
2116
+ type: PartyTypeEnum.NATURAL_PERSON,
2117
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2118
+ name: 'example_name',
2119
+ }
2120
+
2121
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
2122
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
2123
+
2124
+ const party: NonPersistedParty = {
2125
+ uri: 'example.com',
2126
+ partyType: savedPartyType,
2127
+ contact: {
2128
+ firstName: 'example_first_name',
2129
+ middleName: 'example_middle_name',
2130
+ lastName: 'example_last_name',
2131
+ displayName: 'example_display_name',
2132
+ },
2133
+ }
2134
+
2135
+ const partyEntity: PartyEntity = partyEntityFrom(party)
2136
+ partyEntity.partyType = savedPartyType
2137
+ await dbConnection.getRepository(PartyEntity).save(partyEntity, {
2138
+ transaction: true,
2139
+ })
2140
+
2141
+ const fromDb: PartyEntity | null = await dbConnection.getRepository(PartyEntity).findOne({
2142
+ where: { id: partyEntity.id },
2143
+ })
2144
+
2145
+ expect(fromDb).toBeDefined()
2146
+ expect(fromDb?.partyType).toBeDefined()
2147
+ expect(fromDb?.partyType.id).toEqual(savedPartyType.id)
2148
+ expect(fromDb?.partyType.type).toEqual(savedPartyType.type)
2149
+ expect(fromDb?.partyType.tenantId).toEqual(savedPartyType.tenantId)
2150
+ expect(fromDb?.partyType.name).toEqual(savedPartyType.name)
2151
+ })
2152
+
2153
+ it('Should not update creation date when saving party type', async (): Promise<void> => {
2154
+ const partyType: NonPersistedPartyType = {
2155
+ type: PartyTypeEnum.NATURAL_PERSON,
2156
+ tenantId: '0605761c-4113-4ce5-a6b2-9cbae2f9d289',
2157
+ name: 'example_name',
2158
+ }
2159
+
2160
+ const partyTypeEntity: PartyTypeEntity = partyTypeEntityFrom(partyType)
2161
+ const savedPartyType: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).save(partyTypeEntity)
2162
+ await dbConnection.getRepository(PartyTypeEntity).save({ ...savedPartyType, type: PartyTypeEnum.ORGANIZATION })
2163
+
2164
+ const fromDb: PartyTypeEntity | null = await dbConnection.getRepository(PartyTypeEntity).findOne({
2165
+ where: {
2166
+ type: PartyTypeEnum.ORGANIZATION,
2167
+ },
2168
+ })
2169
+
2170
+ expect(fromDb).toBeDefined()
2171
+ expect(fromDb?.createdAt).toEqual(savedPartyType?.createdAt)
2172
+ })
911
2173
  })