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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +77 -77
  3. package/dist/index.d.ts +3 -9
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +2 -15
  6. package/dist/index.js.map +1 -1
  7. package/dist/migrations/generic/index.d.ts +0 -2
  8. package/dist/migrations/generic/index.d.ts.map +1 -1
  9. package/dist/migrations/generic/index.js +1 -4
  10. package/dist/migrations/generic/index.js.map +1 -1
  11. package/dist/migrations/index.d.ts +1 -1
  12. package/dist/migrations/index.d.ts.map +1 -1
  13. package/dist/migrations/index.js +1 -2
  14. package/dist/migrations/index.js.map +1 -1
  15. package/dist/migrations/postgres/1690925872592-CreateContacts.js +1 -1
  16. package/dist/migrations/postgres/1690925872592-CreateContacts.js.map +1 -1
  17. package/dist/migrations/postgres/1708525189001-CreateDigitalCredential.js +22 -22
  18. package/dist/migrations/postgres/1708797018115-CreateMachineStateStore.js +16 -16
  19. package/dist/migrations/sqlite/1708525189002-CreateDigitalCredential.js +21 -21
  20. package/dist/migrations/sqlite/1708796002272-CreateMachineStateStore.js +15 -15
  21. package/dist/types/index.d.ts +0 -2
  22. package/dist/types/index.d.ts.map +1 -1
  23. package/dist/types/index.js +0 -2
  24. package/dist/types/index.js.map +1 -1
  25. package/package.json +4 -5
  26. package/src/__tests__/contact.entities.test.ts +2542 -2542
  27. package/src/__tests__/contact.store.test.ts +2471 -2471
  28. package/src/__tests__/digitalCredential.entities.test.ts +254 -254
  29. package/src/__tests__/digitalCredential.store.test.ts +294 -294
  30. package/src/__tests__/eventLogger.entities.test.ts +73 -73
  31. package/src/__tests__/eventLogger.store.test.ts +136 -136
  32. package/src/__tests__/issuanceBranding.entities.test.ts +844 -844
  33. package/src/__tests__/issuanceBranding.store.test.ts +1884 -1884
  34. package/src/__tests__/machineState.entities.test.ts +51 -51
  35. package/src/__tests__/machineState.store.test.ts +174 -174
  36. package/src/contact/AbstractContactStore.ts +71 -71
  37. package/src/contact/ContactStore.ts +723 -723
  38. package/src/digitalCredential/AbstractDigitalCredentialStore.ts +17 -17
  39. package/src/digitalCredential/DigitalCredentialStore.ts +127 -127
  40. package/src/entities/contact/BaseContactEntity.ts +39 -39
  41. package/src/entities/contact/ConnectionEntity.ts +29 -29
  42. package/src/entities/contact/CorrelationIdentifierEntity.ts +37 -37
  43. package/src/entities/contact/DidAuthConfigEntity.ts +14 -14
  44. package/src/entities/contact/ElectronicAddressEntity.ts +63 -63
  45. package/src/entities/contact/IdentityEntity.ts +97 -97
  46. package/src/entities/contact/IdentityMetadataItemEntity.ts +35 -35
  47. package/src/entities/contact/NaturalPersonEntity.ts +38 -38
  48. package/src/entities/contact/OpenIdConfigEntity.ts +26 -26
  49. package/src/entities/contact/OrganizationEntity.ts +34 -34
  50. package/src/entities/contact/PartyEntity.ts +110 -110
  51. package/src/entities/contact/PartyRelationshipEntity.ts +61 -61
  52. package/src/entities/contact/PartyTypeEntity.ts +62 -62
  53. package/src/entities/contact/PhysicalAddressEntity.ts +87 -87
  54. package/src/entities/digitalCredential/DigitalCredentialEntity.ts +64 -64
  55. package/src/entities/eventLogger/AuditEventEntity.ts +99 -99
  56. package/src/entities/issuanceBranding/CredentialBrandingEntity.ts +78 -78
  57. package/src/entities/issuanceBranding/ImageAttributesEntity.ts +57 -57
  58. package/src/entities/issuanceBranding/IssuerBrandingEntity.ts +72 -72
  59. package/src/entities/machineState/MachineStateInfoEntity.ts +58 -58
  60. package/src/entities/statusList2021/StatusList2021Entity.ts +96 -96
  61. package/src/eventLogger/AbstractEventLoggerStore.ts +7 -7
  62. package/src/eventLogger/EventLoggerStore.ts +62 -62
  63. package/src/index.ts +141 -154
  64. package/src/issuanceBranding/IssuanceBrandingStore.ts +559 -559
  65. package/src/machineState/IAbstractMachineStateStore.ts +65 -65
  66. package/src/machineState/MachineStateStore.ts +149 -149
  67. package/src/migrations/generic/1-CreateContacts.ts +66 -66
  68. package/src/migrations/generic/2-CreateIssuanceBranding.ts +64 -64
  69. package/src/migrations/generic/3-CreateContacts.ts +66 -66
  70. package/src/migrations/generic/4-CreateStatusList.ts +54 -54
  71. package/src/migrations/generic/5-CreateAuditEvents.ts +66 -66
  72. package/src/migrations/generic/6-CreateDigitalCredential.ts +66 -66
  73. package/src/migrations/generic/7-CreateMachineStateStore.ts +66 -66
  74. package/src/migrations/generic/index.ts +33 -36
  75. package/src/migrations/index.ts +9 -10
  76. package/src/migrations/postgres/1659463079428-CreateContacts.ts +63 -63
  77. package/src/migrations/postgres/1685628974232-CreateIssuanceBranding.ts +85 -85
  78. package/src/migrations/postgres/1690925872592-CreateContacts.ts +104 -104
  79. package/src/migrations/postgres/1693866470001-CreateStatusList.ts +24 -24
  80. package/src/migrations/postgres/1701634812183-CreateAuditEvents.ts +33 -33
  81. package/src/migrations/postgres/1708525189001-CreateDigitalCredential.ts +44 -44
  82. package/src/migrations/postgres/1708797018115-CreateMachineStateStore.ts +29 -29
  83. package/src/migrations/sqlite/1659463069549-CreateContacts.ts +110 -110
  84. package/src/migrations/sqlite/1685628973231-CreateIssuanceBranding.ts +119 -119
  85. package/src/migrations/sqlite/1690925872693-CreateContacts.ts +161 -161
  86. package/src/migrations/sqlite/1693866470000-CreateStatusList.ts +24 -24
  87. package/src/migrations/sqlite/1701634819487-CreateAuditEvents.ts +15 -15
  88. package/src/migrations/sqlite/1708525189002-CreateDigitalCredential.ts +34 -34
  89. package/src/migrations/sqlite/1708796002272-CreateMachineStateStore.ts +28 -28
  90. package/src/statusList/StatusListStore.ts +237 -237
  91. package/src/types/contact/IAbstractContactStore.ts +161 -161
  92. package/src/types/contact/contact.ts +237 -237
  93. package/src/types/digitalCredential/IAbstractDigitalCredentialStore.ts +37 -37
  94. package/src/types/digitalCredential/digitalCredential.ts +46 -46
  95. package/src/types/eventLogger/IAbstractEventLoggerStore.ts +12 -12
  96. package/src/types/eventLogger/eventLogger.ts +3 -3
  97. package/src/types/index.ts +10 -12
  98. package/src/types/machineState/IAbstractMachineStateStore.ts +68 -68
  99. package/src/utils/SortingUtils.ts +16 -16
  100. package/src/utils/contact/MappingUtils.ts +385 -385
  101. package/src/utils/digitalCredential/MappingUtils.ts +122 -122
  102. package/dist/entities/presentationDefinitions/PresentationDefinitionItemEntity.d.ts +0 -13
  103. package/dist/entities/presentationDefinitions/PresentationDefinitionItemEntity.d.ts.map +0 -1
  104. package/dist/entities/presentationDefinitions/PresentationDefinitionItemEntity.js +0 -71
  105. package/dist/entities/presentationDefinitions/PresentationDefinitionItemEntity.js.map +0 -1
  106. package/dist/migrations/generic/8-CreatePresentationDefinitions.d.ts +0 -7
  107. package/dist/migrations/generic/8-CreatePresentationDefinitions.d.ts.map +0 -1
  108. package/dist/migrations/generic/8-CreatePresentationDefinitions.js +0 -78
  109. package/dist/migrations/generic/8-CreatePresentationDefinitions.js.map +0 -1
  110. package/dist/migrations/postgres/1716475165345-CreatePresentationDefinitions.d.ts +0 -7
  111. package/dist/migrations/postgres/1716475165345-CreatePresentationDefinitions.d.ts.map +0 -1
  112. package/dist/migrations/postgres/1716475165345-CreatePresentationDefinitions.js +0 -40
  113. package/dist/migrations/postgres/1716475165345-CreatePresentationDefinitions.js.map +0 -1
  114. package/dist/migrations/sqlite/1716475165344-CreatePresentationDefinitions.d.ts +0 -7
  115. package/dist/migrations/sqlite/1716475165344-CreatePresentationDefinitions.d.ts.map +0 -1
  116. package/dist/migrations/sqlite/1716475165344-CreatePresentationDefinitions.js +0 -37
  117. package/dist/migrations/sqlite/1716475165344-CreatePresentationDefinitions.js.map +0 -1
  118. package/dist/pd/AbstractPDStore.d.ts +0 -10
  119. package/dist/pd/AbstractPDStore.d.ts.map +0 -1
  120. package/dist/pd/AbstractPDStore.js +0 -7
  121. package/dist/pd/AbstractPDStore.js.map +0 -1
  122. package/dist/pd/PDStore.d.ts +0 -14
  123. package/dist/pd/PDStore.d.ts.map +0 -1
  124. package/dist/pd/PDStore.js +0 -90
  125. package/dist/pd/PDStore.js.map +0 -1
  126. package/dist/types/pd/IAbstractPDStore.d.ts +0 -14
  127. package/dist/types/pd/IAbstractPDStore.d.ts.map +0 -1
  128. package/dist/types/pd/IAbstractPDStore.js +0 -3
  129. package/dist/types/pd/IAbstractPDStore.js.map +0 -1
  130. package/dist/types/pd/pd.d.ts +0 -15
  131. package/dist/types/pd/pd.d.ts.map +0 -1
  132. package/dist/types/pd/pd.js +0 -3
  133. package/dist/types/pd/pd.js.map +0 -1
  134. package/dist/utils/presentationDefinitions/MappingUtils.d.ts +0 -6
  135. package/dist/utils/presentationDefinitions/MappingUtils.d.ts.map +0 -1
  136. package/dist/utils/presentationDefinitions/MappingUtils.js +0 -50
  137. package/dist/utils/presentationDefinitions/MappingUtils.js.map +0 -1
  138. package/src/entities/presentationDefinitions/PresentationDefinitionItemEntity.ts +0 -41
  139. package/src/migrations/generic/8-CreatePresentationDefinitions.ts +0 -66
  140. package/src/migrations/postgres/1716475165345-CreatePresentationDefinitions.ts +0 -24
  141. package/src/migrations/sqlite/1716475165344-CreatePresentationDefinitions.ts +0 -23
  142. package/src/pd/AbstractPDStore.ts +0 -10
  143. package/src/pd/PDStore.ts +0 -103
  144. package/src/types/pd/IAbstractPDStore.ts +0 -19
  145. package/src/types/pd/pd.ts +0 -16
  146. package/src/utils/presentationDefinitions/MappingUtils.ts +0 -54
@@ -1,237 +1,237 @@
1
- import { OrPromise } from '@sphereon/ssi-types'
2
- import Debug from 'debug'
3
- import { DataSource, In, Repository } from 'typeorm'
4
- import { StatusListEntity } from '../entities/statusList2021/StatusList2021Entity'
5
- import { StatusListEntryEntity } from '../entities/statusList2021/StatusList2021EntryEntity'
6
- import {
7
- IAddStatusListArgs,
8
- IAddStatusListEntryArgs,
9
- IGetStatusListArgs,
10
- IGetStatusListEntriesArgs,
11
- IGetStatusListEntryByCredentialIdArgs,
12
- IGetStatusListEntryByIndexArgs,
13
- IGetStatusListsArgs,
14
- IRemoveStatusListArgs,
15
- IStatusListEntryAvailableArgs,
16
- IUpdateStatusListIndexArgs,
17
- IStatusListEntity,
18
- IStatusListEntryEntity,
19
- } from '../types'
20
- import { IStatusListStore } from './IStatusListStore'
21
-
22
- const debug = Debug('sphereon:ssi-sdk:data-store:status-list')
23
-
24
- export class StatusListStore implements IStatusListStore {
25
- private readonly _dbConnection: OrPromise<DataSource>
26
-
27
- constructor(dbConnection: OrPromise<DataSource>) {
28
- this._dbConnection = dbConnection
29
- }
30
-
31
- /**
32
- * Get's the available status list indices from the provided indices. Meaning it will filter out any index that is already known.
33
- *
34
- * The idea is that the caller provides a set of random status list indices. We can relatively easy check against the DB in an optimized way.
35
- * If the status list is large it is probably best ot also provide at least a good number of indices. So something like 10 or 20 values.
36
- * Callers are also expected to call this function multiple times if it does not yield results
37
- *
38
- * @param args
39
- */
40
- async availableStatusListEntries(args: IStatusListEntryAvailableArgs): Promise<number[]> {
41
- const statusListIndex = Array.isArray(args.statusListIndex) ? args.statusListIndex : [args.statusListIndex]
42
- const statusList = await this.getStatusList({ ...args, id: args.statusListId })
43
- const repo = await this.getStatusListEntryRepo()
44
- const results = (
45
- await repo.find({
46
- where: {
47
- statusList,
48
- statusListIndex: In(statusListIndex),
49
- },
50
- })
51
- ).map((index) => index.statusListIndex)
52
- return statusListIndex.filter((index) => !results.includes(index))
53
- }
54
-
55
- async addStatusListEntry(args: IAddStatusListEntryArgs): Promise<IStatusListEntryEntity> {
56
- return (await this.getStatusListEntryRepo()).save(args)
57
- }
58
-
59
- async updateStatusListEntry(args: IAddStatusListEntryArgs): Promise<IStatusListEntryEntity> {
60
- const statusListId = typeof args.statusList === 'string' ? args.statusList : args.statusList.id
61
- const result = await this.getStatusListEntryByIndex({ ...args, statusListId, errorOnNotFound: false })
62
- const updatedEntry: Partial<IStatusListEntryEntity> = {
63
- value: args.value,
64
- correlationId: args.correlationId,
65
- credentialHash: args.credentialHash,
66
- credentialId: args.credentialId,
67
- }
68
-
69
- const updateResult = await (
70
- await this.getStatusListEntryRepo()
71
- ).upsert(
72
- { ...(result ?? { statusList: args.statusList, statusListIndex: args.statusListIndex }), ...updatedEntry },
73
- { conflictPaths: ['statusList', 'statusListIndex'] },
74
- )
75
- console.log(updateResult)
76
- return (await this.getStatusListEntryByIndex({ ...args, statusListId, errorOnNotFound: true })) as IStatusListEntryEntity
77
- }
78
-
79
- async getStatusListEntryByIndex(args: IGetStatusListEntryByIndexArgs): Promise<StatusListEntryEntity | undefined> {
80
- if (!args.statusListId && !args.correlationId) {
81
- throw Error(`Cannot get statusList entry if not either a statusList id or correlationId is provided`)
82
- }
83
- const result = await (
84
- await this.getStatusListEntryRepo()
85
- ).findOne({
86
- where: {
87
- ...(args.statusListId && { statusList: args.statusListId }),
88
- ...(args.correlationId && { correlationId: args.correlationId }),
89
- statusListIndex: args.statusListIndex,
90
- },
91
- })
92
-
93
- if (!result && args.errorOnNotFound) {
94
- throw Error(`Could not find status list index ${args.statusListIndex} for status list id ${args.statusListId}`)
95
- }
96
- return result ?? undefined
97
- }
98
-
99
- async removeStatusListEntryByIndex(args: IGetStatusListEntryByIndexArgs): Promise<boolean> {
100
- let error = false
101
- try {
102
- await this.getStatusListEntryByIndex(args) // only used to check it exists
103
- } catch (error) {
104
- error = true
105
- }
106
- if (error) {
107
- console.log(`Could not delete statusList ${args.statusListId} entry by index ${args.statusListIndex}`)
108
- } else {
109
- const result = await (
110
- await this.getStatusListEntryRepo()
111
- ).delete({
112
- ...(args.statusListId && { statusList: args.statusListId }),
113
- ...(args.correlationId && { correlationId: args.correlationId }),
114
- statusListIndex: args.statusListIndex,
115
- })
116
- error = !result.affected || result.affected !== 1
117
- }
118
- return !error
119
- }
120
-
121
- async getStatusListEntryByCredentialId(args: IGetStatusListEntryByCredentialIdArgs): Promise<StatusListEntryEntity | undefined> {
122
- const credentialId = args.credentialId
123
- if (!credentialId) {
124
- throw Error('Can only get a credential by credentialId when a credentialId is supplied')
125
- }
126
- const statusList = await this.getStatusList({ id: args.statusListId, correlationId: args.statusListCorrelationId })
127
- const where = {
128
- statusList: statusList.id,
129
- ...(args.entryCorrelationId && { correlationId: args.entryCorrelationId }),
130
- credentialId,
131
- }
132
- console.log(`Entries: ${JSON.stringify(await (await this.getStatusListEntryRepo()).find(), null, 2)}`)
133
- const result = await (await this.getStatusListEntryRepo()).findOne({ where })
134
-
135
- if (!result && args.errorOnNotFound) {
136
- throw Error(`Could not find status list credential id ${credentialId} for status list id ${statusList.id}`)
137
- }
138
- return result ?? undefined
139
- }
140
-
141
- async removeStatusListEntryByCredentialId(args: IGetStatusListEntryByCredentialIdArgs): Promise<boolean> {
142
- let error = false
143
- try {
144
- await this.getStatusListEntryByCredentialId(args) // only used to check it exists
145
- } catch (error) {
146
- error = true
147
- }
148
- if (!error) {
149
- const result = await (
150
- await this.getStatusListEntryRepo()
151
- ).delete({
152
- ...(args.statusListId && { statusList: args.statusListId }),
153
- ...(args.entryCorrelationId && { correlationId: args.entryCorrelationId }),
154
- credentialId: args.credentialId,
155
- })
156
- error = !result.affected || result.affected !== 1
157
- }
158
- return !error
159
- }
160
-
161
- async getStatusListEntries(args: IGetStatusListEntriesArgs): Promise<StatusListEntryEntity[]> {
162
- return (await this.getStatusListEntryRepo()).find({ where: { ...args?.filter, statusList: args.statusListId } })
163
- }
164
-
165
- async getStatusList(args: IGetStatusListArgs): Promise<IStatusListEntity> {
166
- if (!args.id && !args.correlationId) {
167
- throw Error(`At least and 'id' or 'correlationId' needs to be provided to lookup a status list`)
168
- }
169
- const where = []
170
- if (args.id) {
171
- where.push({ id: args.id })
172
- }
173
- if (args.correlationId) {
174
- where.push({ correlationId: args.correlationId })
175
- }
176
- const result = await (await this.getStatusListRepo()).findOne({ where })
177
- if (!result) {
178
- throw Error(`No status list found for id ${args.id}`)
179
- }
180
- return result
181
- }
182
-
183
- async getStatusLists(args: IGetStatusListsArgs): Promise<Array<IStatusListEntity>> {
184
- const result = await (
185
- await this.getStatusListRepo()
186
- ).find({
187
- where: args.filter,
188
- })
189
-
190
- if (!result) {
191
- return []
192
- }
193
- return result
194
- }
195
-
196
- async addStatusList(args: IAddStatusListArgs): Promise<IStatusListEntity> {
197
- const { id, correlationId } = args
198
-
199
- const result = await (
200
- await this.getStatusListRepo()
201
- ).findOne({
202
- where: [{ id }, { correlationId }],
203
- })
204
- if (result) {
205
- throw Error(`Status list for id ${id}, correlationId ${correlationId} already exists`)
206
- }
207
-
208
- debug('Adding status list ', id)
209
- const createdResult = await (await this.getStatusListRepo()).save(args)
210
-
211
- return createdResult
212
- }
213
-
214
- async updateStatusList(args: IUpdateStatusListIndexArgs): Promise<IStatusListEntity> {
215
- const result = await this.getStatusList(args)
216
- debug('Updating status list', result)
217
- const updatedResult = await (await this.getStatusListRepo()).save(args, { transaction: true })
218
- return updatedResult
219
- }
220
-
221
- async removeStatusList(args: IRemoveStatusListArgs): Promise<void> {
222
- const result = await this.getStatusList(args)
223
- await (await this.getStatusListRepo()).delete(result)
224
- }
225
-
226
- private async getDS(): Promise<DataSource> {
227
- return await this._dbConnection
228
- }
229
-
230
- async getStatusListRepo(): Promise<Repository<StatusListEntity>> {
231
- return (await this.getDS()).getRepository(StatusListEntity)
232
- }
233
-
234
- async getStatusListEntryRepo(): Promise<Repository<StatusListEntryEntity>> {
235
- return (await this.getDS()).getRepository(StatusListEntryEntity)
236
- }
237
- }
1
+ import { OrPromise } from '@sphereon/ssi-types'
2
+ import Debug from 'debug'
3
+ import { DataSource, In, Repository } from 'typeorm'
4
+ import { StatusListEntity } from '../entities/statusList2021/StatusList2021Entity'
5
+ import { StatusListEntryEntity } from '../entities/statusList2021/StatusList2021EntryEntity'
6
+ import {
7
+ IAddStatusListArgs,
8
+ IAddStatusListEntryArgs,
9
+ IGetStatusListArgs,
10
+ IGetStatusListEntriesArgs,
11
+ IGetStatusListEntryByCredentialIdArgs,
12
+ IGetStatusListEntryByIndexArgs,
13
+ IGetStatusListsArgs,
14
+ IRemoveStatusListArgs,
15
+ IStatusListEntryAvailableArgs,
16
+ IUpdateStatusListIndexArgs,
17
+ IStatusListEntity,
18
+ IStatusListEntryEntity,
19
+ } from '../types'
20
+ import { IStatusListStore } from './IStatusListStore'
21
+
22
+ const debug = Debug('sphereon:ssi-sdk:data-store:status-list')
23
+
24
+ export class StatusListStore implements IStatusListStore {
25
+ private readonly _dbConnection: OrPromise<DataSource>
26
+
27
+ constructor(dbConnection: OrPromise<DataSource>) {
28
+ this._dbConnection = dbConnection
29
+ }
30
+
31
+ /**
32
+ * Get's the available status list indices from the provided indices. Meaning it will filter out any index that is already known.
33
+ *
34
+ * The idea is that the caller provides a set of random status list indices. We can relatively easy check against the DB in an optimized way.
35
+ * If the status list is large it is probably best ot also provide at least a good number of indices. So something like 10 or 20 values.
36
+ * Callers are also expected to call this function multiple times if it does not yield results
37
+ *
38
+ * @param args
39
+ */
40
+ async availableStatusListEntries(args: IStatusListEntryAvailableArgs): Promise<number[]> {
41
+ const statusListIndex = Array.isArray(args.statusListIndex) ? args.statusListIndex : [args.statusListIndex]
42
+ const statusList = await this.getStatusList({ ...args, id: args.statusListId })
43
+ const repo = await this.getStatusListEntryRepo()
44
+ const results = (
45
+ await repo.find({
46
+ where: {
47
+ statusList,
48
+ statusListIndex: In(statusListIndex),
49
+ },
50
+ })
51
+ ).map((index) => index.statusListIndex)
52
+ return statusListIndex.filter((index) => !results.includes(index))
53
+ }
54
+
55
+ async addStatusListEntry(args: IAddStatusListEntryArgs): Promise<IStatusListEntryEntity> {
56
+ return (await this.getStatusListEntryRepo()).save(args)
57
+ }
58
+
59
+ async updateStatusListEntry(args: IAddStatusListEntryArgs): Promise<IStatusListEntryEntity> {
60
+ const statusListId = typeof args.statusList === 'string' ? args.statusList : args.statusList.id
61
+ const result = await this.getStatusListEntryByIndex({ ...args, statusListId, errorOnNotFound: false })
62
+ const updatedEntry: Partial<IStatusListEntryEntity> = {
63
+ value: args.value,
64
+ correlationId: args.correlationId,
65
+ credentialHash: args.credentialHash,
66
+ credentialId: args.credentialId,
67
+ }
68
+
69
+ const updateResult = await (
70
+ await this.getStatusListEntryRepo()
71
+ ).upsert(
72
+ { ...(result ?? { statusList: args.statusList, statusListIndex: args.statusListIndex }), ...updatedEntry },
73
+ { conflictPaths: ['statusList', 'statusListIndex'] },
74
+ )
75
+ console.log(updateResult)
76
+ return (await this.getStatusListEntryByIndex({ ...args, statusListId, errorOnNotFound: true })) as IStatusListEntryEntity
77
+ }
78
+
79
+ async getStatusListEntryByIndex(args: IGetStatusListEntryByIndexArgs): Promise<StatusListEntryEntity | undefined> {
80
+ if (!args.statusListId && !args.correlationId) {
81
+ throw Error(`Cannot get statusList entry if not either a statusList id or correlationId is provided`)
82
+ }
83
+ const result = await (
84
+ await this.getStatusListEntryRepo()
85
+ ).findOne({
86
+ where: {
87
+ ...(args.statusListId && { statusList: args.statusListId }),
88
+ ...(args.correlationId && { correlationId: args.correlationId }),
89
+ statusListIndex: args.statusListIndex,
90
+ },
91
+ })
92
+
93
+ if (!result && args.errorOnNotFound) {
94
+ throw Error(`Could not find status list index ${args.statusListIndex} for status list id ${args.statusListId}`)
95
+ }
96
+ return result ?? undefined
97
+ }
98
+
99
+ async removeStatusListEntryByIndex(args: IGetStatusListEntryByIndexArgs): Promise<boolean> {
100
+ let error = false
101
+ try {
102
+ await this.getStatusListEntryByIndex(args) // only used to check it exists
103
+ } catch (error) {
104
+ error = true
105
+ }
106
+ if (error) {
107
+ console.log(`Could not delete statusList ${args.statusListId} entry by index ${args.statusListIndex}`)
108
+ } else {
109
+ const result = await (
110
+ await this.getStatusListEntryRepo()
111
+ ).delete({
112
+ ...(args.statusListId && { statusList: args.statusListId }),
113
+ ...(args.correlationId && { correlationId: args.correlationId }),
114
+ statusListIndex: args.statusListIndex,
115
+ })
116
+ error = !result.affected || result.affected !== 1
117
+ }
118
+ return !error
119
+ }
120
+
121
+ async getStatusListEntryByCredentialId(args: IGetStatusListEntryByCredentialIdArgs): Promise<StatusListEntryEntity | undefined> {
122
+ const credentialId = args.credentialId
123
+ if (!credentialId) {
124
+ throw Error('Can only get a credential by credentialId when a credentialId is supplied')
125
+ }
126
+ const statusList = await this.getStatusList({ id: args.statusListId, correlationId: args.statusListCorrelationId })
127
+ const where = {
128
+ statusList: statusList.id,
129
+ ...(args.entryCorrelationId && { correlationId: args.entryCorrelationId }),
130
+ credentialId,
131
+ }
132
+ console.log(`Entries: ${JSON.stringify(await (await this.getStatusListEntryRepo()).find(), null, 2)}`)
133
+ const result = await (await this.getStatusListEntryRepo()).findOne({ where })
134
+
135
+ if (!result && args.errorOnNotFound) {
136
+ throw Error(`Could not find status list credential id ${credentialId} for status list id ${statusList.id}`)
137
+ }
138
+ return result ?? undefined
139
+ }
140
+
141
+ async removeStatusListEntryByCredentialId(args: IGetStatusListEntryByCredentialIdArgs): Promise<boolean> {
142
+ let error = false
143
+ try {
144
+ await this.getStatusListEntryByCredentialId(args) // only used to check it exists
145
+ } catch (error) {
146
+ error = true
147
+ }
148
+ if (!error) {
149
+ const result = await (
150
+ await this.getStatusListEntryRepo()
151
+ ).delete({
152
+ ...(args.statusListId && { statusList: args.statusListId }),
153
+ ...(args.entryCorrelationId && { correlationId: args.entryCorrelationId }),
154
+ credentialId: args.credentialId,
155
+ })
156
+ error = !result.affected || result.affected !== 1
157
+ }
158
+ return !error
159
+ }
160
+
161
+ async getStatusListEntries(args: IGetStatusListEntriesArgs): Promise<StatusListEntryEntity[]> {
162
+ return (await this.getStatusListEntryRepo()).find({ where: { ...args?.filter, statusList: args.statusListId } })
163
+ }
164
+
165
+ async getStatusList(args: IGetStatusListArgs): Promise<IStatusListEntity> {
166
+ if (!args.id && !args.correlationId) {
167
+ throw Error(`At least and 'id' or 'correlationId' needs to be provided to lookup a status list`)
168
+ }
169
+ const where = []
170
+ if (args.id) {
171
+ where.push({ id: args.id })
172
+ }
173
+ if (args.correlationId) {
174
+ where.push({ correlationId: args.correlationId })
175
+ }
176
+ const result = await (await this.getStatusListRepo()).findOne({ where })
177
+ if (!result) {
178
+ throw Error(`No status list found for id ${args.id}`)
179
+ }
180
+ return result
181
+ }
182
+
183
+ async getStatusLists(args: IGetStatusListsArgs): Promise<Array<IStatusListEntity>> {
184
+ const result = await (
185
+ await this.getStatusListRepo()
186
+ ).find({
187
+ where: args.filter,
188
+ })
189
+
190
+ if (!result) {
191
+ return []
192
+ }
193
+ return result
194
+ }
195
+
196
+ async addStatusList(args: IAddStatusListArgs): Promise<IStatusListEntity> {
197
+ const { id, correlationId } = args
198
+
199
+ const result = await (
200
+ await this.getStatusListRepo()
201
+ ).findOne({
202
+ where: [{ id }, { correlationId }],
203
+ })
204
+ if (result) {
205
+ throw Error(`Status list for id ${id}, correlationId ${correlationId} already exists`)
206
+ }
207
+
208
+ debug('Adding status list ', id)
209
+ const createdResult = await (await this.getStatusListRepo()).save(args)
210
+
211
+ return createdResult
212
+ }
213
+
214
+ async updateStatusList(args: IUpdateStatusListIndexArgs): Promise<IStatusListEntity> {
215
+ const result = await this.getStatusList(args)
216
+ debug('Updating status list', result)
217
+ const updatedResult = await (await this.getStatusListRepo()).save(args, { transaction: true })
218
+ return updatedResult
219
+ }
220
+
221
+ async removeStatusList(args: IRemoveStatusListArgs): Promise<void> {
222
+ const result = await this.getStatusList(args)
223
+ await (await this.getStatusListRepo()).delete(result)
224
+ }
225
+
226
+ private async getDS(): Promise<DataSource> {
227
+ return await this._dbConnection
228
+ }
229
+
230
+ async getStatusListRepo(): Promise<Repository<StatusListEntity>> {
231
+ return (await this.getDS()).getRepository(StatusListEntity)
232
+ }
233
+
234
+ async getStatusListEntryRepo(): Promise<Repository<StatusListEntryEntity>> {
235
+ return (await this.getDS()).getRepository(StatusListEntryEntity)
236
+ }
237
+ }