refactor: add devMessage to error
This commit is contained in:
parent
ba65c1fe83
commit
94d77e494c
2 changed files with 66 additions and 16 deletions
|
|
@ -95,7 +95,7 @@ export class BranchController extends Controller {
|
||||||
|
|
||||||
@Get("{branchId}")
|
@Get("{branchId}")
|
||||||
async getBranchById(@Path() branchId: string) {
|
async getBranchById(@Path() branchId: string) {
|
||||||
return await prisma.branch.findFirst({
|
const record = await prisma.branch.findFirst({
|
||||||
include: {
|
include: {
|
||||||
province: true,
|
province: true,
|
||||||
district: true,
|
district: true,
|
||||||
|
|
@ -103,6 +103,12 @@ export class BranchController extends Controller {
|
||||||
},
|
},
|
||||||
where: { id: branchId },
|
where: { id: branchId },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!record) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found");
|
||||||
|
}
|
||||||
|
|
||||||
|
return record;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
|
|
@ -115,13 +121,29 @@ export class BranchController extends Controller {
|
||||||
prisma.branch.findFirst({ where: { id: body.headOfficeId || undefined } }),
|
prisma.branch.findFirst({ where: { id: body.headOfficeId || undefined } }),
|
||||||
]);
|
]);
|
||||||
if (body.provinceId && !province)
|
if (body.provinceId && !province)
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Province cannot be found.");
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Province cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
if (body.districtId && !district)
|
if (body.districtId && !district)
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "District cannot be found.");
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"District cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
if (body.subDistrictId && !subDistrict)
|
if (body.subDistrictId && !subDistrict)
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Sub-district cannot be found.");
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Sub-district cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
if (body.headOfficeId && !branch)
|
if (body.headOfficeId && !branch)
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Head branch cannot be found.");
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Head branch cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body;
|
const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body;
|
||||||
|
|
@ -159,13 +181,29 @@ export class BranchController extends Controller {
|
||||||
prisma.branch.findFirst({ where: { id: body.headOfficeId || undefined } }),
|
prisma.branch.findFirst({ where: { id: body.headOfficeId || undefined } }),
|
||||||
]);
|
]);
|
||||||
if (body.provinceId && !province)
|
if (body.provinceId && !province)
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Province cannot be found.");
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Province cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
if (body.districtId && !district)
|
if (body.districtId && !district)
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "District cannot be found.");
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"District cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
if (body.subDistrictId && !subDistrict)
|
if (body.subDistrictId && !subDistrict)
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Sub-district cannot be found.");
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Sub-district cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
if (body.headOfficeId && !branch)
|
if (body.headOfficeId && !branch)
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Head branch cannot be found.");
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Head branch cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body;
|
const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body;
|
||||||
|
|
@ -196,7 +234,7 @@ export class BranchController extends Controller {
|
||||||
where: { id: branchId },
|
where: { id: branchId },
|
||||||
});
|
});
|
||||||
if (!record) {
|
if (!record) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.");
|
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found");
|
||||||
}
|
}
|
||||||
return record;
|
return record;
|
||||||
}
|
}
|
||||||
|
|
@ -213,11 +251,11 @@ export class BranchController extends Controller {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!record) {
|
if (!record) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.");
|
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (record.status === Status.USED) {
|
if (record.status === Status.USED) {
|
||||||
throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used.");
|
throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used.", "data_exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
return await prisma.branch.delete({ where: { id: branchId } });
|
return await prisma.branch.delete({ where: { id: branchId } });
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,13 @@ export class BranchContactController extends Controller {
|
||||||
async getBranchContactById(@Path() branchId: string, @Path() contactId: string) {
|
async getBranchContactById(@Path() branchId: string, @Path() contactId: string) {
|
||||||
const record = await prisma.branchContact.findFirst({ where: { id: contactId, branchId } });
|
const record = await prisma.branchContact.findFirst({ where: { id: contactId, branchId } });
|
||||||
|
|
||||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.");
|
if (!record) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
"Branch contact cannot be found.",
|
||||||
|
"data_not_found",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return Object.assign(record, {
|
return Object.assign(record, {
|
||||||
qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, imageLocation(record.id)),
|
qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, imageLocation(record.id)),
|
||||||
|
|
@ -79,7 +85,11 @@ export class BranchContactController extends Controller {
|
||||||
@Body() body: BranchContactCreate,
|
@Body() body: BranchContactCreate,
|
||||||
) {
|
) {
|
||||||
if (!(await prisma.branch.findFirst({ where: { id: branchId } }))) {
|
if (!(await prisma.branch.findFirst({ where: { id: branchId } }))) {
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Branch not found.");
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Branch not found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const record = await prisma.branchContact.create({
|
const record = await prisma.branchContact.create({
|
||||||
include: { branch: true },
|
include: { branch: true },
|
||||||
|
|
@ -112,7 +122,7 @@ export class BranchContactController extends Controller {
|
||||||
where: { id: contactId, branchId },
|
where: { id: contactId, branchId },
|
||||||
});
|
});
|
||||||
if (!record) {
|
if (!record) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.");
|
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found");
|
||||||
}
|
}
|
||||||
return Object.assign(record, {
|
return Object.assign(record, {
|
||||||
qrCodeImageUrl: await minio.presignedGetObject(
|
qrCodeImageUrl: await minio.presignedGetObject(
|
||||||
|
|
@ -131,6 +141,8 @@ export class BranchContactController extends Controller {
|
||||||
@Delete("{contactId}")
|
@Delete("{contactId}")
|
||||||
async deleteBranchContact(@Path() branchId: string, @Path() contactId: string) {
|
async deleteBranchContact(@Path() branchId: string, @Path() contactId: string) {
|
||||||
const result = await prisma.branchContact.deleteMany({ where: { id: contactId, branchId } });
|
const result = await prisma.branchContact.deleteMany({ where: { id: contactId, branchId } });
|
||||||
if (result.count <= 0) throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.");
|
if (result.count <= 0) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue