Skip to content

Commit

Permalink
feat(permanentid): generate permanent id when missing (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe authored May 7, 2021
1 parent f7a2002 commit 3d3426a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/documentBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dayjs = require('dayjs');
import {createHash} from 'crypto';
import {CompressionType, Document, Metadata, MetadataValue} from './document';
import {SecurityIdentityBuilder} from './securityIdentityBuilder';

Expand Down Expand Up @@ -227,12 +228,18 @@ export class DocumentBuilder {

private validateAndFillMissing() {
// TODO: validation that cannot be performed on a single property, but requires looking at multiple property at the same time.
// For example, if there's no permanentID set, we want to generate one using the document URI.
// or validate that we don't have both `data` AND `compressedBinaryData`.
// Could also use https://www.npmjs.com/package/@coveo/bueno to validate schema (useful for pure JS users).
if (!this.doc.permanentId) {
this.doc.permanentId = this.generatePermanentId();
}
return;
}

private generatePermanentId() {
return createHash('sha256').update(this.doc.uri).digest('hex');
}

private validateDateAndReturnValidDate(d: Date | string | number) {
const validatedDate = dayjs(d);
return validatedDate.toISOString();
Expand All @@ -243,7 +250,10 @@ export class DocumentBuilder {
permissionSection: 'allowedPermissions' | 'deniedPermissions'
) {
const identities = securityIdentityBuilder.build();
this.doc.permissions![permissionSection] = [].concat(identities);
if (Array.isArray(identities)) {
this.doc.permissions![permissionSection] = identities;
} else {
this.doc.permissions![permissionSection] = [identities];
}
}
}

0 comments on commit 3d3426a

Please sign in to comment.