Files
recipe-app/backend/dist/receipt-import/receipt-import.controller.js
T
Nils-Johan Gynther a68a0ca86f
Test Suite / test (24.15.0) (push) Has been cancelled
feat: add unit mapping functionality
- Added new API path for unit mappings in `api_paths.dart`.
- Implemented `upsertUnitMapping` method in `ImportRepository` to handle unit mapping creation.
- Updated `ReceiptImportTab` to learn and save unit mappings during receipt import.
- Created DTO for unit mapping with validation in `create-unit-mapping.dto.ts`.
- Added SQL migration for `UnitMapping` table creation with necessary constraints.
2026-05-07 10:00:42 +02:00

98 lines
4.6 KiB
JavaScript

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReceiptImportController = void 0;
const common_1 = require("@nestjs/common");
const throttler_1 = require("@nestjs/throttler");
const platform_express_1 = require("@nestjs/platform-express");
const multer_1 = require("multer");
const receipt_import_service_1 = require("./receipt-import.service");
const create_unit_mapping_dto_1 = require("./dto/create-unit-mapping.dto");
const passport_1 = require("@nestjs/passport");
const ALLOWED_MIMES = [
'image/jpeg',
'image/png',
'image/webp',
'image/heic',
'image/heif',
'application/pdf',
'application/octet-stream',
];
let ReceiptImportController = class ReceiptImportController {
constructor(receiptImportService) {
this.receiptImportService = receiptImportService;
}
async parseReceipt(file, req) {
if (!file?.buffer) {
throw new common_1.BadRequestException('Ingen fil skickades med.');
}
if (!ALLOWED_MIMES.includes(file.mimetype)) {
throw new common_1.BadRequestException('Otillåten filtyp. Använd JPEG, PNG, WebP eller PDF.');
}
const isPremium = req?.user?.isPremium === true || req?.user?.role === 'admin';
const userId = typeof req?.user?.id === 'number' ? req.user.id : undefined;
return this.receiptImportService.parseReceipt(file, isPremium, userId);
}
async refreshCategories() {
await this.receiptImportService.loadCategories();
return { message: 'Kategorier har uppdaterats.' };
}
async upsertUnitMapping(dto, req) {
const userId = typeof req?.user?.id === 'number'
? req.user.id
: typeof req?.user?.userId === 'number'
? req.user.userId
: undefined;
if (!userId) {
throw new common_1.BadRequestException('Kunde inte identifiera användaren.');
}
return this.receiptImportService.upsertUnitMapping(userId, dto.productId, dto.originalUnit, dto.preferredUnit);
}
};
exports.ReceiptImportController = ReceiptImportController;
__decorate([
(0, common_1.HttpCode)(200),
(0, common_1.Post)(),
(0, throttler_1.Throttle)({ default: { ttl: 60_000, limit: 20 } }),
(0, common_1.UseInterceptors)((0, platform_express_1.FileInterceptor)('file', {
storage: (0, multer_1.memoryStorage)(),
limits: { fileSize: 15 * 1024 * 1024 },
})),
__param(0, (0, common_1.UploadedFile)()),
__param(1, (0, common_1.Request)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], ReceiptImportController.prototype, "parseReceipt", null);
__decorate([
(0, common_1.Post)('refresh-categories'),
(0, common_1.UseGuards)((0, passport_1.AuthGuard)('jwt')),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], ReceiptImportController.prototype, "refreshCategories", null);
__decorate([
(0, common_1.Post)('unit-mappings'),
(0, common_1.UseGuards)((0, passport_1.AuthGuard)('jwt')),
__param(0, (0, common_1.Body)()),
__param(1, (0, common_1.Request)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [create_unit_mapping_dto_1.CreateUnitMappingDto, Object]),
__metadata("design:returntype", Promise)
], ReceiptImportController.prototype, "upsertUnitMapping", null);
exports.ReceiptImportController = ReceiptImportController = __decorate([
(0, common_1.Controller)('receipt-import'),
__metadata("design:paramtypes", [receipt_import_service_1.ReceiptImportService])
], ReceiptImportController);
//# sourceMappingURL=receipt-import.controller.js.map