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) {
|
||||
|
||||
Reference in New Issue
Block a user