Aller au contenu

Project Financing Request Module

Backend for the Project Financing Request portal — the project-owner-facing interface where borrowers submit their financing requests.

History: Renamed from espace-fi in May 2026 (8-part migration). HTTP routes, S3 provider, config keys, and TypeScript code all use project-financing-request. Most DB tables use the project_financing_request_* prefix; the borrower company table is project_owner_company (renamed from espace_fi_spv, BRI-1501). The shared API contract package remains @bricks-common/api-communication-project-financing.

Stack

Uses the Kysely + Zod stack (see .cursor/rules/api-conventions.mdc § "Stack Espace Financement"):

Tool Purpose
Kysely Query builder (getKysely(), ky_safePgTransaction)
Zod Validation + type derivation
apiErr / throwApiError Structured business errors → HTTP mapping

Database tables

Table Content
project_financing_request Financing request (draft → in-analysis → …) linked to a project owner
project_financing_request_user Owner identity, linked to Better Auth user ID
project_financing_request_presentation JSONB document with pitch fields + S3 image keys
project_owner_company Borrower company data (project owner company)
project_owner_company_beneficial_owner Beneficial owners of a company, synced from Pappers and edited from the BO fiche Société, soft-deleted (deletedAt), never hard-deleted
project_financing_request_offer Versioned financing offer (append-only, pushed by Projet Analyse)

Schemas live in src/__new/lib/kysely/schemas/ (project-financing-request*.schema.ts, project-owner-company.schema.ts).

HTTP endpoints

All routes are prefixed project-financing-request/ and guarded by ProjectFinancingRequestUserGuard (Better Auth session + active row on project_financing_request_user_role when :projectId is present).

The guard enforces project access only when projectId is present in the URL. Routes without projectId (e.g. Pappers search) only require a valid Better Auth session and a project_financing_request_user row.

Project presentation

Method Path Action
GET /project/:projectId/presentation Get presentation (returns presigned GET URLs for images); creates an empty one for projects predating the creation seed
PATCH /project/:projectId/presentation/:presentationId Save presentation body (sets status back to draft)
PUT /project/:projectId/presentation/:presentationId/complete Mark as completed

Presentation images (S3 presigned upload)

Method Path Action
POST /project/:projectId/presentation/:presentationId/images/request-upload Get presigned PUT URLs for upload
POST /project/:projectId/presentation/:presentationId/images/confirm-upload Confirm objects exist + persist keys in JSONB
DELETE /project/:projectId/presentation/:presentationId/images Remove image(s) from presentation

Project owner company (emprunteur)

Method Path Action
GET /project/:projectId/project-owner-company Get or create company record
PATCH /project/:projectId/project-owner-company/:projectOwnerCompanyId Update company fields
PUT /project/:projectId/project-owner-company/:projectOwnerCompanyId/complete Mark company as completed

Submit to analysis

Method Path Action
POST /:projectId/submit Submit draft project to analysis (requires presentation + documents completed; company optional)

MkDocs : phase-one-steps.md.

Financing requests

Method Path Action
GET /projects List financing requests (id, name, status, steps, createdAt, updatedAt)
POST /projects Create a draft financing request, its creator's access row and its seeded presentation

Creation writes three rows in one transaction: the request, the creator's project_financing_request_user_role and the presentation seeded with the body (name, category, fundingAmountRequested, fundsNeededByDate). The creator's role is collaborator unless the body declares apporteur_affaires — creation is the only moment that role can be obtained. representative is rejected (400): it is meant to be granted by designation at the borrower step, which is not implemented yet (BRI-1787). Until it lands, nothing writes that role, and the guard only checks that an access row exists — never its value.

Phase-1 steps (presentation, documents, borrower) are computed per entity. Phase-2 keys (analysis, offer, finalization, signature) are also returned — derived in compute-project-financing-request-steps.ts from macro status (+ borrower company finalized for finalization / signature). The PDP app reads steps from the API as-is via getFunnel, and maps finalization → UI label collect — see phase-one-steps.md.

Project documents

Method Path Action
GET /project/:projectId/documents List files (presigned GET URLs) + onboarding/analysis checklists
POST /project/:projectId/documents/request-upload Presigned PUT URLs for file upload
POST /project/:projectId/documents/confirm-upload Confirm S3 objects + persist document rows
POST /project/:projectId/documents/:documentId/comment Add comment on a file
DELETE /project/:projectId/documents/:documentId/comment Remove comment
DELETE /project/:projectId/documents/:documentId Soft-delete a file (onboarding / analysis / finalization — guard par documentType)
PUT /project/:projectId/documents/base-document-list/check Toggle onboarding checklist item (draft only)
PUT /project/:projectId/documents/analysis-documents/check Toggle analysis missing-doc item (blocked only)
PUT /project/:projectId/documents/complete Mark documents step completed (draft only)
POST /project/:projectId/documents/validate Clear analysis missing docs + notify Projet Analyse (blocked)

MkDocs : project-documents.md.

Project analysis (owner)

Method Path Action
GET /project/:projectId/analysis/status Analysis UI state (pending or blocked, both with questions/requests)
POST /project/:projectId/analysis/question/:questionId/answer Answer a Projet Analyse question
POST /project/:projectId/analysis/reanalysis Request re-analysis from blocked (optional comment)

MkDocs : project-analysis.md.

Project finalization (collecte)

Method Path Action
GET /:projectId/finalization Banking + 4 KYB documents + notary + investor benefits
PATCH /:projectId/finalization Auto-save finalization fields
POST /:projectId/finalization/complete Validate KYB + banking → company finalized, open signature step

MkDocs : project-finalization.md. Document upload uses confirm-upload with context.type = 'finalization' — see project-documents.md.

Referral (parrainage porteur)

No route of its own: GET /project-owner/me creates the code, the better-auth user.create.after hook sets the referrer. MkDocs : referral.md.

Back-office (BO Projets)

Routes under administration/project-financing-request and administration/project-owner-company:

Internal — Projet Analyse (x-api-key: projet-analyse)

Method Path Action
GET /internal/projet-analyse/project/:projectId Full dossier state: instruction (status, accountManager { id, email, firstName, lastName }, questions, requests) + dossier (presentation, borrower, uploaded documents with presigned URLs) + finalization block once the owner validates the finalization step
POST /internal/projet-analyse/project/:projectId/questions Push a question → blocked
POST /internal/projet-analyse/project/:projectId/comment Push analyst comment → blocked
POST /internal/projet-analyse/project/:projectId/document-requests Push a document request → blocked
PUT /internal/projet-analyse/project/:projectId/missing-documents Sync analysis missing document names
PUT /internal/projet-analyse/project/:projectId/document/:documentId/refuse Mark uploaded file as refused (isRefused on owner API)
PUT /internal/projet-analyse/project/:projectId/document-requests/:requestId/waive Waive a document request the owner cannot provide (reason required, in-analysis / blocked only, idempotent)
PATCH /internal/projet-analyse/project/:projectId/archive Abandon (archived: true) or undo by the AM, status kept, any status but completed / rejected, idempotent
POST /internal/projet-analyse/project/:projectId/bank-sync/bypass Let the dossier reach signature without a Bridge bank connection (reason required, finalization only, idempotent)

Internal — CRM archive (x-api-key: crm)

Method Path Action
PATCH /internal/crm/projects/:projectId/archive An account manager abandons (archived: true) or relaunches the dossier from the CRM, author crm, status kept, any status but completed / rejected, idempotent. Pings Projet Analyse like a porteur or BO change

Internal — CRM documents (x-api-key: crm)

Chat attachments for an Espace Financement project. Source crm, kind null. They show up in Projet Analyse (analysisReviewableSources) and on the back-office documents tab. A blank one is classified like any other piece: the fiche offers it, and the submission job sends it. Only empty fields are written, the source stays crm. A replayed s3Key returns the existing document. The CRM keeps the Bricks document id on its attachment. Deleting the chat message does not delete the file.

Method Path Action
POST /internal/crm/projects/:projectId/documents/request-upload Presigned PUT URLs (projects/:projectId/documents/)
POST /internal/crm/projects/:projectId/documents/confirm-upload Persist the uploaded objects. Body context: { type: 'crm' }. A second confirm of the same s3Key returns the existing document
GET /internal/crm/projects/:projectId/documents Live crm documents, without download URLs
GET /internal/crm/projects/:projectId/documents/:documentId/download-url Fresh presigned GET. 404 when the document is not a live crm file of this project

:projectId is the financing-request project id (projectId), the same key the CRM stores as externalId.

Internal — AI Platform (x-api-key: ai-platform)

Method Path Action
POST /internal/project-financing-request/:projectId/documents/classifications Platform results: sets kind and companyId / representativeId on the dossier's onboarding link, blanks only. Contract and error codes: src/internal-routes/docs/consumer.md
GET /internal/project-financing-request/:projectId/documents Onboarding documents with kind (null until classified), companyId / representativeId and fresh presigned URLs. Also readable with the projet-analyse key

Outbound — document classification request (AI Platform)

POST {AI_PLATFORM_API_URL}/classifications (Authorization: Bearer <AI_PLATFORM_API_KEY>), sent by the graphile task project-financing-request-document-classification. The task is enqueued in the submission transaction (draft → in-analysis), again when a deferred borrower company is completed, and on demand from the BO fiche Projet (POST /administration/project-financing-request/:projectFinancingRequestId/documents/classification). One call per dossier with every document that has no kind or no company / representative / beneficial owner (business/needs-document-classification.ts; presigned URLs, signed at send time) and a short description per entity. The platform recognises an already analysed document and replays its first result. The company and each representative carry the id the platform sends back. 202 ends the job. ai-platform-unavailable / ai-platform-rate-limited / s3-get-signing-failed retry (5 attempts); the last failure, like any other, alerts #espace-financement-alertes-tech.

Outbound — beneficial owners (Pappers)

GET https://api.pappers.fr/v2/entreprise?siren=…, the funnel's company call, sent by the graphile task project-owner-company-beneficial-owner-sync. The task is enqueued in the borrower complete transaction, one pending job per company. It reads beneficiaires_effectifs with its own schema: the funnel route never returns them. Rows go to project_owner_company_beneficial_owner (business/evaluate-beneficial-owner-changes.ts): matched on last name and first given name within the company; a row last written by the back-office (updatedBy) is never rewritten, the others are rewritten with the latest filing; an owner Pappers no longer lists is soft-deleted only when Pappers brought it, even after a back-office edit; a deleted owner Pappers lists again comes back on the same row. An empty list changes nothing. pappers-unavailable / pappers-rate-limited retry (5 attempts); the last failure, like any other, alerts #espace-financement-alertes-tech. The BO « Rafraîchir depuis Pappers » runs the same sync inline (POST /administration/project-owner-company/:id/beneficial-owners/refresh): no retry, no alert, the error goes back to the ops.

Outbound — ping Projet Analyse (M2M, static key)

POST {PROJET_ANALYSE_API_URL}/api/integrations/pdp/projects/:projectId/updated with { "reason": …, "eventId": <uuid> }. eventId is required by Projet Analyse (400 CONTRACT_INVALID without it): a fresh uuid per event, its dedup key.

Same shared-secret scheme as the inbound routes above, carried in a different header: Authorization: Bearer <PROJET_ANALYSE_API_KEY> — a static key, not an OAuth token. Separate secret from the inbound x-api-key.

reason Emitted from Transition
submitted ProjectSubmissionService.submit draft → in-analysis
reanalysis-requested ProjectFinancingRequestAnalysisWriteService.requestReanalysis blocked → in-analysis
missing-documents-provided ProjectFinancingRequestDocumentService.validateStep stays blocked
finalized ProjectFinalizationService.complete company → finalized, ping avec bloc finalization renseigné sur le prochain GET interne
account-manager-assigned ProjectFinancingRequestAdministrationService.assignAccountManager accountManagerId posé, changé ou retiré — jamais émis sur un dossier draft
archive-changed ProjectFinancingRequestArchiveService.setArchived archivage posé ou retiré par le porteur, le BO ou le CRM, statut inchangé — jamais émis quand Projet Analyse en est l'auteur

The reason is a hint, never state: Projet Analyse always re-reads the truth from the inbound GET above.

Each ping is a Graphile job (project-financing-request-analysis-ping) enqueued in the write transaction: it leaves with the commit or not at all. No axios retry: Graphile replays the job with the same eventId, so Projet Analyse processes the event once. A network error, a timeout, a 5xx or a 429 is retried, 12 attempts over about 16 hours. Another 4xx, or the last attempt, posts a Slack alert with the status and stops.

Outbound — SEPA mandate (Projet Analyse)

The back-office reads, regenerates and downloads the borrower's SEPA mandate through /administration/project/:projectId/sepa-mandate*, proxied to {PROJET_ANALYSE_API_URL}/api/integrations/pdp/projects/:projectId/sepa-mandate* (same Bearer as above). Projet Analyse owns the IBAN, BIC and RUM, the PDF, the YouSign request and the signed copies; the API stores nothing.

  • Same gate as the signature tab: dossier signature or completed, company finalized.
  • POST …/regenerations answers 202 with the mandate in preparing; generation and sending run in Projet Analyse's background, the back-office polls the GET. No axios retry: a replay would stack regenerations.
  • Every regeneration gets a new RUM, so a new mandate the ops must register with Lemonway once signed (manual, BRI-372).

Outbound — Bridge bank sync

The owner starts a bank connection through POST …/finalization/bank-sync/start (one Bridge user per connection, Connect session URL returned). The back-office reads the Bridge bank sync status of a project through GET /administration/project/:projectId/bank-sync (read-only, same shape as the owner's bankSync on the finalization page, evaluated by business/evaluate-bank-sync.ts).

Inbound — Bridge webhook

Bridge calls POST /bridge/webhook on every item refresh; the request is authenticated by the apiKey query param of the webhook URL declared in the Bridge console (BRIDGE_WEBHOOK_API_KEY, a secret we generate), like the other webhooks. See docs/project-finalization.md.

Outbound — ping CRM (Graphile)

POST {crm.url}/api/external/projects/:projectId/updated with { "reason", "eventId" }, through CrmAxiosClient. projectId is properties.id, never the dossier id.

One task, project-financing-request-crm-sync, one queue, jobKey = dossier id. Graphile replaces a pending job, so a burst in one transaction sends one ping. eventId is set at enqueue and stays the same on retry. Reasons of this slice: project-created, project-status-changed, project-members-changed, project-account-manager-assigned, project-information-changed. The CRM ignores the reason and re-reads GET /internal/crm/projects/:projectId (name, accountManager { id, email, firstName, lastName }, members, plus presentation and borrower). The CRM attaches the dossier to one of its users by the account manager uuid, then by email the first time it meets that uuid.

5xx, timeout and 404 (route not deployed) retry. Any other 4xx alerts #espace-financement-alertes-tech and stops. Outcome created posts « Nouveau projet ! ».

Deploy the CRM first (ping route + extended GET), then this API. The reverse leaves jobs in 404 until the CRM is up; Graphile replays them. document_uploaded, question_answered and contract_signed stay on POST /api/events and do not share this job key.

Pappers

Method Path Action
GET /pappers/search Company search proxy (SIREN/SIRET lookup)
GET /pappers/company Company details proxy by SIREN (siege + representants)

Onboarding document checklist

Method Path Action
PUT /project/:projectId/documents/base-document-list/check Toggle a checklist item (documentKey, checked) — see Documents

Body schema : toggleProjectFinancingRequestOnboardingBaseDocumentCheckBodySchema (@bricks-common/api-communication-project-financing).

Semantics of documentsStep.expectedDocuments (JSON on the draft project) :

  • true = document still pending (not checked off by owner)
  • false = checked off by owner

The toggle inverts the API checked flag when persisting: documentsStep.expectedDocuments[key] = !checked.

Document keys (projectFinancingRequestOnboardingDocumentKeySchema) — 9 items, all default to true on project creation:

presentationDocument, unilateralPurchasePromise, equityProof, subdivisionPermit, buildingPermit, operationForecast, companyKbis, worksQuote, expertValuationReport

steps.documents in GET /projects (compute-project-financing-request-steps.ts) :

  • Projet draft → documentsStep.status persisté (unavailable jusqu'à complétion de la présentation, puis draft / completed)
  • Projet soumis (≠ draft) → toujours completed (soumission exige l'étape complétée)

La checklist expectedDocuments n'influence pas ce statut — elle guide l'UX uniquement.

Error HTTP Cause
project-not-draft 409 Project no longer in draft
onboarding-expected-document-not-found 404 Unknown documentKey

S3 presigned upload flow

The image pipeline uses presigned URLs so the browser uploads directly to S3 without proxying through the API.

Client                          API                              S3
  │                              │                               │
  ├── POST /images/request-upload ──►                            │
  │   (file list: type, size)    │                               │
  │                              ├── generateKey (uuid7 + ext)   │
  │                              ├── computePresignedPutUrl ─────►
  │                              │                               │
  │◄─── { uploads: [{ presignedUrl, imageKey, expiresAt }] } ───┤
  │                              │                               │
  ├── PUT presignedUrl (binary) ─────────────────────────────────►
  │                              │                               │
  ├── POST /images/confirm-upload ──►                            │
  │   (imageKeys + positions)    │                               │
  │                              ├── HeadObject (validate exists)─►
  │                              ├── Transaction: update JSONB   │
  │                              ├── Delete replaced S3 keys ────►
  │                              │                               │
  │◄─── presentation with presigned GET URLs ────────────────────┤

Key details

  • S3 key format: projects/${projectId}/presentation-images/${uuidv7}.${ext}
  • Allowed MIME types: Mapped via PROJECT_PRESENTATION_IMAGE_MIME_TO_EXT from @bricks-common/api-communication-project-financing
  • Browser compatibility: The S3 client disables CRC32 checksum (requestChecksumCalculation: 'WHEN_REQUIRED') because browsers cannot send x-amz-checksum-crc32 on presigned PUTs
  • Atomicity: confirm-upload uses ky_safePgTransaction with row locking. Any failing HeadObject aborts the entire confirmation
  • Reads: get-project-presentation.view.ts signs a fresh GET URL per stored key and returns { key, url } pairs. If any signing fails, the entire response fails (prevents index desync between client and server)
  • Image positions: Presentation supports mainImageKey, secondaryImageKeys[] (indexed), and lots[].imageKey (indexed by lot)
  • Declarative save: A save sends the gallery as it should be (absent = keep, null = clear). Dropped keys are re-tagged pending (a lifecycle rule expires them), kept keys are untagged. Keys are never actively deleted — a concurrent save could re-reference a dropped key

Environment

Variable (via Doppler) Purpose
PROJECT_FINANCING_REQUEST_AWS_ACCESS_KEY_ID AWS credentials for the dedicated S3 bucket
PROJECT_FINANCING_REQUEST_AWS_SECRET_ACCESS_KEY AWS credentials
PROJECT_FINANCING_REQUEST_S3_BUCKET_NAME Target bucket
PROJECT_FINANCING_REQUEST_S3_PUT_TTL_SEC Presigned PUT URL lifetime
PROJECT_FINANCING_REQUEST_S3_GET_TTL_SEC Presigned GET URL lifetime

S3 provider

Located at src/__new/lib/providers/s3/project-financing-request-s3.ts. Built on the generic createS3Service helper which exposes computePresignedPutUrl, computePresignedGetUrl, checkHeadObject, and deleteObject.

File layout

project-financing-request/
├── controllers/
│   ├── project-financing-request.controller.ts
│   ├── project-submission.controller.ts
│   ├── project-presentation.controller.ts
│   ├── project-presentation-images.controller.ts
│   ├── project-document.controller.ts
│   ├── project-document-internal.controller.ts
│   ├── project-analysis.controller.ts
│   ├── project-analysis-internal.controller.ts
│   ├── pappers-search.controller.ts
│   └── project-owner-company.controller.ts
├── services/
│   ├── project-creation.service.ts
│   ├── project-presentation.service.ts
│   ├── project-presentation-images.service.ts
│   ├── project-document.service.ts
│   ├── project-analysis-read.service.ts
│   ├── project-analysis-write.service.ts
│   ├── project-analysis.service.ts
│   ├── project-list.service.ts
│   ├── project-editability.service.ts
│   ├── project-owner.service.ts
│   └── project-owner-company.service.ts
├── business/
│   ├── build-initial-presentation.ts
│   ├── build-presentation-draft.ts
│   ├── collect-presentation-image-keys.ts
│   └── compute-project-financing-request-steps.ts
├── mappers/
│   └── build-document-classification-descriptions.ts
├── views/
│   ├── get-project-presentation.view.ts
│   └── get-project-analysis-status.view.ts
├── repositories/
│   ├── project.repository.ts
│   ├── project-presentation.repository.ts
│   ├── project-document.repository.ts
│   ├── project-question.repository.ts
│   ├── project-document-request.repository.ts
│   ├── project-owner.repository.ts
│   ├── project-offer.repository.ts
│   └── project-owner-company.repository.ts
├── guards/
│   └── project-financing-request-user.guard.ts
├── docs/
│   └── … (MkDocs — voir docs/.pages)
└── README.md

Shared API contract

Validators and types are in @bricks-common/api-communication-project-financing (a dedicated package, separate from investor api-communication).

Naming reference

The rename migrated code progressively. This table clarifies which names are used where:

Layer Naming
Module folder project-financing-request/
HTTP route prefix project-financing-request/
DB tables project_financing_request_*, project_owner_company
TypeScript types/schemas ProjectFinancingRequest*, ProjectOwnerPgSchema, ProjectOwnerCompanyPgSchema
Request augmentation request.espaceFiProjectOwnerId
API contract package @bricks-common/api-communication-project-financing
S3 config key envConfig.projectFinancingRequest