Skip to content

Commit 4cd6c0d

Browse files
committed
feat(api): add importUserLastLoggedAt script
1 parent fa28980 commit 4cd6c0d

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 { usecases } from '../domain/usecases/index.js';
7+
8+
export const csvSchemas = [
9+
{ name: 'userId', schema: Joi.number().required() },
10+
{ name: 'last_activity', schema: Joi.date().required() },
11+
];
12+
13+
export class ImportUserLastLogeedAtScript extends Script {
14+
constructor() {
15+
super({
16+
description: 'This script allows to update user last logged at',
17+
permanent: false,
18+
options: {
19+
file: {
20+
type: 'string',
21+
describe: 'CSV file path',
22+
demandOption: true,
23+
coerce: csvFileParser(csvSchemas),
24+
},
25+
dryRun: {
26+
type: 'boolean',
27+
describe: 'Executes the script in dry run mode',
28+
default: false,
29+
},
30+
},
31+
});
32+
}
33+
async handle({ options, logger, importUserLastLoggedAt = usecases.importUserLastLoggedAt }) {
34+
const { file, dryRun } = options;
35+
36+
const total = file.length;
37+
logger.info(`${total} users must be processed for last logged date update.`);
38+
let count = 0;
39+
let updatedCount = 0;
40+
let notUpdatedCount = 0;
41+
42+
for (const row of file) {
43+
const { userId, last_activity } = row;
44+
count += 1;
45+
const result = await importUserLastLoggedAt({ dryRun, userId, lastActivity: last_activity });
46+
if (result) {
47+
logger.info(`${count}/${total} - user updated`);
48+
updatedCount += 1;
49+
} else {
50+
logger.info(`${count}/${total} - user not updated`);
51+
notUpdatedCount += 1;
52+
}
53+
}
54+
55+
logger.info(`${count} users processed.`);
56+
logger.info(`${updatedCount} users with last logged at updated.`);
57+
logger.info(`${notUpdatedCount} users with last logged at NOT updated.`);
58+
}
59+
}
60+
61+
await ScriptRunner.execute(import.meta.url, ImportUserLastLogeedAtScript);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
userId,last_activity
2+
1234,2017-09-05 14:00:08+0000
3+
4567,2018-03-02 15:26:16+0000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
let logger;
33+
34+
beforeEach(function () {
35+
script = new ImportUserLastLogeedAtScript();
36+
importUserLastLoggedAt = sinon.stub();
37+
logger = { info: sinon.stub() };
38+
});
39+
40+
it('runs the script', async function () {
41+
// given
42+
const file = [
43+
{ userId: 1234, last_activity: '2017-09-05 14:00:08+0000' },
44+
{ userId: 4567, last_activity: '2018-03-02 15:26:16+0000' },
45+
];
46+
47+
// when
48+
await script.handle({ options: { file, dryRun: true }, importUserLastLoggedAt, logger });
49+
50+
// then
51+
expect(importUserLastLoggedAt).to.have.been.calledWith({
52+
dryRun: true,
53+
userId: 1234,
54+
lastActivity: '2017-09-05 14:00:08+0000',
55+
});
56+
expect(importUserLastLoggedAt).to.have.been.calledWith({
57+
dryRun: true,
58+
userId: 4567,
59+
lastActivity: '2018-03-02 15:26:16+0000',
60+
});
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)