- 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.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
HttpCode,
|
||||
Post,
|
||||
@@ -13,6 +14,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 { CreateUnitMappingDto } from './dto/create-unit-mapping.dto';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
const ALLOWED_MIMES = [
|
||||
@@ -61,4 +63,28 @@ export class ReceiptImportController {
|
||||
await this.receiptImportService.loadCategories();
|
||||
return { message: 'Kategorier har uppdaterats.' };
|
||||
}
|
||||
|
||||
@Post('unit-mappings')
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
async upsertUnitMapping(
|
||||
@Body() dto: CreateUnitMappingDto,
|
||||
@Request() req?: any,
|
||||
) {
|
||||
const userId =
|
||||
typeof req?.user?.id === 'number'
|
||||
? req.user.id
|
||||
: typeof req?.user?.userId === 'number'
|
||||
? req.user.userId
|
||||
: undefined;
|
||||
if (!userId) {
|
||||
throw new BadRequestException('Kunde inte identifiera användaren.');
|
||||
}
|
||||
|
||||
return this.receiptImportService.upsertUnitMapping(
|
||||
userId,
|
||||
dto.productId,
|
||||
dto.originalUnit,
|
||||
dto.preferredUnit,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user