29 lines
890 B
TypeScript
29 lines
890 B
TypeScript
import type { ProductTableGroup, ProductTableRow } from '@/types/product';
|
|
|
|
export function groupProductRows(rows: ProductTableRow[]): ProductTableGroup[] {
|
|
const groups: ProductTableGroup[] = [];
|
|
const indexByProductId = new Map<number, number>();
|
|
|
|
for (const row of rows) {
|
|
const existingIndex = indexByProductId.get(row.product_id);
|
|
|
|
if (existingIndex === undefined) {
|
|
indexByProductId.set(row.product_id, groups.length);
|
|
groups.push({
|
|
product_id: row.product_id,
|
|
product_row_number: row.product_row_number,
|
|
product_name: row.product_name,
|
|
categories: row.categories,
|
|
is_active: row.is_active,
|
|
variants: [row],
|
|
});
|
|
|
|
continue;
|
|
}
|
|
|
|
groups[existingIndex].variants.push(row);
|
|
}
|
|
|
|
return groups;
|
|
}
|