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 {
@Get("stats")
@Security("keycloak")
async getStats() {
async getStats(@Request() req: RequestWithUser) {
const list = await prisma.branch.groupBy({
_count: true,
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>>(

View file

@ -177,14 +177,28 @@ function imageLocation(id: string) {
export class UserController extends Controller {
@Get("type-stats")
@Security("keycloak")
async getUserTypeStats() {
async getUserTypeStats(@Request() req: RequestWithUser) {
const list = await prisma.user.groupBy({
by: "userType",
_count: true,
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,
},
],
},
},
},
},
});