feat: count stats
This commit is contained in:
parent
097e2df4fb
commit
277860805b
1 changed files with 38 additions and 25 deletions
|
|
@ -107,10 +107,7 @@ export class BranchController extends Controller {
|
||||||
@Get("stats")
|
@Get("stats")
|
||||||
@Security("keycloak")
|
@Security("keycloak")
|
||||||
async getStats(@Request() req: RequestWithUser) {
|
async getStats(@Request() req: RequestWithUser) {
|
||||||
const list = await prisma.branch.groupBy({
|
const where = {
|
||||||
_count: true,
|
|
||||||
by: "isHeadOffice",
|
|
||||||
where: {
|
|
||||||
AND: isSystem(req.user)
|
AND: isSystem(req.user)
|
||||||
? undefined
|
? undefined
|
||||||
: [
|
: [
|
||||||
|
|
@ -123,16 +120,32 @@ export class BranchController extends Controller {
|
||||||
: undefined,
|
: undefined,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
};
|
||||||
});
|
|
||||||
|
|
||||||
return list.reduce<Record<"hq" | "br", number>>(
|
const [hq, br, virtual] = await prisma.$transaction([
|
||||||
(a, c) => {
|
prisma.branch.count({
|
||||||
a[c.isHeadOffice ? "hq" : "br"] = c._count;
|
where: {
|
||||||
return a;
|
headOfficeId: null,
|
||||||
|
...where,
|
||||||
},
|
},
|
||||||
{ hq: 0, br: 0 },
|
}),
|
||||||
);
|
prisma.branch.count({
|
||||||
|
where: {
|
||||||
|
headOfficeId: { not: null },
|
||||||
|
virtual: false,
|
||||||
|
...where,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
prisma.branch.count({
|
||||||
|
where: {
|
||||||
|
headOfficeId: { not: null },
|
||||||
|
virtual: true,
|
||||||
|
...where,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return { hq, br, virtual };
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("user-stats")
|
@Get("user-stats")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue