/** * The documents-hub "awaiting my signature" tab matches a document's pending * signer against the set of emails the caller owns — their login email AND * their signing_email override. So a user who logs in as `abbie@` but signs as * `sales@` still sees the `sales@`-signer documents in their personal tab. */ import { describe, it, expect } from 'vitest'; import { db } from '@/lib/db'; import { documents, documentSigners } from '@/lib/db/schema/documents'; import { listDocuments } from '@/lib/services/documents.service'; import { makePort, makeClient } from '../helpers/factories'; async function seedPendingDoc(portId: string, clientId: string, signerEmail: string) { const [doc] = await db .insert(documents) .values({ portId, clientId, documentType: 'eoi', title: 'EOI awaiting signature', status: 'sent', createdBy: 'seed', }) .returning(); await db.insert(documentSigners).values({ documentId: doc!.id, signerName: 'Approver', signerEmail, signerRole: 'approver', signingOrder: 1, status: 'pending', }); return doc!.id; } const baseQuery = { page: 1, limit: 50, sort: 'createdAt' as const, order: 'desc' as const, includeArchived: false, tab: 'awaiting_me' as const, }; describe('documents hub - awaiting_me matches owned email set', () => { it('includes a doc whose pending signer matches the caller signing_email (not their login)', async () => { const port = await makePort(); const client = await makeClient({ portId: port.id }); const docId = await seedPendingDoc(port.id, client.id, 'sales@portnimara.com'); const result = await listDocuments(port.id, baseQuery, { currentUserEmails: ['abbie@portnimara.com', 'sales@portnimara.com'], }); const ids = (result.data as Array<{ id: string }>).map((d) => d.id); expect(ids).toContain(docId); }); it('excludes the doc when the caller owns neither the signer email', async () => { const port = await makePort(); const client = await makeClient({ portId: port.id }); const docId = await seedPendingDoc(port.id, client.id, 'sales@portnimara.com'); const result = await listDocuments(port.id, baseQuery, { currentUserEmails: ['abbie@portnimara.com'], }); const ids = (result.data as Array<{ id: string }>).map((d) => d.id); expect(ids).not.toContain(docId); }); });