Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions skills/data-fair-service/SKILL.md
Original file line number Diff line number Diff line change
@@ -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, `<i18n>` 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, `<i18n>` 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
Expand All @@ -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).

Expand Down
95 changes: 95 additions & 0 deletions skills/data-fair-service/references/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# 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: `title`, not `name`,
is what the events UI shows ("Catalogs Worker"), a missing one shows a generic "Internal process".

## 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:<id>`): renaming needs an upgrade script on the events database and on every UI that
subscribes.

New keys: `<service>:<resource>-<action>[:<resource id>]`, 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éé` }),
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,
topicAction: 'create' | 'update' | 'delete',
sessionState: SessionStateAuthenticated,
textAction: keyof typeof importTitles = topicAction
) => {
eventsQueue.pushEvent({
title: importTitles[textAction](imp.remoteResource.title || imp.remoteResource.id),
topic: { key: `catalogs:import-${topicAction}:${imp._id}` },
sender: imp.owner,
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`).
- 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 `<i18n>` block. These titles are labels of a subscription ("Import failure"), not the
event titles.
6 changes: 3 additions & 3 deletions skills/data-fair-service/references/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading