Skip to content

Commit

Permalink
add script to ease adding information banners
Browse files Browse the repository at this point in the history
  • Loading branch information
HEYGUL committed Jan 15, 2025
1 parent c4fd745 commit 8429388
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
2 changes: 2 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
"scripts": {
"clean": "rm -rf node_modules",
"cache:refresh": "node scripts/refresh-cache",
"banner:add": "node scripts/information-banners/add.js",
"banner:remove": "node scripts/information-banners/remove.js",
"datamart:create": "node scripts/datamart/create-datamart",
"datamart:delete": "node scripts/datamart/drop-datamart",
"datamart:new-migration": "npx knex --knexfile datamart/knexfile.js migrate:make --stub $PWD/db/template.js $migrationname",
Expand Down
49 changes: 49 additions & 0 deletions api/scripts/information-banners/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Script } from '../../src/shared/application/scripts/script.js';
import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js';
import {
informationBannersStorage,
quitAllStorages,
} from '../../src/shared/infrastructure/key-value-storages/index.js';

export class AddInformationBanners extends Script {
constructor() {
super({
description: 'Add information banners data to Redis',
permanent: true,
options: {
target: {
type: 'string',
describe: 'application name we want to add information banners to',
required: true,
},
severity: {
type: 'string',
describe: "severity of the message among 'error', 'warning' and 'information'",
required: true,
},
message_fr: {
type: 'string',
describe: 'message content in French',
required: true,
},
message_en: {
type: 'string',
describe: 'message content in English',
required: true,
},
},
});
}

async handle({ options }) {
const { target, severity, message_fr, message_en } = options;
const banners = (await informationBannersStorage.get(target)) ?? [];

banners.push({ severity, message: `[fr]${message_fr}[/fr][en]${message_en}[/en]` });

await informationBannersStorage.save({ key: target, value: banners });
await quitAllStorages();
}
}

await ScriptRunner.execute(import.meta.url, AddInformationBanners);
31 changes: 31 additions & 0 deletions api/scripts/information-banners/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Script } from '../../src/shared/application/scripts/script.js';
import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js';
import {
informationBannersStorage,
quitAllStorages,
} from '../../src/shared/infrastructure/key-value-storages/index.js';

export class RemoveInformationBanners extends Script {
constructor() {
super({
description: 'Remove information banners data from Redis',
permanent: true,
options: {
target: {
type: 'string',
describe: 'application name we want to remove information banners from',
required: true,
},
},
});
}

async handle({ options }) {
const { target } = options;

await informationBannersStorage.delete(target);
await quitAllStorages();
}
}

await ScriptRunner.execute(import.meta.url, RemoveInformationBanners);
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RedisKeyValueStorage extends KeyValueStorage {
}

static createClient(redisUrl, prefix) {
return new RedisClient(redisUrl, { name: 'temporary-storage', prefix });
return new RedisClient(redisUrl, { name: prefix, prefix });
}

async save({ key, value, expirationDelaySeconds }) {
Expand Down
5 changes: 5 additions & 0 deletions api/src/shared/infrastructure/key-value-storages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ function _createKeyValueStorage({ prefix }) {

export const temporaryStorage = _createKeyValueStorage({ prefix: 'temporary-storage:' });
export const informationBannersStorage = _createKeyValueStorage({ prefix: 'information-banners:' });

export async function quitAllStorages() {
await temporaryStorage.quit();
await informationBannersStorage.quit();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Integration | Infrastructure | Repository | Banner | information-banne
const id = 'pix-other-target';
const storedBanner = { message: '[fr]Texte de la bannière[/fr][en]Banner text[/en]', severity: 'info' };

await informationBannersStorage.save({ key: id, value: [storedBanner], expirationDelaySeconds: 10 });
await informationBannersStorage.save({ key: id, value: [storedBanner] });

const expectedInformationBanner = domainBuilder.banner.buildInformationBanner({
id,
Expand Down

0 comments on commit 8429388

Please sign in to comment.