"feat-sharepoint-test-button-and-error-feedback"

This commit is contained in:
deploy
2026-04-29 15:40:21 +02:00
parent 0d5ddc7eb0
commit 9adacb17de
5 changed files with 58 additions and 6 deletions
+6 -2
View File
@@ -337,6 +337,7 @@ router.post('/:id/send', wrap(async (req: AuthRequest, res: Response): Promise<v
// ── SharePoint (non bloquant) ─────────────────────────────
let trackingAdded = false;
let trackingError: string | null = null;
if (invoice.add_to_tracking) {
try {
const [year, month, day] = dateStr.split('-');
@@ -351,8 +352,10 @@ router.post('/:id/send', wrap(async (req: AuthRequest, res: Response): Promise<v
});
trackingAdded = true;
} catch (err: any) {
// Non bloquant : l'email est déjà envoyé, on log l'erreur
// Non bloquant : l'email est déjà envoyé, mais on remonte l'erreur
// pour que le frontend puisse afficher un avertissement.
console.warn('[SharePoint] Erreur non bloquante :', err.message);
trackingError = err.message;
}
}
@@ -365,7 +368,8 @@ router.post('/:id/send', wrap(async (req: AuthRequest, res: Response): Promise<v
[path.join('pdfs', `${invoice.id}.pdf`), pdfFilename, trackingAdded, invoice.id]
);
res.json(await getInvoiceById(invoice.id));
const updated = await getInvoiceById(invoice.id);
res.json({ ...updated, tracking_error: trackingError });
}));
/**
+11
View File
@@ -140,4 +140,15 @@ router.put('/app', validate(appSettingsSchema), wrap(async (req: AuthRequest, re
res.json({ success: true });
}));
/** POST /api/settings/sharepoint/test — Vérifie la connexion Graph + accès au fichier Excel */
router.post('/sharepoint/test', wrap(async (_req: AuthRequest, res: Response): Promise<void> => {
try {
const { testSharepointConnection } = await import('../services/sharepoint');
await testSharepointConnection();
res.json({ success: true, message: 'Connexion SharePoint OK — fichier Excel accessible.' });
} catch (err: any) {
res.status(400).json({ error: err.message });
}
}));
export default router;
+21
View File
@@ -76,6 +76,27 @@ async function getAccessToken(cfg: GraphConfig): Promise<string> {
return data.access_token;
}
// ─── Test de connexion (sans écriture) ───────────────────────
/**
* Vérifie que le token Graph s'obtient et que la feuille Excel est accessible.
* Utilisé par POST /api/settings/sharepoint/test.
*/
export async function testSharepointConnection(): Promise<void> {
const cfg = await getGraphConfig();
const token = await getAccessToken(cfg);
const baseUrl = `https://graph.microsoft.com/v1.0/sites/${cfg.sharepointSiteId}/drive/items/${cfg.sharepointItemId}/workbook/worksheets/${encodeURIComponent(cfg.sharepointSheet)}`;
const resp = await fetch(`${baseUrl}/usedRange?$select=rowCount`, {
headers: { Authorization: `Bearer ${token}` },
});
if (!resp.ok) {
const body = await resp.text();
throw new Error(`Impossible d'accéder à la feuille "${cfg.sharepointSheet}" (${resp.status}) : ${body.slice(0, 200)}`);
}
}
// ─── Fonction principale ──────────────────────────────────────
/**