updates
This commit is contained in:
@@ -43,19 +43,83 @@ 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)
|
||||
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']
|
||||
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
|
||||
}
|
||||
};
|
||||
|
||||
// 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
|
||||
@@ -105,8 +169,6 @@ 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) {
|
||||
|
||||
148
server/api/eoi/delete-generated-document.ts
Normal file
148
server/api/eoi/delete-generated-document.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import { getInterestById, updateInterest } from '~/server/utils/nocodb';
|
||||
import { getDocumesoDocumentByExternalId, checkDocumentSignatureStatus } from '~/server/utils/documeso';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const xTagHeader = getRequestHeader(event, "x-tag");
|
||||
|
||||
console.log('[EOI Delete Generated] Request received with x-tag:', xTagHeader);
|
||||
|
||||
if (!xTagHeader || (xTagHeader !== "094ut234" && xTagHeader !== "pjnvü1230")) {
|
||||
console.error('[EOI Delete Generated] Authentication failed - invalid x-tag');
|
||||
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await readBody(event);
|
||||
const { interestId } = body;
|
||||
|
||||
console.log('[EOI Delete Generated] Interest ID:', interestId);
|
||||
|
||||
if (!interestId) {
|
||||
console.error('[EOI Delete Generated] No interest ID provided');
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Interest ID is required',
|
||||
});
|
||||
}
|
||||
|
||||
// Get current interest data
|
||||
const interest = await getInterestById(interestId);
|
||||
if (!interest) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: 'Interest not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Check if there are signature links (indicating a generated document)
|
||||
const hasSignatureLinks = !!(interest['Signature Link Client'] ||
|
||||
interest['Signature Link CC'] ||
|
||||
interest['Signature Link Developer']);
|
||||
|
||||
if (!hasSignatureLinks) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'No generated EOI document found for this interest',
|
||||
});
|
||||
}
|
||||
|
||||
// Check signature status to prevent deletion of fully signed documents
|
||||
const externalId = `loi-${interestId}`;
|
||||
let documentId: number | null = null;
|
||||
|
||||
try {
|
||||
const document = await getDocumesoDocumentByExternalId(externalId);
|
||||
if (document) {
|
||||
documentId = document.id;
|
||||
|
||||
// Check if all parties have signed
|
||||
const signatureStatus = await checkDocumentSignatureStatus(documentId);
|
||||
|
||||
if (signatureStatus.allSigned) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: 'Cannot delete: All parties have already signed this document',
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[EOI Delete Generated] Document found in Documenso:', documentId);
|
||||
console.log('[EOI Delete Generated] Signature status:', {
|
||||
allSigned: signatureStatus.allSigned,
|
||||
clientSigned: signatureStatus.clientSigned,
|
||||
signedCount: signatureStatus.signedRecipients.length,
|
||||
unsignedCount: signatureStatus.unsignedRecipients.length
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.warn('[EOI Delete Generated] Could not find or check Documenso document:', error.message);
|
||||
// Continue with deletion even if Documenso document not found
|
||||
}
|
||||
|
||||
// Delete document from Documenso if it exists
|
||||
if (documentId) {
|
||||
try {
|
||||
const documensoApiKey = process.env.NUXT_DOCUMENSO_API_KEY;
|
||||
const documensoBaseUrl = process.env.NUXT_DOCUMENSO_BASE_URL;
|
||||
|
||||
if (!documensoApiKey || !documensoBaseUrl) {
|
||||
console.error('[EOI Delete Generated] Documenso configuration missing');
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Documenso configuration missing',
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[EOI Delete Generated] Deleting document from Documenso:', documentId);
|
||||
|
||||
const deleteResponse = await fetch(`${documensoBaseUrl}/api/v1/documents/${documentId}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${documensoApiKey}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!deleteResponse.ok) {
|
||||
const errorText = await deleteResponse.text();
|
||||
console.error('[EOI Delete Generated] Failed to delete from Documenso:', errorText);
|
||||
throw new Error(`Failed to delete document from Documenso: ${deleteResponse.statusText}`);
|
||||
}
|
||||
|
||||
console.log('[EOI Delete Generated] Successfully deleted document from Documenso');
|
||||
} catch (error: any) {
|
||||
console.error('[EOI Delete Generated] Error deleting from Documenso:', error);
|
||||
// Don't throw here - continue with database cleanup even if Documenso deletion fails
|
||||
}
|
||||
}
|
||||
|
||||
// Reset interest fields
|
||||
const updateData = {
|
||||
'EOI Status': 'Awaiting Further Details' as const,
|
||||
'Sales Process Level': 'Specific Qualified Interest' as const,
|
||||
'EOI Time Sent': undefined,
|
||||
'Signature Link Client': undefined,
|
||||
'Signature Link CC': undefined,
|
||||
'Signature Link Developer': undefined,
|
||||
'Documeso ID': undefined,
|
||||
'reminder_enabled': false
|
||||
};
|
||||
|
||||
console.log('[EOI Delete Generated] Resetting interest fields');
|
||||
|
||||
// Update the interest
|
||||
await updateInterest(interestId, updateData);
|
||||
|
||||
console.log('[EOI Delete Generated] Delete completed successfully');
|
||||
return {
|
||||
success: true,
|
||||
message: 'Generated EOI document deleted successfully',
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error('[EOI Delete Generated] Failed to delete generated EOI document:', error);
|
||||
console.error('[EOI Delete Generated] Error stack:', error.stack);
|
||||
throw createError({
|
||||
statusCode: error.statusCode || 500,
|
||||
statusMessage: error.statusMessage || error.message || 'Failed to delete generated EOI document',
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user