refactor: update branch endpoint

This commit is contained in:
Methapon2001 2024-04-17 13:42:11 +07:00
parent eaa41f49be
commit 63293b671b
2 changed files with 15 additions and 10 deletions

View file

@ -14,17 +14,10 @@ import {
} from "tsoa";
import prisma from "../db";
import minio from "../services/minio";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
}
const MINIO_BUCKET = process.env.MINIO_BUCKET;
type BranchContactCreate = {
telephoneNo: string;
};

View file

@ -35,7 +35,8 @@ type BranchCreate = {
address: string;
zipCode: string;
email: string;
telephoneNo: string;
contactName: string;
telephoneNo: string | string[];
lineId: string;
longitude: string;
latitude: string;
@ -55,7 +56,8 @@ type BranchUpdate = {
address?: string;
zipCode?: string;
email?: string;
telephoneNo?: string;
contactName?: string;
telephoneNo?: string | string[];
lineId?: string;
longitude?: string;
latitude?: string;
@ -238,7 +240,7 @@ export class BranchController extends Controller {
"missing_or_invalid_parameter",
);
const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body;
const { provinceId, districtId, subDistrictId, headOfficeId, telephoneNo, ...rest } = body;
const year = new Date().getFullYear();
@ -279,7 +281,17 @@ export class BranchController extends Controller {
this.setStatus(HttpStatus.CREATED);
if (record && telephoneNo) {
await prisma.branchContact.createMany({
data:
typeof telephoneNo === "string"
? [{ telephoneNo, branchId: record.id }]
: telephoneNo.map((v) => ({ telephoneNo: v, branchId: record.id })),
});
}
return Object.assign(record, {
contact: await prisma.branchContact.findMany({ where: { branchId: record.id } }),
qrCodeImageUrl: await minio.presignedGetObject(
MINIO_BUCKET,
lineImageLoc(record.id),