57fe168543
Co-authored-by: Copilot <copilot@github.com>
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import {
|
|
Controller,
|
|
HttpCode,
|
|
Post,
|
|
Request,
|
|
UploadedFile,
|
|
UseInterceptors,
|
|
BadRequestException,
|
|
} from '@nestjs/common';
|
|
import { Throttle } from '@nestjs/throttler';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { memoryStorage } from 'multer';
|
|
import { ReceiptImportService } from './receipt-import.service';
|
|
import { ParsedReceiptItem } from './dto/parsed-receipt-item.dto';
|
|
|
|
const ALLOWED_MIMES = [
|
|
'image/jpeg',
|
|
'image/png',
|
|
'image/webp',
|
|
'image/heic',
|
|
'image/heif',
|
|
'application/pdf',
|
|
'application/octet-stream', // Flutter Web skickar detta för PDF-filer
|
|
];
|
|
|
|
@Controller('receipt-import')
|
|
export class ReceiptImportController {
|
|
constructor(private readonly receiptImportService: ReceiptImportService) {}
|
|
|
|
@HttpCode(200)
|
|
@Post()
|
|
@Throttle({ default: { ttl: 60_000, limit: 20 } })
|
|
@UseInterceptors(
|
|
FileInterceptor('file', {
|
|
storage: memoryStorage(),
|
|
limits: { fileSize: 15 * 1024 * 1024 }, // 15 MB
|
|
}),
|
|
)
|
|
async parseReceipt(
|
|
@UploadedFile() file?: Express.Multer.File,
|
|
@Request() req?: any,
|
|
): Promise<ParsedReceiptItem[]> {
|
|
if (!file?.buffer) {
|
|
throw new BadRequestException('Ingen fil skickades med.');
|
|
}
|
|
if (!ALLOWED_MIMES.includes(file.mimetype)) {
|
|
throw new BadRequestException(
|
|
'Otillåten filtyp. Använd JPEG, PNG, WebP eller PDF.',
|
|
);
|
|
}
|
|
const isPremium = req?.user?.isPremium === true || req?.user?.role === 'admin';
|
|
return this.receiptImportService.parseReceipt(file, isPremium);
|
|
}
|
|
}
|