feat(receipt-import): add refresh categories endpoint and UI integration

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Nils-Johan Gynther
2026-05-03 10:48:06 +02:00
parent 6503d29801
commit b2eb870ec7
7 changed files with 79 additions and 16 deletions
@@ -12,6 +12,7 @@ import { FileInterceptor } from '@nestjs/platform-express';
import { memoryStorage } from 'multer';
import { ReceiptImportService } from './receipt-import.service';
import { ParsedReceiptItem } from './dto/parsed-receipt-item.dto';
import { AuthGuard } from '@nestjs/passport';
const ALLOWED_MIMES = [
'image/jpeg',
@@ -52,4 +53,11 @@ export class ReceiptImportController {
const userId = typeof req?.user?.id === 'number' ? req.user.id : undefined;
return this.receiptImportService.parseReceipt(file, isPremium, userId);
}
@Post('refresh-categories')
@UseGuards(AuthGuard('jwt'))
async refreshCategories() {
await this.receiptImportService.loadCategories();
return { message: 'Kategorier har uppdaterats.' };
}
}
@@ -102,12 +102,21 @@ function inferPackageDebugFromRawName(rawName: string): {
@Injectable()
export class ReceiptImportService {
private readonly logger = new Logger(ReceiptImportService.name);
private cachedCategories: any[] = [];
constructor(
private readonly prisma: PrismaService,
private readonly aiService: AiService,
private readonly categoriesService: CategoriesService,
) {}
) {
this.loadCategories();
}
async loadCategories() {
this.cachedCategories = await this.prisma.category.findMany({
include: { children: true },
});
}
async parseReceipt(file: Express.Multer.File, _isPremium = false, userId?: number): Promise<ParsedReceiptItem[]> {
// Steg 1: Delegera AI-parsning till microservice-importer
@@ -149,11 +158,7 @@ export class ReceiptImportService {
} catch {
// ignorera parse-fel
}
this.logger.error(`Importer-api kvittoparsfel: ${message}`);
if (response.status >= 400 && response.status < 500) {
throw new BadRequestException(message);
}
throw new ServiceUnavailableException(message);
throw new BadRequestException(message);
}
return response.json() as Promise<ParsedReceiptItem[]>;