From 8429388d289c00f101e80771244a8d4ae4cda920 Mon Sep 17 00:00:00 2001 From: Guillaume Lagorce Date: Wed, 15 Jan 2025 17:11:09 +0100 Subject: [PATCH] add script to ease adding information banners --- api/package.json | 2 + api/scripts/information-banners/add.js | 49 +++++++++++++++++++ api/scripts/information-banners/remove.js | 31 ++++++++++++ .../RedisKeyValueStorage.js | 2 +- .../key-value-storages/index.js | 5 ++ .../information-banner-repository_test.js | 2 +- 6 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 api/scripts/information-banners/add.js create mode 100644 api/scripts/information-banners/remove.js diff --git a/api/package.json b/api/package.json index 39b7767bfc7..e26f1932f4d 100644 --- a/api/package.json +++ b/api/package.json @@ -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", diff --git a/api/scripts/information-banners/add.js b/api/scripts/information-banners/add.js new file mode 100644 index 00000000000..bda1309331b --- /dev/null +++ b/api/scripts/information-banners/add.js @@ -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); diff --git a/api/scripts/information-banners/remove.js b/api/scripts/information-banners/remove.js new file mode 100644 index 00000000000..15d0dd4d0f3 --- /dev/null +++ b/api/scripts/information-banners/remove.js @@ -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); diff --git a/api/src/shared/infrastructure/key-value-storages/RedisKeyValueStorage.js b/api/src/shared/infrastructure/key-value-storages/RedisKeyValueStorage.js index 937891ce14b..643a4b4bdb2 100644 --- a/api/src/shared/infrastructure/key-value-storages/RedisKeyValueStorage.js +++ b/api/src/shared/infrastructure/key-value-storages/RedisKeyValueStorage.js @@ -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 }) { diff --git a/api/src/shared/infrastructure/key-value-storages/index.js b/api/src/shared/infrastructure/key-value-storages/index.js index 40e12e9f603..62743fea14c 100644 --- a/api/src/shared/infrastructure/key-value-storages/index.js +++ b/api/src/shared/infrastructure/key-value-storages/index.js @@ -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(); +} diff --git a/api/tests/banner/integration/infrastructure/repositories/information-banner-repository_test.js b/api/tests/banner/integration/infrastructure/repositories/information-banner-repository_test.js index a20f31bf016..68fa14f999d 100644 --- a/api/tests/banner/integration/infrastructure/repositories/information-banner-repository_test.js +++ b/api/tests/banner/integration/infrastructure/repositories/information-banner-repository_test.js @@ -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,