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 HttpStatus from "../interfaces/http-status";
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 = {
status?: Status;
@ -104,6 +111,7 @@ export class BranchController extends Controller {
async getBranch(
@Query() zipCode?: string,
@Query() filter?: "head" | "sub",
@Query() headOfficeId?: string,
@Query() tree?: boolean,
@Query() query: string = "",
@Query() page: number = 1,
@ -111,8 +119,8 @@ export class BranchController extends Controller {
) {
const where = {
AND: {
headOfficeId: filter === "head" || tree ? null : undefined,
NOT: { headOfficeId: filter === "sub" ? null : undefined },
headOfficeId: headOfficeId ?? (filter === "head" || tree ? null : undefined),
NOT: { headOfficeId: filter === "sub" && !headOfficeId ? null : undefined },
},
OR: [
{ nameEN: { contains: query }, zipCode },
@ -147,7 +155,11 @@ export class BranchController extends Controller {
}
@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({
include: {
province: true,
@ -160,6 +172,7 @@ export class BranchController extends Controller {
subDistrict: true,
},
},
contact: includeContact,
},
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");
}
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()