7table and PosExecutive (elementary)

This commit is contained in:
AdisakKanthawilang 2024-02-02 10:47:19 +07:00
parent e90e3a27c6
commit 04874676f8
10 changed files with 433 additions and 80 deletions

View file

@ -0,0 +1,113 @@
import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { PosExecutive } from "../entities/PosExecutive";
import HttpError from "../interfaces/http-error";
import { CreatePosMaster, PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
@Route("api/v1/org/pos")
@Tags("Position")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class PosExecutiveController extends Controller {
private posExecutiveRepository = AppDataSource.getRepository(PosExecutive);
private positionRepository = AppDataSource.getRepository(Position);
/**
* API
*
* @summary ORG_041 - (ADMIN) #44
*
*/
@Post("executive")
async createPosExecutive(
@Body()
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> },
) {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
try {
posExecutive.createdUserId = request.user.sub;
posExecutive.createdFullName = request.user.name;
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_042 - (ADMIN) #45
*
* @param {string} id Id
*/
@Put("executive/{id}")
async updatePosExecutive(
@Path() id: string,
@Body()
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> },
) {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
try {
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_043 - (ADMIN) #46
*
* @param {string} id Id
*/
@Delete("executive/{id}")
async deletePosExecutive(@Path() id: string) {
const delPosExecutive = await this.posExecutiveRepository.findOne({
where: { id },
});
if (!delPosExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id);
}
try {
await this.positionRepository.delete({ posExecutiveId: id });
await this.posExecutiveRepository.delete({ id });
return new HttpSuccess();
} catch (error) {
return error;
}
}
}

View file

@ -1035,86 +1035,6 @@ export class PositionController extends Controller {
}
}
/**
* API
*
* @summary ORG_041 - (ADMIN) #44
*
*/
@Post("executive")
async createPosExecutive(
@Body()
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> },
) {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
try {
posExecutive.createdUserId = request.user.sub;
posExecutive.createdFullName = request.user.name;
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_042 - (ADMIN) #45
*
* @param {string} id Id
*/
@Put("executive/{id}")
async updatePosExecutive(
@Path() id: string,
@Body()
requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> },
) {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
try {
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_043 - (ADMIN) #46
*
* @param {string} id Id
*/
@Delete("executive/{id}")
async deletePosExecutive(@Path() id: string) {
const delPosExecutive = await this.posExecutiveRepository.findOne({
where: { id },
});
if (!delPosExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id);
}
try {
await this.positionRepository.delete({ posExecutiveId: id });
await this.posExecutiveRepository.delete({ id });
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*