import { BaseCommand, args } from '@adonisjs/core/ace' import type { CommandOptions } from '@adonisjs/core/types/ace' import Organization from '#models/organization' import { reconcileTransactionsForOrg } from '#services/banking/reconcile_transactions' /** * Lance la réconciliation transactions ↔ factures pour une organization * donnée (ou la première trouvée si pas d'arg). Pratique en dev pour * re-tester la logique de match sans devoir reconnecter la banque. * * Usage : * node ace banking:reconcile # 1re org trouvée * node ace banking:reconcile # org spécifique */ export default class BankingReconcile extends BaseCommand { static commandName = 'banking:reconcile' static description = 'Relance la réconciliation banking pour une org' static options: CommandOptions = { startApp: true, } @args.string({ description: 'Organization id (UUID). Si absent, prend la 1re org avec une banque connectée.', required: false, }) declare orgId?: string async run() { let orgId = this.orgId if (!orgId) { const org = await Organization.query() .whereNotNull('powensUserId') .orderBy('createdAt', 'asc') .first() if (!org) { this.logger.error('Aucune org avec un user Powens trouvée. Connecte une banque d\'abord.') return } orgId = org.id this.logger.info(`Org auto-sélectionnée : ${org.name || org.id} (${org.id})`) } const org = await Organization.findOrFail(orgId) this.logger.info(`Mode de réconciliation : ${org.reconciliationMode}`) const result = await reconcileTransactionsForOrg(orgId) this.logger.success( `Scanné : ${result.scanned} · Auto-confirmé : ${result.autoConfirmed} · Suggéré : ${result.suggested}` ) } }