This commit is contained in:
2025-06-11 13:54:04 +02:00
parent fca6321dcf
commit 0b6601fabc
8 changed files with 65 additions and 292 deletions

View File

@@ -13,7 +13,6 @@ interface EmailMessage {
timestamp: string;
direction: 'sent' | 'received';
threadId?: string;
attachments?: any[];
}
export default defineEventHandler(async (event) => {
@@ -305,15 +304,6 @@ async function fetchImapEmails(
return;
}
// Extract attachments
const attachments = parsed.attachments ? parsed.attachments.map((att: any) => ({
filename: att.filename || 'attachment',
name: att.filename || 'attachment',
size: att.size || 0,
type: att.contentType || 'application/octet-stream',
cid: att.cid || undefined
})) : [];
const email: EmailMessage = {
id: parsed.messageId || `${Date.now()}-${seqno}`,
from: fromEmail,
@@ -322,8 +312,7 @@ async function fetchImapEmails(
body: parsed.text || '',
html: parsed.html || undefined,
timestamp: parsed.date?.toISOString() || new Date().toISOString(),
direction: fromEmail.toLowerCase().includes(userEmail.toLowerCase()) ? 'sent' : 'received',
attachments: attachments
direction: fromEmail.toLowerCase().includes(userEmail.toLowerCase()) ? 'sent' : 'received'
};
if (parsed.headers.has('in-reply-to')) {

View File

@@ -29,9 +29,6 @@ export default defineEventHandler(async (event) => {
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
}
// Set longer timeout for this endpoint to prevent 502 errors
event.node.res.setTimeout(60000); // 60 seconds
try {
const body = await readBody(event);
const { interestId } = body;
@@ -46,83 +43,19 @@ export default defineEventHandler(async (event) => {
throw createError({ statusCode: 404, statusMessage: "Interest not found" });
}
// Documenso API configuration (declare early for use in regeneration)
const documensoApiKey = process.env.NUXT_DOCUMENSO_API_KEY;
const documensoBaseUrl = process.env.NUXT_DOCUMENSO_BASE_URL;
// Check if EOI already exists (has signature links)
const hasExistingEOI = !!(interest['Signature Link Client'] && interest['Signature Link CC'] && interest['Signature Link Developer']);
if (hasExistingEOI) {
console.log('EOI already exists, checking if regeneration is needed');
// For regeneration, we need to delete the old document first
const regenerateRequested = body.regenerate === true;
if (!regenerateRequested) {
console.log('Returning existing EOI links');
return {
success: true,
documentId: 'existing',
clientSigningUrl: interest['Signature Link Client'],
signingLinks: {
'Client': interest['Signature Link Client'],
'CC': interest['Signature Link CC'],
'Developer': interest['Signature Link Developer']
}
};
} else {
console.log('Regeneration requested, deleting old document first');
// Try to delete the old document from Documenso
try {
const externalId = `loi-${interestId}`;
// Import the delete utility functions
const { getDocumesoDocumentByExternalId, checkDocumentSignatureStatus } = await import('~/server/utils/documeso');
const document = await getDocumesoDocumentByExternalId(externalId);
if (document) {
// Check if all parties have signed
const signatureStatus = await checkDocumentSignatureStatus(document.id);
if (signatureStatus.allSigned) {
throw createError({
statusCode: 403,
statusMessage: 'Cannot regenerate: All parties have already signed this document',
});
}
console.log('Deleting old document from Documenso:', document.id);
const deleteResponse = await fetch(`${documensoBaseUrl}/api/v1/documents/${document.id}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${documensoApiKey}`,
'Content-Type': 'application/json'
}
});
if (!deleteResponse.ok) {
console.warn('Failed to delete old document from Documenso, continuing with new generation');
} else {
console.log('Successfully deleted old document from Documenso');
}
}
} catch (error: any) {
console.warn('Error during old document cleanup:', error.message);
// Continue with new document generation even if cleanup fails
if (interest['Signature Link Client'] && interest['Signature Link CC'] && interest['Signature Link Developer']) {
console.log('EOI already exists, returning existing links');
return {
success: true,
documentId: 'existing',
clientSigningUrl: interest['Signature Link Client'],
signingLinks: {
'Client': interest['Signature Link Client'],
'CC': interest['Signature Link CC'],
'Developer': interest['Signature Link Developer']
}
// Reset signature links so new ones will be generated
await updateInterest(interestId, {
'Signature Link Client': undefined,
'Signature Link CC': undefined,
'Signature Link Developer': undefined
});
console.log('Old document cleanup completed, proceeding with new generation');
}
};
}
// Validate required fields
@@ -172,6 +105,8 @@ export default defineEventHandler(async (event) => {
const berthNumbers = berths.map(b => b['Mooring Number']).join(', ');
// Documenso API configuration
const documensoApiKey = process.env.NUXT_DOCUMENSO_API_KEY;
const documensoBaseUrl = process.env.NUXT_DOCUMENSO_BASE_URL;
const templateId = '9';
if (!documensoApiKey || !documensoBaseUrl) {

View File

@@ -181,8 +181,7 @@ export default defineEventHandler(async (event) => {
html: htmlBody,
timestamp: new Date().toISOString(),
direction: 'sent',
interestId: interestId,
attachments: attachments // Include attachment info
interestId: interestId
};
const objectName = `interest-${interestId}/${Date.now()}-sent.json`;