api ประเภทตำแหน่ง

This commit is contained in:
Bright 2024-02-02 12:54:57 +07:00
parent 4adf3590f8
commit e51ac2dd09
3 changed files with 298 additions and 47 deletions

View file

@ -0,0 +1,242 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { PosType, CreatePosType, UpdatePosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
@Route("api/v1/org/pos/type")
@Tags("PosType")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class PosTypeController extends Controller {
private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
/**
* API
*
* @summary ORG_045 - (ADMIN) #48
*
*/
@Post()
@Example(
{
positionName: "นักบริหาร",
posTypeRank: 1,
},
)
async createType(
@Body()
requestBody: CreatePosType,
@Request() request: { user: Record<string, any> },
) {
const posType = Object.assign(new PosType(), requestBody);
if (!posType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chkPosTypeName = await this.posTypeRepository.findOne({ where: {
posTypeName: requestBody.posTypeName }
})
if(chkPosTypeName){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อประเภทตำแหน่ง: " + requestBody.posTypeName + " มีอยู่ในระบบแล้ว");
}
try {
posType.createdUserId = request.user.sub;
posType.createdFullName = request.user.name;
posType.lastUpdateUserId = request.user.sub;
posType.lastUpdateFullName = request.user.name;
await this.posTypeRepository.save(posType);
return new HttpSuccess(posType);
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_046 - (ADMIN) #49
*
* @param {string} id Id
*/
@Put("{id}")
@Example(
{
positionName: "นักบริหาร",
posTypeRank: 1,
},
)
async editType(
@Path() id: string,
@Body() requestBody: UpdatePosType,
@Request() request: { user: Record<string, any> },
) {
const posType = await this.posTypeRepository.findOne({ where: { id } });
if (!posType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
const chkPosTypeName = await this.posTypeRepository.findOne({ where: {
id: Not(id),
posTypeName: requestBody.posTypeName }
})
if(chkPosTypeName){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อประเภทตำแหน่ง: " + requestBody.posTypeName + " มีอยู่ในระบบแล้ว");
}
try {
posType.lastUpdateUserId = request.user.sub;
posType.lastUpdateFullName = request.user.name;
this.posTypeRepository.merge(posType, requestBody);
await this.posTypeRepository.save(posType);
return new HttpSuccess(posType.id);
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_047 - (ADMIN) #50
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteType(@Path() id: string) {
const delPosType = await this.posTypeRepository.findOne({ where: { id } });
if (!delPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
try {
await this.posTypeRepository.remove(delPosType);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_048 - (ADMIN) #51
*
* @param {string} id Id
*/
@Get("{id}")
@Example([
{
id: "00000000-0000-0000-0000-000000000000",
posTypeName: "นักบริหาร",
posLevelposTypeRanks: 1,
posLevels: [
{
id: "00000000-0000-0000-0000-000000000000",
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD"
}
]
},
])
async GetTypeDetail(@Path() id: string) {
try {
const getPosType = await this.posTypeRepository.findOne({
select: ["id", "posTypeName", "posTypeRank"],
relations: ["posLevels"],
where: { id: id }
});
if (!getPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
const mapGetPosType = {
id : getPosType.id,
posTypeName : getPosType.posTypeName,
posTypeRank : getPosType.posTypeRank,
posLevels : getPosType.posLevels.map((posLevel) => ({
id: posLevel.id,
posLevelName: posLevel.posLevelName,
posLevelRank: posLevel.posLevelRank,
posLevelAuthority: posLevel.posLevelAuthority,
})),
}
return new HttpSuccess(mapGetPosType);
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_027 - (ADMIN) #29
*
*/
@Get()
@Example([
{
id: "00000000-0000-0000-0000-000000000000",
posTypeName: "นักบริหาร",
posTypeRank: 1,
posLevels: [
{
id: "00000000-0000-0000-0000-000000000000",
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD",
},
],
},
])
async GetPosType() {
try {
const posType = await this.posTypeRepository.find({
select: ["id", "posTypeName", "posTypeRank"],
relations: ["posLevels"],
});
if (!posType) {
return new HttpSuccess([]);
}
const mapPosType = posType.map((item) => ({
id: item.id,
posTypeName: item.posTypeName,
posTypeRank: item.posTypeRank,
posLevels: item.posLevels.map((posLevel) => ({
id: posLevel.id,
posLevelName: posLevel.posLevelName,
posLevelRank: posLevel.posLevelRank,
posLevelAuthority: posLevel.posLevelAuthority,
})),
}));
return new HttpSuccess(mapPosType);
} catch (error) {
return error;
}
}
}

View file

@ -85,53 +85,53 @@ export class PositionController extends Controller {
}
}
/**
* API
*
* @summary ORG_027 - (ADMIN) #29
*
*/
@Get("type")
@Example([
{
id: "00000000-0000-0000-0000-000000000000",
posTypeName: "นักบริหาร",
posTypeRank: 1,
posLevels: [
{
id: "00000000-0000-0000-0000-000000000000",
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD",
},
],
},
])
async GetPosType() {
try {
const posType = await this.posTypeRepository.find({
select: ["id", "posTypeName", "posTypeRank"],
relations: ["posLevels"],
});
if (!posType) {
return new HttpSuccess([]);
}
const mapPosType = posType.map((item) => ({
id: item.id,
posTypeName: item.posTypeName,
posTypeRank: item.posTypeRank,
posLevels: item.posLevels.map((posLevel) => ({
id: posLevel.id,
posLevelName: posLevel.posLevelName,
posLevelRank: posLevel.posLevelRank,
posLevelAuthority: posLevel.posLevelAuthority,
})),
}));
return new HttpSuccess(mapPosType);
} catch (error) {
return error;
}
}
// /**
// * API รายการประเภทตำแหน่ง
// *
// * @summary ORG_027 - รายการประเภทตำแหน่ง (ADMIN) #29
// *
// */
// @Get("type")
// @Example([
// {
// id: "00000000-0000-0000-0000-000000000000",
// posTypeName: "นักบริหาร",
// posTypeRank: 1,
// posLevels: [
// {
// id: "00000000-0000-0000-0000-000000000000",
// posLevelName: "นักบริหาร",
// posLevelRank: 1,
// posLevelAuthority: "HEAD",
// },
// ],
// },
// ])
// async GetPosType() {
// try {
// const posType = await this.posTypeRepository.find({
// select: ["id", "posTypeName", "posTypeRank"],
// relations: ["posLevels"],
// });
// if (!posType) {
// return new HttpSuccess([]);
// }
// const mapPosType = posType.map((item) => ({
// id: item.id,
// posTypeName: item.posTypeName,
// posTypeRank: item.posTypeRank,
// posLevels: item.posLevels.map((posLevel) => ({
// id: posLevel.id,
// posLevelName: posLevel.posLevelName,
// posLevelRank: posLevel.posLevelRank,
// posLevelAuthority: posLevel.posLevelAuthority,
// })),
// }));
// return new HttpSuccess(mapPosType);
// } catch (error) {
// return error;
// }
// }
/**
* API