fix: stats not take scope into account

This commit is contained in:
Methapon Metanipat 2024-09-04 17:01:18 +07:00
parent 91d741f363
commit 2cd92b2234
2 changed files with 33 additions and 5 deletions

View file

@ -119,10 +119,24 @@ function mapImageLoc(id: string) {
export class BranchController extends Controller { export class BranchController extends Controller {
@Get("stats") @Get("stats")
@Security("keycloak") @Security("keycloak")
async getStats() { async getStats(@Request() req: RequestWithUser) {
const list = await prisma.branch.groupBy({ const list = await prisma.branch.groupBy({
_count: true, _count: true,
by: "isHeadOffice", by: "isHeadOffice",
where: {
AND: isSystem(req.user)
? undefined
: [
{
user: { some: { userId: req.user.sub } },
},
{
headOffice: globalAllow(req.user)
? { user: { some: { userId: req.user.sub } } }
: undefined,
},
],
},
}); });
return list.reduce<Record<"hq" | "br", number>>( return list.reduce<Record<"hq" | "br", number>>(

View file

@ -177,14 +177,28 @@ function imageLocation(id: string) {
export class UserController extends Controller { export class UserController extends Controller {
@Get("type-stats") @Get("type-stats")
@Security("keycloak") @Security("keycloak")
async getUserTypeStats() { async getUserTypeStats(@Request() req: RequestWithUser) {
const list = await prisma.user.groupBy({ const list = await prisma.user.groupBy({
by: "userType", by: "userType",
_count: true, _count: true,
where: { where: {
AND: { userRole: { not: "system" },
userRole: { not: "system" }, branch: isSystem(req.user)
}, ? undefined
: {
some: {
branch: {
OR: [
{ user: { some: { userId: req.user.sub } } },
{
headOffice: !globalAllow(req.user)
? { user: { some: { userId: req.user.sub } } }
: undefined,
},
],
},
},
},
}, },
}); });