import refreshTokenHelper from "../../utils/refreshTokenHelper" import forceLogoutHelper from "../../utils/forceLogoutHelper" import errorHandlingHelper from "../../utils/errorHandlingHelper" const handleFunc = async (event: any, authToken: any = null) => { let data: any = {} const token = authToken ?? await getTokenHelper(event) const body = await readBody(event) const invoiceIds: number[] = body.ids || [] if (!invoiceIds.length) { return { status: 400, message: 'No invoice IDs provided' } } const errors: string[] = [] let resetCount = 0 for (const invoiceId of invoiceIds) { try { // Update invoice to reset Lexoffice status await event.context.fetch( `models/c_invoice/${invoiceId}`, 'PUT', token, { isUploadToLexoffice: false, UploadDateLexoffice: null } ) resetCount++ } catch (err: any) { errors.push(`Error resetting invoice ${invoiceId}: ${err.message || 'Unknown error'}`) } } if (resetCount > 0) { data = { status: 200, message: `Successfully reset Lexoffice status for ${resetCount} invoice(s)`, resetCount, errors: errors.length > 0 ? errors : undefined } } else { data = { status: 400, message: 'No invoices were reset', errors } } return data } export default defineEventHandler(async (event) => { let data: any = {} try { data = await handleFunc(event) } catch (err: any) { try { let authToken: any = await refreshTokenHelper(event) data = await handleFunc(event, authToken) } catch (error) { data = errorHandlingHelper(err?.data ?? err, error?.data ?? error) forceLogoutHelper(event, data) } } return data })