import { getDocumesoDocumentByExternalId, checkDocumentSignatureStatus } from '~/server/utils/documeso'; import { getInterestById } from '~/server/utils/nocodb'; 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; console.log('[check-signature-status] Request params:', { interestId, documentId }); 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) { console.log('[check-signature-status] Using provided document ID:', documentId); const status = await checkDocumentSignatureStatus(parseInt(documentId)); return { success: true, ...status }; } // 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']); const status = await checkDocumentSignatureStatus(parseInt(interest['documensoID'])); return { success: true, documentId: parseInt(interest['documensoID']), ...status }; } // Otherwise, try to find by external ID (using interestId) - fallback method console.log('[check-signature-status] No documensoID stored, trying external ID fallback'); const externalId = `loi-${interestId}`; const document = await getDocumesoDocumentByExternalId(externalId); if (!document) { console.log('[check-signature-status] No document found by external ID either'); throw createError({ statusCode: 404, statusMessage: 'Document not found for this interest - no documensoID stored and external ID lookup failed', }); } console.log('[check-signature-status] Found document by external ID:', document.id); const status = await checkDocumentSignatureStatus(document.id); return { success: true, documentId: document.id, ...status }; } catch (error: any) { console.error('[check-signature-status] Failed to check signature status:', error); throw createError({ statusCode: error.statusCode || 500, statusMessage: error.statusMessage || 'Failed to check signature status', }); } });