From 68d6e1cd2c8fed50bcd199f2c5a82b8aa72f64f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Mon, 19 Jun 2023 11:24:55 +0200 Subject: [PATCH] Fix api units --- api/source/index.ts | 5 +++-- api/source/units.ts | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 api/source/units.ts diff --git a/api/source/index.ts b/api/source/index.ts index 50a9bc390..1a84d45a4 100644 --- a/api/source/index.ts +++ b/api/source/index.ts @@ -13,6 +13,7 @@ import { redisCacheMiddleware } from './redis-cache.js' import { docRoutes } from './route/doc.js' import { openapiRoutes } from './route/openapi.js' import Sentry, { requestHandler, tracingMiddleWare } from './sentry.js' +import { getUnitKey } from './units.js' type State = Koa.DefaultState type Context = Koa.DefaultContext @@ -42,7 +43,7 @@ app.use(cors()) router.use('/api/v1', docRoutes(), openapiRoutes(openapi)) -const apiRoutes = publicodesAPI(new Engine(rules)) +const apiRoutes = publicodesAPI(new Engine(rules, { getUnitKey })) router.use( '/api/v1', @@ -65,4 +66,4 @@ const server = app.listen(port, function () { console.log('listening on port:', port) }) -export { server, app } +export { app, server } diff --git a/api/source/units.ts b/api/source/units.ts new file mode 100644 index 000000000..e99c75380 --- /dev/null +++ b/api/source/units.ts @@ -0,0 +1,25 @@ +// units from site/source/locales/units.yaml: +const units = Object.entries({ + heure_plural: 'heures', + jour_plural: 'jours', + 'jour ouvré_plural': 'jours ouvrés', + semaine_plural: 'semaine', + trimestre_plural: 'trimestres', + 'trimestre validé_plural': 'trimestres validés', + an_plural: 'ans', + employé_plural: 'employés', + point_plural: 'points', + mois_plural: 'mois', + manifestation_plural: 'manifestations', + 'titre-restaurant_plural': 'titres-restaurant', + part_plural: 'parts', + enfant_plural: 'enfants', +}) + +export const getUnitKey = (unit: string): string => { + const key = units + .find(([, trans]) => trans === unit)?.[0] + .replace(/_plural$/, '') + + return key || unit +}