diff --git a/packages/oslo-converter-stakeholders/lib/StakeholdersConversionService.ts b/packages/oslo-converter-stakeholders/lib/StakeholdersConversionService.ts index 6591eff..6f7f0c2 100644 --- a/packages/oslo-converter-stakeholders/lib/StakeholdersConversionService.ts +++ b/packages/oslo-converter-stakeholders/lib/StakeholdersConversionService.ts @@ -1,6 +1,6 @@ import { writeFile } from 'fs/promises'; import type { IService } from '@oslo-flanders/core'; -import type { StakeholdersDocument } from '@oslo-converter-stakeholders/interfaces/StakeholdersDocument'; +import type { StakeholdersDocument, Stakeholder } from '@oslo-converter-stakeholders/interfaces/StakeholdersDocument'; import { fetchFileOrUrl, Logger, ServiceIdentifier } from '@oslo-flanders/core'; import { parse } from 'csv-parse'; @@ -10,7 +10,7 @@ import { } from '@oslo-converter-stakeholders/config/StakeholdersConversionServiceConfiguration'; import { ContributorType } from '@oslo-converter-stakeholders/enums/ContributorType'; import { context } from '@oslo-converter-stakeholders/utils/JsonLdContext'; -import { ToJsonLdTransformer } from '@oslo-converter-stakeholders/utils/ToJsonLdTransformer'; +import { ToJsonTransformer } from '@oslo-converter-stakeholders/utils/ToJsonTransformer'; @injectable() export class StakeholdersConversionService implements IService { public readonly logger: Logger; @@ -29,7 +29,7 @@ export class StakeholdersConversionService implements IService { } // helper methods for creating the StakeholdersDocument in the different output formats - private createJsonLdDocument(authors: object[], contributors: object[], editors: object[]): StakeholdersDocument { + private createJsonLdDocument(authors: Stakeholder[], contributors: Stakeholder[], editors: Stakeholder[]): StakeholdersDocument { const doc: StakeholdersDocument = {}; doc['@context'] = context; doc.contributors = contributors; @@ -38,7 +38,7 @@ export class StakeholdersConversionService implements IService { return doc; } - private createJsonDocument(authors: object[], contributors: object[], editors: object[]): StakeholdersDocument { + private createJsonDocument(authors: Stakeholder[], contributors: Stakeholder[], editors: Stakeholder[]): StakeholdersDocument { const doc: StakeholdersDocument = {}; doc.contributors = contributors; doc.authors = authors; @@ -46,7 +46,7 @@ export class StakeholdersConversionService implements IService { return doc; } - private createDocument(authors: object[], contributors: object[], editors: object[]): StakeholdersDocument { + private createDocument(authors: Stakeholder[], contributors: Stakeholder[], editors: Stakeholder[]): StakeholdersDocument { switch (this.configuration.outputFormat) { case 'application/json': return this.createJsonDocument(authors, contributors, editors); @@ -57,13 +57,13 @@ export class StakeholdersConversionService implements IService { } } - private async parseData(data: Buffer): Promise<{ authors: object[], contributors: object[], editors: object[] }> { + private async parseData(data: Buffer): Promise<{ authors: Stakeholder[], contributors: Stakeholder[], editors: Stakeholder[] }> { const parser = parse({ delimiter: ';', columns: true }); - const transformer = new ToJsonLdTransformer(); + const transformer = new ToJsonTransformer(this.configuration.outputFormat); - const contributors: object[] = []; - const authors: object[] = []; - const editors: object[] = []; + const contributors: Stakeholder[] = []; + const authors: Stakeholder[] = []; + const editors: Stakeholder[] = []; await new Promise((resolve, reject) => { parser.pipe(transformer) diff --git a/packages/oslo-converter-stakeholders/lib/interfaces/StakeholdersDocument.ts b/packages/oslo-converter-stakeholders/lib/interfaces/StakeholdersDocument.ts index 64295bf..3753609 100644 --- a/packages/oslo-converter-stakeholders/lib/interfaces/StakeholdersDocument.ts +++ b/packages/oslo-converter-stakeholders/lib/interfaces/StakeholdersDocument.ts @@ -1,6 +1,20 @@ +import type { ContributorType } from '@oslo-converter-stakeholders/enums/ContributorType'; + export interface StakeholdersDocument { '@context'?: object; - contributors?: object[]; - authors?: object[]; - editors?: object[]; + contributors?: Stakeholder[]; + authors?: Stakeholder[]; + editors?: Stakeholder[]; +} + +export interface Stakeholder { + '@type'?: string; + firstName: string; + lastName: string; + affiliation: { + affiliationName: string; + homepage?: string; + }; + email: string; + contributorType: ContributorType; } \ No newline at end of file diff --git a/packages/oslo-converter-stakeholders/lib/utils/ToJsonLdTransformer.ts b/packages/oslo-converter-stakeholders/lib/utils/ToJsonTransformer.ts similarity index 82% rename from packages/oslo-converter-stakeholders/lib/utils/ToJsonLdTransformer.ts rename to packages/oslo-converter-stakeholders/lib/utils/ToJsonTransformer.ts index 3bf8143..3b52bec 100644 --- a/packages/oslo-converter-stakeholders/lib/utils/ToJsonLdTransformer.ts +++ b/packages/oslo-converter-stakeholders/lib/utils/ToJsonTransformer.ts @@ -2,13 +2,15 @@ import type { TransformCallback } from 'stream'; import { Transform } from 'stream'; import { ContributorType } from '@oslo-converter-stakeholders/enums/ContributorType'; -export class ToJsonLdTransformer extends Transform { +export class ToJsonTransformer extends Transform { private readonly columnNames = ['Voornaam', 'Naam', 'Affiliatie', 'E-mail', 'Website']; + private readonly outputFormat: string; - public constructor() { + public constructor(outputFormat: string) { super({ objectMode: true, }); + this.outputFormat = outputFormat; } public _transform(chunk: any, encoding: string, callback: TransformCallback): void { @@ -18,7 +20,11 @@ export class ToJsonLdTransformer extends Transform { private createContributor(data: any): any { const contributor: any = {}; - contributor['@type'] = 'Person'; + // If the output format is JSON-LD, we need to add the @type attribute + if (this.outputFormat === 'application/ld+json') { + contributor['@type'] = 'Person'; + } + contributor.firstName = data.Voornaam; contributor.lastName = data.Naam; contributor.affiliation = {};