159 lines
4.6 KiB
TypeScript
159 lines
4.6 KiB
TypeScript
|
|
import {
|
||
|
|
Controller,
|
||
|
|
Post,
|
||
|
|
Put,
|
||
|
|
Delete,
|
||
|
|
Route,
|
||
|
|
Security,
|
||
|
|
Tags,
|
||
|
|
Body,
|
||
|
|
Path,
|
||
|
|
Request,
|
||
|
|
SuccessResponse,
|
||
|
|
Response,
|
||
|
|
Get,
|
||
|
|
} from "tsoa";
|
||
|
|
import { AppDataSource } from "../database/data-source";
|
||
|
|
import HttpSuccess from "../interfaces/http-success";
|
||
|
|
import HttpStatusCode from "../interfaces/http-status";
|
||
|
|
import HttpError from "../interfaces/http-error";
|
||
|
|
import { Profile, CreateProfile, UpdateProfile } from "../entities/Profile";
|
||
|
|
import { Not } from "typeorm";
|
||
|
|
|
||
|
|
@Route("api/v1/org/profile")
|
||
|
|
@Tags("Profile")
|
||
|
|
@Security("bearerAuth")
|
||
|
|
@Response(
|
||
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||
|
|
)
|
||
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||
|
|
export class ProfileController extends Controller {
|
||
|
|
private profileRepository = AppDataSource.getRepository(Profile);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API สร้างทะเบียนประวัติ
|
||
|
|
*
|
||
|
|
* @summary ORG_065 - สร้างทะเบียนประวัติ (ADMIN) #70
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Post()
|
||
|
|
async createProfile(
|
||
|
|
@Body()
|
||
|
|
requestBody: CreateProfile,
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
|
||
|
|
try {
|
||
|
|
const profile = Object.assign(new Profile(), requestBody);
|
||
|
|
profile.createdUserId = request.user.sub;
|
||
|
|
profile.createdFullName = request.user.name;
|
||
|
|
profile.lastUpdateUserId = request.user.sub;
|
||
|
|
profile.lastUpdateFullName = request.user.name;
|
||
|
|
await this.profileRepository.save(profile);
|
||
|
|
return new HttpSuccess();
|
||
|
|
} catch (error) {
|
||
|
|
return error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API แก้ไขทะเบียนประวัติ
|
||
|
|
*
|
||
|
|
* @summary ORG_065 - แก้ไขทะเบียนประวัติ (ADMIN) #70
|
||
|
|
*
|
||
|
|
* @param {string} id Id ทะเบียนประวัติ
|
||
|
|
*/
|
||
|
|
@Put("{id}")
|
||
|
|
async updateProfile(
|
||
|
|
@Path() id: string,
|
||
|
|
@Body()
|
||
|
|
requestBody: CreateProfile,
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
const profile = await this.profileRepository.findOne({ where: { id: id } });
|
||
|
|
if (!profile) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
profile.lastUpdateUserId = request.user.sub;
|
||
|
|
profile.lastUpdateFullName = request.user.name;
|
||
|
|
this.profileRepository.merge(profile, requestBody);
|
||
|
|
await this.profileRepository.save(profile);
|
||
|
|
return new HttpSuccess();
|
||
|
|
} catch (error) {
|
||
|
|
return error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API ลบทะเบียนประวัติ
|
||
|
|
*
|
||
|
|
* @summary ORG_065 - ลบทะเบียนประวัติ (ADMIN) #70
|
||
|
|
*
|
||
|
|
* @param {string} id Id ทะเบียนประวัติ
|
||
|
|
*/
|
||
|
|
@Delete("{id}")
|
||
|
|
async deleteProfile(@Path() id: string) {
|
||
|
|
const delProfile = await this.profileRepository.findOne({
|
||
|
|
where: { id },
|
||
|
|
});
|
||
|
|
if (!delProfile) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id);
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
await this.profileRepository.delete({ id: id });
|
||
|
|
return new HttpSuccess();
|
||
|
|
} catch (error) {
|
||
|
|
return error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API รายละเอียดรายการทะเบียนประวัติ
|
||
|
|
*
|
||
|
|
* @summary ORG_065 - รายละเอียดรายการทะเบียนประวัติ (ADMIN) #70
|
||
|
|
*
|
||
|
|
* @param {string} id Id ทะเบียนประวัติ
|
||
|
|
*/
|
||
|
|
@Get("{id}")
|
||
|
|
async detailProfile(@Path() id: string) {
|
||
|
|
const profile = await this.profileRepository.findOne({
|
||
|
|
where: { id },
|
||
|
|
select: ["id","prefix","firstName","lastName","citizenId","position","posLevelId","posTypeId"],
|
||
|
|
});
|
||
|
|
if (!profile) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
return new HttpSuccess(profile);
|
||
|
|
} catch (error) {
|
||
|
|
return error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API รายการทะเบียนประวัติ
|
||
|
|
*
|
||
|
|
* @summary ORG_065 - รายการทะเบียนประวัติ (ADMIN) #70
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Get()
|
||
|
|
async listProfile() {
|
||
|
|
const profile = await this.profileRepository.find({
|
||
|
|
select: ["id","prefix","firstName","lastName","citizenId","position","posLevelId","posTypeId"],
|
||
|
|
order: { createdAt: "ASC" },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!profile) {
|
||
|
|
return new HttpSuccess([]);
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
return new HttpSuccess(profile);
|
||
|
|
} catch (error) {
|
||
|
|
return error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|