refactor: where conditions
This commit is contained in:
parent
916ea84565
commit
114f008934
2 changed files with 53 additions and 60 deletions
|
|
@ -184,12 +184,24 @@ export class CustomerBranchController extends Controller {
|
|||
: {
|
||||
registeredBranch: {
|
||||
OR: [
|
||||
{ user: { some: { userId: req.user.sub } } },
|
||||
{
|
||||
user: { some: { userId: req.user.sub } },
|
||||
},
|
||||
{
|
||||
branch: globalAllow(req.user)
|
||||
? { some: { user: { some: { userId: req.user.sub } } } }
|
||||
: undefined,
|
||||
},
|
||||
{
|
||||
headOffice: globalAllow(req.user)
|
||||
? { branch: { some: { user: { some: { userId: req.user.sub } } } } }
|
||||
: undefined,
|
||||
},
|
||||
{
|
||||
headOffice: globalAllow(req.user)
|
||||
? { user: { some: { userId: req.user.sub } } }
|
||||
: undefined,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import HttpStatus from "../interfaces/http-status";
|
|||
import HttpError from "../interfaces/http-error";
|
||||
import minio, { presignedGetObjectIfExist } from "../services/minio";
|
||||
import { isSystem } from "../utils/keycloak";
|
||||
import { filterStatus } from "../services/prisma";
|
||||
|
||||
if (!process.env.MINIO_BUCKET) {
|
||||
throw Error("Require MinIO bucket.");
|
||||
|
|
@ -234,29 +235,52 @@ export class EmployeeController extends Controller {
|
|||
@Get("stats/gender")
|
||||
@Security("keycloak")
|
||||
async getEmployeeStatsGender(
|
||||
@Request() req: RequestWithUser,
|
||||
@Query() customerBranchId?: string,
|
||||
@Query() status?: Status,
|
||||
@Query() query: string = "",
|
||||
) {
|
||||
const filterStatus = (val?: Status) => {
|
||||
if (!val) return {};
|
||||
|
||||
return val !== Status.CREATED && val !== Status.ACTIVE
|
||||
? { status: val }
|
||||
: { OR: [{ status: Status.CREATED }, { status: Status.ACTIVE }] };
|
||||
};
|
||||
|
||||
return await prisma.employee
|
||||
.groupBy({
|
||||
_count: true,
|
||||
by: ["gender"],
|
||||
where: {
|
||||
OR: [
|
||||
{ firstName: { contains: query }, customerBranchId, ...filterStatus(status) },
|
||||
{ firstNameEN: { contains: query }, customerBranchId, ...filterStatus(status) },
|
||||
{ lastName: { contains: query }, customerBranchId, ...filterStatus(status) },
|
||||
{ lastNameEN: { contains: query }, customerBranchId, ...filterStatus(status) },
|
||||
{ firstName: { contains: query } },
|
||||
{ firstNameEN: { contains: query } },
|
||||
{ lastName: { contains: query } },
|
||||
{ lastNameEN: { contains: query } },
|
||||
],
|
||||
AND: {
|
||||
...filterStatus(status),
|
||||
customerBranchId,
|
||||
customerBranch: {
|
||||
customer: isSystem(req.user)
|
||||
? undefined
|
||||
: {
|
||||
registeredBranch: {
|
||||
OR: [
|
||||
{
|
||||
user: { some: { userId: req.user.sub } },
|
||||
},
|
||||
{
|
||||
branch: { some: { user: { some: { userId: req.user.sub } } } },
|
||||
},
|
||||
{
|
||||
headOffice: globalAllow(req.user)
|
||||
? { branch: { some: { user: { some: { userId: req.user.sub } } } } }
|
||||
: undefined,
|
||||
},
|
||||
{
|
||||
headOffice: globalAllow(req.user)
|
||||
? { user: { some: { userId: req.user.sub } } }
|
||||
: undefined,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
.then((res) =>
|
||||
|
|
@ -279,14 +303,6 @@ export class EmployeeController extends Controller {
|
|||
@Query() page: number = 1,
|
||||
@Query() pageSize: number = 30,
|
||||
) {
|
||||
const filterStatus = (val?: Status) => {
|
||||
if (!val) return {};
|
||||
|
||||
return val !== Status.CREATED && val !== Status.ACTIVE
|
||||
? { status: val }
|
||||
: { OR: [{ status: Status.CREATED }, { status: Status.ACTIVE }] };
|
||||
};
|
||||
|
||||
const where = {
|
||||
OR: [
|
||||
{ firstName: { contains: query } },
|
||||
|
|
@ -350,16 +366,7 @@ export class EmployeeController extends Controller {
|
|||
]);
|
||||
|
||||
return {
|
||||
result: await Promise.all(
|
||||
result.map(async (v) => ({
|
||||
...v,
|
||||
profileImageUrl: await presignedGetObjectIfExist(
|
||||
MINIO_BUCKET,
|
||||
imageLocation(v.id),
|
||||
12 * 60 * 60,
|
||||
),
|
||||
})),
|
||||
),
|
||||
result,
|
||||
page,
|
||||
pageSize,
|
||||
total,
|
||||
|
|
@ -580,18 +587,7 @@ export class EmployeeController extends Controller {
|
|||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
||||
return Object.assign(record, {
|
||||
profileImageUrl: await presignedGetObjectIfExist(
|
||||
MINIO_BUCKET,
|
||||
imageLocation(record.id),
|
||||
12 * 60 * 60,
|
||||
),
|
||||
profileImageUploadUrl: await minio.presignedPutObject(
|
||||
MINIO_BUCKET,
|
||||
imageLocation(record.id),
|
||||
12 * 60 * 60,
|
||||
),
|
||||
});
|
||||
return record;
|
||||
}
|
||||
|
||||
@Put("{employeeId}")
|
||||
|
|
@ -896,18 +892,7 @@ export class EmployeeController extends Controller {
|
|||
})),
|
||||
});
|
||||
|
||||
return Object.assign(record, {
|
||||
profileImageUrl: await presignedGetObjectIfExist(
|
||||
MINIO_BUCKET,
|
||||
imageLocation(record.id),
|
||||
12 * 60 * 60,
|
||||
),
|
||||
profileImageUploadUrl: await minio.presignedPutObject(
|
||||
MINIO_BUCKET,
|
||||
imageLocation(record.id),
|
||||
12 * 60 * 60,
|
||||
),
|
||||
});
|
||||
return record;
|
||||
}
|
||||
|
||||
@Delete("{employeeId}")
|
||||
|
|
@ -998,11 +983,7 @@ export class EmployeeAttachmentController extends Controller {
|
|||
}
|
||||
|
||||
@Get("{filename}")
|
||||
async getAttachment(
|
||||
@Request() req: RequestWithUser,
|
||||
@Path() employeeId: string,
|
||||
@Path() filename: string,
|
||||
) {
|
||||
async getAttachment(@Path() employeeId: string, @Path() filename: string) {
|
||||
const record = await prisma.employee.findFirst({
|
||||
where: { id: employeeId },
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue