feat: list by headOfficeId & get with contact

This commit is contained in:
Methapon2001 2024-04-11 14:28:44 +07:00
parent ed2c7daabb
commit 30acdfdd32

View file

@ -18,6 +18,13 @@ import prisma from "../db";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user"; import { RequestWithUser } from "../interfaces/user";
import minio from "../services/minio";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
}
const MINIO_BUCKET = process.env.MINIO_BUCKET;
type BranchCreate = { type BranchCreate = {
status?: Status; status?: Status;
@ -104,6 +111,7 @@ export class BranchController extends Controller {
async getBranch( async getBranch(
@Query() zipCode?: string, @Query() zipCode?: string,
@Query() filter?: "head" | "sub", @Query() filter?: "head" | "sub",
@Query() headOfficeId?: string,
@Query() tree?: boolean, @Query() tree?: boolean,
@Query() query: string = "", @Query() query: string = "",
@Query() page: number = 1, @Query() page: number = 1,
@ -111,8 +119,8 @@ export class BranchController extends Controller {
) { ) {
const where = { const where = {
AND: { AND: {
headOfficeId: filter === "head" || tree ? null : undefined, headOfficeId: headOfficeId ?? (filter === "head" || tree ? null : undefined),
NOT: { headOfficeId: filter === "sub" ? null : undefined }, NOT: { headOfficeId: filter === "sub" && !headOfficeId ? null : undefined },
}, },
OR: [ OR: [
{ nameEN: { contains: query }, zipCode }, { nameEN: { contains: query }, zipCode },
@ -147,7 +155,11 @@ export class BranchController extends Controller {
} }
@Get("{branchId}") @Get("{branchId}")
async getBranchById(@Path() branchId: string, @Query() includeSubBranch?: boolean) { async getBranchById(
@Path() branchId: string,
@Query() includeSubBranch?: boolean,
@Query() includeContact?: boolean,
) {
const record = await prisma.branch.findFirst({ const record = await prisma.branch.findFirst({
include: { include: {
province: true, province: true,
@ -160,6 +172,7 @@ export class BranchController extends Controller {
subDistrict: true, subDistrict: true,
}, },
}, },
contact: includeContact,
}, },
where: { id: branchId }, where: { id: branchId },
}); });
@ -168,7 +181,21 @@ export class BranchController extends Controller {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found");
} }
return record; return {
...record,
contact: record.contact
? await Promise.all(
record.contact.map(async (v) =>
Object.assign(v, {
qrCodeImageUrl: await minio.presignedGetObject(
MINIO_BUCKET,
`branch/contact-${record.id}`,
),
}),
),
)
: undefined,
};
} }
@Post() @Post()