Files
recipe-app/backend/src/flyer-import/flyer-import.controller.ts
T
Nils-Johan Gynther d5f903db98
Test Suite / backend-pr-quick (push) Has been skipped
Test Suite / quick-import-pr-quick (push) Has been skipped
Test Suite / backend-full (push) Failing after 3m41s
Test Suite / flutter-quality (push) Successful in 2m3s
chore(import): improve error handling and add flyer integration
- Replace BadRequestException with UnauthorizedException for authentication failures in flyer-import and flyer-selection controllers
- Add bulk selection endpoint in FlyerSelectionController for creating multiple selections in one request
- Update FlyerSelectionModule to include new FlyerSelectionMatcherService and FlyerSelectionSyncController
- Extend FlyerSelectionService with createMany method for bulk operations
- Add new DTOs for bulk selection and receipt matching functionality
- Update ReceiptImportService to accept FlyerSelectionService dependency and track successful rows
- Extend SaveReceiptResponse with flyerAutoSync field for receipt-to-flyer matching results
- Add new API paths for flyer import and selection endpoints
- Update Flutter UI to include Flyer import tab and adjust tab controller length
- Add new domain models and repository methods for flyer import functionality
- Update test files to include new FlyerSelectionService dependency
- Modify .kilo plan documentation to reflect current system architecture
2026-05-18 22:51:27 +02:00

61 lines
1.6 KiB
TypeScript

import {
BadRequestException,
Controller,
HttpCode,
Post,
Request,
UnauthorizedException,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { Throttle } from '@nestjs/throttler';
import { FileInterceptor } from '@nestjs/platform-express';
import { memoryStorage } from 'multer';
import { FlyerImportResponse } from './dto/flyer-import.response';
import { FlyerImportService } from './flyer-import.service';
const ALLOWED_MIMES = [
'application/pdf',
'application/octet-stream',
'text/plain',
];
@Controller('flyer-import')
export class FlyerImportController {
constructor(private readonly flyerImportService: FlyerImportService) {}
@Post('parse')
@HttpCode(200)
@Throttle({ default: { ttl: 60_000, limit: 10 } })
@UseInterceptors(
FileInterceptor('file', {
storage: memoryStorage(),
limits: { fileSize: 15 * 1024 * 1024 },
}),
)
async parseFlyer(
@UploadedFile() file?: Express.Multer.File,
@Request() req?: any,
): Promise<FlyerImportResponse> {
if (!file?.buffer) {
throw new BadRequestException('Ingen fil skickades med.');
}
if (!ALLOWED_MIMES.includes(file.mimetype)) {
throw new BadRequestException('Otillåten filtyp. Använd PDF eller textfil.');
}
const userId =
typeof req?.user?.id === 'number'
? req.user.id
: typeof req?.user?.userId === 'number'
? req.user.userId
: undefined;
if (!userId) {
throw new UnauthorizedException('Kunde inte identifiera användaren.');
}
return this.flyerImportService.parseAndMatch(file, userId);
}
}