@twin.org/document-management-service 0.0.1-next.9 → 0.0.2-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.
@@ -1,12 +1,12 @@
1
1
  'use strict';
2
2
 
3
+ var auditableItemGraphModels = require('@twin.org/auditable-item-graph-models');
3
4
  var core = require('@twin.org/core');
4
5
  var documentManagementModels = require('@twin.org/document-management-models');
5
6
  var standardsSchemaOrg = require('@twin.org/standards-schema-org');
6
7
  var standardsUnece = require('@twin.org/standards-unece');
7
8
  var web = require('@twin.org/web');
8
9
  var attestationModels = require('@twin.org/attestation-models');
9
- var auditableItemGraphModels = require('@twin.org/auditable-item-graph-models');
10
10
  var blobStorageModels = require('@twin.org/blob-storage-models');
11
11
  var crypto = require('@twin.org/crypto');
12
12
  var dataJsonLd = require('@twin.org/data-json-ld');
@@ -147,9 +147,13 @@ function generateRestRoutesDocumentManagement(baseRouteName, componentName) {
147
147
  id: "DocumentManagementGetResponseExample",
148
148
  response: {
149
149
  body: {
150
- "@context": [documentManagementModels.DocumentContexts.ContextRoot, documentManagementModels.DocumentContexts.ContextRootCommon],
151
- type: documentManagementModels.DocumentTypes.DocumentList,
152
- documents: [
150
+ "@context": [
151
+ standardsSchemaOrg.SchemaOrgContexts.ContextRoot,
152
+ documentManagementModels.DocumentContexts.ContextRoot,
153
+ documentManagementModels.DocumentContexts.ContextRootCommon
154
+ ],
155
+ type: standardsSchemaOrg.SchemaOrgTypes.ItemList,
156
+ [standardsSchemaOrg.SchemaOrgTypes.ItemListElement]: [
153
157
  {
154
158
  "@context": [
155
159
  documentManagementModels.DocumentContexts.ContextRoot,
@@ -187,9 +191,13 @@ function generateRestRoutesDocumentManagement(baseRouteName, componentName) {
187
191
  id: "DocumentManagementGetResponseExample",
188
192
  response: {
189
193
  body: {
190
- "@context": [documentManagementModels.DocumentContexts.ContextRoot, documentManagementModels.DocumentContexts.ContextRootCommon],
191
- type: documentManagementModels.DocumentTypes.DocumentList,
192
- documents: [
194
+ "@context": [
195
+ standardsSchemaOrg.SchemaOrgContexts.ContextRoot,
196
+ documentManagementModels.DocumentContexts.ContextRoot,
197
+ documentManagementModels.DocumentContexts.ContextRootCommon
198
+ ],
199
+ type: standardsSchemaOrg.SchemaOrgTypes.ItemList,
200
+ [standardsSchemaOrg.SchemaOrgTypes.ItemListElement]: [
193
201
  {
194
202
  "@context": [
195
203
  documentManagementModels.DocumentContexts.ContextRoot,
@@ -224,7 +232,101 @@ function generateRestRoutesDocumentManagement(baseRouteName, componentName) {
224
232
  }
225
233
  ]
226
234
  };
227
- const documentManagementRemoveRoute = {
235
+ const documentManagementGetRevisionRoute = {
236
+ operationId: "DocumentManagementGetRevision",
237
+ summary: "Get the data for a document revision from document management",
238
+ tag: tagsDocumentManagement[0].name,
239
+ method: "GET",
240
+ path: `${baseRouteName}/:auditableItemGraphDocumentId/:revision`,
241
+ handler: async (httpRequestContext, request) => documentManagementGetRevision(httpRequestContext, componentName, request),
242
+ requestType: {
243
+ type: "IDocumentManagementGetRequest",
244
+ examples: [
245
+ {
246
+ id: "DocumentManagementGetRevisionRequestExample",
247
+ request: {
248
+ pathParams: {
249
+ auditableItemGraphDocumentId: "aig:123456",
250
+ revision: "1"
251
+ }
252
+ }
253
+ }
254
+ ]
255
+ },
256
+ responseType: [
257
+ {
258
+ type: "IDocumentManagementGetRevisionResponse",
259
+ examples: [
260
+ {
261
+ id: "DocumentManagementGetRevisionResponseExample",
262
+ response: {
263
+ body: {
264
+ "@context": [
265
+ documentManagementModels.DocumentContexts.ContextRoot,
266
+ documentManagementModels.DocumentContexts.ContextRootCommon,
267
+ standardsSchemaOrg.SchemaOrgContexts.ContextRoot
268
+ ],
269
+ type: documentManagementModels.DocumentTypes.Document,
270
+ id: "2721000:0",
271
+ documentId: "2721000",
272
+ documentIdFormat: "bol",
273
+ documentCode: standardsUnece.UneceDocumentCodes.BillOfLading,
274
+ documentRevision: 1,
275
+ blobStorageId: "blob-memory:c57d94b088f4c6d2cb32ded014813d0c786aa00134c8ee22f84b1e2545602a70",
276
+ blobHash: "sha256:123456",
277
+ dateCreated: "2024-01-01T00:00:00Z",
278
+ annotationObject: {
279
+ "@context": "https://schema.org",
280
+ "@type": "DigitalDocument",
281
+ name: "myfile.pdf"
282
+ },
283
+ nodeIdentity: "did:entity-storage:0x6363636363636363636363636363636363636363636363636363636363636363",
284
+ userIdentity: "did:entity-storage:0x6363636363636363636363636363636363636363636363636363636363636363"
285
+ }
286
+ }
287
+ }
288
+ ]
289
+ },
290
+ {
291
+ type: "IDocumentManagementGetRevisionResponse",
292
+ mimeType: web.MimeTypes.JsonLd,
293
+ examples: [
294
+ {
295
+ id: "DocumentManagementGetRevisionResponseExample",
296
+ response: {
297
+ body: {
298
+ "@context": [
299
+ documentManagementModels.DocumentContexts.ContextRoot,
300
+ documentManagementModels.DocumentContexts.ContextRootCommon,
301
+ standardsSchemaOrg.SchemaOrgContexts.ContextRoot
302
+ ],
303
+ type: documentManagementModels.DocumentTypes.Document,
304
+ id: "2721000:0",
305
+ documentId: "2721000",
306
+ documentIdFormat: "bol",
307
+ documentCode: standardsUnece.UneceDocumentCodes.BillOfLading,
308
+ documentRevision: 1,
309
+ blobStorageId: "blob-memory:c57d94b088f4c6d2cb32ded014813d0c786aa00134c8ee22f84b1e2545602a70",
310
+ blobHash: "sha256:123456",
311
+ dateCreated: "2024-01-01T00:00:00Z",
312
+ annotationObject: {
313
+ "@context": "https://schema.org",
314
+ "@type": "DigitalDocument",
315
+ name: "myfile.pdf"
316
+ },
317
+ nodeIdentity: "did:entity-storage:0x6363636363636363636363636363636363636363636363636363636363636363",
318
+ userIdentity: "did:entity-storage:0x6363636363636363636363636363636363636363636363636363636363636363"
319
+ }
320
+ }
321
+ }
322
+ ]
323
+ },
324
+ {
325
+ type: "INotFoundResponse"
326
+ }
327
+ ]
328
+ };
329
+ const documentManagementRemoveRevisionRoute = {
228
330
  operationId: "DocumentManagementRemove",
229
331
  summary: "Remove an document from an auditable item graph vertex",
230
332
  tag: tagsDocumentManagement[0].name,
@@ -275,98 +377,76 @@ function generateRestRoutesDocumentManagement(baseRouteName, componentName) {
275
377
  ]
276
378
  },
277
379
  responseType: [
278
- // {
279
- // type: nameof<IDocumentManagementQueryResponse>(),
280
- // examples: [
281
- // {
282
- // id: "DocumentManagementQueryResponseExample",
283
- // response: {
284
- // body: {
285
- // "@context": [DocumentContexts.ContextRoot, DocumentContexts.ContextRootCommon],
286
- // type: DocumentTypes.DocumentList,
287
- // documents: [
288
- // {
289
- // "@context": [
290
- // DocumentContexts.ContextRoot,
291
- // DocumentContexts.ContextRootCommon,
292
- // SchemaOrgContexts.ContextRoot
293
- // ],
294
- // type: DocumentTypes.Document,
295
- // id: "2721000:0",
296
- // documentId: "2721000",
297
- // documentIdFormat: "bol",
298
- // documentCode: UneceDocumentCodes.BillOfLading,
299
- // documentRevision: 0,
300
- // blobStorageId:
301
- // "blob-memory:c57d94b088f4c6d2cb32ded014813d0c786aa00134c8ee22f84b1e2545602a70",
302
- // blobHash: "sha256:123456",
303
- // dateCreated: "2024-01-01T00:00:00Z",
304
- // annotationObject: {
305
- // "@context": "https://schema.org",
306
- // "@type": "DigitalDocument",
307
- // name: "myfile.pdf"
308
- // },
309
- // nodeIdentity:
310
- // "did:entity-storage:0x6363636363636363636363636363636363636363636363636363636363636363",
311
- // userIdentity:
312
- // "did:entity-storage:0x6363636363636363636363636363636363636363636363636363636363636363"
313
- // }
314
- // ]
315
- // }
316
- // }
317
- // }
318
- // ]
319
- // },
320
- // {
321
- // type: nameof<IDocumentManagementQueryResponse>(),
322
- // mimeType: MimeTypes.JsonLd,
323
- // examples: [
324
- // {
325
- // id: "DocumentManagementListResponseJsonLdExample",
326
- // response: {
327
- // body: {
328
- // "@context": [DocumentContexts.ContextRoot, DocumentContexts.ContextRootCommon],
329
- // type: DocumentTypes.DocumentList,
330
- // documents: [
331
- // {
332
- // "@context": [
333
- // DocumentContexts.ContextRoot,
334
- // DocumentContexts.ContextRootCommon,
335
- // SchemaOrgContexts.ContextRoot
336
- // ],
337
- // type: DocumentTypes.Document,
338
- // id: "2721000:0",
339
- // documentId: "2721000",
340
- // documentIdFormat: "bol",
341
- // documentCode: UneceDocumentCodes.BillOfLading,
342
- // documentRevision: 0,
343
- // blobStorageId:
344
- // "blob-memory:c57d94b088f4c6d2cb32ded014813d0c786aa00134c8ee22f84b1e2545602a70",
345
- // blobHash: "sha256:123456",
346
- // dateCreated: "2024-01-01T00:00:00Z",
347
- // annotationObject: {
348
- // "@context": "https://schema.org",
349
- // "@type": "DigitalDocument",
350
- // name: "myfile.pdf"
351
- // },
352
- // nodeIdentity:
353
- // "did:entity-storage:0x6363636363636363636363636363636363636363636363636363636363636363",
354
- // userIdentity:
355
- // "did:entity-storage:0x6363636363636363636363636363636363636363636363636363636363636363"
356
- // }
357
- // ]
358
- // }
359
- // }
360
- // }
361
- // ]
362
- // }
380
+ {
381
+ type: "IDocumentManagementQueryResponse",
382
+ examples: [
383
+ {
384
+ id: "DocumentManagementQueryResponseExample",
385
+ response: {
386
+ body: {
387
+ "@context": [standardsSchemaOrg.SchemaOrgContexts.ContextRoot, auditableItemGraphModels.AuditableItemGraphContexts.ContextRoot],
388
+ type: [standardsSchemaOrg.SchemaOrgTypes.ItemList, auditableItemGraphModels.AuditableItemGraphTypes.VertexList],
389
+ [standardsSchemaOrg.SchemaOrgTypes.ItemListElement]: [
390
+ {
391
+ "@context": [
392
+ auditableItemGraphModels.AuditableItemGraphContexts.ContextRoot,
393
+ auditableItemGraphModels.AuditableItemGraphContexts.ContextRootCommon
394
+ ],
395
+ id: "aig:c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7",
396
+ type: auditableItemGraphModels.AuditableItemGraphTypes.Vertex,
397
+ dateCreated: "2024-08-22T04:13:20.000Z",
398
+ aliases: [
399
+ {
400
+ "@context": [auditableItemGraphModels.AuditableItemGraphContexts.ContextRoot],
401
+ id: "test-id-0",
402
+ type: auditableItemGraphModels.AuditableItemGraphTypes.Alias,
403
+ dateCreated: "2024-08-22T04:13:20.000Z"
404
+ }
405
+ ],
406
+ resources: [
407
+ {
408
+ "@context": auditableItemGraphModels.AuditableItemGraphContexts.ContextRoot,
409
+ type: auditableItemGraphModels.AuditableItemGraphTypes.Resource,
410
+ dateCreated: "2024-08-22T04:13:20.000Z",
411
+ resourceObject: {
412
+ "@context": [
413
+ "https://schema.twindev.org/documents/",
414
+ "https://schema.twindev.org/common/",
415
+ "https://schema.org"
416
+ ],
417
+ type: "Document",
418
+ id: "test-id-0:0",
419
+ documentId: "test-id-0",
420
+ documentCode: "unece:DocumentCodeList#705",
421
+ documentRevision: 0,
422
+ annotationObject: {
423
+ "@context": "https://schema.org",
424
+ type: "DigitalDocument",
425
+ name: "bill-of-lading"
426
+ },
427
+ blobHash: "sha256:E3Duqrp6bHojSx+CzDttAToAiP1eFkCDAPBbKLABVGM=",
428
+ blobStorageId: "blob:memory:1370eeaaba7a6c7a234b1f82cc3b6d013a0088fd5e16408300f05b28b0015463",
429
+ dateCreated: "2024-08-22T04:13:20.000Z",
430
+ nodeIdentity: "did:entity-storage:0x0101010101010101010101010101010101010101010101010101010101010101",
431
+ userIdentity: "did:entity-storage:0x0404040404040404040404040404040404040404040404040404040404040404"
432
+ }
433
+ }
434
+ ]
435
+ }
436
+ ]
437
+ }
438
+ }
439
+ }
440
+ ]
441
+ }
363
442
  ]
364
443
  };
365
444
  return [
366
445
  documentManagementCreateRoute,
367
446
  documentManagementUpdateRoute,
368
447
  documentManagementGetRoute,
369
- documentManagementRemoveRoute,
448
+ documentManagementGetRevisionRoute,
449
+ documentManagementRemoveRevisionRoute,
370
450
  documentManagementQueryRoute
371
451
  ];
372
452
  }
@@ -411,7 +491,9 @@ async function documentManagementGet(httpRequestContext, componentName, request)
411
491
  includeBlobStorageMetadata: core.Coerce.boolean(request.query?.includeBlobStorageMetadata),
412
492
  includeBlobStorageData: core.Coerce.boolean(request.query?.includeBlobStorageData),
413
493
  includeAttestation: core.Coerce.boolean(request.query?.includeAttestation),
414
- includeRemoved: core.Coerce.boolean(request.query?.includeRemoved)
494
+ includeRemoved: core.Coerce.boolean(request.query?.includeRemoved),
495
+ extractRuleGroupId: request.query?.extractRuleGroupId,
496
+ extractMimeType: request.query?.extractMimeType
415
497
  }, request.query?.cursor, core.Coerce.integer(request.query?.pageSize), httpRequestContext.userIdentity, httpRequestContext.nodeIdentity);
416
498
  return {
417
499
  headers: {
@@ -421,7 +503,36 @@ async function documentManagementGet(httpRequestContext, componentName, request)
421
503
  };
422
504
  }
423
505
  /**
424
- * UPdate the document from the auditable item graph vertex.
506
+ * Get the document revision from the auditable item graph vertex.
507
+ * @param httpRequestContext The request context for the API.
508
+ * @param componentName The name of the component to use in the routes.
509
+ * @param request The request.
510
+ * @returns The response object with additional http response properties.
511
+ */
512
+ async function documentManagementGetRevision(httpRequestContext, componentName, request) {
513
+ core.Guards.object(ROUTES_SOURCE, "request", request);
514
+ core.Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
515
+ core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.auditableItemGraphDocumentId", request.pathParams.auditableItemGraphDocumentId);
516
+ const revision = core.Coerce.integer(request.pathParams.revision);
517
+ core.Guards.integer(ROUTES_SOURCE, "revision", revision);
518
+ const mimeType = request.headers?.[web.HeaderTypes.Accept] === web.MimeTypes.JsonLd ? "jsonld" : "json";
519
+ const component = core.ComponentFactory.get(componentName);
520
+ const result = await component.getRevision(request.pathParams.auditableItemGraphDocumentId, revision, {
521
+ includeBlobStorageMetadata: core.Coerce.boolean(request.query?.includeBlobStorageMetadata),
522
+ includeBlobStorageData: core.Coerce.boolean(request.query?.includeBlobStorageData),
523
+ includeAttestation: core.Coerce.boolean(request.query?.includeAttestation),
524
+ extractRuleGroupId: request.query?.extractRuleGroupId,
525
+ extractMimeType: request.query?.extractMimeType
526
+ }, httpRequestContext.userIdentity, httpRequestContext.nodeIdentity);
527
+ return {
528
+ headers: {
529
+ [web.HeaderTypes.ContentType]: mimeType === "json" ? web.MimeTypes.Json : web.MimeTypes.JsonLd
530
+ },
531
+ body: result
532
+ };
533
+ }
534
+ /**
535
+ * Update the document from the auditable item graph vertex.
425
536
  * @param httpRequestContext The request context for the API.
426
537
  * @param componentName The name of the component to use in the routes.
427
538
  * @param request The request.
@@ -482,10 +593,6 @@ async function documentManagementQuery(httpRequestContext, componentName, reques
482
593
  * Service for performing document management operations.
483
594
  */
484
595
  class DocumentManagementService {
485
- /**
486
- * The namespace supported by the document management service.
487
- */
488
- static NAMESPACE = "documents";
489
596
  /**
490
597
  * Runtime name for the class.
491
598
  */
@@ -505,6 +612,11 @@ class DocumentManagementService {
505
612
  * @internal
506
613
  */
507
614
  _attestationComponent;
615
+ /**
616
+ * The connector for the data processing.
617
+ * @internal
618
+ */
619
+ _dataProcessingComponent;
508
620
  /**
509
621
  * Create a new instance of DocumentManagementService.
510
622
  * @param options The options for the service.
@@ -513,6 +625,7 @@ class DocumentManagementService {
513
625
  this._auditableItemGraphComponent = core.ComponentFactory.get(options?.auditableItemGraphComponentType ?? "auditable-item-graph");
514
626
  this._blobStorageComponent = core.ComponentFactory.get(options?.blobStorageComponentType ?? "blob-storage");
515
627
  this._attestationComponent = core.ComponentFactory.get(options?.attestationComponentType ?? "attestation");
628
+ this._dataProcessingComponent = core.ComponentFactory.get(options?.dataProcessingComponentType ?? "data-processing");
516
629
  standardsSchemaOrg.SchemaOrgDataTypes.registerRedirects();
517
630
  }
518
631
  /**
@@ -567,7 +680,7 @@ class DocumentManagementService {
567
680
  standardsSchemaOrg.SchemaOrgContexts.ContextRoot
568
681
  ],
569
682
  type: documentManagementModels.DocumentTypes.Document,
570
- id: `${documentId}:0`,
683
+ id: this.createDocumentId(documentId, 0),
571
684
  documentId,
572
685
  documentIdFormat,
573
686
  documentCode,
@@ -621,12 +734,13 @@ class DocumentManagementService {
621
734
  core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
622
735
  core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
623
736
  try {
624
- const documentVertex = await this._auditableItemGraphComponent.get(auditableItemGraphDocumentId);
737
+ const documentVertex = await this._auditableItemGraphComponent.get(auditableItemGraphDocumentId, { includeDeleted: true });
625
738
  if (core.Is.empty(documentVertex.resources)) {
626
739
  throw new core.NotFoundError(this.CLASS_NAME, "documentRevisionNone");
627
740
  }
628
741
  const documents = await this.getDocumentsFromVertex(documentVertex);
629
- const latestRevision = documents.documents[0];
742
+ const latestRevision = documents.itemListElement[0];
743
+ documentVertex.resources = documentVertex.resources.filter(r => core.Is.empty(r.dateDeleted));
630
744
  if (core.Is.empty(latestRevision)) {
631
745
  throw new core.NotFoundError(this.CLASS_NAME, "documentRevisionNone");
632
746
  }
@@ -657,7 +771,7 @@ class DocumentManagementService {
657
771
  const blobStorageId = await this._blobStorageComponent.create(core.Converter.bytesToBase64(blob), undefined, undefined, undefined, undefined, userIdentity, nodeIdentity);
658
772
  const newRevision = core.ObjectHelper.clone(latestRevision);
659
773
  newRevision.documentRevision++;
660
- newRevision.id = `${newRevision.documentId}:${newRevision.documentRevision}`;
774
+ newRevision.id = this.createDocumentId(newRevision.documentId, newRevision.documentRevision);
661
775
  newRevision.blobHash = newBlobHash;
662
776
  newRevision.blobStorageId = blobStorageId;
663
777
  newRevision.annotationObject = annotationObject;
@@ -708,6 +822,8 @@ class DocumentManagementService {
708
822
  * @param options.includeBlobStorageData Flag to include the blob storage data for the document, defaults to false.
709
823
  * @param options.includeAttestation Flag to include the attestation information for the document, defaults to false.
710
824
  * @param options.includeRemoved Flag to include deleted documents, defaults to false.
825
+ * @param options.extractRuleGroupId If provided will extract data from the document using the specified rule group id.
826
+ * @param options.extractMimeType By default extraction will auto detect the mime type of the document, this can be used to override the detection.
711
827
  * @param cursor The cursor to get the next chunk of revisions.
712
828
  * @param pageSize Page size of items to return, defaults to 1 so only most recent is returned.
713
829
  * @param userIdentity The identity to perform the auditable item graph operation with.
@@ -729,6 +845,43 @@ class DocumentManagementService {
729
845
  throw new core.GeneralError(this.CLASS_NAME, "getFailed", undefined, error);
730
846
  }
731
847
  }
848
+ /**
849
+ * Get a document revision using it's auditable item graph vertex id.
850
+ * @param auditableItemGraphDocumentId The auditable item graph vertex id which contains the document.
851
+ * @param revision The revision id for the document.
852
+ * @param options Additional options for the get operation.
853
+ * @param options.includeBlobStorageMetadata Flag to include the blob storage metadata for the document, defaults to false.
854
+ * @param options.includeBlobStorageData Flag to include the blob storage data for the document, defaults to false.
855
+ * @param options.includeAttestation Flag to include the attestation information for the document, defaults to false.
856
+ * @param options.extractRuleGroupId If provided will extract data from the document using the specified rule group id.
857
+ * @param options.extractMimeType By default extraction will auto detect the mime type of the document, this can be used to override the detection.
858
+ * @param userIdentity The identity to perform the auditable item graph operation with.
859
+ * @param nodeIdentity The node identity to use for vault operations.
860
+ * @returns The documents and revisions if requested, ordered by revision descending, cursor is set if there are more document revisions.
861
+ */
862
+ async getRevision(auditableItemGraphDocumentId, revision, options, userIdentity, nodeIdentity) {
863
+ core.Urn.guard(this.CLASS_NAME, "auditableItemGraphDocumentId", auditableItemGraphDocumentId);
864
+ core.Guards.integer(this.CLASS_NAME, "revision", revision);
865
+ try {
866
+ const documentVertex = await this._auditableItemGraphComponent.get(auditableItemGraphDocumentId, { includeDeleted: true });
867
+ if (core.Is.empty(documentVertex.resources)) {
868
+ throw new core.NotFoundError(this.CLASS_NAME, "documentRevisionNone");
869
+ }
870
+ documentVertex.resources = documentVertex.resources.filter(d => d.resourceObject?.documentRevision === revision);
871
+ if (documentVertex.resources.length === 0) {
872
+ throw new core.NotFoundError(this.CLASS_NAME, "documentRevisionNotFound", revision.toString());
873
+ }
874
+ // Populate the document and revisions with the options set
875
+ const docList = await this.getDocumentsFromVertex(documentVertex, options, undefined, undefined, userIdentity, nodeIdentity);
876
+ return dataJsonLd.JsonLdProcessor.compact(docList.itemListElement[0], docList.itemListElement[0]["@context"]);
877
+ }
878
+ catch (error) {
879
+ if (core.BaseError.someErrorName(error, "NotFoundError")) {
880
+ throw error;
881
+ }
882
+ throw new core.GeneralError(this.CLASS_NAME, "getRevisionFailed", undefined, error);
883
+ }
884
+ }
732
885
  /**
733
886
  * Remove an auditable item graph vertex using it's id.
734
887
  * The document dateDeleted will be set, but can still be queried with the includeRemoved flag.
@@ -941,7 +1094,8 @@ class DocumentManagementService {
941
1094
  * @param options.includeBlobStorageMetadata Flag to include the blob storage metadata for the document, defaults to false.
942
1095
  * @param options.includeBlobStorageData Flag to include the blob storage data for the document, defaults to false.
943
1096
  * @param options.includeAttestation Flag to include the attestation information for the document, defaults to false.
944
- * @param options.includeRemoved Flag to include deleted documents, defaults to false.
1097
+ * @param options.extractRuleGroupId If provided will extract data from the document using the specified rule group id.
1098
+ * @param options.extractMimeType By default extraction will auto detect the mime type of the document, this can be used to override the detection.
945
1099
  * @param cursor The cursor to get the next chunk of revisions.
946
1100
  * @param pageSize Page size of items to return, defaults to 1 so only most recent is returned.
947
1101
  * @param userIdentity The identity to perform the auditable item graph operation with.
@@ -952,12 +1106,12 @@ class DocumentManagementService {
952
1106
  async getDocumentsFromVertex(documentVertex, options, cursor, pageSize, userIdentity, nodeIdentity) {
953
1107
  const docList = {
954
1108
  "@context": [
1109
+ standardsSchemaOrg.SchemaOrgContexts.ContextRoot,
955
1110
  documentManagementModels.DocumentContexts.ContextRoot,
956
- documentManagementModels.DocumentContexts.ContextRootCommon,
957
- standardsSchemaOrg.SchemaOrgContexts.ContextRoot
1111
+ documentManagementModels.DocumentContexts.ContextRootCommon
958
1112
  ],
959
- type: documentManagementModels.DocumentTypes.DocumentList,
960
- documents: []
1113
+ type: standardsSchemaOrg.SchemaOrgTypes.ItemList,
1114
+ [standardsSchemaOrg.SchemaOrgTypes.ItemListElement]: []
961
1115
  };
962
1116
  if (core.Is.arrayValue(documentVertex.resources)) {
963
1117
  // Sort by newest revision first
@@ -966,20 +1120,39 @@ class DocumentManagementService {
966
1120
  const startIndex = core.Coerce.integer(cursor) ?? 0;
967
1121
  const endIndex = Math.min(startIndex + (pageSize ?? 1), documentVertex.resources.length);
968
1122
  const slicedResources = documentVertex.resources.slice(startIndex, endIndex);
969
- docList.cursor =
1123
+ docList[standardsSchemaOrg.SchemaOrgTypes.NextItem] =
970
1124
  documentVertex.resources.length > endIndex ? (endIndex + 1).toString() : undefined;
971
1125
  const includeBlobStorageMetadata = options?.includeBlobStorageMetadata ?? false;
972
1126
  const includeBlobStorageData = options?.includeBlobStorageData ?? false;
973
1127
  const includeAttestation = options?.includeAttestation ?? false;
1128
+ const extractData = core.Is.stringValue(options?.extractRuleGroupId);
974
1129
  for (let i = 0; i < slicedResources.length; i++) {
975
1130
  const document = slicedResources[i].resourceObject;
976
1131
  if (core.Is.object(document)) {
977
- docList.documents.push(document);
978
- if (includeBlobStorageMetadata || includeBlobStorageData) {
979
- const blobEntry = await this._blobStorageComponent.get(document.blobStorageId, includeBlobStorageData, userIdentity, nodeIdentity);
980
- document.blobStorageEntry = blobEntry;
981
- if (!docList["@context"].includes(blobStorageModels.BlobStorageContexts.ContextRoot)) {
982
- docList["@context"].push(blobStorageModels.BlobStorageContexts.ContextRoot);
1132
+ document.dateDeleted = slicedResources[i].dateDeleted;
1133
+ docList[standardsSchemaOrg.SchemaOrgTypes.ItemListElement].push(document);
1134
+ const blobRequired = includeBlobStorageMetadata || includeBlobStorageData;
1135
+ if (blobRequired || extractData) {
1136
+ const blobEntry = await this._blobStorageComponent.get(document.blobStorageId, {
1137
+ includeContent: includeBlobStorageData || extractData
1138
+ }, userIdentity, nodeIdentity);
1139
+ if (blobRequired) {
1140
+ document.blobStorageEntry = blobEntry;
1141
+ if (!docList["@context"].includes(blobStorageModels.BlobStorageContexts.ContextRoot)) {
1142
+ docList["@context"].push(blobStorageModels.BlobStorageContexts.ContextRoot);
1143
+ }
1144
+ }
1145
+ if (core.Is.stringValue(options?.extractRuleGroupId) && core.Is.stringValue(blobEntry.blob)) {
1146
+ const binaryBlob = core.Converter.base64ToBytes(blobEntry.blob);
1147
+ document.extractedData = await this._dataProcessingComponent.extract(options.extractRuleGroupId, binaryBlob, undefined, options?.extractMimeType);
1148
+ }
1149
+ // If we have the blob data due to extraction but we weren't asked for it
1150
+ // then we remove it from the document
1151
+ if (!blobRequired) {
1152
+ delete document.blobStorageEntry;
1153
+ }
1154
+ else if (!includeBlobStorageData) {
1155
+ delete document.blobStorageEntry?.blob;
983
1156
  }
984
1157
  }
985
1158
  if (includeAttestation && core.Is.stringValue(document.attestationId)) {
@@ -1026,12 +1199,23 @@ class DocumentManagementService {
1026
1199
  };
1027
1200
  return this._attestationComponent.create(documentAttestation, undefined, userIdentity, nodeIdentity);
1028
1201
  }
1202
+ /**
1203
+ * Create a document id from the document id and revision.
1204
+ * @param documentId The document id to create.
1205
+ * @param revision The revision of the document.
1206
+ * @returns The document id.
1207
+ * @internal
1208
+ */
1209
+ createDocumentId(documentId, revision) {
1210
+ const documentIdHash = core.Converter.bytesToBase64Url(crypto.Sha256.sum256(core.Converter.utf8ToBytes(documentId)));
1211
+ return `document:${documentIdHash}:${revision}`;
1212
+ }
1029
1213
  }
1030
1214
 
1031
1215
  const restEntryPoints = [
1032
1216
  {
1033
- name: "documents",
1034
- defaultBaseRoute: "documents",
1217
+ name: "document-management",
1218
+ defaultBaseRoute: "document-management",
1035
1219
  tags: tagsDocumentManagement,
1036
1220
  generateRoutes: generateRestRoutesDocumentManagement
1037
1221
  }
@@ -1040,6 +1224,7 @@ const restEntryPoints = [
1040
1224
  exports.DocumentManagementService = DocumentManagementService;
1041
1225
  exports.documentManagementCreate = documentManagementCreate;
1042
1226
  exports.documentManagementGet = documentManagementGet;
1227
+ exports.documentManagementGetRevision = documentManagementGetRevision;
1043
1228
  exports.documentManagementQuery = documentManagementQuery;
1044
1229
  exports.documentManagementRemove = documentManagementRemove;
1045
1230
  exports.documentManagementUpdate = documentManagementUpdate;