checkpoint

This commit is contained in:
AdisakKanthawilang 2024-02-08 15:40:47 +07:00
parent 840974d968
commit 72abe0cae5

View file

@ -19,6 +19,8 @@ import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { Profile, CreateProfile, UpdateProfile } from "../entities/Profile";
import { Not } from "typeorm";
import { PosLevel } from "../entities/PosLevel";
import { PosType } from "../entities/PosType";
@Route("api/v1/org/profile")
@Tags("Profile")
@ -30,6 +32,8 @@ import { Not } from "typeorm";
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class ProfileController extends Controller {
private profileRepository = AppDataSource.getRepository(Profile);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
private posTypeRepository = AppDataSource.getRepository(PosType);
/**
* API
@ -43,6 +47,29 @@ export class ProfileController extends Controller {
requestBody: CreateProfile,
@Request() request: { user: Record<string, any> },
) {
if (requestBody.posLevelId) {
const checkPosLevel = await this.posLevelRepository.findOne({
where: { id: requestBody.posLevelId },
});
if (!checkPosLevel) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลใน PosLevel จากไอดีนี้ : " + requestBody.posLevelId,
);
}
}
if (requestBody.posTypeId) {
const checkPosType = await this.posTypeRepository.findOne({
where: { id: requestBody.posTypeId },
});
if (!checkPosType) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลใน PosType จากไอดีนี้ : " + requestBody.posTypeId,
);
}
}
try {
const profile = Object.assign(new Profile(), requestBody);
@ -76,6 +103,30 @@ export class ProfileController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
if (requestBody.posLevelId) {
const checkPosLevel = await this.posLevelRepository.findOne({
where: { id: requestBody.posLevelId },
});
if (!checkPosLevel) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลใน PosLevel จากไอดีนี้ : " + requestBody.posLevelId,
);
}
}
if (requestBody.posTypeId) {
const checkPosType = await this.posTypeRepository.findOne({
where: { id: requestBody.posTypeId },
});
if (!checkPosType) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลใน PosType จากไอดีนี้ : " + requestBody.posTypeId,
);
}
}
try {
profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name;
@ -121,7 +172,16 @@ export class ProfileController extends Controller {
async detailProfile(@Path() id: string) {
const profile = await this.profileRepository.findOne({
where: { id },
select: ["id","prefix","firstName","lastName","citizenId","position","posLevelId","posTypeId"],
select: [
"id",
"prefix",
"firstName",
"lastName",
"citizenId",
"position",
"posLevelId",
"posTypeId",
],
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
@ -142,7 +202,16 @@ export class ProfileController extends Controller {
@Get()
async listProfile() {
const profile = await this.profileRepository.find({
select: ["id","prefix","firstName","lastName","citizenId","position","posLevelId","posTypeId"],
select: [
"id",
"prefix",
"firstName",
"lastName",
"citizenId",
"position",
"posLevelId",
"posTypeId",
],
order: { createdAt: "ASC" },
});