56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import prisma from "@/lib/prisma";
|
|
import { AnalysisData } from "../types";
|
|
|
|
export const formatBrandStats = (userAnalysis: AnalysisData[]) => {
|
|
const countedProductIds = new Set<number>();
|
|
|
|
const brandCounts = userAnalysis.reduce(
|
|
(acc: Record<string, number>, analysis) => {
|
|
const productId = analysis.product?.productId;
|
|
const rawBrand = analysis.product?.brandName || "Unknown";
|
|
const reviewCount = analysis.product?.reviewCount || 0;
|
|
|
|
if (productId && countedProductIds.has(productId)) {
|
|
return acc;
|
|
}
|
|
|
|
if (productId) {
|
|
countedProductIds.add(productId);
|
|
}
|
|
|
|
const formattedBrand = rawBrand
|
|
.trim()
|
|
.toLowerCase()
|
|
.replace(/\b\w/g, (char: any) => char.toUpperCase());
|
|
|
|
if (!acc[formattedBrand]) {
|
|
acc[formattedBrand] = 0;
|
|
}
|
|
|
|
acc[formattedBrand] += reviewCount;
|
|
|
|
return acc;
|
|
},
|
|
{},
|
|
);
|
|
|
|
const formattedBrands = Object.entries(brandCounts).map(([name, count]) => ({
|
|
name,
|
|
count,
|
|
}));
|
|
|
|
formattedBrands.sort((a, b) => b.count - a.count);
|
|
|
|
return formattedBrands;
|
|
};
|
|
|
|
export const getBrandId = async (brandName: string) => {
|
|
const response = await fetch(
|
|
`/api/brand?brandName=${encodeURIComponent(brandName)}`,
|
|
);
|
|
if (!response.ok) return null;
|
|
|
|
const data = await response.json();
|
|
return data.brandId;
|
|
};
|