feat: setup the attachment to the passenger api

This commit is contained in:
Nathnael
2026-07-18 09:44:14 +00:00
parent 4f043ea4bf
commit bdcd5e957e
12 changed files with 781 additions and 116 deletions

View File

@@ -0,0 +1,39 @@
-- Support chat attachments.
--
-- `SupportMessage.text` becomes nullable so an attachment-only message can say
-- "there is no text" instead of smuggling that through an empty string. This is
-- a catalog-only change in Postgres — no table rewrite, no long lock.
ALTER TABLE "SupportMessage" ALTER COLUMN "text" DROP NOT NULL;
-- The `attachments` JSONB column has been dead since the init migration: never
-- written, never read, absent from every DTO. It is dropped rather than reused —
-- an untyped blob gives no file identity, no size accounting, and nothing to
-- cascade on delete. Real rows replace it below. (The name is also needed for
-- the new relation.)
ALTER TABLE "SupportMessage" DROP COLUMN "attachments";
-- Backs keyset pagination of a thread (newest-first over (createdAt, id)).
-- Without it, paging a long thread degrades to a scan per page.
-- CreateIndex
CREATE INDEX "SupportMessage_conversationId_createdAt_idx" ON "SupportMessage"("conversationId", "createdAt");
-- CreateTable
CREATE TABLE "SupportAttachment" (
"id" TEXT NOT NULL,
"messageId" TEXT NOT NULL,
"name" TEXT NOT NULL,
"mimeType" TEXT NOT NULL,
"size" INTEGER NOT NULL,
"url" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "SupportAttachment_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "SupportAttachment_messageId_idx" ON "SupportAttachment"("messageId");
-- AddForeignKey
-- CASCADE: an attachment has no meaning without its message. (Object bytes in
-- MinIO are not reaped by this — deleting messages is not a flow that exists.)
ALTER TABLE "SupportAttachment" ADD CONSTRAINT "SupportAttachment_messageId_fkey" FOREIGN KEY ("messageId") REFERENCES "SupportMessage"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -916,10 +916,37 @@ model SupportMessage {
id String @id @default(uuid())
conversationId String
sender SupportSender
text String
attachments Json?
/// NULL for an attachment-only message — absence of text is representable
/// rather than smuggled through "". The DTO maps NULL -> "".
text String?
createdAt DateTime @default(now())
conversation SupportConversation @relation(fields: [conversationId], references: [id])
attachments SupportAttachment[]
/// Backs keyset pagination of a thread (newest-first over (createdAt, id)).
/// Without it, paging a long thread degrades to a scan per page.
@@index([conversationId, createdAt])
@@schema("passenger")
}
/// A file posted on a support message.
///
/// The freight side stores the equivalent in its polymorphic `freight.files`
/// table; this app has no such table (and no TypeORM), so chat attachments get a
/// purpose-built model rather than a shared one. Bytes live in MinIO — `url` is
/// the unsigned object path, signed on read for preview.
model SupportAttachment {
id String @id @default(uuid())
messageId String
name String
mimeType String
/// Bytes.
size Int
/// Unsigned MinIO object URL. Not directly fetchable by a browser — the API
/// mints a short-lived signed URL per response.
url String
createdAt DateTime @default(now())
message SupportMessage @relation(fields: [messageId], references: [id], onDelete: Cascade)
@@index([messageId])
@@schema("passenger")
}