feat: implement receipt alias functionality with CRUD operations and integrate with receipt import
This commit is contained in:
@@ -8,6 +8,7 @@ import { QuickImportModule } from './quick-import/quick-import.module';
|
||||
import { PantryModule } from './pantry/pantry.module';
|
||||
import { MealPlanModule } from './meal-plan/meal-plan.module';
|
||||
import { ReceiptImportModule } from './receipt-import/receipt-import.module';
|
||||
import { ReceiptAliasModule } from './receipt-alias/receipt-alias.module';
|
||||
|
||||
|
||||
@Module({
|
||||
@@ -21,6 +22,7 @@ import { ReceiptImportModule } from './receipt-import/receipt-import.module';
|
||||
PantryModule,
|
||||
MealPlanModule,
|
||||
ReceiptImportModule,
|
||||
ReceiptAliasModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { IsInt, IsString, MinLength } from 'class-validator';
|
||||
|
||||
export class CreateReceiptAliasDto {
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
receiptName!: string;
|
||||
|
||||
@IsInt()
|
||||
productId!: number;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post } from '@nestjs/common';
|
||||
import { ReceiptAliasService } from './receipt-alias.service';
|
||||
import { CreateReceiptAliasDto } from './dto/create-receipt-alias.dto';
|
||||
|
||||
@Controller('receipt-aliases')
|
||||
export class ReceiptAliasController {
|
||||
constructor(private readonly receiptAliasService: ReceiptAliasService) {}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.receiptAliasService.findAll();
|
||||
}
|
||||
|
||||
@Post()
|
||||
upsert(@Body() dto: CreateReceiptAliasDto) {
|
||||
return this.receiptAliasService.upsert(dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.receiptAliasService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ReceiptAliasController } from './receipt-alias.controller';
|
||||
import { ReceiptAliasService } from './receipt-alias.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [ReceiptAliasController],
|
||||
providers: [ReceiptAliasService],
|
||||
exports: [ReceiptAliasService],
|
||||
})
|
||||
export class ReceiptAliasModule {}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateReceiptAliasDto } from './dto/create-receipt-alias.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ReceiptAliasService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
findAll() {
|
||||
return this.prisma.receiptAlias.findMany({
|
||||
include: { product: { select: { id: true, name: true, canonicalName: true } } },
|
||||
orderBy: { receiptName: 'asc' },
|
||||
});
|
||||
}
|
||||
|
||||
async upsert(dto: CreateReceiptAliasDto) {
|
||||
const normalized = dto.receiptName.toLowerCase().trim();
|
||||
return this.prisma.receiptAlias.upsert({
|
||||
where: { receiptName: normalized },
|
||||
create: { receiptName: normalized, productId: dto.productId },
|
||||
update: { productId: dto.productId },
|
||||
});
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return this.prisma.receiptAlias.delete({ where: { id } });
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,10 @@ export interface ParsedReceiptItem {
|
||||
quantity: number;
|
||||
unit: string;
|
||||
price?: number | null;
|
||||
// alias-match: säker, användaren slipper bekräfta
|
||||
matchedProductId?: number;
|
||||
matchedProductName?: string;
|
||||
// ordbaserad match: förslag, kräver bekräftelse
|
||||
suggestedProductId?: number;
|
||||
suggestedProductName?: string;
|
||||
}
|
||||
|
||||
@@ -131,8 +131,16 @@ export class ReceiptImportService {
|
||||
): Promise<ParsedReceiptItem[]> {
|
||||
if (!response.ok) {
|
||||
const err = await response.text();
|
||||
this.logger.error(`Mistral API svarade ${response.status}: ${err}`);
|
||||
throw new ServiceUnavailableException('Mistral API returnerade ett fel — kontrollera API-nyckeln');
|
||||
this.logger.error(`Mistral API svarade ${response.status} (${source}): ${err}`);
|
||||
const hint =
|
||||
response.status === 401
|
||||
? 'Ogiltig API-nyckel (401)'
|
||||
: response.status === 429
|
||||
? 'För många förfrågningar — försök igen om en stund (429)'
|
||||
: `HTTP ${response.status}`;
|
||||
throw new ServiceUnavailableException(
|
||||
`Mistral API returnerade ett fel: ${hint}`,
|
||||
);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as {
|
||||
@@ -156,35 +164,61 @@ export class ReceiptImportService {
|
||||
private async matchProducts(
|
||||
items: ParsedReceiptItem[],
|
||||
): Promise<ParsedReceiptItem[]> {
|
||||
const products = await this.prisma.product.findMany({
|
||||
select: { id: true, name: true, canonicalName: true },
|
||||
});
|
||||
// Hämta alias och produkter parallellt
|
||||
const [aliases, products] = await Promise.all([
|
||||
this.prisma.receiptAlias.findMany({
|
||||
select: { receiptName: true, productId: true, product: { select: { id: true, name: true, canonicalName: true } } },
|
||||
}),
|
||||
this.prisma.product.findMany({
|
||||
where: { isActive: true },
|
||||
select: { id: true, name: true, canonicalName: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
return items.map((item) => {
|
||||
const raw = (item.rawName ?? '').toLowerCase().trim();
|
||||
if (!raw) return item;
|
||||
|
||||
// Exakt matchning först
|
||||
let match = products.find((p) => {
|
||||
const n = (p.canonicalName ?? p.name).toLowerCase();
|
||||
return n === raw || p.name.toLowerCase() === raw;
|
||||
});
|
||||
|
||||
// Delvis matchning
|
||||
if (!match) {
|
||||
match = products.find((p) => {
|
||||
const n = (p.canonicalName ?? p.name).toLowerCase();
|
||||
return n.includes(raw) || raw.includes(n);
|
||||
});
|
||||
// 1. Alias-match (säker, användaren behöver inte bekräfta)
|
||||
const alias = aliases.find((a) => a.receiptName === raw);
|
||||
if (alias) {
|
||||
return {
|
||||
...item,
|
||||
matchedProductId: alias.product.id,
|
||||
matchedProductName: alias.product.canonicalName ?? alias.product.name,
|
||||
};
|
||||
}
|
||||
|
||||
// 2. Ordbaserad matchning (förslag, kräver bekräftelse)
|
||||
const suggestion = this.findWordMatch(raw, products);
|
||||
return {
|
||||
...item,
|
||||
matchedProductId: match?.id,
|
||||
matchedProductName: match
|
||||
? (match.canonicalName ?? match.name)
|
||||
suggestedProductId: suggestion?.id,
|
||||
suggestedProductName: suggestion
|
||||
? (suggestion.canonicalName ?? suggestion.name)
|
||||
: undefined,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private findWordMatch(
|
||||
raw: string,
|
||||
products: { id: number; name: string; canonicalName: string | null }[],
|
||||
): { id: number; name: string; canonicalName: string | null } | undefined {
|
||||
// Dela upp kvittonamnet i ord (min 3 tecken)
|
||||
const rawWords = raw.split(/[\s\-_]+/).filter((w) => w.length >= 3);
|
||||
if (rawWords.length === 0) return undefined;
|
||||
|
||||
// Fortsätt med att hitta produkter där ett produktnamn-ord finns i kvittonamnet
|
||||
// Exempel: produktord "ost" finns i kvittoord "prästost", "herrgårdsost", "brieost"
|
||||
return products.find((p) => {
|
||||
const productWords = (p.canonicalName ?? p.name)
|
||||
.toLowerCase()
|
||||
.split(/[\s\-_]+/)
|
||||
.filter((w) => w.length >= 3);
|
||||
return productWords.some((pw) =>
|
||||
rawWords.some((rw) => rw.includes(pw) || pw.includes(rw)),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user