Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
import { and, eq, inArray, isNotNull } from 'drizzle-orm';
|
|
|
|
|
|
|
|
|
|
import { db } from '@/lib/db';
|
|
|
|
|
import { documents, documentSigners } from '@/lib/db/schema/documents';
|
|
|
|
|
import { getDocument as getDocumensoDoc } from '@/lib/services/documenso-client';
|
|
|
|
|
import {
|
|
|
|
|
handleRecipientSigned,
|
|
|
|
|
handleDocumentCompleted,
|
|
|
|
|
handleDocumentExpired,
|
|
|
|
|
} from '@/lib/services/documents.service';
|
|
|
|
|
import { logger } from '@/lib/logger';
|
|
|
|
|
|
|
|
|
|
export async function processDocumensoPoll(): Promise<void> {
|
|
|
|
|
// Find all documents that are in-progress signing and have a documensoId
|
|
|
|
|
const pendingDocs = await db.query.documents.findMany({
|
|
|
|
|
where: and(
|
|
|
|
|
inArray(documents.status, ['sent', 'partially_signed']),
|
|
|
|
|
isNotNull(documents.documensoId),
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (pendingDocs.length === 0) return;
|
|
|
|
|
|
|
|
|
|
logger.info({ count: pendingDocs.length }, 'Polling Documenso for document statuses');
|
|
|
|
|
|
|
|
|
|
for (const doc of pendingDocs) {
|
|
|
|
|
if (!doc.documensoId) continue;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const remoteDoc = await getDocumensoDoc(doc.documensoId);
|
|
|
|
|
|
|
|
|
|
// Reconcile signer statuses
|
|
|
|
|
for (const remoteRecipient of remoteDoc.recipients) {
|
|
|
|
|
if (remoteRecipient.status === 'SIGNED') {
|
|
|
|
|
// Check if we already have this signer as signed locally
|
|
|
|
|
const localSigner = await db.query.documentSigners.findFirst({
|
|
|
|
|
where: and(
|
|
|
|
|
eq(documentSigners.documentId, doc.id),
|
|
|
|
|
eq(documentSigners.signerEmail, remoteRecipient.email),
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (localSigner && localSigner.status !== 'signed') {
|
|
|
|
|
logger.info(
|
2026-05-11 13:56:46 +02:00
|
|
|
{ documentId: doc.id, email: remoteRecipient.email, portId: doc.portId },
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
'Reconciling signed signer from poll',
|
|
|
|
|
);
|
2026-05-11 13:56:46 +02:00
|
|
|
// Thread portId from the workflow's port context so the webhook
|
|
|
|
|
// handlers run port-scoped lookups (resolveWebhookDocument) rather
|
|
|
|
|
// than the port-ambiguous fallback.
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
await handleRecipientSigned({
|
|
|
|
|
documentId: doc.documensoId,
|
|
|
|
|
recipientEmail: remoteRecipient.email,
|
2026-05-11 13:56:46 +02:00
|
|
|
portId: doc.portId,
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reconcile document status
|
|
|
|
|
if (remoteDoc.status === 'COMPLETED' && doc.status !== 'completed') {
|
2026-05-11 13:56:46 +02:00
|
|
|
logger.info({ documentId: doc.id, portId: doc.portId }, 'Reconciling completed document from poll');
|
|
|
|
|
await handleDocumentCompleted({ documentId: doc.documensoId, portId: doc.portId });
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
} else if (remoteDoc.status === 'EXPIRED' && doc.status !== 'expired') {
|
2026-05-11 13:56:46 +02:00
|
|
|
logger.info({ documentId: doc.id, portId: doc.portId }, 'Reconciling expired document from poll');
|
|
|
|
|
await handleDocumentExpired({ documentId: doc.documensoId, portId: doc.portId });
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
}
|
|
|
|
|
} catch (err) {
|
2026-05-11 13:01:47 +02:00
|
|
|
logger.error(
|
|
|
|
|
{ err, documentId: doc.id, documensoId: doc.documensoId },
|
|
|
|
|
'Documenso poll failed for document',
|
|
|
|
|
);
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|