feat: lancer manuellement les imports SFR
This commit is contained in:
@@ -94,6 +94,7 @@ import {
|
||||
getWebImportSourceById,
|
||||
createWebImportSource,
|
||||
updateWebImportSource,
|
||||
updateWebImportSourceStatus,
|
||||
deleteWebImportSource,
|
||||
} from "./db";
|
||||
import { loginLocal, hashPassword, getAzureAuthUrl, isAzureAdConfigured } from "./auth";
|
||||
@@ -105,9 +106,10 @@ import { localStoragePut, generateStorageKey } from "./localStorage";
|
||||
import { testSftpConnection, exportInvoiceToSftp, getUserSftpConfig } from "./sftpExport";
|
||||
import { drawBapCartouche } from "./bapCartouche";
|
||||
import { resolveBapExportDestination } from "./exportDestinationResolver";
|
||||
import { startEmailImportService, stopEmailImportService, isEmailImportServiceRunning, triggerEmailCheck, triggerManualEmailCheck, testImapConnection } from "./emailImportService";
|
||||
import { createEmailImportAccountController, startEmailImportService, stopEmailImportService, isEmailImportServiceRunning, triggerEmailCheck, triggerManualEmailCheck, testImapConnection } from "./emailImportService";
|
||||
import { startFolderImportService, stopFolderImportService, isFolderImportServiceRunning } from "./folderImportService";
|
||||
import { getSafeWebImportSourceError, logWebImportSourceSaveFailure } from "./webImportSourcesError";
|
||||
import { getManualWebImportAvailability, runManualWebImport } from "./manualWebImport";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { processFreeproExcel } from "./freeproService";
|
||||
import { runFreeproAutoImport, testFreeproConnection, startFreeproAutoJob, stopFreeproAutoJob, frequencyToMs } from "./freeproAutoImport";
|
||||
@@ -1717,8 +1719,7 @@ export const appRouter = router({
|
||||
// ============= EMAIL IMPORT SERVICE ROUTES =============
|
||||
emailImportService: router({
|
||||
start: protectedProcedure.mutation(async ({ ctx }) => {
|
||||
const started = await startEmailImportService(ctx.user.id);
|
||||
return { success: started };
|
||||
return startEmailImportService(ctx.user.id);
|
||||
}),
|
||||
|
||||
stop: protectedProcedure.mutation(async ({ ctx }) => {
|
||||
@@ -1740,6 +1741,27 @@ export const appRouter = router({
|
||||
return getEmailImportAccounts();
|
||||
}),
|
||||
|
||||
/** Démarre la réception automatique pour la boîte source sélectionnée par un administrateur. */
|
||||
startAccount: adminProcedure
|
||||
.input(z.object({ userId: z.number().int().positive() }))
|
||||
.mutation(async ({ input }) => {
|
||||
return createEmailImportAccountController().startAccount(input.userId);
|
||||
}),
|
||||
|
||||
/** Arrête la réception automatique pour la boîte source sélectionnée par un administrateur. */
|
||||
stopAccount: adminProcedure
|
||||
.input(z.object({ userId: z.number().int().positive() }))
|
||||
.mutation(async ({ input }) => {
|
||||
return createEmailImportAccountController().stopAccount(input.userId);
|
||||
}),
|
||||
|
||||
/** Retourne le statut en mémoire d’une boîte source administrée. */
|
||||
accountStatus: adminProcedure
|
||||
.input(z.object({ userId: z.number().int().positive() }))
|
||||
.query(async ({ input }) => {
|
||||
return createEmailImportAccountController().accountStatus(input.userId);
|
||||
}),
|
||||
|
||||
/** Déclenche une seule lecture IMAP sans activer ni planifier le service automatique. */
|
||||
checkAccountNow: adminProcedure
|
||||
.input(z.object({ userId: z.number().int().positive() }))
|
||||
@@ -2579,7 +2601,7 @@ export const appRouter = router({
|
||||
saveSettings: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
portalUrl: webPortalUrlSchema.optional(),
|
||||
portalUrl: z.string().url().optional(),
|
||||
loginEmail: z.string().email().optional().or(z.literal('')),
|
||||
loginPassword: z.string().optional(), // vide = ne pas changer
|
||||
frequency: z.enum(['manual', 'daily', 'weekly', 'monthly']).optional(),
|
||||
@@ -2747,7 +2769,7 @@ export const appRouter = router({
|
||||
id: z.number(),
|
||||
name: z.string().min(1).max(100).optional(),
|
||||
connectorType: z.string().min(1).max(50).optional(),
|
||||
portalUrl: z.string().url().optional(),
|
||||
portalUrl: webPortalUrlSchema.optional(),
|
||||
loginEmail: z.string().min(1).optional(),
|
||||
loginPassword: z.string().optional(),
|
||||
frequency: z.enum(['manual', 'daily', 'weekly', 'monthly']).optional(),
|
||||
@@ -2783,6 +2805,38 @@ export const appRouter = router({
|
||||
}
|
||||
return { apiToken: source.apiToken };
|
||||
}),
|
||||
|
||||
/** Lance ponctuellement un connecteur pris en charge, sans activer de planification. */
|
||||
runManual: protectedProcedure
|
||||
.input(z.object({ id: z.number() }))
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const source = await getWebImportSourceById(input.id);
|
||||
if (!source || source.userId !== ctx.user.id) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Source introuvable" });
|
||||
}
|
||||
|
||||
const availability = getManualWebImportAvailability(source.connectorType);
|
||||
if (!availability.available) {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: availability.reason });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await runManualWebImport(source);
|
||||
return { success: true, ...result };
|
||||
} catch (error) {
|
||||
// Ne jamais inclure une erreur de portail dans l’interface : elle peut contenir
|
||||
// des informations de session ou des sélecteurs internes.
|
||||
const technicalCode = error && typeof error === "object" && "code" in error && typeof error.code === "string"
|
||||
? error.code
|
||||
: "unknown";
|
||||
console.error("[WebImport] Échec d’import manuel", { sourceId: source.id, technicalCode });
|
||||
await updateWebImportSourceStatus(source.id, "Erreur lors de l’import manuel", 0, false);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "L’import manuel n’a pas abouti. Consultez le statut du connecteur puis réessayez.",
|
||||
});
|
||||
}
|
||||
}),
|
||||
}),
|
||||
|
||||
// ============= BACKUP ROUTES =============
|
||||
|
||||
Reference in New Issue
Block a user