-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filter by partnerId, payoutId & status in useSalesCount
- Loading branch information
1 parent
0359f33
commit d376467
Showing
6 changed files
with
92 additions
and
73 deletions.
There are no files selected for viewing
75 changes: 42 additions & 33 deletions
75
apps/web/app/api/programs/[programId]/sales/count/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,51 @@ | ||
import { getProgramOrThrow } from "@/lib/api/programs/get-program"; | ||
import { withWorkspace } from "@/lib/auth"; | ||
import { prisma } from "@/lib/prisma"; | ||
import { getSalesCountQuerySchema } from "@/lib/zod/schemas/partners"; | ||
import { SaleStatus } from "@prisma/client"; | ||
import { NextResponse } from "next/server"; | ||
|
||
// GET /api/programs/[programId]/sales/count | ||
export const GET = withWorkspace(async ({ workspace, params }) => { | ||
const { programId } = params; | ||
export const GET = withWorkspace( | ||
async ({ workspace, params, searchParams }) => { | ||
const { programId } = params; | ||
|
||
await getProgramOrThrow({ | ||
workspaceId: workspace.id, | ||
programId, | ||
}); | ||
|
||
const salesCount = await prisma.sale.groupBy({ | ||
by: ["status"], | ||
where: { | ||
await getProgramOrThrow({ | ||
workspaceId: workspace.id, | ||
programId, | ||
}, | ||
_count: true, | ||
}); | ||
|
||
const counts = salesCount.reduce( | ||
(acc, p) => { | ||
acc[p.status] = p._count; | ||
return acc; | ||
}, | ||
{} as Record<SaleStatus | "all", number>, | ||
); | ||
|
||
// fill in missing statuses with 0 | ||
Object.values(SaleStatus).forEach((status) => { | ||
if (!(status in counts)) { | ||
counts[status] = 0; | ||
} | ||
}); | ||
|
||
counts.all = salesCount.reduce((acc, p) => acc + p._count, 0); | ||
|
||
return NextResponse.json(counts); | ||
}); | ||
}); | ||
|
||
const { status, partnerId, payoutId } = | ||
getSalesCountQuerySchema.parse(searchParams); | ||
|
||
const salesCount = await prisma.sale.groupBy({ | ||
by: ["status"], | ||
where: { | ||
programId, | ||
status, | ||
partnerId, | ||
payoutId, | ||
}, | ||
_count: true, | ||
}); | ||
|
||
const counts = salesCount.reduce( | ||
(acc, p) => { | ||
acc[p.status] = p._count; | ||
return acc; | ||
}, | ||
{} as Record<SaleStatus | "all", number>, | ||
); | ||
|
||
// fill in missing statuses with 0 | ||
Object.values(SaleStatus).forEach((status) => { | ||
if (!(status in counts)) { | ||
counts[status] = 0; | ||
} | ||
}); | ||
|
||
counts.all = salesCount.reduce((acc, p) => acc + p._count, 0); | ||
|
||
return NextResponse.json(counts); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters