Skip to content

Commit 11d9688

Browse files
committed
feat(api): add script to save a new legal document version
1 parent 402408d commit 11d9688

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'dotenv/config';
2+
3+
import Joi from 'joi';
4+
5+
import { usecases } from '../../src/legal-documents/domain/usecases/index.js';
6+
import { Script } from '../../src/shared/application/scripts/script.js';
7+
import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js';
8+
9+
export class AddNewLegalDocumentVersion extends Script {
10+
constructor() {
11+
super({
12+
description: 'Add a new legal document version.',
13+
permanent: true,
14+
options: {
15+
type: {
16+
type: 'string',
17+
describe: 'Type of document (ex: "TOS", "PDP")',
18+
demandOption: true,
19+
requiresArg: true,
20+
},
21+
service: {
22+
type: 'string',
23+
describe: 'Associated service (ex: "pix-app", "pix-orga",...)',
24+
demandOption: true,
25+
requiresArg: true,
26+
},
27+
versionAt: {
28+
type: 'string',
29+
describe: 'Version date of the legal document, format "YYYY-MM-DD", (ex: "2020-02-27")',
30+
demandOption: true,
31+
requiresArg: true,
32+
coerce: (value) => {
33+
const schema = Joi.string()
34+
.pattern(/^\d{4}-\d{2}-\d{2}$/)
35+
.message('Invalid date format. Expected "YYYY-MM-DD".');
36+
const { error, value: validatedDate } = schema.validate(value);
37+
if (error) {
38+
throw new Error(error.message);
39+
}
40+
return new Date(validatedDate);
41+
},
42+
},
43+
},
44+
});
45+
}
46+
47+
async handle({ options, logger }) {
48+
let { type, service, versionAt } = options;
49+
50+
type = type.trim();
51+
service = service.trim();
52+
versionAt = versionAt.trim();
53+
54+
logger.info(`Adding new legal document for type:${type}, service:${service}, versionAt:${versionAt}`);
55+
56+
await usecases.createLegalDocument({ type, service, versionAt });
57+
logger.info(`New legal document for type:${type}, service:${service}, versionAt:${versionAt} added successfully.`);
58+
}
59+
}
60+
61+
await ScriptRunner.execute(import.meta.url, AddNewLegalDocumentVersion);

0 commit comments

Comments
 (0)