feat: export manuel (exportToPdf) supporte maintenant SharePoint en plus du dossier local
This commit is contained in:
@@ -1136,6 +1136,8 @@ export const appRouter = router({
|
||||
// Get export folder from settings
|
||||
const settings = await getImportSettingsByUser(ctx.user.id);
|
||||
const exportFolder = settings?.exportFolder;
|
||||
const exportFolderType = (settings as any)?.exportFolderType || 'local';
|
||||
const isSharePoint = exportFolderType === 'sharepoint';
|
||||
|
||||
// Load service→signature associations
|
||||
const serviceAssociations = await getServiceSignaturesByUser(ctx.user.id);
|
||||
@@ -1163,7 +1165,8 @@ export const appRouter = router({
|
||||
});
|
||||
}
|
||||
|
||||
// Create export folder if it doesn't exist
|
||||
// Create export folder if not SharePoint
|
||||
if (!isSharePoint) {
|
||||
try {
|
||||
await fs.mkdir(exportFolder, { recursive: true });
|
||||
} catch (error) {
|
||||
@@ -1173,8 +1176,11 @@ export const appRouter = router({
|
||||
message: `Impossible de créer le dossier d'export: ${exportFolder}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Copy PDFs to export folder
|
||||
console.log(`[exportToPdf] Mode: ${exportFolderType}, dossier: ${exportFolder}, ${invoices.length} facture(s)`);
|
||||
|
||||
// Copy/upload PDFs to export folder
|
||||
const copiedFiles: string[] = [];
|
||||
const errors: string[] = [];
|
||||
const STORAGE_BASE_PATH = process.env.STORAGE_BASE_PATH || path.join(process.cwd(), "storage");
|
||||
@@ -1261,39 +1267,89 @@ export const appRouter = router({
|
||||
});
|
||||
|
||||
const signedPdfBytes = await pdfDoc.save();
|
||||
console.log(`[Export] Signature apposée pour ${serviceName} sur ${filename}`);
|
||||
if (isSharePoint) {
|
||||
const { uploadToSharePoint } = await import('./sharepoint');
|
||||
console.log(`[exportToPdf] Upload SharePoint: ${filename}`);
|
||||
const spResult = await uploadToSharePoint(
|
||||
{ tenantId: (settings as any)?.azureTenantId || '', clientId: (settings as any)?.azureClientId || '', clientSecret: (settings as any)?.azureClientSecret || '', sharepointUrl: exportFolder },
|
||||
Buffer.from(signedPdfBytes), filename
|
||||
);
|
||||
if (!spResult.success) throw new Error(`SharePoint: ${spResult.error}`);
|
||||
console.log(`[exportToPdf] Upload SharePoint réussi: ${spResult.webUrl}`);
|
||||
copiedFiles.push(spResult.webUrl || filename);
|
||||
} else {
|
||||
await fs.writeFile(destPath, signedPdfBytes);
|
||||
copiedFiles.push(destPath);
|
||||
console.log(`[Export] Signature apposée pour ${serviceName} sur ${filename}`);
|
||||
}
|
||||
} else {
|
||||
// Signature not found, copy without signature
|
||||
try { await fs.copyFile(sourcePath, destPath); } catch (_) {
|
||||
// Signature not found, upload/copy without signature
|
||||
let pdfBuf: Buffer;
|
||||
try { pdfBuf = await fs.readFile(sourcePath); } catch (_) {
|
||||
const fu = invoice!.fileUrl; if (!fu) throw new Error('PDF introuvable');
|
||||
const au = fu.startsWith('/') ? `${process.env.APP_BASE_URL||`http://localhost:${process.env.PORT||3000}`}${fu}` : fu;
|
||||
const rb = await fetch(au); if (!rb.ok) throw new Error(`HTTP ${rb.status}`);
|
||||
await fs.writeFile(destPath, Buffer.from(await rb.arrayBuffer()));
|
||||
pdfBuf = Buffer.from(await rb.arrayBuffer());
|
||||
}
|
||||
if (isSharePoint) {
|
||||
const { uploadToSharePoint } = await import('./sharepoint');
|
||||
const spResult = await uploadToSharePoint(
|
||||
{ tenantId: (settings as any)?.azureTenantId || '', clientId: (settings as any)?.azureClientId || '', clientSecret: (settings as any)?.azureClientSecret || '', sharepointUrl: exportFolder },
|
||||
pdfBuf, filename
|
||||
);
|
||||
if (!spResult.success) throw new Error(`SharePoint: ${spResult.error}`);
|
||||
copiedFiles.push(spResult.webUrl || filename);
|
||||
} else {
|
||||
try { await fs.copyFile(sourcePath, destPath); } catch (_) { await fs.writeFile(destPath, pdfBuf); }
|
||||
copiedFiles.push(destPath);
|
||||
}
|
||||
}
|
||||
} catch (sigError: any) {
|
||||
console.warn(`[Export] Impossible d'apposer la signature: ${sigError.message}. Copie sans signature.`);
|
||||
try { await fs.copyFile(sourcePath, destPath); } catch (_) {
|
||||
let pdfBuf: Buffer;
|
||||
try { pdfBuf = await fs.readFile(sourcePath); } catch (_) {
|
||||
const fu = invoice!.fileUrl; if (!fu) throw new Error('PDF introuvable');
|
||||
const au = fu.startsWith('/') ? `${process.env.APP_BASE_URL||`http://localhost:${process.env.PORT||3000}`}${fu}` : fu;
|
||||
const rb = await fetch(au); if (!rb.ok) throw new Error(`HTTP ${rb.status}`);
|
||||
await fs.writeFile(destPath, Buffer.from(await rb.arrayBuffer()));
|
||||
pdfBuf = Buffer.from(await rb.arrayBuffer());
|
||||
}
|
||||
if (isSharePoint) {
|
||||
const { uploadToSharePoint } = await import('./sharepoint');
|
||||
const spResult = await uploadToSharePoint(
|
||||
{ tenantId: (settings as any)?.azureTenantId || '', clientId: (settings as any)?.azureClientId || '', clientSecret: (settings as any)?.azureClientSecret || '', sharepointUrl: exportFolder },
|
||||
pdfBuf, filename
|
||||
);
|
||||
if (!spResult.success) throw new Error(`SharePoint: ${spResult.error}`);
|
||||
copiedFiles.push(spResult.webUrl || filename);
|
||||
} else {
|
||||
try { await fs.copyFile(sourcePath, destPath); } catch (_) { await fs.writeFile(destPath, pdfBuf); }
|
||||
copiedFiles.push(destPath);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No signature association, copy as-is
|
||||
try { await fs.copyFile(sourcePath, destPath); } catch (_) {
|
||||
// No signature association, upload/copy as-is
|
||||
let pdfBuf: Buffer;
|
||||
try { pdfBuf = await fs.readFile(sourcePath); } catch (_) {
|
||||
const fu = invoice!.fileUrl; if (!fu) throw new Error('PDF introuvable');
|
||||
const au = fu.startsWith('/') ? `${process.env.APP_BASE_URL||`http://localhost:${process.env.PORT||3000}`}${fu}` : fu;
|
||||
const rb = await fetch(au); if (!rb.ok) throw new Error(`HTTP ${rb.status}`);
|
||||
await fs.writeFile(destPath, Buffer.from(await rb.arrayBuffer()));
|
||||
pdfBuf = Buffer.from(await rb.arrayBuffer());
|
||||
}
|
||||
if (isSharePoint) {
|
||||
const { uploadToSharePoint } = await import('./sharepoint');
|
||||
console.log(`[exportToPdf] Upload SharePoint sans signature: ${filename}`);
|
||||
const spResult = await uploadToSharePoint(
|
||||
{ tenantId: (settings as any)?.azureTenantId || '', clientId: (settings as any)?.azureClientId || '', clientSecret: (settings as any)?.azureClientSecret || '', sharepointUrl: exportFolder },
|
||||
pdfBuf, filename
|
||||
);
|
||||
if (!spResult.success) throw new Error(`SharePoint: ${spResult.error}`);
|
||||
console.log(`[exportToPdf] Upload SharePoint réussi: ${spResult.webUrl}`);
|
||||
copiedFiles.push(spResult.webUrl || filename);
|
||||
} else {
|
||||
try { await fs.copyFile(sourcePath, destPath); } catch (_) { await fs.writeFile(destPath, pdfBuf); }
|
||||
copiedFiles.push(destPath);
|
||||
}
|
||||
}
|
||||
|
||||
// Update export status
|
||||
await updateInvoice(invoice.id, {
|
||||
|
||||
Reference in New Issue
Block a user