feat(origin-goods): include merged secondary references in tree stats
This commit is contained in:
@@ -77,4 +77,69 @@ describe('OriginGoodsService', () => {
|
||||
expect(result.total).toBe(0);
|
||||
expect(result.items.length).toBe(0);
|
||||
});
|
||||
|
||||
describe('getTree merged references', () => {
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
function findOgNode(
|
||||
treeResponse: { tree: any[] },
|
||||
ogId: string,
|
||||
): { configuredCount: number; configuredCountries: string[] } {
|
||||
let found: { configuredCount: number; configuredCountries: string[] } | null = null;
|
||||
const walk = (nodes: any[]) => {
|
||||
for (const n of nodes) {
|
||||
const hit = (n.originGoods ?? []).find((o: any) => o.id === ogId);
|
||||
if (hit) {
|
||||
found = hit;
|
||||
return;
|
||||
}
|
||||
if (n.children?.length) walk(n.children);
|
||||
}
|
||||
};
|
||||
walk(treeResponse.tree);
|
||||
if (!found) throw new Error(`og node ${ogId} not found in tree`);
|
||||
return found;
|
||||
}
|
||||
|
||||
it('counts secondary references as configured', async () => {
|
||||
const sdsCat = `tree-cat-${stamp}`;
|
||||
await prisma.originGood.createMany({
|
||||
data: [
|
||||
{ sdsGoodId: `tree-a-${stamp}`, goodName: `Tree A ${stamp}`, sdsCategoryId: sdsCat },
|
||||
{ sdsGoodId: `tree-b-${stamp}`, goodName: `Tree B ${stamp}`, sdsCategoryId: sdsCat },
|
||||
],
|
||||
});
|
||||
createdSds.push(`tree-a-${stamp}`, `tree-b-${stamp}`);
|
||||
const originA = await prisma.originGood.findUniqueOrThrow({ where: { sdsGoodId: `tree-a-${stamp}` } });
|
||||
const originB = await prisma.originGood.findUniqueOrThrow({ where: { sdsGoodId: `tree-b-${stamp}` } });
|
||||
|
||||
const cat = await prisma.category.create({
|
||||
data: { categoryName: `Tree Cat ${stamp}`, sdsCategoryId: sdsCat },
|
||||
});
|
||||
const country = await prisma.country.create({
|
||||
data: { countryName: `Tree Country ${stamp}` },
|
||||
});
|
||||
const good = await prisma.good.create({
|
||||
data: {
|
||||
goodName: `Tree Good ${stamp}`,
|
||||
originGoodId: originA.id,
|
||||
countryId: country.id,
|
||||
categoryId: cat.id,
|
||||
},
|
||||
});
|
||||
await prisma.goodOriginGood.create({
|
||||
data: { goodId: good.id, originGoodId: originB.id },
|
||||
});
|
||||
|
||||
try {
|
||||
const tree = await service.getTree();
|
||||
const nodeB = findOgNode(tree, originB.id.toString());
|
||||
expect(nodeB.configuredCount).toBeGreaterThanOrEqual(1);
|
||||
expect(nodeB.configuredCountries).toContain(`Tree Country ${stamp}`);
|
||||
} finally {
|
||||
await prisma.good.delete({ where: { id: good.id } }).catch(() => undefined);
|
||||
await prisma.country.delete({ where: { id: country.id } }).catch(() => undefined);
|
||||
await prisma.category.delete({ where: { id: cat.id } }).catch(() => undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user