แก้child1 3 4

This commit is contained in:
Kittapath 2024-01-26 16:41:52 +07:00
parent a40aadef97
commit f4c54936f5
4 changed files with 128 additions and 94 deletions

View file

@ -21,7 +21,8 @@ import {
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
@Route("api/v1/organization/child1") import { Not } from "typeorm";
@Route("api/v1/org/child1")
@Tags("OrgChild1") @Tags("OrgChild1")
@Security("bearerAuth") @Security("bearerAuth")
@Response( @Response(
@ -30,7 +31,7 @@ import HttpError from "../interfaces/http-error";
) )
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") @SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild1Controller { export class OrgChild1Controller {
private orgRootRepository = AppDataSource.getRepository(OrgRoot) private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private child1Repository = AppDataSource.getRepository(OrgChild1); private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2); private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3); private child3Repository = AppDataSource.getRepository(OrgChild3);
@ -101,12 +102,14 @@ export class OrgChild1Controller {
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
try { try {
const rootIdExits = await this.orgRootRepository.findOne({ where: {id:requestBody.orgRootId} }); const rootIdExits = await this.orgRootRepository.findOne({
if(!rootIdExits){ where: { id: requestBody.orgRootId },
});
if (!rootIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId");
} }
const validOrgChild1Ranks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"]; const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) { if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild1Rank"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild1Rank");
} }
@ -115,15 +118,19 @@ export class OrgChild1Controller {
where: { orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code }, where: { orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code },
}); });
if (chkCode != null) { if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว",
);
} }
const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1; const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1;
child1.orgChild1Name = requestBody.orgChild1Name; child1.orgChild1Name = requestBody.orgChild1Name;
child1.createdUserId = request.user.sub; child1.createdUserId = request.user.sub;
child1.createdFullName = request.user.name; child1.createdFullName = request.user.name;
child1.lastUpdateUserId = request.user.sub; child1.lastUpdateUserId = request.user.sub;
child1.lastUpdateFullName = request.user.name; child1.lastUpdateFullName = request.user.name;
child1.orgRootId = String(rootIdExits?.id);
await this.child1Repository.save(child1); await this.child1Repository.save(child1);
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
@ -145,17 +152,23 @@ export class OrgChild1Controller {
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
try { try {
const rootIdExits = await this.orgRootRepository.findOne({ where: {id:requestBody.orgRootId} }); const rootIdExits = await this.orgRootRepository.findOne({
if(!rootIdExits){ where: { id: requestBody.orgRootId },
});
if (!rootIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId"); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId");
} }
const child1 = await this.child1Repository.findOne({ where: { id } }); const child1 = await this.child1Repository.findOne({ where: { id } });
if (!child1) { if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
} }
const chkCode = await this.child1Repository.findOne({ const chkCode = await this.child1Repository.findOne({
where: { id: id, orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code }, where: {
id: Not(id),
orgRootId: requestBody.orgRootId,
orgChild1Code: requestBody.orgChild1Code,
},
}); });
if (chkCode != null) { if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
@ -163,6 +176,7 @@ export class OrgChild1Controller {
child1.lastUpdateUserId = request.user.sub; child1.lastUpdateUserId = request.user.sub;
child1.lastUpdateFullName = request.user.name; child1.lastUpdateFullName = request.user.name;
child1.lastUpdatedAt = new Date(); child1.lastUpdatedAt = new Date();
child1.orgRootId = String(rootIdExits?.id);
this.child1Repository.merge(child1, requestBody); this.child1Repository.merge(child1, requestBody);
await this.child1Repository.save(child1); await this.child1Repository.save(child1);
return new HttpSuccess(); return new HttpSuccess();

View file

@ -20,7 +20,7 @@ import {
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
@Route("api/v1/organization/child3") @Route("api/v1/org/child3")
@Tags("OrgChild3") @Tags("OrgChild3")
@Security("bearerAuth") @Security("bearerAuth")
@Response( @Response(
@ -52,17 +52,22 @@ export class OrgChild3Controller {
if (!child2) { if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
} }
const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild3Rank");
}
const child3 = Object.assign(new OrgChild3(), requestBody) as OrgChild3; const child3 = Object.assign(new OrgChild3(), requestBody) as OrgChild3;
child3.orgChild3Name = requestBody.orgChild3Name; child3.orgChild3Name = requestBody.orgChild3Name;
child3.createdUserId = request.user.sub; child3.createdUserId = request.user.sub;
child3.createdFullName = request.user.name; child3.createdFullName = request.user.name;
child3.createdAt = new Date();
child3.lastUpdateUserId = request.user.sub; child3.lastUpdateUserId = request.user.sub;
child3.lastUpdateFullName = request.user.name; child3.lastUpdateFullName = request.user.name;
child3.lastUpdatedAt = new Date(); child3.orgRootId = String(child2?.orgRootId);
(child3.orgRootId = String(child2?.orgRootId)), child3.orgChild1Id = String(child2?.orgChild1Id);
(child3.orgChild1Id = String(child2?.orgChild1Id)), child3.orgChild2Id = String(child2?.id);
await this.child3Repository.save(child3); await this.child3Repository.save(child3);
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;
@ -83,6 +88,13 @@ export class OrgChild3Controller {
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
try { try {
const child2IdExits = await this.child2Repository.findOne({
where: { id: requestBody.orgChild2Id },
});
if (!child2IdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2Id");
}
const child3 = await this.child3Repository.findOne({ where: { id } }); const child3 = await this.child3Repository.findOne({ where: { id } });
if (!child3) { if (!child3) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
@ -91,6 +103,9 @@ export class OrgChild3Controller {
child3.lastUpdateUserId = request.user.sub; child3.lastUpdateUserId = request.user.sub;
child3.lastUpdateFullName = request.user.name; child3.lastUpdateFullName = request.user.name;
child3.lastUpdatedAt = new Date(); child3.lastUpdatedAt = new Date();
child3.orgRootId = String(child2IdExits?.orgRootId);
child3.orgChild1Id = String(child2IdExits?.orgChild1Id);
child3.orgChild2Id = String(child2IdExits?.id);
this.child3Repository.merge(child3, requestBody); this.child3Repository.merge(child3, requestBody);
await this.child3Repository.save(child3); await this.child3Repository.save(child3);
return new HttpSuccess(); return new HttpSuccess();
@ -116,7 +131,7 @@ export class OrgChild3Controller {
const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } }); const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } });
if (exitsChild4) { if (exitsChild4) {
throw new HttpError( throw new HttpError(
HttpStatusCode.NOT_FOUND, HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
); );
} }

View file

@ -13,20 +13,27 @@ import {
Body, Body,
Request, Request,
Example, Example,
SuccessResponse,
Response,
} from "tsoa"; } from "tsoa";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { CreateOrgChild4, OrgChild4 } from "../entities/OrgChild4"; import { CreateOrgChild4, OrgChild4, UpdateOrgChild4 } from "../entities/OrgChild4";
import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild3 } from "../entities/OrgChild3";
@Route("organization") @Route("api/v1/org/child4")
@Tags("OrgChild4") @Tags("OrgChild4")
// @Security("bearerAuth") @Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild4Controller extends Controller { export class OrgChild4Controller extends Controller {
private orgChild3Repository = AppDataSource.getRepository(OrgChild3); private child3Repository = AppDataSource.getRepository(OrgChild3);
private orgChild4Repository = AppDataSource.getRepository(OrgChild4); private child4Repository = AppDataSource.getRepository(OrgChild4);
/** /**
* 4 Child4 * 4 Child4
@ -54,36 +61,31 @@ export class OrgChild4Controller extends Controller {
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
try { try {
const orgChild3 = await this.orgChild3Repository.findOne({ //BE ใช้ orgChild3Id หา orgChild1Id, orgRootId
const child3 = await this.child3Repository.findOne({
where: { id: requestBody.orgChild3Id }, where: { id: requestBody.orgChild3Id },
}); });
const orgChild4 = Object.assign(new OrgChild4(), requestBody); if (!child3) {
if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
} }
if (orgChild3) { const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"];
orgChild4.orgChild4Name = requestBody.orgChild4Name; if (!validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase())) {
orgChild4.orgChild4ShortName = requestBody.orgChild4ShortName; throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild4Rank");
orgChild4.orgChild4Code = requestBody.orgChild4Code;
// orgChild4.orgChild4Order = requestBody.orgChild4Order;
orgChild4.orgChild4PhoneEx = requestBody.orgChild4PhoneEx;
orgChild4.orgChild4PhoneIn = requestBody.orgChild4PhoneIn;
orgChild4.orgChild4Fax = requestBody.orgChild4Fax;
// orgChild4.orgChild4IsNormal = requestBody.orgChild4IsNormal;
orgChild4.orgRootId = orgChild3.orgRootId;
orgChild4.orgChild1Id = orgChild3.orgChild1Id;
orgChild4.orgChild2Id = orgChild3.orgChild2Id;
orgChild4.orgChild3Id = orgChild3.id;
orgChild4.createdUserId = request.user.sub;
orgChild4.createdFullName = request.user.name;
orgChild4.lastUpdateUserId = request.user.sub;
orgChild4.lastUpdateFullName = request.user.name;
await this.orgChild4Repository.save(orgChild4);
} else {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ3");
} }
const child4 = Object.assign(new OrgChild4(), requestBody) as OrgChild4;
child4.orgChild4Name = requestBody.orgChild4Name;
child4.createdUserId = request.user.sub;
child4.createdFullName = request.user.name;
child4.lastUpdateUserId = request.user.sub;
child4.lastUpdateFullName = request.user.name;
child4.orgRootId = String(child3?.orgRootId);
child4.orgChild1Id = String(child3?.orgChild1Id);
child4.orgChild2Id = String(child3?.orgChild2Id);
child4.orgChild3Id = String(child3?.id);
await this.child4Repository.save(child4);
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;
@ -114,33 +116,30 @@ export class OrgChild4Controller extends Controller {
async update( async update(
@Path() id: string, @Path() id: string,
@Body() @Body()
requestBody: CreateOrgChild4, requestBody: UpdateOrgChild4,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
try { try {
const orgChild3 = await this.orgChild3Repository.findOne({ const child3IdExits = await this.child3Repository.findOne({
where: { id: requestBody.orgChild3Id }, where: { id: requestBody.orgChild3Id },
}); });
const orgChild4 = await this.orgChild4Repository.findOne({ where: { id } }); if (!child3IdExits) {
if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child3Id");
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
} }
if (orgChild3) { const child4 = await this.child4Repository.findOne({ where: { id } });
orgChild4.orgChild4Name = requestBody.orgChild4Name; if (!child4) {
orgChild4.orgChild4ShortName = requestBody.orgChild4ShortName; throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
orgChild4.orgChild4Code = requestBody.orgChild4Code;
// orgChild4.orgChild4Order = requestBody.orgChild4Order;
orgChild4.orgChild4PhoneEx = requestBody.orgChild4PhoneEx;
orgChild4.orgChild4PhoneIn = requestBody.orgChild4PhoneIn;
orgChild4.orgChild4Fax = requestBody.orgChild4Fax;
// orgChild4.orgChild4IsNormal = requestBody.orgChild4IsNormal;
orgChild4.lastUpdateUserId = request.user.sub;
orgChild4.lastUpdateFullName = request.user.name;
await this.orgChild4Repository.save(orgChild4);
} else {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ3");
} }
child4.lastUpdateUserId = request.user.sub;
child4.lastUpdateFullName = request.user.name;
child4.lastUpdatedAt = new Date();
child4.orgRootId = String(child3IdExits?.orgRootId);
child4.orgChild1Id = String(child3IdExits?.orgChild1Id);
child4.orgChild3Id = String(child3IdExits?.id);
this.child4Repository.merge(child4, requestBody);
await this.child4Repository.save(child4);
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;
@ -157,14 +156,11 @@ export class OrgChild4Controller extends Controller {
@Delete("child4/{id}") @Delete("child4/{id}")
async delete(@Path() id: string) { async delete(@Path() id: string) {
try { try {
const orgChild4 = await this.orgChild4Repository.findOne({ where: { id } }); const child4 = await this.child4Repository.findOne({ where: { id } });
if (!child4) {
if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
} }
await this.child4Repository.remove(child4);
await this.orgChild4Repository.remove(orgChild4);
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;

View file

@ -12,6 +12,8 @@ import {
Path, Path,
Request, Request,
Example, Example,
SuccessResponse,
Response,
} from "tsoa"; } from "tsoa";
import { OrgRevision } from "../entities/OrgRevision"; import { OrgRevision } from "../entities/OrgRevision";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
@ -20,11 +22,15 @@ import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
@Route("organization") @Route("api/v1/org")
@Tags("Organization") @Tags("Organization")
@Security("bearerAuth") @Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrganizationController extends Controller { export class OrganizationController extends Controller {
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/** /**
@ -37,10 +43,17 @@ export class OrganizationController extends Controller {
async GetHistory() { async GetHistory() {
try { try {
const orgRevision = await this.orgRevisionRepository.find({ const orgRevision = await this.orgRevisionRepository.find({
select: ["id", "orgRevisionName", "orgRevisionIsCurrent", "orgRevisionCreatedAt", "orgRevisionIsDraft"], select: [
"id",
"orgRevisionName",
"orgRevisionIsCurrent",
"orgRevisionCreatedAt",
"orgRevisionIsDraft",
],
order: { orgRevisionCreatedAt: "DESC" },
}); });
if (!orgRevision) { if (!orgRevision) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); return new HttpSuccess([]);
} }
const mapOrgRevisions = orgRevision.map((revision) => ({ const mapOrgRevisions = orgRevision.map((revision) => ({
orgRevisionId: revision.id, orgRevisionId: revision.id,
@ -49,7 +62,7 @@ export class OrganizationController extends Controller {
orgRevisionCreatedAt: revision.orgRevisionCreatedAt, orgRevisionCreatedAt: revision.orgRevisionCreatedAt,
orgRevisionIsDraft: revision.orgRevisionIsDraft, orgRevisionIsDraft: revision.orgRevisionIsDraft,
})); }));
return new HttpSuccess(mapOrgRevisions); return new HttpSuccess(mapOrgRevisions);
} catch (error) { } catch (error) {
return error; return error;
@ -66,19 +79,16 @@ export class OrganizationController extends Controller {
async GetActive() { async GetActive() {
try { try {
const orgRevisionActive = await this.orgRevisionRepository.findOne({ const orgRevisionActive = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent : true, orgRevisionIsDraft : false } where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
}); });
const orgRevisionDraf = await this.orgRevisionRepository.findOne({ const orgRevisionDraf = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent : false, orgRevisionIsDraft : true } where: { orgRevisionIsCurrent: false, orgRevisionIsDraft: true },
}); });
if (!orgRevisionActive || !orgRevisionDraf) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const mapData = { const mapData = {
activeId: orgRevisionActive.id, activeId: orgRevisionActive == null ? null : orgRevisionActive.id,
activeName: orgRevisionActive.orgRevisionName, activeName: orgRevisionActive == null ? null : orgRevisionActive.orgRevisionName,
draftId: orgRevisionDraf.id, draftId: orgRevisionDraf == null ? null : orgRevisionDraf.id,
draftName: orgRevisionDraf.orgRevisionName draftName: orgRevisionDraf == null ? null : orgRevisionDraf.orgRevisionName,
}; };
return new HttpSuccess(mapData); return new HttpSuccess(mapData);
} catch (error) { } catch (error) {
@ -96,12 +106,11 @@ export class OrganizationController extends Controller {
async CreateOrgRevision( async CreateOrgRevision(
@Body() requestBody: CreateOrgChild1, @Body() requestBody: CreateOrgChild1,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
){ ) {
try { try {
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;
}
} }
}
} }