From 9735bdc7b80fa94ee869708abd2285736c8dbfc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rard=20Dethier?= Date: Mon, 5 Sep 2022 11:36:24 +0200 Subject: [PATCH] feat: remove useless CSV resource. logion-network/logion-internal#561 --- package.json | 1 - .../controllers/locrequest.controller.ts | 91 ------------------- src/logion/lib/CsvCreator.ts | 52 ----------- .../controllers/locrequest.controller.spec.ts | 17 ---- yarn.lock | 8 -- 5 files changed, 169 deletions(-) delete mode 100644 src/logion/lib/CsvCreator.ts diff --git a/package.json b/package.json index 0477d0ef..2a242103 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "body-parser": "^1.19.1", "bson": "^4.6.5", "cors": "^2.8.5", - "csv-writer": "^1.6.0", "dinoloop": "^2.4.0", "express": "^4.17.2", "express-fileupload": "^1.2.1", diff --git a/src/logion/controllers/locrequest.controller.ts b/src/logion/controllers/locrequest.controller.ts index aefb3b93..3e3e5fef 100644 --- a/src/logion/controllers/locrequest.controller.ts +++ b/src/logion/controllers/locrequest.controller.ts @@ -37,7 +37,6 @@ import { UUID } from "@logion/node-api/dist/UUID"; import { badRequest } from "./errors"; import { CollectionRepository } from "../model/collection.model"; import { getUploadedFile } from "./fileupload"; -import { CsvCreator } from "../lib/CsvCreator"; const { logger } = Log; @@ -52,7 +51,6 @@ export function fillInSpec(spec: OpenAPIV3.Document): void { LocRequestController.createLocRequest(spec); LocRequestController.fetchRequests(spec); LocRequestController.getLocRequest(spec); - LocRequestController.getCsvLocRequest(spec); LocRequestController.getPublicLoc(spec); LocRequestController.rejectLocRequest(spec); LocRequestController.acceptLocRequest(spec); @@ -305,95 +303,6 @@ export class LocRequestController extends ApiController { } } - static getCsvLocRequest(spec: OpenAPIV3.Document) { - const operationObject = spec.paths["/api/loc-request/{requestId}"].get!; - operationObject.summary = "Gets a single LOC Request CSV file"; - operationObject.description = "The authenticated user must be either expected requester or expected owner."; - operationObject.responses = { - "200": { - description: "OK", - content: { - "text/csv": { - example: "loc.id,loc.ownerAddress\n3e67427a-d80f-41d7-9c86-75a63b8563a1,5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" - } - } - } - }; - setPathParameters(operationObject, { 'requestId': "The ID of the LOC request" }) - } - - @HttpGet('/:requestId/csv') - @Async() - @SendsResponse() - async getCsvLocRequest(_body: any, requestId: string): Promise { - - type Prefix = "loc" | "userIdentity" | "voidInfo" | "file" | "link" | "metadata"; - - const csvCreator = new CsvCreator({ - "loc": [ - "id", - "ownerAddress", - "requesterAddress", - "requesterIdentityLoc", - "description", - "createdOn", - "decisionOn", - "closedOn", - "status", - "rejectReason", - "locType", - ], - "userIdentity": [ - "firstName", - "lastName", - "email", - "phoneNumber" - ], - "voidInfo": [ - "reason", - "voidedOn" - ], - "file": [ - "name", - "hash", - "nature", - "addedOn", - "submitter" - ], - "link": [ - "target", - "addedOn", - "nature" - ], - "metadata": [ - "name", - "value", - "addedOn", - "submitter" - ], - }); - const request = requireDefined(await this.locRequestRepository.findById(requestId)); - await this.authenticationService.authenticatedUserIsOneOf(this.request, - request.requesterAddress, request.ownerAddress); - const ui = await this.findUserIdentity(request); - const locRequestView = this.toView(request, ui); - this.response.type('text/csv') - const csv = - csvCreator.getHeaderString() + - csvCreator.stringifyRecords( - { - "loc": locRequestView, - "userIdentity": locRequestView.userIdentity, - "voidInfo": locRequestView.voidInfo - }, - { - "file": locRequestView.files, - "link": locRequestView.links, - "metadata": locRequestView.metadata - }) - this.response.send(csv) - } - static getPublicLoc(spec: OpenAPIV3.Document) { const operationObject = spec.paths["/api/loc-request/{requestId}/public"].get!; operationObject.summary = "Gets the published attributes of a single LOC"; diff --git a/src/logion/lib/CsvCreator.ts b/src/logion/lib/CsvCreator.ts deleted file mode 100644 index 48641c6d..00000000 --- a/src/logion/lib/CsvCreator.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { ObjectCsvStringifier } from "csv-writer/src/lib/csv-stringifiers/object"; -import { ObjectHeaderItem, Field } from "csv-writer/src/lib/record"; -import { createObjectCsvStringifier } from "csv-writer"; -import { ObjectMap } from "csv-writer/src/lib/lang/object"; - -export class CsvCreator

{ - - constructor(attributes: Record) { - let header: ObjectHeaderItem[] = []; - for (const prefix in attributes) { - const items = attributes[prefix].map(attribute => ({ - id: `${ prefix }.${ attribute }`, - title: `${ prefix }.${ attribute }` - })); - header = header.concat(items); - } - this.stringifier = createObjectCsvStringifier({ header }) - } - - private stringifier: ObjectCsvStringifier; - - getHeaderString(): string | null { - return this.stringifier.getHeaderString(); - } - - stringifyRecords(parent: Partial | undefined>>, children: Partial[] | undefined>>): string { - let mappedParent = {}; - for (const prefix in parent) { - mappedParent = { - ...mappedParent, - ...this.map(prefix, parent[prefix]) - } - } - let records: ObjectMap[] = [ mappedParent ]; - for (const prefix in children) { - if (children[prefix] !== undefined) { - const items = children[prefix]!.map(item => this.map(prefix, item)); - records = records.concat(items); - } - } - return this.stringifier.stringifyRecords(records) - } - - private map(prefix: P, item?: ObjectMap): ObjectMap | undefined { - const result: ObjectMap = {}; - for (const attribute in item) { - const id = `${ prefix }.${ attribute }`; - result[id] = item[attribute]; - } - return result; - } -} diff --git a/test/unit/controllers/locrequest.controller.spec.ts b/test/unit/controllers/locrequest.controller.spec.ts index 2ab02f50..d946b0ed 100644 --- a/test/unit/controllers/locrequest.controller.spec.ts +++ b/test/unit/controllers/locrequest.controller.spec.ts @@ -450,23 +450,6 @@ describe('LocRequestController', () => { }); }); - it('succeeds to get CSV-formatted loc request', async () => { - const app = setupApp(LocRequestController, mockModelForGetSingle) - await request(app) - .get(`/api/loc-request/${REQUEST_ID}/csv`) - .expect(200) - .expect('Content-Type', /text\/csv/) - .then(response => { - console.log(response.text) - const csv = response.text.split("\n") - expect(csv[0]).toEqual("loc.id,loc.ownerAddress,loc.requesterAddress,loc.requesterIdentityLoc,loc.description,loc.createdOn,loc.decisionOn,loc.closedOn,loc.status,loc.rejectReason,loc.locType,userIdentity.firstName,userIdentity.lastName,userIdentity.email,userIdentity.phoneNumber,voidInfo.reason,voidInfo.voidedOn,file.name,file.hash,file.nature,file.addedOn,file.submitter,link.target,link.addedOn,link.nature,metadata.name,metadata.value,metadata.addedOn,metadata.submitter") - expect(csv[1]).toEqual("3e67427a-d80f-41d7-9c86-75a63b8563a1,5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY,5CXLTF2PFBE89tTYsrofGPkSfGTdmW4ciw4vAfgcKhjggRgZ,,I want to open a case,2022-08-31T16:01:15.651Z,,,OPEN,,Transaction,,,,,,,,,,,,,,,,,,"); - expect(csv[2]).toEqual(",,,,,,,,,,,,,,,,,test-file,0x9383cd5dfeb5870027088289c665c3bae2d339281840473f35311954e984dea9,file-nature,2022-08-31T15:53:12.741Z,5DDGQertEH5qvKVXUmpT3KNGViCX582Qa2WWb8nGbkmkRHvw,,,,,,,"); - expect(csv[3]).toEqual(",,,,,,,,,,,,,,,,,,,,,,507a00a1-7387-44b8-ac4d-fa57ccbf6da5,,link-nature,,,,"); - expect(csv[4]).toEqual(",,,,,,,,,,,,,,,,,,,,,,,,,test-data,test-data-value,,5DDGQertEH5qvKVXUmpT3KNGViCX582Qa2WWb8nGbkmkRHvw"); - }); - }); - it('deletes a file', async () => { const app = setupApp(LocRequestController, mockModelForDeleteFile) await request(app) diff --git a/yarn.lock b/yarn.lock index 1d94165e..88116179 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2377,13 +2377,6 @@ __metadata: languageName: node linkType: hard -"csv-writer@npm:^1.6.0": - version: 1.6.0 - resolution: "csv-writer@npm:1.6.0" - checksum: 2e62cb46f00b674f0710eb90586000601f3a467aabe529464dcb402d453a1322a716d7522ef3282dd6551f1059305c7dd3db49def1201caaf340597dcf7b4c7e - languageName: node - linkType: hard - "d@npm:1, d@npm:^1.0.1": version: 1.0.1 resolution: "d@npm:1.0.1" @@ -4151,7 +4144,6 @@ __metadata: body-parser: ^1.19.1 bson: ^4.6.5 cors: ^2.8.5 - csv-writer: ^1.6.0 dinoloop: ^2.4.0 dotenv: ^16.0.1 express: ^4.17.2