เพิ่มข้อมูลเส้น org_023 [orgRootId,orgChild1Id,orgChild2Id,,orgChild3Id] ตามลำดับ Root

This commit is contained in:
AdisakKanthawilang 2024-01-30 11:53:29 +07:00
parent 4896b4f8fe
commit 2d422de102
2 changed files with 111 additions and 89 deletions

View file

@ -38,23 +38,23 @@ export class OrgRootController extends Controller {
*/ */
@Get("{id}") @Get("{id}")
async GetRoot(@Path() id: string) { async GetRoot(@Path() id: string) {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root");
}
try { try {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root");
}
const getOrgRoot = { const getOrgRoot = {
"orgRootId" : orgRoot.id, orgRootId: orgRoot.id,
"orgRootName" : orgRoot.orgRootName, orgRootName: orgRoot.orgRootName,
"orgRootShortName" : orgRoot.orgRootShortName, orgRootShortName: orgRoot.orgRootShortName,
"orgRootCode" : orgRoot.orgRootCode, orgRootCode: orgRoot.orgRootCode,
"orgRootRank" : orgRoot.orgRootRank, orgRootRank: orgRoot.orgRootRank,
"orgRootOrder" : orgRoot.orgRootOrder, orgRootOrder: orgRoot.orgRootOrder,
"orgRootPhoneEx" : orgRoot.orgRootPhoneEx, orgRootPhoneEx: orgRoot.orgRootPhoneEx,
"orgRootPhoneIn" : orgRoot.orgRootPhoneIn, orgRootPhoneIn: orgRoot.orgRootPhoneIn,
"orgRootFax" : orgRoot.orgRootFax, orgRootFax: orgRoot.orgRootFax,
"orgRevisionId" : orgRoot.orgRevisionId, orgRevisionId: orgRoot.orgRevisionId,
"orgCode" : orgRoot.orgRootCode + "00", orgCode: orgRoot.orgRootCode + "00",
}; };
return new HttpSuccess(getOrgRoot); return new HttpSuccess(getOrgRoot);
} catch (error) { } catch (error) {
@ -86,38 +86,43 @@ export class OrgRootController extends Controller {
requestBody: CreateOrgRoot, requestBody: CreateOrgRoot,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const validOrgRootRanks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"];
if (!validOrgRootRanks.includes(requestBody.orgRootRank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootRank");
}
const orgRoot = Object.assign(new OrgRoot(), requestBody);
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chkCode = await this.orgRootRepository.findOne({
where: { orgRootCode: requestBody.orgRootCode },
});
if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const orgRevision = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
});
if (!orgRevision) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if (orgRevision.orgRevisionIsDraft != true && orgRevision.orgRevisionIsCurrent != false) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
}
try { try {
const validOrgRootRanks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"];
if (!validOrgRootRanks.includes(requestBody.orgRootRank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootRank");
}
const orgRoot = Object.assign(new OrgRoot(), requestBody);
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chkCode = await this.orgRootRepository.findOne({
where: { orgRootCode: requestBody.orgRootCode },
});
if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const orgRevision = await this.orgRevisionRepository.findOne({ where: { id: requestBody.orgRevisionId }});
if (!orgRevision) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if(orgRevision.orgRevisionIsDraft != true && orgRevision.orgRevisionIsCurrent != false){
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false");
}
orgRoot.createdUserId = request.user.sub; orgRoot.createdUserId = request.user.sub;
orgRoot.createdFullName = request.user.name; orgRoot.createdFullName = request.user.name;
orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateUserId = request.user.sub;
orgRoot.lastUpdateFullName = request.user.name; orgRoot.lastUpdateFullName = request.user.name;
await this.orgRootRepository.save(orgRoot); await this.orgRootRepository.save(orgRoot);
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;
@ -149,32 +154,39 @@ export class OrgRootController extends Controller {
requestBody: CreateOrgRoot, requestBody: CreateOrgRoot,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const validOrgRootRanks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"];
if (!validOrgRootRanks.includes(requestBody.orgRootRank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootRank");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if (
revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
}
const chkCode = await this.orgRootRepository.findOne({
where: { orgRootCode: requestBody.orgRootCode },
});
if (chkCode?.id != id && chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
try { try {
const validOrgRootRanks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"];
if (!validOrgRootRanks.includes(requestBody.orgRootRank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootRank");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({ where: {id: requestBody.orgRevisionId} });
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false");
}
const chkCode = await this.orgRootRepository.findOne({
where: { orgRootCode: requestBody.orgRootCode },
});
if (chkCode?.id != id && chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
orgRoot.lastUpdateUserId = request.user.sub; orgRoot.lastUpdateUserId = request.user.sub;
orgRoot.lastUpdateFullName = request.user.name; orgRoot.lastUpdateFullName = request.user.name;
orgRoot.lastUpdatedAt = new Date(); orgRoot.lastUpdatedAt = new Date();
@ -196,31 +208,37 @@ export class OrgRootController extends Controller {
*/ */
@Delete("{id}") @Delete("{id}")
async delete(@Path() id: string) { async delete(@Path() id: string) {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } });
if (orgChild1 != null) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",
);
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: orgRoot.orgRevisionId },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if (
revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
}
try { try {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } });
if (orgChild1 != null) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",
);
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: orgRoot.orgRevisionId }
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false");
}
await this.orgRootRepository.remove(orgRoot); await this.orgRootRepository.remove(orgRoot);
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;

View file

@ -419,6 +419,7 @@ export class OrganizationController extends Controller {
.filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id)
.map((orgChild1) => ({ .map((orgChild1) => ({
orgTreeId: orgChild1.id, orgTreeId: orgChild1.id,
orgRootId: orgRoot.id,
orgLevel: 1, orgLevel: 1,
orgTreeName: orgChild1.orgChild1Name, orgTreeName: orgChild1.orgChild1Name,
orgTreeShortName: orgChild1.orgChild1ShortName, orgTreeShortName: orgChild1.orgChild1ShortName,
@ -435,6 +436,7 @@ export class OrganizationController extends Controller {
.filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id)
.map((orgChild2) => ({ .map((orgChild2) => ({
orgTreeId: orgChild2.id, orgTreeId: orgChild2.id,
orgChild1Id: orgChild1.id,
orgLevel: 2, orgLevel: 2,
orgTreeName: orgChild2.orgChild2Name, orgTreeName: orgChild2.orgChild2Name,
orgTreeShortName: orgChild2.orgChild2ShortName, orgTreeShortName: orgChild2.orgChild2ShortName,
@ -451,6 +453,7 @@ export class OrganizationController extends Controller {
.filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id)
.map((orgChild3) => ({ .map((orgChild3) => ({
orgTreeId: orgChild3.id, orgTreeId: orgChild3.id,
orgChild2Id: orgChild2.id,
orgLevel: 3, orgLevel: 3,
orgTreeName: orgChild3.orgChild3Name, orgTreeName: orgChild3.orgChild3Name,
orgTreeShortName: orgChild3.orgChild3ShortName, orgTreeShortName: orgChild3.orgChild3ShortName,
@ -467,6 +470,7 @@ export class OrganizationController extends Controller {
.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)
.map((orgChild4) => ({ .map((orgChild4) => ({
orgTreeId: orgChild4.id, orgTreeId: orgChild4.id,
orgChild3Id: orgChild3.id,
orgLevel: 4, orgLevel: 4,
orgTreeName: orgChild4.orgChild4Name, orgTreeName: orgChild4.orgChild4Name,
orgTreeShortName: orgChild4.orgChild4ShortName, orgTreeShortName: orgChild4.orgChild4ShortName,