Aller au contenu

CRM

Bricks' in-house CRM, built by Alban — not a SaaS. There is only one, hence the short folder name and prefix (CrmApi, CrmEmbedUrl, crm.* in the env config).

It serves three surfaces:

Surface Auth Code Used by
Conversations iframe session token in the query, minted server-side with crm.embedApiKey CrmApi then CrmEmbedUrl project-conversation (BO Projets, /embed/conversations?sessionToken=)
Embed API, admin (session token, unread count) crm.embedApiKey as X-API-Key on the client, then the session token as a per-call Bearer CrmApi over crm-embed.axios.ts project-conversation, bo-projects-pending-work (global unread count)
Embed API, porteur (session token, callback, person message) crm.embedApiKey as X-API-Key on the client; the 24 h session token the CRM mints goes to the porteur's browser, never the key CrmApi over crm-embed.axios.ts project-financing-request/crm-chat (App PDP widget /embed/chat?sessionToken=)
Server-to-server external API crm.embedApiKey, as X-Api-Key CrmApi over crm.axios.ts dossier ping (POST /projects/:id/updated), porteur events (POST /api/events)

CrmApi makes every HTTP call, on two clients so each carries one credential: crm.axios.ts retries (graphile jobs), crm-embed.axios.ts does not (a porteur or a BO poll is waiting). The admin session token is bound to one admin (and, for a project iframe or the unread count, to one project), so it rides on the call, never on a client. CrmEmbedUrl only builds the iframe URLs and never calls the CRM: the caller exchanges the api key first (createAdminProjectSession, createAdminGlobalSession) and the browser receives the session token alone. Both clients read the CRM's { code } error body through crm-error-code.ts; the embed replies are parsed by crm-embed.model.ts. On the porteur calls, PROJECT_NOT_FOUND / PERSON_NOT_FOUND / ACCESS_DENIED are the one Err (crm-not-synced, answered 409 upstream); every other failure throws an ApiException (500).

Two CRM origins: CRM_API_URL is the Express back (every HTTP call above), CRM_FRONT_URL is the SPA. CrmEmbedUrl and the porteur widget session build browser URLs from CRM_FRONT_URL.

One secret: CRM_EMBED_API_KEY is the CRM EMBED_API_KEY. No signing secret is shared with the CRM.

What the external API exposes

  • POST /api/external/projects/:projectExternalId/updated — { reason, eventId }. The CRM re-reads GET /internal/crm/projects/:projectId and upserts the project, the step, the account manager and the members. eventId is generated when the job is enqueued and stays stable across Graphile retries.
  • POST /api/external/projects/:projectExternalId/callback-request — porteur asks to be called
  • POST /api/external/projects/:projectExternalId/person-message — message written as the porteur
  • POST /api/events — porteur activity (document_uploaded, question_answered, contract_signed). A 404 is retried by a graphile job; the call itself does not use axios-retry, so a timeout cannot post the event twice
  • GET /api/auth/embed/token?personExternalId&projectExternalId — porteur chat session token

Deploy the CRM (the ping route and the project read) before this API. The other way round queues pings that 404 until the CRM is up; Graphile retries them.

External ids are our uuids: the project id for the project (properties.id, reserved as project_financing_request.projectId when the dossier is created — never the dossier id, so the fiche, the CRM and the funded project share one key), the project_financing_request_user id for the person, the project_financing_request_account_manager id for the account manager — which is why the seeded account manager uuids are frozen and identical across environments.

Guardrails

  • The back-office repair (POST …/crm-sync, reason project-created) creates the CRM project of a dossier the CRM does not know. On the porteur chat, a project the CRM does not hold yet is crm-not-synced (409 upstream).
  • A ping 404 means the route is not deployed yet and is retried. Every other 4xx is permanent and alerts Slack. 5xx and timeouts throw, so Graphile retries; the last attempt alerts Slack before failing.
  • The embed API has no retry: a porteur is waiting on the click, and the BO unread poll retries on its next tick.
  • One Graphile job per dossier (project-financing-request-crm-sync). A burst replaces the pending payload. The CRM does not branch its write on reason. Slack « Nouveau projet ! » is posted when the CRM answers created, not from the reason.