@twin.org/document-management-service 0.0.2-next.3 → 0.0.3-next.1

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.
@@ -0,0 +1,622 @@
1
+ import { AttestationContexts } from "@twin.org/attestation-models";
2
+ import { AuditableItemGraphContexts, AuditableItemGraphTypes } from "@twin.org/auditable-item-graph-models";
3
+ import { BlobStorageContexts } from "@twin.org/blob-storage-models";
4
+ import { ContextIdKeys, ContextIdStore } from "@twin.org/context";
5
+ import { BaseError, Coerce, ComponentFactory, Converter, GeneralError, Guards, Is, NotFoundError, ObjectHelper, Urn } from "@twin.org/core";
6
+ import { Sha256 } from "@twin.org/crypto";
7
+ import { JsonLdProcessor } from "@twin.org/data-json-ld";
8
+ import { DocumentContexts, DocumentTypes } from "@twin.org/document-management-models";
9
+ import { SchemaOrgContexts, SchemaOrgDataTypes, SchemaOrgTypes } from "@twin.org/standards-schema-org";
10
+ import { UneceDocumentCodes } from "@twin.org/standards-unece";
11
+ /**
12
+ * Service for performing document management operations.
13
+ */
14
+ export class DocumentManagementService {
15
+ /**
16
+ * Runtime name for the class.
17
+ */
18
+ static CLASS_NAME = "DocumentManagementService";
19
+ /**
20
+ * The component for the auditable item graph.
21
+ * @internal
22
+ */
23
+ _auditableItemGraphComponent;
24
+ /**
25
+ * The connector for the blob component.
26
+ * @internal
27
+ */
28
+ _blobStorageComponent;
29
+ /**
30
+ * The connector for the attestation.
31
+ * @internal
32
+ */
33
+ _attestationComponent;
34
+ /**
35
+ * The connector for the data processing.
36
+ * @internal
37
+ */
38
+ _dataProcessingComponent;
39
+ /**
40
+ * Create a new instance of DocumentManagementService.
41
+ * @param options The options for the service.
42
+ */
43
+ constructor(options) {
44
+ this._auditableItemGraphComponent = ComponentFactory.get(options?.auditableItemGraphComponentType ?? "auditable-item-graph");
45
+ this._blobStorageComponent = ComponentFactory.get(options?.blobStorageComponentType ?? "blob-storage");
46
+ this._attestationComponent = ComponentFactory.get(options?.attestationComponentType ?? "attestation");
47
+ this._dataProcessingComponent = ComponentFactory.get(options?.dataProcessingComponentType ?? "data-processing");
48
+ SchemaOrgDataTypes.registerRedirects();
49
+ }
50
+ /**
51
+ * Returns the class name of the component.
52
+ * @returns The class name of the component.
53
+ */
54
+ className() {
55
+ return DocumentManagementService.CLASS_NAME;
56
+ }
57
+ /**
58
+ * Store a document as an auditable item graph vertex and add its content to blob storage.
59
+ * If the document id already exists and the blob data is different a new revision will be created.
60
+ * For any other changes the current revision will be updated.
61
+ * @param documentId The document id to create.
62
+ * @param documentIdFormat The format of the document identifier.
63
+ * @param documentCode The code for the document type.
64
+ * @param blob The data to create the document with.
65
+ * @param annotationObject Additional information to associate with the document.
66
+ * @param auditableItemGraphEdges The auditable item graph vertices to connect the document to.
67
+ * @param options Additional options for the set operation.
68
+ * @param options.createAttestation Flag to create an attestation for the document, defaults to false.
69
+ * @param options.addAlias Flag to add the document id as an alias to the aig vertex, defaults to true.
70
+ * @param options.aliasAnnotationObject Annotation object for the alias.
71
+ * @returns The auditable item graph vertex created for the document including its revision.
72
+ */
73
+ async create(documentId, documentIdFormat, documentCode, blob, annotationObject, auditableItemGraphEdges, options) {
74
+ Guards.stringValue(DocumentManagementService.CLASS_NAME, "documentId", documentId);
75
+ Guards.arrayOneOf(DocumentManagementService.CLASS_NAME, "documentCode", documentCode, Object.values(UneceDocumentCodes));
76
+ Guards.uint8Array(DocumentManagementService.CLASS_NAME, "blob", blob);
77
+ const contextIds = await ContextIdStore.getContextIds();
78
+ try {
79
+ // Get the connected vertices first, if one fails we abort the create
80
+ const connectedVertices = {};
81
+ if (Is.arrayValue(auditableItemGraphEdges)) {
82
+ for (const edge of auditableItemGraphEdges) {
83
+ connectedVertices[edge.targetId] = await this._auditableItemGraphComponent.get(edge.targetId);
84
+ }
85
+ }
86
+ const documentVertex = {};
87
+ if (options?.addAlias ?? true) {
88
+ documentVertex.aliases ??= [];
89
+ documentVertex.aliases.push({
90
+ "@context": AuditableItemGraphContexts.ContextRoot,
91
+ type: AuditableItemGraphTypes.Alias,
92
+ id: documentId,
93
+ aliasFormat: documentIdFormat,
94
+ annotationObject: options?.aliasAnnotationObject
95
+ });
96
+ }
97
+ // Add the blob to blob storage
98
+ const blobStorageId = await this._blobStorageComponent.create(Converter.bytesToBase64(blob));
99
+ const currentRevision = {
100
+ "@context": [
101
+ DocumentContexts.ContextRoot,
102
+ DocumentContexts.ContextRootCommon,
103
+ SchemaOrgContexts.ContextRoot
104
+ ],
105
+ type: DocumentTypes.Document,
106
+ id: this.createDocumentId(documentId, 0),
107
+ documentId,
108
+ documentIdFormat,
109
+ documentCode,
110
+ documentRevision: 0,
111
+ annotationObject,
112
+ blobHash: this.generateBlobHash(blob),
113
+ blobStorageId,
114
+ dateCreated: new Date(Date.now()).toISOString(),
115
+ organizationIdentity: contextIds?.[ContextIdKeys.Organization],
116
+ userIdentity: contextIds?.[ContextIdKeys.User]
117
+ };
118
+ if (options?.createAttestation ?? false) {
119
+ currentRevision.attestationId = await this.createAttestation(currentRevision);
120
+ }
121
+ // Add the new revision in to the vertex
122
+ documentVertex.resources ??= [];
123
+ documentVertex.resources.push({
124
+ "@context": AuditableItemGraphContexts.ContextRoot,
125
+ type: AuditableItemGraphTypes.Resource,
126
+ resourceObject: currentRevision
127
+ });
128
+ // Add the edges from the document to the items
129
+ this.updateEdges(documentVertex, auditableItemGraphEdges);
130
+ // And create the vertex
131
+ const vertexId = await this._auditableItemGraphComponent.create(documentVertex);
132
+ // Now add the edges to the connected vertices
133
+ await this.updateConnectedEdges(connectedVertices, vertexId, [], auditableItemGraphEdges, documentId, documentIdFormat);
134
+ return vertexId;
135
+ }
136
+ catch (error) {
137
+ if (BaseError.someErrorName(error, "NotFoundError")) {
138
+ throw error;
139
+ }
140
+ throw new GeneralError(DocumentManagementService.CLASS_NAME, "createFailed", undefined, error);
141
+ }
142
+ }
143
+ /**
144
+ * Update a document as an auditable item graph vertex and add its content to blob storage.
145
+ * If the blob data is different a new revision will be created.
146
+ * For any other changes the current revision will be updated.
147
+ * @param auditableItemGraphDocumentId The auditable item graph vertex id which contains the document.
148
+ * @param blob The data to update the document with.
149
+ * @param annotationObject Additional information to associate with the document.
150
+ * @param auditableItemGraphEdges The auditable item graph vertices to connect the document to, if undefined retains current connections.
151
+ * @returns Nothing.
152
+ */
153
+ async update(auditableItemGraphDocumentId, blob, annotationObject, auditableItemGraphEdges) {
154
+ Urn.guard(DocumentManagementService.CLASS_NAME, "auditableItemGraphDocumentId", auditableItemGraphDocumentId);
155
+ try {
156
+ const documentVertex = await this._auditableItemGraphComponent.get(auditableItemGraphDocumentId, { includeDeleted: true });
157
+ if (Is.empty(documentVertex.resources)) {
158
+ throw new NotFoundError(DocumentManagementService.CLASS_NAME, "documentRevisionNone");
159
+ }
160
+ const documents = await this.getDocumentsFromVertex(documentVertex);
161
+ const latestRevision = documents.itemListElement[0];
162
+ documentVertex.resources = documentVertex.resources.filter(r => Is.empty(r.dateDeleted));
163
+ if (Is.empty(latestRevision)) {
164
+ throw new NotFoundError(DocumentManagementService.CLASS_NAME, "documentRevisionNone");
165
+ }
166
+ // If auditableItemGraphEdges is undefined we are not updating the edges
167
+ // an empty array can be passed to remove all edges
168
+ const connectedVertices = {};
169
+ if (Is.array(auditableItemGraphEdges)) {
170
+ // Get the updated connected vertices first, if one fails we abort the update
171
+ for (const edge of auditableItemGraphEdges) {
172
+ connectedVertices[edge.targetId] = await this._auditableItemGraphComponent.get(edge.targetId);
173
+ }
174
+ // Also get the current edges in case some need disconnecting
175
+ if (Is.arrayValue(documents.edges)) {
176
+ for (const edgeId of documents.edges) {
177
+ // If we haven't retrieved the edge then it must be one that needs removing
178
+ if (Is.empty(connectedVertices[edgeId])) {
179
+ connectedVertices[edgeId] = await this._auditableItemGraphComponent.get(edgeId);
180
+ }
181
+ }
182
+ }
183
+ }
184
+ let updatedVertex = false;
185
+ // If the blob is set and its hash has changed then we create a new revision
186
+ if (Is.uint8Array(blob)) {
187
+ const newBlobHash = this.generateBlobHash(blob);
188
+ if (latestRevision.blobHash !== newBlobHash) {
189
+ // Add the blob to blob storage
190
+ const blobStorageId = await this._blobStorageComponent.create(Converter.bytesToBase64(blob));
191
+ const newRevision = ObjectHelper.clone(latestRevision);
192
+ newRevision.documentRevision++;
193
+ newRevision.id = this.createDocumentId(newRevision.documentId, newRevision.documentRevision);
194
+ newRevision.blobHash = newBlobHash;
195
+ newRevision.blobStorageId = blobStorageId;
196
+ newRevision.annotationObject = annotationObject;
197
+ if (Is.stringValue(latestRevision.attestationId)) {
198
+ newRevision.attestationId = await this.createAttestation(newRevision);
199
+ }
200
+ documentVertex.resources.push({
201
+ "@context": AuditableItemGraphContexts.ContextRoot,
202
+ type: AuditableItemGraphTypes.Resource,
203
+ resourceObject: newRevision
204
+ });
205
+ updatedVertex = true;
206
+ }
207
+ }
208
+ // If the blob wasn't updated but the annotation object has then update the current revision
209
+ // instead of creating a new one
210
+ if (!updatedVertex &&
211
+ !ObjectHelper.equal(latestRevision.annotationObject, annotationObject)) {
212
+ updatedVertex = true;
213
+ latestRevision.annotationObject = annotationObject;
214
+ latestRevision.dateModified = new Date(Date.now()).toISOString();
215
+ }
216
+ const existingEdgeIds = documentVertex.edges?.map(e => e.targetId) ?? [];
217
+ // Update the edges from the document to the items
218
+ const edgesUpdated = this.updateEdges(documentVertex, auditableItemGraphEdges);
219
+ if (edgesUpdated) {
220
+ updatedVertex = true;
221
+ }
222
+ if (updatedVertex) {
223
+ await this._auditableItemGraphComponent.update(documentVertex);
224
+ }
225
+ if (edgesUpdated) {
226
+ await this.updateConnectedEdges(connectedVertices, auditableItemGraphDocumentId, existingEdgeIds, auditableItemGraphEdges, latestRevision.documentId, latestRevision.documentIdFormat);
227
+ }
228
+ }
229
+ catch (error) {
230
+ if (BaseError.someErrorName(error, "NotFoundError")) {
231
+ throw error;
232
+ }
233
+ throw new GeneralError(DocumentManagementService.CLASS_NAME, "updateFailed", undefined, error);
234
+ }
235
+ }
236
+ /**
237
+ * Get a document using it's auditable item graph vertex id and optional revision.
238
+ * @param auditableItemGraphDocumentId The auditable item graph vertex id which contains the document.
239
+ * @param options Additional options for the get operation.
240
+ * @param options.includeBlobStorageMetadata Flag to include the blob storage metadata for the document, defaults to false.
241
+ * @param options.includeBlobStorageData Flag to include the blob storage data for the document, defaults to false.
242
+ * @param options.includeAttestation Flag to include the attestation information for the document, defaults to false.
243
+ * @param options.includeRemoved Flag to include deleted documents, defaults to false.
244
+ * @param options.extractRuleGroupId If provided will extract data from the document using the specified rule group id.
245
+ * @param options.extractMimeType By default extraction will auto detect the mime type of the document, this can be used to override the detection.
246
+ * @param cursor The cursor to get the next chunk of revisions.
247
+ * @param limit Limit the number of items to return, defaults to 1 so only most recent is returned.
248
+ * @returns The documents and revisions if requested, ordered by revision descending, cursor is set if there are more document revisions.
249
+ */
250
+ async get(auditableItemGraphDocumentId, options, cursor, limit) {
251
+ Urn.guard(DocumentManagementService.CLASS_NAME, "auditableItemGraphDocumentId", auditableItemGraphDocumentId);
252
+ try {
253
+ const documentVertex = await this._auditableItemGraphComponent.get(auditableItemGraphDocumentId, { includeDeleted: options?.includeRemoved });
254
+ // Populate the document and revisions with the options set
255
+ const documents = await this.getDocumentsFromVertex(documentVertex, options, cursor, limit);
256
+ const result = await JsonLdProcessor.compact(documents, documents["@context"]);
257
+ return result;
258
+ }
259
+ catch (error) {
260
+ if (BaseError.someErrorName(error, "NotFoundError")) {
261
+ throw error;
262
+ }
263
+ throw new GeneralError(DocumentManagementService.CLASS_NAME, "getFailed", undefined, error);
264
+ }
265
+ }
266
+ /**
267
+ * Get a document revision using it's auditable item graph vertex id.
268
+ * @param auditableItemGraphDocumentId The auditable item graph vertex id which contains the document.
269
+ * @param revision The revision id for the document.
270
+ * @param options Additional options for the get operation.
271
+ * @param options.includeBlobStorageMetadata Flag to include the blob storage metadata for the document, defaults to false.
272
+ * @param options.includeBlobStorageData Flag to include the blob storage data for the document, defaults to false.
273
+ * @param options.includeAttestation Flag to include the attestation information for the document, defaults to false.
274
+ * @param options.extractRuleGroupId If provided will extract data from the document using the specified rule group id.
275
+ * @param options.extractMimeType By default extraction will auto detect the mime type of the document, this can be used to override the detection.
276
+ * @returns The documents and revisions if requested, ordered by revision descending, cursor is set if there are more document revisions.
277
+ */
278
+ async getRevision(auditableItemGraphDocumentId, revision, options) {
279
+ Urn.guard(DocumentManagementService.CLASS_NAME, "auditableItemGraphDocumentId", auditableItemGraphDocumentId);
280
+ Guards.integer(DocumentManagementService.CLASS_NAME, "revision", revision);
281
+ try {
282
+ const documentVertex = await this._auditableItemGraphComponent.get(auditableItemGraphDocumentId, { includeDeleted: true });
283
+ if (Is.empty(documentVertex.resources)) {
284
+ throw new NotFoundError(DocumentManagementService.CLASS_NAME, "documentRevisionNone");
285
+ }
286
+ documentVertex.resources = documentVertex.resources.filter(d => d.resourceObject?.documentRevision === revision);
287
+ if (documentVertex.resources.length === 0) {
288
+ throw new NotFoundError(DocumentManagementService.CLASS_NAME, "documentRevisionNotFound", revision.toString());
289
+ }
290
+ // Populate the document and revisions with the options set
291
+ const docList = await this.getDocumentsFromVertex(documentVertex, options);
292
+ const result = await JsonLdProcessor.compact(docList.itemListElement[0], docList.itemListElement[0]["@context"]);
293
+ return result;
294
+ }
295
+ catch (error) {
296
+ if (BaseError.someErrorName(error, "NotFoundError")) {
297
+ throw error;
298
+ }
299
+ throw new GeneralError(DocumentManagementService.CLASS_NAME, "getRevisionFailed", undefined, error);
300
+ }
301
+ }
302
+ /**
303
+ * Remove an auditable item graph vertex using it's id.
304
+ * The document dateDeleted will be set, but can still be queried with the includeRemoved flag.
305
+ * @param auditableItemGraphDocumentId The auditable item graph vertex id which contains the document.
306
+ * @param revision The revision of the document to remove.
307
+ * @returns Nothing.
308
+ */
309
+ async removeRevision(auditableItemGraphDocumentId, revision) {
310
+ Urn.guard(DocumentManagementService.CLASS_NAME, "auditableItemGraphDocumentId", auditableItemGraphDocumentId);
311
+ Guards.number(DocumentManagementService.CLASS_NAME, "revision", revision);
312
+ try {
313
+ const documentVertex = await this._auditableItemGraphComponent.get(auditableItemGraphDocumentId);
314
+ if (Is.empty(documentVertex.resources)) {
315
+ throw new NotFoundError(DocumentManagementService.CLASS_NAME, "documentRevisionNone");
316
+ }
317
+ const docRevisionIndex = documentVertex.resources.findIndex(d => d.resourceObject?.documentRevision === revision);
318
+ if (docRevisionIndex === -1) {
319
+ throw new NotFoundError(DocumentManagementService.CLASS_NAME, "documentRevisionNotFound", revision.toString());
320
+ }
321
+ documentVertex.resources.splice(docRevisionIndex, 1);
322
+ await this._auditableItemGraphComponent.update(documentVertex);
323
+ }
324
+ catch (error) {
325
+ if (BaseError.someErrorName(error, "NotFoundError")) {
326
+ throw error;
327
+ }
328
+ throw new GeneralError(DocumentManagementService.CLASS_NAME, "removeRevisionFailed", undefined, error);
329
+ }
330
+ }
331
+ /**
332
+ * Find all the document with a specific id.
333
+ * @param documentId The document id to find in the graph.
334
+ * @param cursor The cursor to get the next chunk of documents.
335
+ * @param limit The limit to get the next chunk of documents.
336
+ * @returns The graph vertices that contain documents referencing the specified document id.
337
+ */
338
+ async query(documentId, cursor, limit) {
339
+ Guards.stringValue(DocumentManagementService.CLASS_NAME, "documentId", documentId);
340
+ try {
341
+ const result = await this._auditableItemGraphComponent.query({
342
+ id: documentId,
343
+ idMode: "both",
344
+ resourceTypes: [DocumentTypes.Document]
345
+ }, undefined, undefined, undefined, ["id", "dateCreated", "dateModified", "aliases", "annotationObject", "resources", "edges"], cursor, limit);
346
+ return result;
347
+ }
348
+ catch (error) {
349
+ if (BaseError.someErrorName(error, "NotFoundError")) {
350
+ throw error;
351
+ }
352
+ throw new GeneralError(DocumentManagementService.CLASS_NAME, "queryFailed", undefined, error);
353
+ }
354
+ }
355
+ /**
356
+ * Update the edges of the document vertex.
357
+ * @param documentVertex The document vertex to update.
358
+ * @param auditableItemGraphEdges The list of edges to use.
359
+ * @returns True if the edges were updated.
360
+ * @internal
361
+ */
362
+ updateEdges(documentVertex, auditableItemGraphEdges) {
363
+ let changed = false;
364
+ const existingEdgeIds = documentVertex.edges?.map(e => e.targetId) ?? [];
365
+ if (Is.array(auditableItemGraphEdges)) {
366
+ for (const aigEdge of auditableItemGraphEdges) {
367
+ const existingIndex = existingEdgeIds.indexOf(aigEdge.targetId);
368
+ if (existingIndex !== -1) {
369
+ // If the edge already exists then we don't need to add it again
370
+ // We just need to remove it from the list of existing ids
371
+ // any remaining after this loop will be need to be removed
372
+ existingEdgeIds.splice(existingIndex, 1);
373
+ }
374
+ else {
375
+ const vertexEdge = {
376
+ "@context": AuditableItemGraphContexts.ContextRoot,
377
+ type: AuditableItemGraphTypes.Edge,
378
+ targetId: aigEdge.targetId,
379
+ edgeRelationships: ["document"]
380
+ };
381
+ documentVertex.edges ??= [];
382
+ documentVertex.edges?.push(vertexEdge);
383
+ changed = true;
384
+ }
385
+ }
386
+ // Anything left in the existingEdgeIds array means they need to be removed
387
+ if (existingEdgeIds.length > 0 && Is.array(documentVertex.edges)) {
388
+ for (const existingEdgeId of existingEdgeIds) {
389
+ const existingIndex = documentVertex.edges.findIndex(e => e.targetId === existingEdgeId);
390
+ if (existingIndex !== -1) {
391
+ documentVertex.edges.splice(existingIndex, 1);
392
+ changed = true;
393
+ }
394
+ }
395
+ }
396
+ }
397
+ return changed;
398
+ }
399
+ /**
400
+ * Update the edges.
401
+ * @param connectedVertices The connected vertices for the edges.
402
+ * @param auditableItemGraphDocumentId The document id to use.
403
+ * @param documentVertex The document vertex to update.
404
+ * @param auditableItemGraphEdges The list of edges to use.
405
+ * @param documentId The document identifier.
406
+ * @param documentIdFormat The format of the document identifier.
407
+ * @internal
408
+ */
409
+ async updateConnectedEdges(connectedVertices, auditableItemGraphDocumentId, existingEdgeIds, auditableItemGraphEdges, documentId, documentIdFormat) {
410
+ if (Is.array(auditableItemGraphEdges)) {
411
+ for (const aigEdge of auditableItemGraphEdges) {
412
+ const connected = connectedVertices[aigEdge.targetId];
413
+ if (!Is.empty(connected)) {
414
+ let updatedConnected = false;
415
+ const existingIndex = existingEdgeIds.indexOf(aigEdge.targetId);
416
+ if (existingIndex !== -1) {
417
+ // If the edge already exists we remove it from the list of existing ids
418
+ // any remaining after this loop will be need to be disconnected
419
+ existingEdgeIds.splice(existingIndex, 1);
420
+ }
421
+ // Add the edge with the document vertex id if it doesn't already exist
422
+ const hasEdge = connected.edges?.some(e => e.targetId === auditableItemGraphDocumentId);
423
+ if (!hasEdge) {
424
+ const vertexEdge = {
425
+ "@context": AuditableItemGraphContexts.ContextRoot,
426
+ type: AuditableItemGraphTypes.Edge,
427
+ targetId: auditableItemGraphDocumentId,
428
+ edgeRelationships: ["document"]
429
+ };
430
+ connected.edges ??= [];
431
+ connected.edges?.push(vertexEdge);
432
+ updatedConnected = true;
433
+ }
434
+ // Add alias with the document id if option flag is set and it doesn't already exist
435
+ if (aigEdge.addAlias) {
436
+ const alias = connected.aliases?.find(a => a.id === documentId);
437
+ if (Is.empty(alias)) {
438
+ // No existing alias, so create one
439
+ const vertexAlias = {
440
+ "@context": AuditableItemGraphContexts.ContextRoot,
441
+ type: AuditableItemGraphTypes.Alias,
442
+ id: documentId,
443
+ aliasFormat: documentIdFormat,
444
+ annotationObject: aigEdge.aliasAnnotationObject
445
+ };
446
+ connected.aliases ??= [];
447
+ connected.aliases?.push(vertexAlias);
448
+ updatedConnected = true;
449
+ }
450
+ else if (!ObjectHelper.equal(alias.annotationObject, aigEdge.aliasAnnotationObject) ||
451
+ documentIdFormat !== alias.aliasFormat) {
452
+ // The alias already exists, but the format or annotation object has changed
453
+ alias.annotationObject = aigEdge.aliasAnnotationObject;
454
+ alias.aliasFormat = documentIdFormat;
455
+ updatedConnected = true;
456
+ }
457
+ }
458
+ if (updatedConnected) {
459
+ await this._auditableItemGraphComponent.update(connected);
460
+ }
461
+ }
462
+ }
463
+ }
464
+ // Anything left in the existingEdgeIds array means they need to be removed
465
+ if (existingEdgeIds.length > 0) {
466
+ for (const existingEdgeId of existingEdgeIds) {
467
+ const connected = connectedVertices[existingEdgeId];
468
+ if (!Is.empty(connected)) {
469
+ let updatedConnected = false;
470
+ // Remove the edge from the connected vertex
471
+ if (Is.arrayValue(connected.edges)) {
472
+ const existingIndex = connected.edges.findIndex(e => e.targetId === auditableItemGraphDocumentId);
473
+ if (existingIndex !== -1) {
474
+ connected.edges.splice(existingIndex, 1);
475
+ updatedConnected = true;
476
+ }
477
+ }
478
+ // Remove the alias from the connected vertex
479
+ if (Is.arrayValue(connected.aliases)) {
480
+ const existingIndex = connected.aliases.findIndex(e => e.id === documentId);
481
+ if (existingIndex !== -1) {
482
+ connected.aliases.splice(existingIndex, 1);
483
+ updatedConnected = true;
484
+ }
485
+ }
486
+ if (updatedConnected) {
487
+ await this._auditableItemGraphComponent.update(connected);
488
+ }
489
+ }
490
+ }
491
+ }
492
+ }
493
+ /**
494
+ * Generate a hash for the blob data.
495
+ * @param blob The blob data to hash.
496
+ * @returns The hash.
497
+ * @internal
498
+ */
499
+ generateBlobHash(blob) {
500
+ return `sha256:${Converter.bytesToBase64(Sha256.sum256(blob))}`;
501
+ }
502
+ /**
503
+ * Get the documents from the auditable item graph vertex.
504
+ * @param documentVertex The vertex containing the documents.
505
+ * @param options Additional options for the get operation.
506
+ * @param options.includeBlobStorageMetadata Flag to include the blob storage metadata for the document, defaults to false.
507
+ * @param options.includeBlobStorageData Flag to include the blob storage data for the document, defaults to false.
508
+ * @param options.includeAttestation Flag to include the attestation information for the document, defaults to false.
509
+ * @param options.extractRuleGroupId If provided will extract data from the document using the specified rule group id.
510
+ * @param options.extractMimeType By default extraction will auto detect the mime type of the document, this can be used to override the detection.
511
+ * @param cursor The cursor to get the next chunk of revisions.
512
+ * @param limit Limit the number of items to return, defaults to 1 so only most recent is returned.
513
+ * @returns The finalised list of documents.
514
+ * @internal
515
+ */
516
+ async getDocumentsFromVertex(documentVertex, options, cursor, limit) {
517
+ const docList = {
518
+ "@context": [
519
+ SchemaOrgContexts.ContextRoot,
520
+ DocumentContexts.ContextRoot,
521
+ DocumentContexts.ContextRootCommon
522
+ ],
523
+ type: SchemaOrgTypes.ItemList,
524
+ [SchemaOrgTypes.ItemListElement]: []
525
+ };
526
+ if (Is.arrayValue(documentVertex.resources)) {
527
+ // Sort by newest revision first
528
+ documentVertex.resources.sort((a, b) => (Coerce.number(b.resourceObject?.documentRevision) ?? 0) -
529
+ (Coerce.number(a.resourceObject?.documentRevision) ?? 0));
530
+ const startIndex = Coerce.integer(cursor) ?? 0;
531
+ const endIndex = Math.min(startIndex + (limit ?? 1), documentVertex.resources.length);
532
+ const slicedResources = documentVertex.resources.slice(startIndex, endIndex);
533
+ docList[SchemaOrgTypes.NextItem] =
534
+ documentVertex.resources.length > endIndex ? (endIndex + 1).toString() : undefined;
535
+ const includeBlobStorageMetadata = options?.includeBlobStorageMetadata ?? false;
536
+ const includeBlobStorageData = options?.includeBlobStorageData ?? false;
537
+ const includeAttestation = options?.includeAttestation ?? false;
538
+ const extractData = Is.stringValue(options?.extractRuleGroupId);
539
+ for (let i = 0; i < slicedResources.length; i++) {
540
+ const document = slicedResources[i].resourceObject;
541
+ if (Is.object(document)) {
542
+ document.dateDeleted = slicedResources[i].dateDeleted;
543
+ docList[SchemaOrgTypes.ItemListElement].push(document);
544
+ const blobRequired = includeBlobStorageMetadata || includeBlobStorageData;
545
+ if (blobRequired || extractData) {
546
+ const blobEntry = await this._blobStorageComponent.get(document.blobStorageId, {
547
+ includeContent: includeBlobStorageData || extractData
548
+ });
549
+ if (blobRequired) {
550
+ document.blobStorageEntry = blobEntry;
551
+ if (!docList["@context"].includes(BlobStorageContexts.ContextRoot)) {
552
+ docList["@context"].push(BlobStorageContexts.ContextRoot);
553
+ }
554
+ }
555
+ if (Is.stringValue(options?.extractRuleGroupId) && Is.stringValue(blobEntry.blob)) {
556
+ const binaryBlob = Converter.base64ToBytes(blobEntry.blob);
557
+ document.extractedData = await this._dataProcessingComponent.extract(options.extractRuleGroupId, binaryBlob, undefined, options?.extractMimeType);
558
+ }
559
+ // If we have the blob data due to extraction but we weren't asked for it
560
+ // then we remove it from the document
561
+ if (!blobRequired) {
562
+ delete document.blobStorageEntry;
563
+ }
564
+ else if (!includeBlobStorageData) {
565
+ delete document.blobStorageEntry?.blob;
566
+ }
567
+ }
568
+ if (includeAttestation && Is.stringValue(document.attestationId)) {
569
+ const attestationInformation = await this._attestationComponent.get(document.attestationId);
570
+ document.attestationInformation = attestationInformation;
571
+ if (!docList["@context"].includes(AttestationContexts.ContextRoot)) {
572
+ docList["@context"].push(AttestationContexts.ContextRoot);
573
+ }
574
+ }
575
+ }
576
+ }
577
+ }
578
+ if (Is.arrayValue(documentVertex.edges)) {
579
+ docList.edges ??= [];
580
+ for (const edge of documentVertex.edges) {
581
+ if (Is.object(edge)) {
582
+ docList.edges.push(edge.targetId);
583
+ }
584
+ }
585
+ }
586
+ return docList;
587
+ }
588
+ /**
589
+ * Create an attestation for the document.
590
+ * @param document The document to create the attestation for.
591
+ * @returns The attestation identifier.
592
+ */
593
+ async createAttestation(document) {
594
+ const documentAttestation = {
595
+ "@context": [
596
+ DocumentContexts.ContextRoot,
597
+ DocumentContexts.ContextRootCommon,
598
+ SchemaOrgContexts.ContextRoot
599
+ ],
600
+ type: DocumentTypes.DocumentAttestation,
601
+ id: document.id,
602
+ documentId: document.documentId,
603
+ documentCode: document.documentCode,
604
+ documentRevision: document.documentRevision,
605
+ dateCreated: document.dateCreated,
606
+ blobHash: document.blobHash
607
+ };
608
+ return this._attestationComponent.create(documentAttestation);
609
+ }
610
+ /**
611
+ * Create a document id from the document id and revision.
612
+ * @param documentId The document id to create.
613
+ * @param revision The revision of the document.
614
+ * @returns The document id.
615
+ * @internal
616
+ */
617
+ createDocumentId(documentId, revision) {
618
+ const documentIdHash = Converter.bytesToBase64Url(Sha256.sum256(Converter.utf8ToBytes(documentId)));
619
+ return `document:${documentIdHash}:${revision}`;
620
+ }
621
+ }
622
+ //# sourceMappingURL=documentManagementService.js.map