Skip to content

Commit 98a1f03

Browse files
committed
feat(api): add importUserLastLoggedAt script
1 parent cc294e4 commit 98a1f03

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Joi from 'joi';
2+
3+
import { csvFileParser } from '../../shared/application/scripts/parsers.js';
4+
import { Script } from '../../shared/application/scripts/script.js';
5+
import { ScriptRunner } from '../../shared/application/scripts/script-runner.js';
6+
import { DomainTransaction } from '../../shared/domain/DomainTransaction.js';
7+
import { usecases } from '../domain/usecases/index.js';
8+
9+
export const csvSchemas = [
10+
{ name: 'userId', schema: Joi.number().required() },
11+
{ name: 'last_activity', schema: Joi.date().required() },
12+
];
13+
14+
export class ImportUserLAstLogeedAtScript extends Script {
15+
constructor() {
16+
super({
17+
description: 'This script allows to update user last logged at',
18+
permanent: false,
19+
options: {
20+
file: {
21+
type: 'string',
22+
describe: 'CSV file path',
23+
demandOption: true,
24+
coerce: csvFileParser(csvSchemas),
25+
},
26+
},
27+
});
28+
}
29+
async handle({ options, importUserLastLoggedAt = usecases.importUserLastLoggedAt }) {
30+
const { file } = options;
31+
32+
await DomainTransaction.execute(async () => {
33+
for (const row of file) {
34+
const { userId, last_activity } = row;
35+
await importUserLastLoggedAt({
36+
userId,
37+
lastActivity: last_activity,
38+
});
39+
}
40+
});
41+
}
42+
}
43+
44+
await ScriptRunner.execute(import.meta.url, ImportUserLAstLogeedAtScript);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as url from 'node:url';
2+
3+
import sinon from 'sinon';
4+
5+
import { ImportUserLAstLogeedAtScript } from '../../../../src/identity-access-management/scripts/import-user-last-logged-at.script.js';
6+
import { expect } from '../../../test-helper.js';
7+
8+
const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));
9+
10+
describe('ImportUserLAstLogeedAtScript', function () {
11+
describe('Options', function () {
12+
it('parses CSV file correctly', async function () {
13+
// given
14+
const testCsvFile = `${currentDirectory}files/new-update-last-logged-at.csv`;
15+
const script = new ImportUserLAstLogeedAtScript();
16+
17+
// when
18+
const { options } = script.metaInfo;
19+
const fileData = await options.file.coerce(testCsvFile);
20+
21+
// then
22+
expect(fileData).to.deep.equal([
23+
{ userId: 1234, last_activity: new Date('2017-09-05T14:00:08Z') },
24+
{ userId: 4567, last_activity: new Date('2018-03-02T15:26:16Z') },
25+
]);
26+
});
27+
});
28+
29+
describe('#handle', function () {
30+
let script;
31+
let importUserLastLoggedAt;
32+
33+
beforeEach(function () {
34+
script = new ImportUserLAstLogeedAtScript();
35+
importUserLastLoggedAt = sinon.stub();
36+
});
37+
38+
it('runs the script', async function () {
39+
// given
40+
const file = [
41+
{ userId: 1234, last_activity: '2017-09-05 14:00:08+0000' },
42+
{ userId: 4567, last_activity: '2018-03-02 15:26:16+0000' },
43+
];
44+
45+
// when
46+
await script.handle({ options: { file }, importUserLastLoggedAt });
47+
48+
// then
49+
expect(importUserLastLoggedAt).to.have.been.calledWith({
50+
userId: 1234,
51+
lastActivity: '2017-09-05 14:00:08+0000',
52+
});
53+
expect(importUserLastLoggedAt).to.have.been.calledWith({
54+
userId: 4567,
55+
lastActivity: '2018-03-02 15:26:16+0000',
56+
});
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)