2025-06-10 13:59:09 +02:00
|
|
|
import { getDocumesoDocumentByExternalId, checkDocumentSignatureStatus } from '~/server/utils/documeso';
|
2025-06-12 17:04:45 +02:00
|
|
|
import { getInterestById, updateInterest } from '~/server/utils/nocodb';
|
|
|
|
|
import type { InterestSalesProcessLevel, EOIStatus } from '~/utils/types';
|
2025-06-10 13:59:09 +02:00
|
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
|
const xTagHeader = getRequestHeader(event, "x-tag");
|
|
|
|
|
|
|
|
|
|
if (!xTagHeader || (xTagHeader !== "094ut234" && xTagHeader !== "pjnvü1230")) {
|
|
|
|
|
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const query = getQuery(event);
|
|
|
|
|
const interestId = query.interestId as string;
|
|
|
|
|
const documentId = query.documentId as string;
|
|
|
|
|
|
2025-06-11 16:10:19 +02:00
|
|
|
console.log('[check-signature-status] Request params:', { interestId, documentId });
|
|
|
|
|
|
2025-06-10 13:59:09 +02:00
|
|
|
if (!interestId && !documentId) {
|
|
|
|
|
throw createError({
|
|
|
|
|
statusCode: 400,
|
|
|
|
|
statusMessage: 'Either interest ID or document ID is required',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we have a document ID, check directly
|
|
|
|
|
if (documentId) {
|
2025-06-11 16:10:19 +02:00
|
|
|
console.log('[check-signature-status] Using provided document ID:', documentId);
|
2025-06-10 13:59:09 +02:00
|
|
|
const status = await checkDocumentSignatureStatus(parseInt(documentId));
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
...status
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 16:10:19 +02:00
|
|
|
// Get the interest to check for stored documensoID
|
|
|
|
|
const interest = await getInterestById(interestId);
|
|
|
|
|
console.log('[check-signature-status] Interest retrieved:', {
|
|
|
|
|
id: interest.Id,
|
|
|
|
|
documensoID: interest['documensoID'],
|
|
|
|
|
documensoID_type: typeof interest['documensoID'],
|
|
|
|
|
has_signature_links: !!(interest['Signature Link Client'] || interest['Signature Link CC'] || interest['Signature Link Developer'])
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// If we have a stored documensoID, use it directly
|
|
|
|
|
if (interest && interest['documensoID']) {
|
|
|
|
|
console.log('[check-signature-status] Using stored documensoID:', interest['documensoID']);
|
2025-06-12 17:04:45 +02:00
|
|
|
try {
|
|
|
|
|
const status = await checkDocumentSignatureStatus(parseInt(interest['documensoID']));
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
documentId: parseInt(interest['documensoID']),
|
|
|
|
|
...status
|
|
|
|
|
};
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('[check-signature-status] Failed to check status for stored documensoID:', error);
|
|
|
|
|
|
|
|
|
|
// If document not found (404), clean up the database
|
|
|
|
|
if (error.status === 404 || error.statusCode === 404 || error.message?.includes('404')) {
|
|
|
|
|
console.log('[check-signature-status] Document not found - cleaning up orphaned database records');
|
|
|
|
|
|
|
|
|
|
const updateData = {
|
|
|
|
|
'EOI Status': 'Awaiting Further Details' as EOIStatus,
|
|
|
|
|
'Sales Process Level': 'Specific Qualified Interest' as InterestSalesProcessLevel,
|
|
|
|
|
'EOI Time Sent': undefined,
|
|
|
|
|
'Signature Link Client': undefined,
|
|
|
|
|
'Signature Link CC': undefined,
|
|
|
|
|
'Signature Link Developer': undefined,
|
|
|
|
|
'EmbeddedSignatureLinkClient': undefined,
|
|
|
|
|
'EmbeddedSignatureLinkCC': undefined,
|
|
|
|
|
'EmbeddedSignatureLinkDeveloper': undefined,
|
|
|
|
|
'documensoID': undefined,
|
|
|
|
|
'reminder_enabled': false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await updateInterest(interestId, updateData);
|
|
|
|
|
|
|
|
|
|
throw createError({
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
statusMessage: 'Document not found - database cleaned up',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Re-throw other errors
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2025-06-11 16:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, try to find by external ID (using interestId) - fallback method
|
|
|
|
|
console.log('[check-signature-status] No documensoID stored, trying external ID fallback');
|
2025-06-10 13:59:09 +02:00
|
|
|
const externalId = `loi-${interestId}`;
|
|
|
|
|
const document = await getDocumesoDocumentByExternalId(externalId);
|
|
|
|
|
|
|
|
|
|
if (!document) {
|
2025-06-11 16:10:19 +02:00
|
|
|
console.log('[check-signature-status] No document found by external ID either');
|
2025-06-10 13:59:09 +02:00
|
|
|
throw createError({
|
|
|
|
|
statusCode: 404,
|
2025-06-11 16:10:19 +02:00
|
|
|
statusMessage: 'Document not found for this interest - no documensoID stored and external ID lookup failed',
|
2025-06-10 13:59:09 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 16:10:19 +02:00
|
|
|
console.log('[check-signature-status] Found document by external ID:', document.id);
|
2025-06-10 13:59:09 +02:00
|
|
|
const status = await checkDocumentSignatureStatus(document.id);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
documentId: document.id,
|
|
|
|
|
...status
|
|
|
|
|
};
|
|
|
|
|
} catch (error: any) {
|
2025-06-11 16:10:19 +02:00
|
|
|
console.error('[check-signature-status] Failed to check signature status:', error);
|
2025-06-10 13:59:09 +02:00
|
|
|
throw createError({
|
|
|
|
|
statusCode: error.statusCode || 500,
|
|
|
|
|
statusMessage: error.statusMessage || 'Failed to check signature status',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|