Explorar o código

Keep portal conversations through a customers-only restore and their authors within the workshop (#605)

* Keep portal conversations through a customers-only restore and their authors within the workshop

* Lock the customers and their chat before a restore sets the conversations aside

* Name the schema in the restore's lock queries
Bernt Christian Egeland hai 8 horas
pai
achega
18ad546826

+ 6 - 3
src/__tests__/lib/backup/import-from-disk.test.ts

@@ -22,8 +22,10 @@ vi.mock('@/lib/db', () => {
   const tx = new Proxy(
     {},
     {
-      get: (_target, model: string) =>
-        new Proxy(
+      get: (_target, model: string) => {
+        // The row locks a restore takes before it reads the portal chat.
+        if (model === '$queryRaw') return async () => [{ count: 0 }]
+        return new Proxy(
           {},
           {
             get: (_t, method: string) => async (args: any) => {
@@ -33,7 +35,8 @@ vi.mock('@/lib/db', () => {
               return { count: 0 }
             },
           }
-        ),
+        )
+      },
     }
   )
   return { db: { $transaction: async (fn: (t: unknown) => unknown) => fn(tx) } }

+ 6 - 3
src/__tests__/lib/backup/import-redaction.test.ts

@@ -16,8 +16,10 @@ vi.mock('@/lib/db', () => {
   const tx = new Proxy(
     {},
     {
-      get: (_target, model: string) =>
-        new Proxy(
+      get: (_target, model: string) => {
+        // The row locks a restore takes before it reads the portal chat.
+        if (model === '$queryRaw') return async () => [{ count: 0 }]
+        return new Proxy(
           {},
           {
             get: (_t, method: string) => async (args: any) => {
@@ -29,7 +31,8 @@ vi.mock('@/lib/db', () => {
               return { count: 0 }
             },
           }
-        ),
+        )
+      },
     }
   )
   return { db: { $transaction: async (fn: (t: unknown) => unknown) => fn(tx) } }

+ 87 - 3
src/__tests__/lib/backup/import-tenancy.test.ts

@@ -15,6 +15,8 @@ const state = vi.hoisted(() => ({
   own: {} as Record<string, string[]>,
   /** People who are members of this workshop. */
   members: [] as string[],
+  /** The portal chat this workshop holds before the restore. */
+  portalMessages: [] as Record<string, unknown>[],
   calls: [] as { model: string; method: string; args: any }[],
   cloud: false,
   role: 'admin',
@@ -24,8 +26,15 @@ vi.mock('@/lib/db', () => {
   const tx = new Proxy(
     {},
     {
-      get: (_target, model: string) =>
-        new Proxy(
+      get: (_target, model: string) => {
+        // The row locks a restore takes before it reads the portal chat.
+        if (model === '$queryRaw') {
+          return async () => {
+            state.calls.push({ model: '$queryRaw', method: 'lock', args: undefined })
+            return [{ count: 0 }]
+          }
+        }
+        return new Proxy(
           {},
           {
             get: (_t, method: string) => async (args: any) => {
@@ -39,12 +48,18 @@ vi.mock('@/lib/db', () => {
                 const table = 'NOT' in args.where ? state.foreign : state.own
                 return (table[model] ?? []).filter((id) => ids.includes(id)).map((id) => ({ id }))
               }
+              if (method === 'findMany' && model === 'portalMessage') return state.portalMessages
+              // Asked without ids: the customers in the workshop once the file's are in.
+              if (method === 'findMany' && model === 'customer') {
+                return (state.own.customer ?? []).map((id) => ({ id }))
+              }
               if (method === 'findMany') return []
               if (method === 'findFirst' || method === 'findUnique') return null
               return { count: 0 }
             },
           }
-        ),
+        )
+      },
     }
   )
   return {
@@ -121,6 +136,7 @@ beforeEach(() => {
   state.foreign = {}
   state.own = {}
   state.members = []
+  state.portalMessages = []
   state.calls = []
   state.cloud = false
   state.role = 'admin'
@@ -326,3 +342,71 @@ describe('on the cloud', () => {
     expect(allowed.status).toBe(200)
   })
 })
+
+describe('the portal chat when the file replaces customers without carrying it', () => {
+  const message = (id: string, extra: Record<string, unknown> = {}) => ({
+    id,
+    direction: 'outbound',
+    body: 'Your car is ready',
+    attachments: [],
+    readAt: null,
+    createdAt: new Date('2026-11-14T10:00:00Z'),
+    organizationId: 'org-a',
+    customerId: 'cust-a',
+    authorUserId: null,
+    ...extra,
+  })
+
+  it('puts the conversations back under the recreated customers', async () => {
+    state.own = { customer: ['cust-a'] }
+    state.members = ['staff-1']
+    state.portalMessages = [
+      message('msg-1', { authorUserId: 'staff-1' }),
+      message('msg-2', { authorUserId: 'someone-from-another-workshop' }),
+      message('msg-3', { customerId: 'cust-not-in-the-file' }),
+    ]
+
+    const response = await importJson({ customers: [{ id: 'cust-a', name: 'Kari', userId: 'u' }] })
+    expect(response.status).toBe(200)
+
+    const restored = state.calls.find(
+      (c) => c.model === 'portalMessage' && c.method === 'createMany'
+    )
+    expect(restored?.args.data).toEqual([
+      expect.objectContaining({ id: 'msg-1', customerId: 'cust-a', authorUserId: 'staff-1' }),
+      expect.objectContaining({ id: 'msg-2', customerId: 'cust-a', authorUserId: null }),
+    ])
+  })
+
+  it('locks the rows before reading them, and reads them before the customers go', async () => {
+    state.own = { customer: ['cust-a'] }
+    state.portalMessages = [message('msg-1')]
+
+    await importJson({ customers: [{ id: 'cust-a', name: 'Kari', userId: 'u' }] })
+
+    const at = (model: string, method: string) =>
+      state.calls.findIndex((c) => c.model === model && c.method === method)
+    const read = state.calls.findIndex(
+      (c) => c.model === 'portalMessage' && c.method === 'findMany' && !c.args?.where?.id
+    )
+    expect(at('$queryRaw', 'lock')).toBeGreaterThanOrEqual(0)
+    expect(at('$queryRaw', 'lock')).toBeLessThan(read)
+    expect(read).toBeLessThan(at('customer', 'deleteMany'))
+  })
+
+  it('takes no locks and keeps nothing aside when the file carries the chat itself', async () => {
+    state.own = { customer: ['cust-a'] }
+    state.portalMessages = [message('msg-old')]
+
+    await importJson({
+      customers: [{ id: 'cust-a', name: 'Kari', userId: 'u' }],
+      portalMessages: [message('msg-from-file', { createdAt: '2026-11-10T10:00:00.000Z' })],
+    })
+
+    expect(state.calls.some((c) => c.model === '$queryRaw')).toBe(false)
+    const restored = state.calls.find(
+      (c) => c.model === 'portalMessage' && c.method === 'createMany'
+    )
+    expect(restored?.args.data.map((r: { id: string }) => r.id)).toEqual(['msg-from-file'])
+  })
+})

+ 60 - 0
src/__tests__/lib/backup/portal-messages.test.ts

@@ -0,0 +1,60 @@
+import { describe, expect, it } from 'vitest'
+import {
+  keepsCurrentPortalMessages,
+  portalMessageAuthorIds,
+  portalMessageRowsToRestore,
+} from '@/lib/backup/portal-messages'
+
+describe('the portal chat through a restore', () => {
+  it('holds on to the conversations when the file replaces customers without carrying the chat', () => {
+    expect(keepsCurrentPortalMessages({ customers: [] })).toBe(true)
+  })
+
+  it('takes the chat from the file when the file has it, even empty', () => {
+    expect(keepsCurrentPortalMessages({ customers: [], portalMessages: [] })).toBe(false)
+  })
+
+  it('leaves the chat alone when the file does not touch customers', () => {
+    expect(keepsCurrentPortalMessages({})).toBe(false)
+  })
+
+  it('puts back only messages whose customer is in the workshop after the restore', () => {
+    const rows = portalMessageRowsToRestore(
+      [
+        { id: 'm1', customerId: 'c1', authorUserId: null },
+        { id: 'm2', customerId: 'gone', authorUserId: null },
+        { id: 'm3', authorUserId: null },
+      ],
+      new Set(['c1']),
+      new Set()
+    )
+    expect(rows.map((r) => r.id)).toEqual(['m1'])
+  })
+
+  it('keeps an author who is a member of this workshop and drops one from another workshop', () => {
+    const rows = portalMessageRowsToRestore(
+      [
+        { id: 'm1', customerId: 'c1', authorUserId: 'member' },
+        { id: 'm2', customerId: 'c1', authorUserId: 'other-workshop-user' },
+      ],
+      new Set(['c1']),
+      new Set(['member'])
+    )
+    expect(rows).toEqual([
+      { id: 'm1', customerId: 'c1', authorUserId: 'member' },
+      { id: 'm2', customerId: 'c1', authorUserId: null },
+    ])
+  })
+
+  it('names each author once and skips messages the customer wrote', () => {
+    expect(
+      portalMessageAuthorIds([
+        { authorUserId: 'u1' },
+        { authorUserId: 'u1' },
+        { authorUserId: null },
+        { authorUserId: '' },
+        { authorUserId: 'u2' },
+      ])
+    ).toEqual(['u1', 'u2'])
+  })
+})

+ 47 - 17
src/lib/backup/import.ts

@@ -3,6 +3,11 @@ import { copyFile, mkdir, readdir, rename } from 'node:fs/promises'
 import path from 'node:path'
 import { db } from '@/lib/db'
 import type { TxClient } from '@/lib/db'
+import {
+  keepsCurrentPortalMessages,
+  portalMessageAuthorIds,
+  portalMessageRowsToRestore,
+} from '@/lib/backup/portal-messages'
 import { clearPlanFor, UPLOAD_CATEGORIES } from '@/lib/backup/manifest'
 import {
   rewriteFileUrl,
@@ -640,6 +645,32 @@ export async function restoreBackup(
             tx.documentAssetSnapshot.deleteMany({ where: { organizationId } }),
         }
 
+        // The portal chat goes with its customers by cascade. A file that
+        // replaces the customers without carrying the chat would delete every
+        // conversation, so those are read first and put back further down.
+        //
+        // The chat stays open while a restore runs, so the rows are locked
+        // before they are read. A message being sent takes a share of its
+        // customer's row, and a read receipt takes its message's row: locking
+        // both waits for the writes already under way, which the read below
+        // then sees, and holds new ones until the restore commits. One that
+        // waited finds its customer or message replaced and fails or changes
+        // nothing; it is never taken in and then silently dropped.
+        let currentPortalMessages: Record<string, unknown>[] = []
+        if (keepsCurrentPortalMessages(data)) {
+          await raw.$queryRaw(Prisma.sql`
+            SELECT count(*) FROM (
+              SELECT 1 FROM "public"."customers" WHERE "organizationId" = ${organizationId} FOR UPDATE
+            ) locked
+          `)
+          await raw.$queryRaw(Prisma.sql`
+            SELECT count(*) FROM (
+              SELECT 1 FROM "public"."portal_messages" WHERE "organizationId" = ${organizationId} FOR UPDATE
+            ) locked
+          `)
+          currentPortalMessages = await tx.portalMessage.findMany({ where: { organizationId } })
+        }
+
         for (const model of clearPlanFor(Object.keys(data))) {
           const clear = clearable[model]
           if (clear) await clear()
@@ -1762,34 +1793,33 @@ export async function restoreBackup(
         // The chat in the customer portal. A message belongs to a customer, so
         // one whose customer is not in this workshop after the restore has no
         // thread to sit in and is left out. Its author is kept only when that
-        // person exists on this install; its photos point at this workshop.
-        const portalMessages = data.portalMessages as Record<string, unknown>[] | undefined
-        if (portalMessages?.length) {
+        // person is a member of this workshop; its photos point at this
+        // workshop. Without the chat in the file, the workshop's own messages
+        // (read before the customers were replaced) go back as they were.
+        const filePortalMessages = data.portalMessages as Record<string, unknown>[] | undefined
+        const portalMessages: Record<string, unknown>[] =
+          filePortalMessages ?? currentPortalMessages
+        if (portalMessages.length) {
           const customerIds = new Set(
             (await tx.customer.findMany({ where: { organizationId }, select: { id: true } })).map(
               (c) => c.id
             )
           )
-          const authorIds = [
-            ...new Set(
-              portalMessages
-                .map((m) => m.authorUserId)
-                .filter((id): id is string => typeof id === 'string' && id.length > 0)
-            ),
-          ]
-          const authors = new Set(
+          const authorIds = portalMessageAuthorIds(portalMessages)
+          const members = new Set(
             authorIds.length
               ? (
-                  await tx.user.findMany({ where: { id: { in: authorIds } }, select: { id: true } })
-                ).map((u) => u.id)
+                  await tx.organizationMember.findMany({
+                    where: { organizationId, userId: { in: authorIds } },
+                    select: { userId: true },
+                  })
+                ).map((m) => m.userId)
               : []
           )
           await restoreRows(
             'portal messages',
-            (rows) => tx.portalMessage.createMany({ data: rows as never }),
-            portalMessages
-              .filter((m) => typeof m.customerId === 'string' && customerIds.has(m.customerId))
-              .map((m) => ({ ...m, authorUserId: keptReference(m.authorUserId, authors) })),
+            (batch) => tx.portalMessage.createMany({ data: batch as never }),
+            portalMessageRowsToRestore(portalMessages, customerIds, members),
             { organizationId },
             { fields: ['attachments'], organizationId }
           )

+ 54 - 0
src/lib/backup/portal-messages.ts

@@ -0,0 +1,54 @@
+/**
+ * The portal chat through a restore. A message belongs to a customer and goes
+ * with them when they are deleted, and a restore that carries customers
+ * deletes and recreates them. So two things are decided here, apart from the
+ * restore itself so they can be tested without a database.
+ */
+
+type Row = Record<string, unknown>
+
+/**
+ * A file with customers but without the chat (a customers-only export) would
+ * take every conversation with it: the customers are replaced, the messages
+ * cascade, and the file has none to put back. The restore then holds on to
+ * the workshop's own messages and puts them back under the recreated
+ * customers.
+ */
+export function keepsCurrentPortalMessages(data: {
+  customers?: unknown
+  portalMessages?: unknown
+}): boolean {
+  return data.customers !== undefined && data.portalMessages === undefined
+}
+
+/**
+ * The rows that go back in: only those whose customer is in this workshop
+ * after the restore, since a message without one has no thread to sit in.
+ * The author is kept only when they are a member of this workshop; a user id
+ * from another workshop on the same install would otherwise show that
+ * person's name in the inbox and the portal as this workshop's staff.
+ */
+export function portalMessageRowsToRestore<T extends Row>(
+  messages: readonly T[],
+  customerIds: ReadonlySet<string>,
+  memberIds: ReadonlySet<string>
+): T[] {
+  return messages
+    .filter((m) => typeof m.customerId === 'string' && customerIds.has(m.customerId))
+    .map((m) => ({
+      ...m,
+      authorUserId:
+        typeof m.authorUserId === 'string' && memberIds.has(m.authorUserId) ? m.authorUserId : null,
+    }))
+}
+
+/** The author ids a set of messages names, each once. */
+export function portalMessageAuthorIds(messages: readonly Row[]): string[] {
+  return [
+    ...new Set(
+      messages
+        .map((m) => m.authorUserId)
+        .filter((id): id is string => typeof id === 'string' && id.length > 0)
+    ),
+  ]
+}