From 567e203d1c68559b110d7e3fcf00c0e25f0671b9 Mon Sep 17 00:00:00 2001 From: P-Jeremy Date: Wed, 11 Dec 2024 14:57:41 +0100 Subject: [PATCH] feat(api): add script to save a new legal document version --- .../add-new-legal-document-version.js | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 api/scripts/legal-documents/add-new-legal-document-version.js diff --git a/api/scripts/legal-documents/add-new-legal-document-version.js b/api/scripts/legal-documents/add-new-legal-document-version.js new file mode 100644 index 00000000000..0063c9b7f03 --- /dev/null +++ b/api/scripts/legal-documents/add-new-legal-document-version.js @@ -0,0 +1,61 @@ +import 'dotenv/config'; + +import Joi from 'joi'; + +import { usecases } from '../../src/legal-documents/domain/usecases/index.js'; +import { Script } from '../../src/shared/application/scripts/script.js'; +import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js'; + +export class AddNewLegalDocumentVersion extends Script { + constructor() { + super({ + description: 'Add a new legal document version.', + permanent: true, + options: { + type: { + type: 'string', + describe: 'Type of document (ex: "TOS", "PDP")', + demandOption: true, + requiresArg: true, + }, + service: { + type: 'string', + describe: 'Associated service (ex: "pix-app", "pix-orga",...)', + demandOption: true, + requiresArg: true, + }, + versionAt: { + type: 'string', + describe: 'Version date of the legal document, format "YYYY-MM-DD", (ex: "2020-02-27")', + demandOption: true, + requiresArg: true, + coerce: (value) => { + const schema = Joi.string() + .pattern(/^\d{4}-\d{2}-\d{2}$/) + .message('Invalid date format. Expected "YYYY-MM-DD".'); + const { error, value: validatedDate } = schema.validate(value); + if (error) { + throw new Error(error.message); + } + return new Date(validatedDate); + }, + }, + }, + }); + } + + async handle({ options, logger }) { + let { type, service } = options; + const { versionAt } = options; + + type = type.trim(); + service = service.trim(); + + logger.info(`Adding new legal document for type:${type}, service:${service}, versionAt:${versionAt}`); + + await usecases.createLegalDocument({ type, service, versionAt }); + logger.info(`New legal document for type:${type}, service:${service}, versionAt:${versionAt} added successfully.`); + } +} + +await ScriptRunner.execute(import.meta.url, AddNewLegalDocumentVersion);