diff --git a/src/controllers/OrgBloodGroupController.ts b/src/controllers/OrgBloodGroupController.ts new file mode 100644 index 00000000..8475705a --- /dev/null +++ b/src/controllers/OrgBloodGroupController.ts @@ -0,0 +1,173 @@ +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 { CreateBloodGroup, BloodGroup } from "../entities/BloodGroup"; + import { Not } from "typeorm"; + @Route("api/v1/org/bloodGroup") + @Tags("BloodGroup") + @Security("bearerAuth") + @Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", + ) + @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") + export class BloodGroupController extends Controller { + private bloodGroupRepository = AppDataSource.getRepository(BloodGroup); + + /** + * API สร้างกลุ่มเลือด + * + * @summary ORG_062 - สร้างกลุ่มเลือด (ADMIN) #66 + * + */ + @Post() + async createBloodGroup( + @Body() + requestBody: CreateBloodGroup, + @Request() request: { user: Record }, + ) { + const checkName = await this.bloodGroupRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + const bloodGroup = Object.assign(new BloodGroup(), requestBody); + bloodGroup.createdUserId = request.user.sub; + bloodGroup.createdFullName = request.user.name; + bloodGroup.lastUpdateUserId = request.user.sub; + bloodGroup.lastUpdateFullName = request.user.name; + await this.bloodGroupRepository.save(bloodGroup); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API แก้ไขกลุ่มเลือด + * + * @summary ORG_062 - แก้ไขกลุ่มเลือด (ADMIN) #66 + * + * @param {string} id Id กลุ่มเลือด + */ + @Put("{id}") + async updateBloodGroup( + @Path() id: string, + @Body() + requestBody: CreateBloodGroup, + @Request() request: { user: Record }, + ) { + const bloodGroup = await this.bloodGroupRepository.findOne({ where: { id: id } }); + if (!bloodGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); + } + + const checkName = await this.bloodGroupRepository.findOne({ + where: { id:Not(id),name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + bloodGroup.lastUpdateUserId = request.user.sub; + bloodGroup.lastUpdateFullName = request.user.name; + this.bloodGroupRepository.merge(bloodGroup, requestBody); + await this.bloodGroupRepository.save(bloodGroup); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API ลบกลุ่มเลือด + * + * @summary ORG_062 - ลบกลุ่มเลือด (ADMIN) #66 + * + * @param {string} id Id กลุ่มเลือด + */ + @Delete("{id}") + async deleteBloodGroup(@Path() id: string) { + const delBloodGroup = await this.bloodGroupRepository.findOne({ + where: { id }, + }); + if (!delBloodGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); + } + try { + await this.bloodGroupRepository.delete({ id: id }); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API รายละเอียดรายการกลุ่มเลือด + * + * @summary ORG_062 - รายละเอียดรายการกลุ่มเลือด (ADMIN) #66 + * + * @param {string} id Id กลุ่มเลือด + */ + @Get("{id}") + async detailBloodGroup(@Path() id: string) { + const bloodGroup = await this.bloodGroupRepository.findOne({ + where: { id }, + select: ["id", "name"], + }); + if (!bloodGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + return new HttpSuccess(bloodGroup); + } catch (error) { + return error; + } + } + + /** + * API รายการกลุ่มเลือด + * + * @summary ORG_062 - รายการกลุ่มเลือด (ADMIN) #66 + * + */ + @Get() + async listBloodGroup() { + const bloodGroup = await this.bloodGroupRepository.find({ + select: ["id", "name" , "createdAt" , "lastUpdatedAt" , "createdFullName" , "lastUpdateFullName"], + order:{ createdAt:"ASC" } + }); + + if (!bloodGroup) { + return new HttpSuccess([]); + } + try { + return new HttpSuccess(bloodGroup); + } catch (error) { + return error; + } + } + } + \ No newline at end of file diff --git a/src/controllers/EducationLevelController.ts b/src/controllers/OrgEducationLevelController.ts similarity index 97% rename from src/controllers/EducationLevelController.ts rename to src/controllers/OrgEducationLevelController.ts index b168f72c..92d8e0ba 100644 --- a/src/controllers/EducationLevelController.ts +++ b/src/controllers/OrgEducationLevelController.ts @@ -156,7 +156,8 @@ export class EducationLevelController extends Controller { @Get() async listEducationLevel() { const educationLevel = await this.educationLevelRepository.find({ - select: ["id", "name" ], + select: ["id", "name" , "createdAt" , "lastUpdatedAt" , "createdFullName" , "lastUpdateFullName"], + order:{ createdAt:"ASC" } }); if (!educationLevel) { diff --git a/src/controllers/GenderController.ts b/src/controllers/OrgGenderController.ts similarity index 95% rename from src/controllers/GenderController.ts rename to src/controllers/OrgGenderController.ts index 5134251c..fdc6acf6 100644 --- a/src/controllers/GenderController.ts +++ b/src/controllers/OrgGenderController.ts @@ -38,9 +38,11 @@ export class GenderController extends Controller { * */ @Get() - async Get() { + async GetResult() { + console.log("asds") const _gender = await this.genderRepository.find({ - select: ["id", "name"], + select: ["id", "name" , "createdAt" , "lastUpdatedAt" , "createdFullName" , "lastUpdateFullName"], + order:{ createdAt:"ASC" } }); if (!_gender) { return new HttpSuccess([]); @@ -48,6 +50,7 @@ export class GenderController extends Controller { try { return new HttpSuccess(_gender); } catch (error) { + console.log(error) return error; } } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index a1e30a1f..7fadb6d6 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -691,29 +691,31 @@ export class PositionController extends Controller { keywordAsInt = ""; } masterId = [...new Set(masterId)]; - const posMaster = await this.posMasterRepository.find({ - where: [ - { - ...checkChildConditions, - ...typeCondition, - }, - { - ...checkChildConditions, - ...typeCondition, - id: In(masterId), - }, - { - ...checkChildConditions, - ...typeCondition, - posMasterNo: Like(`%${keywordAsInt}%`), - }, - ], + + const keywordConditions = [ + { + ...checkChildConditions, + ...typeCondition, + }, + { + ...checkChildConditions, + ...typeCondition, + id: In(masterId), + }, + { + ...checkChildConditions, + ...typeCondition, + posMasterNo: Like(`%${keywordAsInt}%`), + }, + ]; + + const [posMaster, total] = await this.posMasterRepository.findAndCount({ + where: keywordConditions, order: { posMasterOrder: "ASC" }, relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], skip: (body.page - 1) * body.pageSize, take: body.pageSize, }); - const total = posMaster.length; const formattedData = await Promise.all( posMaster.map(async (posMaster) => { diff --git a/src/controllers/PrefixController.ts b/src/controllers/PrefixController.ts index 8fd37aa4..5c19cf64 100644 --- a/src/controllers/PrefixController.ts +++ b/src/controllers/PrefixController.ts @@ -40,7 +40,8 @@ export class PrefixController extends Controller { @Get() async Get() { const _prefix = await this.prefixRepository.find({ - select: ["id", "name"], + select: ["id", "name" , "createdAt" , "lastUpdatedAt" , "createdFullName" , "lastUpdateFullName"] , + order:{ createdAt:"ASC" } }); if (!_prefix) { return new HttpSuccess([]); diff --git a/src/controllers/RankController.ts b/src/controllers/RankController.ts index 3cb690fb..af676981 100644 --- a/src/controllers/RankController.ts +++ b/src/controllers/RankController.ts @@ -156,7 +156,8 @@ import { @Get() async listRank() { const rank = await this.rankRepository.find({ - select: ["id", "name" ], + select: ["id", "name" , "createdAt" , "lastUpdatedAt" , "createdFullName" , "lastUpdateFullName"], + order:{ createdAt:"ASC" } }); if (!rank) { diff --git a/src/controllers/RelationshipController.ts b/src/controllers/RelationshipController.ts index cf6ae0bf..0a2288b8 100644 --- a/src/controllers/RelationshipController.ts +++ b/src/controllers/RelationshipController.ts @@ -156,7 +156,8 @@ export class RelationshipController extends Controller { @Get() async listRelationship() { const relationship = await this.relationshipRepository.find({ - select: ["id", "name"], + select: ["id", "name" , "createdAt" , "lastUpdatedAt" , "createdFullName" , "lastUpdateFullName"], + order:{ createdAt:"ASC" } }); if (!relationship) { return new HttpSuccess([]); diff --git a/src/controllers/ReligionController.ts b/src/controllers/ReligionController.ts index 692e03e9..99ba6fc1 100644 --- a/src/controllers/ReligionController.ts +++ b/src/controllers/ReligionController.ts @@ -156,7 +156,8 @@ export class ReligionController extends Controller { @Get() async listReligion() { const religion = await this.religionRepository.find({ - select: ["id", "name" ], + select: ["id", "name" , "createdAt" , "lastUpdatedAt" , "createdFullName" , "lastUpdateFullName"], + order:{ createdAt:"ASC" } }); if (!religion) {