fix: branch code

This commit is contained in:
puriphat 2024-06-14 05:44:03 +00:00
parent c978d3adec
commit 5cc0f1c659
3 changed files with 24 additions and 24 deletions

View file

@ -62,7 +62,7 @@ export class BranchContactController extends Controller {
throw new HttpError( throw new HttpError(
HttpStatus.NOT_FOUND, HttpStatus.NOT_FOUND,
"Branch contact cannot be found.", "Branch contact cannot be found.",
"data_not_found", "branchContactNotFound",
); );
} }
@ -78,8 +78,8 @@ export class BranchContactController extends Controller {
if (!(await prisma.branch.findFirst({ where: { id: branchId } }))) { if (!(await prisma.branch.findFirst({ where: { id: branchId } }))) {
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Branch not found.", "Branch cannot be found.",
"missing_or_invalid_parameter", "branchBadReq",
); );
} }
const record = await prisma.branchContact.create({ const record = await prisma.branchContact.create({
@ -106,7 +106,7 @@ export class BranchContactController extends Controller {
throw new HttpError( throw new HttpError(
HttpStatus.NOT_FOUND, HttpStatus.NOT_FOUND,
"Branch contact cannot be found.", "Branch contact cannot be found.",
"data_not_found", "branchContactNotFound",
); );
} }
@ -125,7 +125,7 @@ export class BranchContactController extends Controller {
throw new HttpError( throw new HttpError(
HttpStatus.NOT_FOUND, HttpStatus.NOT_FOUND,
"Branch contact cannot be found.", "Branch contact cannot be found.",
"data_not_found", "branchContactNotFound",
); );
} }
} }

View file

@ -208,7 +208,7 @@ export class BranchController extends Controller {
}); });
if (!record) { if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
} }
return Object.assign(record, { return Object.assign(record, {
@ -229,25 +229,25 @@ export class BranchController extends Controller {
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Province cannot be found.", "Province cannot be found.",
"missing_or_invalid_parameter", "relationProvinceNotFound",
); );
if (body.districtId && !district) if (body.districtId && !district)
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"District cannot be found.", "District cannot be found.",
"missing_or_invalid_parameter", "relationDistrictNotFound",
); );
if (body.subDistrictId && !subDistrict) if (body.subDistrictId && !subDistrict)
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Sub-district cannot be found.", "Sub-district cannot be found.",
"missing_or_invalid_parameter", "relationSubDistrictNotFound",
); );
if (body.headOfficeId && !head) if (body.headOfficeId && !head)
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Head branch cannot be found.", "Headquaters cannot be found.",
"missing_or_invalid_parameter", "relationHQNotFound",
); );
const { provinceId, districtId, subDistrictId, headOfficeId, contact, ...rest } = body; const { provinceId, districtId, subDistrictId, headOfficeId, contact, ...rest } = body;
@ -337,8 +337,8 @@ export class BranchController extends Controller {
if (body.headOfficeId === branchId) if (body.headOfficeId === branchId)
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Cannot make this as head office and branch at the same time.", "Cannot make this as headquaters and branch at the same time.",
"missing_or_invalid_parameter", "cantMakeHQAndBranchSameTime",
); );
if (body.subDistrictId || body.districtId || body.provinceId || body.headOfficeId) { if (body.subDistrictId || body.districtId || body.provinceId || body.headOfficeId) {
@ -352,32 +352,32 @@ export class BranchController extends Controller {
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Province cannot be found.", "Province cannot be found.",
"missing_or_invalid_parameter", "relationProvinceNotFound",
); );
if (body.districtId && !district) if (body.districtId && !district)
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"District cannot be found.", "District cannot be found.",
"missing_or_invalid_parameter", "relationDistrictNotFound",
); );
if (body.subDistrictId && !subDistrict) if (body.subDistrictId && !subDistrict)
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Sub-district cannot be found.", "Sub-district cannot be found.",
"missing_or_invalid_parameter", "relationSubDistrictNotFound",
); );
if (body.headOfficeId && !branch) if (body.headOfficeId && !branch)
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Head branch cannot be found.", "Headquaters cannot be found.",
"missing_or_invalid_parameter", "relationHQNotFound",
); );
} }
const { provinceId, districtId, subDistrictId, headOfficeId, contact, ...rest } = body; const { provinceId, districtId, subDistrictId, headOfficeId, contact, ...rest } = body;
if (!(await prisma.branch.findUnique({ where: { id: branchId } }))) { if (!(await prisma.branch.findUnique({ where: { id: branchId } }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
} }
const record = await prisma.branch.update({ const record = await prisma.branch.update({
@ -445,11 +445,11 @@ export class BranchController extends Controller {
}); });
if (!record) { if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
} }
if (record.status !== Status.CREATED) { if (record.status !== Status.CREATED) {
throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used.", "data_in_used"); throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used.", "branchInUsed");
} }
await minio.removeObject(MINIO_BUCKET, lineImageLoc(branchId), { await minio.removeObject(MINIO_BUCKET, lineImageLoc(branchId), {

View file

@ -116,7 +116,7 @@ export class BranchUserController extends Controller {
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Branch cannot be found.", "Branch cannot be found.",
"missing_or_invalid_parameter", "branchBadReq",
); );
} }
@ -124,7 +124,7 @@ export class BranchUserController extends Controller {
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"One or more user cannot be found.", "One or more user cannot be found.",
"missing_or_invalid_parameter", "oneOrMoreUserBadReq",
); );
} }
@ -234,7 +234,7 @@ export class UserBranchController extends Controller {
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"One or more branch cannot be found.", "One or more branch cannot be found.",
"missing_or_invalid_parameter", "oneOrMoreBranchBadReq",
); );
} }