api จัดลำดับโครงสร้าง
This commit is contained in:
parent
24d0d06b62
commit
e8f5f905bd
1 changed files with 124 additions and 0 deletions
|
|
@ -788,4 +788,128 @@ export class OrganizationController extends Controller {
|
|||
return new HttpSuccess(_data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API จัดลำดับโครงสร้าง
|
||||
*
|
||||
* @summary ORG_038 - จัดลำดับโครงสร้าง (ADMIN) #41
|
||||
*
|
||||
*/
|
||||
@Post("sort")
|
||||
async Sort(
|
||||
@Body() requestBody: {
|
||||
id: string, type: number, sortId: string[]
|
||||
}
|
||||
){
|
||||
try {
|
||||
switch(requestBody.type){
|
||||
case 0 :{
|
||||
const revisionId = await this.orgRevisionRepository.findOne({ where: {id : requestBody.id} })
|
||||
if(!revisionId?.id){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found revisionId: "+ requestBody.id);
|
||||
}
|
||||
const listRootId = await this.orgRootRepository.find({
|
||||
where: { orgRevisionId: requestBody.id},
|
||||
select: ["id", "orgRootOrder"],
|
||||
})
|
||||
if(!listRootId){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId.");
|
||||
}
|
||||
const sortData = listRootId.map((data) => ({
|
||||
id : data.id,
|
||||
orgRootOrder: requestBody.sortId.indexOf(data.id) + 1
|
||||
}));
|
||||
await this.orgRootRepository.save(sortData);
|
||||
break;
|
||||
}
|
||||
|
||||
case 1 :{
|
||||
const rootId = await this.orgRootRepository.findOne({ where: {id : requestBody.id} })
|
||||
if(!rootId?.id){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId: "+ requestBody.id);
|
||||
}
|
||||
const listChild1Id = await this.child1Repository.find({
|
||||
where: { orgRootId: requestBody.id},
|
||||
select: ["id", "orgChild1Order"],
|
||||
})
|
||||
if(!listChild1Id){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id.");
|
||||
}
|
||||
const sortData = listChild1Id.map((data) => ({
|
||||
id : data.id,
|
||||
orgChild1Order: requestBody.sortId.indexOf(data.id) + 1
|
||||
}));
|
||||
await this.child1Repository.save(sortData);
|
||||
break;
|
||||
}
|
||||
|
||||
case 2 :{
|
||||
const child1Id = await this.child1Repository.findOne({ where: {id : requestBody.id} })
|
||||
if(!child1Id?.id){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id: "+ requestBody.id);
|
||||
}
|
||||
const listChild2Id = await this.child2Repository.find({
|
||||
where: { orgChild1Id: requestBody.id},
|
||||
select: ["id", "orgChild2Order"],
|
||||
})
|
||||
if(!listChild2Id){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id.");
|
||||
}
|
||||
const sortData = listChild2Id.map((data) => ({
|
||||
id : data.id,
|
||||
orgChild2Order: requestBody.sortId.indexOf(data.id) + 1
|
||||
}));
|
||||
await this.child2Repository.save(sortData);
|
||||
break;
|
||||
}
|
||||
|
||||
case 3 :{
|
||||
const child2Id = await this.child2Repository.findOne({ where: {id : requestBody.id} })
|
||||
if(!child2Id?.id){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id: "+ requestBody.id);
|
||||
}
|
||||
const listChild3Id = await this.child3Repository.find({
|
||||
where: { orgChild2Id: requestBody.id},
|
||||
select: ["id", "orgChild3Order"],
|
||||
})
|
||||
if(!listChild3Id){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id.");
|
||||
}
|
||||
const sortData = listChild3Id.map((data) => ({
|
||||
id : data.id,
|
||||
orgChild3Order: requestBody.sortId.indexOf(data.id) + 1
|
||||
}));
|
||||
await this.child3Repository.save(sortData);
|
||||
break;
|
||||
}
|
||||
|
||||
case 4 :{
|
||||
const child3Id = await this.child3Repository.findOne({ where: {id : requestBody.id} })
|
||||
if(!child3Id?.id){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id: "+ requestBody.id);
|
||||
}
|
||||
const listChild4Id = await this.child4Repository.find({
|
||||
where: { orgChild3Id: requestBody.id},
|
||||
select: ["id", "orgChild4Order"],
|
||||
})
|
||||
if(!listChild4Id){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id.");
|
||||
}
|
||||
const sortData = listChild4Id.map((data) => ({
|
||||
id : data.id,
|
||||
orgChild4Order: requestBody.sortId.indexOf(data.id) + 1
|
||||
}));
|
||||
await this.child4Repository.save(sortData);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: "+ requestBody.type);
|
||||
}
|
||||
return new HttpSuccess();
|
||||
}
|
||||
catch(error){
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue