import { getDocumesoDocumentByExternalId, checkDocumentSignatureStatus } from '~/server/utils/documeso'; 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; 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) { const status = await checkDocumentSignatureStatus(parseInt(documentId)); return { success: true, ...status }; } // Otherwise, try to find by external ID (using interestId) const externalId = `loi-${interestId}`; const document = await getDocumesoDocumentByExternalId(externalId); if (!document) { throw createError({ statusCode: 404, statusMessage: 'Document not found for this interest', }); } const status = await checkDocumentSignatureStatus(document.id); return { success: true, documentId: document.id, ...status }; } catch (error: any) { console.error('Failed to check signature status:', error); throw createError({ statusCode: error.statusCode || 500, statusMessage: error.statusMessage || 'Failed to check signature status', }); } });