สร้างโครงสร้าง
This commit is contained in:
parent
042422938c
commit
8184152ac0
16 changed files with 295 additions and 84 deletions
|
|
@ -15,12 +15,17 @@ import {
|
|||
SuccessResponse,
|
||||
Response,
|
||||
} from "tsoa";
|
||||
import { OrgRevision } from "../entities/OrgRevision";
|
||||
import { CreateOrgRevision, OrgRevision } from "../entities/OrgRevision";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { In, Not } from "typeorm";
|
||||
import { OrgRoot } from "../entities/OrgRoot";
|
||||
import { OrgChild2 } from "../entities/OrgChild2";
|
||||
import { OrgChild3 } from "../entities/OrgChild3";
|
||||
import { OrgChild4 } from "../entities/OrgChild4";
|
||||
|
||||
@Route("api/v1/org")
|
||||
@Tags("Organization")
|
||||
|
|
@ -32,6 +37,11 @@ import HttpStatusCode from "../interfaces/http-status";
|
|||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
export class OrganizationController extends Controller {
|
||||
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
|
||||
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
|
||||
private child1Repository = AppDataSource.getRepository(OrgChild1);
|
||||
private child2Repository = AppDataSource.getRepository(OrgChild2);
|
||||
private child3Repository = AppDataSource.getRepository(OrgChild3);
|
||||
private child4Repository = AppDataSource.getRepository(OrgChild4);
|
||||
|
||||
/**
|
||||
* API รายการประวัติโครงสร้าง
|
||||
|
|
@ -104,16 +114,172 @@ export class OrganizationController extends Controller {
|
|||
*/
|
||||
@Post()
|
||||
async CreateOrgRevision(
|
||||
@Body() requestBody: CreateOrgChild1,
|
||||
@Body() requestBody: CreateOrgRevision,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
try {
|
||||
return new HttpSuccess();
|
||||
const revision = Object.assign(new OrgRevision(), requestBody) as OrgRevision;
|
||||
revision.orgRevisionIsDraft = true;
|
||||
revision.orgRevisionIsCurrent = false;
|
||||
revision.createdUserId = request.user.sub;
|
||||
revision.createdFullName = request.user.name;
|
||||
revision.lastUpdateUserId = request.user.sub;
|
||||
revision.lastUpdateFullName = request.user.name;
|
||||
await this.orgRevisionRepository.save(revision);
|
||||
|
||||
if (requestBody.typeDraft.toUpperCase() == "ORG") {
|
||||
if (requestBody.orgRevisionId == null)
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
const _revision = await this.orgRevisionRepository.findOne({
|
||||
where: { id: requestBody.orgRevisionId },
|
||||
});
|
||||
if (!_revision) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
|
||||
//clone data
|
||||
const orgRoot = await this.orgRootRepository.find({
|
||||
where: { orgRevisionId: requestBody.orgRevisionId },
|
||||
});
|
||||
const _orgRoot = orgRoot.map((x) => ({
|
||||
...x,
|
||||
isAncestorDNA:
|
||||
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
||||
? x.id
|
||||
: x.isAncestorDNA,
|
||||
}));
|
||||
await this.orgRootRepository.save(_orgRoot);
|
||||
|
||||
_orgRoot.forEach(async (x: any) => {
|
||||
delete x.id;
|
||||
const data = Object.assign(new OrgRoot(), x);
|
||||
data.orgRevisionId = revision.id;
|
||||
data.createdUserId = request.user.sub;
|
||||
data.createdFullName = request.user.name;
|
||||
data.createdAt = new Date();
|
||||
data.lastUpdateUserId = request.user.sub;
|
||||
data.lastUpdateFullName = request.user.name;
|
||||
data.lastUpdatedAt = new Date();
|
||||
await this.orgRootRepository.save(data);
|
||||
});
|
||||
//clone data
|
||||
const orgChild1 = await this.child1Repository.find({
|
||||
where: { orgRevisionId: requestBody.orgRevisionId },
|
||||
});
|
||||
const _orgChild1 = orgChild1.map((x) => ({
|
||||
...x,
|
||||
isAncestorDNA:
|
||||
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
||||
? x.id
|
||||
: x.isAncestorDNA,
|
||||
}));
|
||||
await this.child1Repository.save(_orgChild1);
|
||||
|
||||
_orgChild1.forEach(async (x: any) => {
|
||||
delete x.id;
|
||||
const data = Object.assign(new OrgChild1(), x);
|
||||
data.orgRevisionId = revision.id;
|
||||
data.createdUserId = request.user.sub;
|
||||
data.createdFullName = request.user.name;
|
||||
data.createdAt = new Date();
|
||||
data.lastUpdateUserId = request.user.sub;
|
||||
data.lastUpdateFullName = request.user.name;
|
||||
data.lastUpdatedAt = new Date();
|
||||
await this.child1Repository.save(data);
|
||||
});
|
||||
//clone data
|
||||
const orgChild2 = await this.child2Repository.find({
|
||||
where: { orgRevisionId: requestBody.orgRevisionId },
|
||||
});
|
||||
const _orgChild2 = orgChild2.map((x) => ({
|
||||
...x,
|
||||
isAncestorDNA:
|
||||
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
||||
? x.id
|
||||
: x.isAncestorDNA,
|
||||
}));
|
||||
await this.child2Repository.save(_orgChild2);
|
||||
|
||||
_orgChild2.forEach(async (x: any) => {
|
||||
delete x.id;
|
||||
const data = Object.assign(new OrgChild2(), x);
|
||||
data.orgRevisionId = revision.id;
|
||||
data.createdUserId = request.user.sub;
|
||||
data.createdFullName = request.user.name;
|
||||
data.createdAt = new Date();
|
||||
data.lastUpdateUserId = request.user.sub;
|
||||
data.lastUpdateFullName = request.user.name;
|
||||
data.lastUpdatedAt = new Date();
|
||||
await this.child2Repository.save(data);
|
||||
});
|
||||
//clone data
|
||||
const orgChild3 = await this.child3Repository.find({
|
||||
where: { orgRevisionId: requestBody.orgRevisionId },
|
||||
});
|
||||
const _orgChild3 = orgChild3.map((x) => ({
|
||||
...x,
|
||||
isAncestorDNA:
|
||||
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
||||
? x.id
|
||||
: x.isAncestorDNA,
|
||||
}));
|
||||
await this.child3Repository.save(_orgChild3);
|
||||
|
||||
_orgChild3.forEach(async (x: any) => {
|
||||
delete x.id;
|
||||
const data = Object.assign(new OrgChild3(), x);
|
||||
data.orgRevisionId = revision.id;
|
||||
data.createdUserId = request.user.sub;
|
||||
data.createdFullName = request.user.name;
|
||||
data.createdAt = new Date();
|
||||
data.lastUpdateUserId = request.user.sub;
|
||||
data.lastUpdateFullName = request.user.name;
|
||||
data.lastUpdatedAt = new Date();
|
||||
await this.child3Repository.save(data);
|
||||
});
|
||||
//clone data
|
||||
const orgChild4 = await this.child4Repository.find({
|
||||
where: { orgRevisionId: requestBody.orgRevisionId },
|
||||
});
|
||||
const _orgChild4 = orgChild4.map((x) => ({
|
||||
...x,
|
||||
isAncestorDNA:
|
||||
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
||||
? x.id
|
||||
: x.isAncestorDNA,
|
||||
}));
|
||||
await this.child4Repository.save(_orgChild4);
|
||||
|
||||
_orgChild4.forEach(async (x: any) => {
|
||||
delete x.id;
|
||||
const data = Object.assign(new OrgChild4(), x);
|
||||
data.orgRevisionId = revision.id;
|
||||
data.createdUserId = request.user.sub;
|
||||
data.createdFullName = request.user.name;
|
||||
data.createdAt = new Date();
|
||||
data.lastUpdateUserId = request.user.sub;
|
||||
data.lastUpdateFullName = request.user.name;
|
||||
data.lastUpdatedAt = new Date();
|
||||
await this.child4Repository.save(data);
|
||||
});
|
||||
} else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") {
|
||||
} else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") {
|
||||
}
|
||||
|
||||
const orgRevisions = await this.orgRevisionRepository.find({
|
||||
where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }],
|
||||
});
|
||||
await this.child4Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
||||
await this.child3Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
||||
await this.child2Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
||||
await this.child1Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
||||
await this.orgRootRepository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
||||
await this.orgRevisionRepository.remove(orgRevisions);
|
||||
|
||||
return new HttpSuccess(revision);
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* API รายละเอียดโครงสร้าง
|
||||
*
|
||||
|
|
@ -123,10 +289,13 @@ export class OrganizationController extends Controller {
|
|||
// @Get()
|
||||
// async detail(@Path() id: string) {
|
||||
// try {
|
||||
|
||||
|
||||
// return new HttpSuccess();
|
||||
// } catch (error) {
|
||||
// return error;
|
||||
// }
|
||||
// }
|
||||
}
|
||||
function ANY(arg0: OrgRevision[]): string | import("typeorm").FindOperator<string> | undefined {
|
||||
throw new Error("Function not implemented.");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue