chore: update flyer import features and resources
- Remove outdated Willys flyer PDF (0001-0008_WIL_V21_ED1pdf.pdf) - Add new Willys flyer PDF (willys_reklamblad.pdf) - Improve offer detection logic in backend flyer-import service - Add offer limit text extraction and sanitization in Flutter UI - Fix Swedish character encoding issues in UI text
This commit is contained in:
@@ -28,12 +28,20 @@ type FlyerParseItem = {
|
||||
reasonCodes: string[];
|
||||
};
|
||||
|
||||
type FlyerParseResponse = {
|
||||
retailer: 'willys';
|
||||
parserVersion: 'v1';
|
||||
items: FlyerParseItem[];
|
||||
warnings: string[];
|
||||
};
|
||||
type FlyerParseResponse = {
|
||||
retailer: 'willys';
|
||||
parserVersion: 'v1';
|
||||
items: FlyerParseItem[];
|
||||
warnings: string[];
|
||||
};
|
||||
|
||||
type ExtractedOfferSignals = {
|
||||
price: number | null;
|
||||
priceUnit: string | null;
|
||||
comparisonPrice: number | null;
|
||||
comparisonUnit: string | null;
|
||||
hasCampaignPattern: boolean;
|
||||
};
|
||||
|
||||
type ProductLite = {
|
||||
id: number;
|
||||
@@ -77,23 +85,28 @@ export class FlyerImportService {
|
||||
productById.set(product.id, product);
|
||||
}
|
||||
|
||||
const items: FlyerImportItem[] = parsed.items.map((item) => {
|
||||
const match = this.matchItem(item, products, aliasToProduct, productById);
|
||||
const offerLimitText = this.extractOfferLimitText(item.offerText);
|
||||
return {
|
||||
flyerItemId: null,
|
||||
rawName: item.rawName,
|
||||
normalizedName: item.normalizedName,
|
||||
category: item.category,
|
||||
price: item.price,
|
||||
priceUnit: item.priceUnit,
|
||||
comparisonPrice: item.comparisonPrice,
|
||||
comparisonUnit: item.comparisonUnit,
|
||||
offerText: item.offerText,
|
||||
isOffer: this.isOfferItem(item),
|
||||
offerLimitText,
|
||||
parseConfidence: item.confidence,
|
||||
parseReasons: item.reasonCodes,
|
||||
const items: FlyerImportItem[] = parsed.items.map((item) => {
|
||||
const match = this.matchItem(item, products, aliasToProduct, productById);
|
||||
const signals = this.extractOfferSignals(item.offerText);
|
||||
const price = item.price ?? signals.price;
|
||||
const priceUnit = this.normalizeUnit(item.priceUnit) ?? signals.priceUnit;
|
||||
const comparisonPrice = item.comparisonPrice ?? signals.comparisonPrice;
|
||||
const comparisonUnit = this.normalizeUnit(item.comparisonUnit) ?? signals.comparisonUnit;
|
||||
const offerLimitText = this.extractOfferLimitText(item.offerText);
|
||||
return {
|
||||
flyerItemId: null,
|
||||
rawName: item.rawName,
|
||||
normalizedName: item.normalizedName,
|
||||
category: item.category,
|
||||
price,
|
||||
priceUnit,
|
||||
comparisonPrice,
|
||||
comparisonUnit,
|
||||
offerText: item.offerText,
|
||||
isOffer: this.isOfferItem(item, signals.hasCampaignPattern),
|
||||
offerLimitText,
|
||||
parseConfidence: item.confidence,
|
||||
parseReasons: item.reasonCodes,
|
||||
matchedProductId: match.product?.id ?? null,
|
||||
matchedProductName: match.product?.name ?? null,
|
||||
matchedVia: match.via,
|
||||
@@ -260,28 +273,103 @@ export class FlyerImportService {
|
||||
return intersection / union;
|
||||
}
|
||||
|
||||
private isOfferItem(item: FlyerParseItem): boolean {
|
||||
return item.price != null || item.comparisonPrice != null || !!item.offerText?.trim();
|
||||
}
|
||||
private isOfferItem(item: FlyerParseItem, hasCampaignPattern: boolean): boolean {
|
||||
return (
|
||||
item.price != null
|
||||
|| item.comparisonPrice != null
|
||||
|| !!item.offerText?.trim()
|
||||
|| hasCampaignPattern
|
||||
);
|
||||
}
|
||||
|
||||
private extractOfferLimitText(offerText: string | null): string | null {
|
||||
if (!offerText) return null;
|
||||
|
||||
const normalized = offerText.replace(/\s+/g, ' ' ).trim();
|
||||
if (!normalized) return null;
|
||||
|
||||
const limitMatch = normalized.match(/(?:max|högst)\s+[^,.;]+(?:hushåll|kund)?/i);
|
||||
if (limitMatch?.[0]) {
|
||||
return limitMatch[0].trim();
|
||||
}
|
||||
|
||||
const householdMatch = normalized.match(/[^,.;]*(?:hushåll|kund)[^,.;]*/i);
|
||||
if (householdMatch?.[0]) {
|
||||
return householdMatch[0].trim();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
const normalized = offerText.replace(/\s+/g, ' ').trim();
|
||||
if (!normalized) return null;
|
||||
|
||||
const limitMatch = normalized.match(
|
||||
/(?:max|högst|begränsat\s+antal)\s+[^,.;]*(?:hushåll|kund|köp)?(?:\s*\/\s*(?:hushåll|kund))?/i,
|
||||
);
|
||||
if (limitMatch?.[0]) {
|
||||
return limitMatch[0].trim();
|
||||
}
|
||||
|
||||
const perCustomerMatch = normalized.match(
|
||||
/[^,.;]*(?:per\s+(?:hushåll|kund)|\/\s*(?:hushåll|kund))[^,.;]*/i,
|
||||
);
|
||||
if (perCustomerMatch?.[0]) {
|
||||
return perCustomerMatch[0].trim();
|
||||
}
|
||||
|
||||
const householdMatch = normalized.match(/[^,.;]*(?:hushåll|kund)[^,.;]*/i);
|
||||
if (householdMatch?.[0]) {
|
||||
return householdMatch[0].trim();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private extractOfferSignals(offerText: string | null): ExtractedOfferSignals {
|
||||
const empty: ExtractedOfferSignals = {
|
||||
price: null,
|
||||
priceUnit: null,
|
||||
comparisonPrice: null,
|
||||
comparisonUnit: null,
|
||||
hasCampaignPattern: false,
|
||||
};
|
||||
|
||||
if (!offerText?.trim()) return empty;
|
||||
|
||||
const normalized = offerText.replace(/\s+/g, ' ').trim().toLowerCase();
|
||||
const campaignPattern = /(\b\d+\s*för\s*\d+[,.:]?\d*\b)|(ta\s*\d+\s*betala\s*för\s*\d+)/i;
|
||||
const priceWithUnit = normalized.match(/(\d{1,3}[:.,]\d{2}|\d{1,3})\s*(?:kr)?\s*\/?\s*(kg|hg|g|l|dl|cl|ml|st|styck|pkt|förp|fp)/i);
|
||||
const priceOnly = normalized.match(/(\d{1,3}[:.,]\d{2}|\d{1,3})\s*kr\b/i);
|
||||
const comparison = normalized.match(
|
||||
/(?:jfr\s*pris|jamforpris|jämförpris|jfr)\s*[:]?\s*(\d{1,3}[:.,]\d{2}|\d{1,3})\s*(?:kr)?\s*\/?\s*(kg|hg|g|l|dl|cl|ml|st|styck|pkt|förp|fp)/i,
|
||||
);
|
||||
|
||||
const signals: ExtractedOfferSignals = {
|
||||
...empty,
|
||||
hasCampaignPattern: campaignPattern.test(normalized),
|
||||
};
|
||||
|
||||
if (priceWithUnit) {
|
||||
signals.price = this.parseSwedishPrice(priceWithUnit[1]);
|
||||
signals.priceUnit = this.normalizeUnit(priceWithUnit[2]);
|
||||
} else if (priceOnly) {
|
||||
signals.price = this.parseSwedishPrice(priceOnly[1]);
|
||||
}
|
||||
|
||||
if (comparison) {
|
||||
signals.comparisonPrice = this.parseSwedishPrice(comparison[1]);
|
||||
signals.comparisonUnit = this.normalizeUnit(comparison[2]);
|
||||
}
|
||||
|
||||
return signals;
|
||||
}
|
||||
|
||||
private parseSwedishPrice(value: string | null | undefined): number | null {
|
||||
if (!value) return null;
|
||||
const normalized = value.trim().replace(':', '.').replace(',', '.');
|
||||
const parsed = Number.parseFloat(normalized);
|
||||
if (!Number.isFinite(parsed)) return null;
|
||||
return parsed;
|
||||
}
|
||||
|
||||
private normalizeUnit(unit: string | null | undefined): string | null {
|
||||
if (!unit) return null;
|
||||
const cleaned = unit.trim().toLowerCase().replace(/\./g, '');
|
||||
if (!cleaned) return null;
|
||||
|
||||
if (cleaned === 'styck') return 'st';
|
||||
if (cleaned === 'fp' || cleaned === 'forp' || cleaned === 'förp' || cleaned === 'pkt') {
|
||||
return 'pkt';
|
||||
}
|
||||
|
||||
const allowed = new Set(['kg', 'hg', 'g', 'l', 'dl', 'cl', 'ml', 'st', 'pkt']);
|
||||
return allowed.has(cleaned) ? cleaned : cleaned;
|
||||
}
|
||||
|
||||
private async parseViaImporter(file: Express.Multer.File): Promise<FlyerParseResponse> {
|
||||
const form = new FormData();
|
||||
|
||||
Reference in New Issue
Block a user