From 087ed39e574ade757711d972f0f3da8e3b39d4cf Mon Sep 17 00:00:00 2001 From: BatLeDev Date: Thu, 12 Nov 2026 17:40:03 +0100 Subject: [PATCH 1/2] docs(skills): one standard to send events and notifications from a service - references/events.md: the shared queue started unconditionally, topic keys as a contract never renamed, one emitter per resource with its { en, fr } texts next to it - i18n.md no longer recommends a central text table with a parity unit test - data-fair and simple-directory keep their locale files --- skills/data-fair-service/SKILL.md | 4 +- skills/data-fair-service/references/events.md | 76 +++++++++++++++++++ skills/data-fair-service/references/i18n.md | 6 +- 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 skills/data-fair-service/references/events.md diff --git a/skills/data-fair-service/SKILL.md b/skills/data-fair-service/SKILL.md index b438588..c67f145 100644 --- a/skills/data-fair-service/SKILL.md +++ b/skills/data-fair-service/SKILL.md @@ -1,6 +1,6 @@ --- name: data-fair-service -description: Use when working on the dev environment or the shared architecture of a data-fair service (data-fair, portals, catalogs, processings, events, simple-directory, registry, agents…) - migrating a repo to @data-fair/lib-dev / df-dev, editing .zellij.kdl, dev/dev.json, docker-compose dev services or profiles, init-env, worktree scripts, status or log panes, an agent needing a service's logs, status or a restart, a crash at dev startup, build-types restarting the API, RAM of dev stacks, aligning a service's tooling (eslint, commitlint, husky, tsconfig, .nvmrc, Dockerfile, CI workflows), its dev docker-compose (mongo / ES versions, service blocks) or dev nginx config, a convention shared by services (ping route, BUILD.json, admin routes, /v1 prefix, vite.config, generated .d.ts, /dev pages), or the fr/en internationalisation of a service (vue-i18n on the session, `` blocks, document `lang`, localized dates and numbers, `{ fr, en }` notifications to events). +description: Use when working on the dev environment or the shared architecture of a data-fair service (data-fair, portals, catalogs, processings, events, simple-directory, registry, agents…) - migrating a repo to @data-fair/lib-dev / df-dev, editing .zellij.kdl, dev/dev.json, docker-compose dev services or profiles, init-env, worktree scripts, status or log panes, an agent needing a service's logs, status or a restart, a crash at dev startup, build-types restarting the API, RAM of dev stacks, aligning a service's tooling (eslint, commitlint, husky, tsconfig, .nvmrc, Dockerfile, CI workflows), its dev docker-compose (mongo / ES versions, service blocks) or dev nginx config, a convention shared by services (ping route, BUILD.json, admin routes, /v1 prefix, vite.config, generated .d.ts, /dev pages), or the fr/en internationalisation of a service (vue-i18n on the session, `` blocks, document `lang`, localized dates and numbers, `{ en, fr }` notifications to events), or sending events and notifications to the events service (eventsQueue, pushEvent, topic keys, notification texts). --- # data-fair service: dev environment and shared conventions @@ -9,7 +9,7 @@ description: Use when working on the dev environment or the shared architecture Every service runs the same dev environment: a zellij (>= 0.44) session per checkout, readable by humans, operated by agents only through `df-dev` (package `@data-fair/lib-dev`). The repo declares what is specific to it in `dev/dev.json` and `.zellij.kdl`; everything else (ports, setup, status, logs, restart, worktrees) comes from the package. **RAM is the scarce resource**: nothing in a pane may hold memory it does not need. -Package reference: `packages/dev/README.md` in data-fair/lib. Full migration recipe with templates: [references/migrate-a-service.md](references/migrate-a-service.md). Architecture conventions, settled and open: [references/architecture-conventions.md](references/architecture-conventions.md). Tooling (eslint, commitlint, husky, tsconfig, CI, Docker, .gitignore): [references/tooling.md](references/tooling.md). Dev docker-compose (service blocks, mongo/ES versions, shared dev secrets, maildev): [references/docker-compose.md](references/docker-compose.md), the only place where those versions change. Dev nginx template (shared head, the 3 location shapes, per-repo deviations): [references/nginx.md](references/nginx.md). Internationalisation fr + en of a service (UI on the session language, document `lang`, dates, numbers, notifications): [references/i18n.md](references/i18n.md). +Package reference: `packages/dev/README.md` in data-fair/lib. Full migration recipe with templates: [references/migrate-a-service.md](references/migrate-a-service.md). Architecture conventions, settled and open: [references/architecture-conventions.md](references/architecture-conventions.md). Tooling (eslint, commitlint, husky, tsconfig, CI, Docker, .gitignore): [references/tooling.md](references/tooling.md). Dev docker-compose (service blocks, mongo/ES versions, shared dev secrets, maildev): [references/docker-compose.md](references/docker-compose.md), the only place where those versions change. Dev nginx template (shared head, the 3 location shapes, per-repo deviations): [references/nginx.md](references/nginx.md). Internationalisation fr + en of a service (UI on the session language, document `lang`, dates, numbers, notifications): [references/i18n.md](references/i18n.md). Sending events and notifications (the queue, topic keys, texts): [references/events.md](references/events.md). When aligning a service: one branch per service, a handful of commits: `chore(dev)` dev environment, `chore` shared tooling and conventions (lint, commitlint, tsconfig, gitignore, node, CI order, vite template), `build(docker)` images, then one `fix` per bug that could be cherry-picked alone; list the details in each commit body (lines ≤ 100 characters). diff --git a/skills/data-fair-service/references/events.md b/skills/data-fair-service/references/events.md new file mode 100644 index 0000000..cef9cb1 --- /dev/null +++ b/skills/data-fair-service/references/events.md @@ -0,0 +1,76 @@ +# Sending events and notifications to the events service + +Every service that tells users what happened (data-fair, portals, catalogs, processings, simple-directory, +customers, agents…) sends it the same way. Model to copy: `catalogs/api/src/imports/service.ts`. + +## The queue + +- `import eventsQueue from '@data-fair/lib-node/events-queue.js'`, the shared singleton. +- Started once at server (and worker) startup, unconditionally: + `await eventsQueue.start({ eventsUrl: config.privateEventsUrl, eventsSecret: config.secretKeys.events })`. + Without the url the queue is inactive and `pushEvent` does nothing, so call sites never check the config and + never use an optional queue (`eventsQueue?.pushEvent`). +- `await eventsQueue.stop()` on shutdown, so the last events are sent. +- `pushEvent(event, sessionState)`: pass the session when a user triggered it, the queue fills `originator`. + A process with no user sets `originator: { internalProcess: { id, title } }` itself. + +## Topic keys are a contract + +A subscription or a webhook stores a topic key prefix, and events matches every prefix of an event's key +(`a:b:c` matches subscriptions on `a`, `a:b` and `a:b:c`). Users' subscriptions are in the events database, +so **an existing key is never renamed**, even when it does not follow the shape below (portals sends +`pages:page-create:`): renaming needs an upgrade script on the events database and on every UI that +subscribes. + +New keys: `:-[:]`, the resource and action in kebab-case, the id last +so that a subscription can target one resource or all of them. + +## One emitter per resource, texts next to it + +Each resource that sends events has one function, in the module that owns the resource, that builds every +event of that resource. Its texts are written right there, English first, both languages always: + +```ts +const importTitles = { + create: (title: string) => ({ en: `The import of the resource ${title} was created`, fr: `L'import de la ressource ${title} a été créé` }), + delete: (title: string) => ({ en: `The import of the resource ${title} was deleted`, fr: `L'import de la ressource ${title} a été supprimé` }) +} + +export const sendImportEvent = (imp: Import, action: keyof typeof importTitles, sessionState?: SessionStateAuthenticated) => { + eventsQueue.pushEvent({ + title: importTitles[action](imp.remoteResource.title || imp.remoteResource.id), + topic: { key: `catalogs:import-${action}:${imp._id}` }, + sender: imp.owner, + resource: { type: 'import', id: imp._id, title: imp.remoteResource.title } + }, sessionState) +} +``` + +- No key table, no `{title}` interpolation helper, no unit test comparing keys, and no type of our own for the + `{ en, fr }` objects: a parameter that receives a title or a body is typed `PushEvent['title']` / + `PushEvent['body']` (from `@data-fair/lib-node/events-queue.js`). +- No text table shared between modules or packages (`shared/event-texts.ts`): when the api and the worker send + the same event, the emitter lives in the package that sends it, or in `shared/` as a whole emitter, never as + a list of texts. +- `title` is short and names the resource; `body` gives the details (`{ en, fr }` too). A text that comes from + elsewhere (a remote error message, a user comment) goes in `body` as is. +- `resource.title` is the resource's own title, never a sentence ("Associated processing: …"): it is not + translated. +- `visibility: 'private'` unless the event is meant for the public subscribers of a public resource. + +## Exceptions + +data-fair and simple-directory translate their API with locale files (`i18n.__`, `__all`): their events take +their texts from those files, like the rest of their messages. Do not move them to inline texts. + +## Notifications to one user + +`pushNotification` sends a notification to its `recipient` without any subscription (simple-directory tells an +invited user that they joined an organization). events has no language for that user, so the service sends a +single text in the language of the request (`__(req, …)`), never `{ en, fr }`. + +## Subscribing from a UI + +The UI that opens the events subscribe embed passes the topic keys and their titles in the session language, +from the component's `` block. These titles are labels of a subscription ("Import failure"), not the +event titles. diff --git a/skills/data-fair-service/references/i18n.md b/skills/data-fair-service/references/i18n.md index aadff56..5d86899 100644 --- a/skills/data-fair-service/references/i18n.md +++ b/skills/data-fair-service/references/i18n.md @@ -34,9 +34,9 @@ the `vjsf` skill. - **Logs and error messages stay in English.** Translating API errors server side (`i18nHttpError` and the request language) is data-fair's choice only: ask the maintainer before adopting it in another service. -- **Notifications sent to events** carry `title` and `body` as `{ fr, en }`; events picks the language of each - subscription. Keep every text in one table (`portals/api/src/utils/event-texts.ts`, - `catalogs/shared/event-texts.ts`) with a unit test that checks the fr/en keys and parameters match. +- **Notifications sent to events** carry `title` and `body` as `{ en, fr }`; events picks the language of each + subscription. The texts are written next to the function that sends the event, see + [events.md](events.md). - **Subscriptions.** The events subscribe embed sends the language of its interface. A service that subscribes server side sends `locale`, or forwards the user's cookie so events can take the session language. - **Texts written into data or sent to remote platforms** (column titles, resources published on a CKAN or From e193ac54d1e5c7213976515b1788f1b889827453 Mon Sep 17 00:00:00 2001 From: BatLeDev Date: Fri, 13 Nov 2026 10:28:00 +0100 Subject: [PATCH 2/2] docs(skills): events example matches its catalogs model, topic action and text apart The example now copies catalogs/api/src/imports/service.ts (resource is the catalog, required session, a text more precise than the topic action), and the reference says why the topic action and the text are two parameters. internalProcess takes a title, not a name. --- skills/data-fair-service/references/events.md | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/skills/data-fair-service/references/events.md b/skills/data-fair-service/references/events.md index cef9cb1..c658bf3 100644 --- a/skills/data-fair-service/references/events.md +++ b/skills/data-fair-service/references/events.md @@ -12,7 +12,8 @@ customers, agents…) sends it the same way. Model to copy: `catalogs/api/src/im never use an optional queue (`eventsQueue?.pushEvent`). - `await eventsQueue.stop()` on shutdown, so the last events are sent. - `pushEvent(event, sessionState)`: pass the session when a user triggered it, the queue fills `originator`. - A process with no user sets `originator: { internalProcess: { id, title } }` itself. + A process with no user sets `originator: { internalProcess: { id, title } }` itself: `title`, not `name`, + is what the events UI shows ("Catalogs Worker"), a missing one shows a generic "Internal process". ## Topic keys are a contract @@ -33,19 +34,37 @@ event of that resource. Its texts are written right there, English first, both l ```ts const importTitles = { create: (title: string) => ({ en: `The import of the resource ${title} was created`, fr: `L'import de la ressource ${title} a été créé` }), - delete: (title: string) => ({ en: `The import of the resource ${title} was deleted`, fr: `L'import de la ressource ${title} a été supprimé` }) + update: (title: string) => ({ en: `The import of the resource ${title} was updated`, fr: `L'import de la ressource ${title} a été modifié` }), + delete: (title: string) => ({ en: `The import of the resource ${title} was deleted`, fr: `L'import de la ressource ${title} a été supprimé` }), + replace: (title: string) => ({ + en: `The import of the resource ${title} was deleted (replaced by a new import)`, + fr: `L'import de la ressource ${title} a été supprimé (remplacé par un nouvel import)` + }) } -export const sendImportEvent = (imp: Import, action: keyof typeof importTitles, sessionState?: SessionStateAuthenticated) => { +export const sendImportEvent = ( + imp: Import, + topicAction: 'create' | 'update' | 'delete', + sessionState: SessionStateAuthenticated, + textAction: keyof typeof importTitles = topicAction +) => { eventsQueue.pushEvent({ - title: importTitles[action](imp.remoteResource.title || imp.remoteResource.id), - topic: { key: `catalogs:import-${action}:${imp._id}` }, + title: importTitles[textAction](imp.remoteResource.title || imp.remoteResource.id), + topic: { key: `catalogs:import-${topicAction}:${imp._id}` }, sender: imp.owner, - resource: { type: 'import', id: imp._id, title: imp.remoteResource.title } + resource: { type: 'catalog', id: imp.catalog.id, title: imp.catalog.title } }, sessionState) } + +// the replaced import is a deletion for the subscribers, with a more precise title +sendImportEvent(existingImport, 'delete', sessionState, 'replace') ``` +- The topic action and the text are two parameters: the topic action is a closed list (the keys users + subscribe to), the text table can be more precise. A variant of an action ("deleted with its + publications", "triggered by a webhook") is a new text, never a new topic key, so nobody's subscription + misses it. The text defaults to the topic action, most calls pass only that. + - No key table, no `{title}` interpolation helper, no unit test comparing keys, and no type of our own for the `{ en, fr }` objects: a parameter that receives a title or a body is typed `PushEvent['title']` / `PushEvent['body']` (from `@data-fair/lib-node/events-queue.js`).