org_053 v.queryBuilder ยังไม่เสร็จ

This commit is contained in:
AdisakKanthawilang 2024-02-01 17:28:47 +07:00
parent be71b978bf
commit 009e315378

View file

@ -33,6 +33,7 @@ import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4"; import { OrgChild4 } from "../entities/OrgChild4";
import { Position } from "../entities/Position"; import { Position } from "../entities/Position";
import { Brackets } from "typeorm/browser";
@Route("api/v1/org/pos") @Route("api/v1/org/pos")
@Tags("Position") @Tags("Position")
@Security("bearerAuth") @Security("bearerAuth")
@ -733,103 +734,140 @@ export class PositionController extends Controller {
let typeCondition: any = {}; let typeCondition: any = {};
let checkChildConditions: any = {}; let checkChildConditions: any = {};
if (body.type === 0) { // if (body.type === 0) {
typeCondition = { // data = data
orgRootId: body.id, // .andWhere("position.orgRootId = :orgRootId", { orgRootId: body.id });
};
if (!body.isAll) {
checkChildConditions = {
orgChild1Id: IsNull(),
};
}
} else if (body.type === 1) {
typeCondition = {
orgChild1Id: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild2Id: IsNull(),
};
}
} else if (body.type === 2) {
typeCondition = {
orgChild2Id: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild3Id: IsNull(),
};
}
} else if (body.type === 3) {
typeCondition = {
orgChild3Id: body.id,
};
if (!body.isAll) {
checkChildConditions = {
orgChild4Id: IsNull(),
};
}
} else if (body.type === 4) {
typeCondition = {
orgChild4Id: body.id,
};
}
const keywordCondition = {}
? {
posMasterNoPrefix: Like(`%${body.keyword}%`),
positionName: Like(`%${body.keyword}%`),
posTypeName: Like(`%${body.keyword}%`),
posLevelName: Like(`%${body.keyword}%`),
}
: {};
const posMaster = await this.posMasterRepository.find({
where: {
...typeCondition,
...checkChildConditions,
...keywordCondition,
},
order: { posMasterOrder: "ASC" },
skip: (body.page - 1) * body.pageSize,
take: body.pageSize,
});
const total = posMaster.length;
// if (!posMaster || posMaster.length === 0) { // if (!body.isAll) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // checkChildConditions = {
// orgChild1Id: IsNull(),
// };
// }
// } else if (body.type === 1) {
// typeCondition = {
// orgChild1Id: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild2Id: IsNull(),
// };
// }
// } else if (body.type === 2) {
// typeCondition = {
// orgChild2Id: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild3Id: IsNull(),
// };
// }
// } else if (body.type === 3) {
// typeCondition = {
// orgChild3Id: body.id,
// };
// if (!body.isAll) {
// checkChildConditions = {
// orgChild4Id: IsNull(),
// };
// }
// } else if (body.type === 4) {
// typeCondition = {
// orgChild4Id: body.id,
// };
// } // }
const formattedData = await Promise.all( // const keywordPosMasterCondition = body.keyword
posMaster.map(async (posMaster) => { // ? {
const positions = await this.positionRepository.find({ // posMasterNoPrefix: Like(`%${body.keyword}%`),
where: { posMasterId: posMaster.id }, // positionName: Like(`%${body.keyword}%`),
relations: ["posLevel", "posType", "posExecutive"], // posTypeName: Like(`%${body.keyword}%`),
}); // posLevelName: Like(`%${body.keyword}%`),
// }
// : {};
return { let data = AppDataSource.getRepository(PosMaster)
id: posMaster.id, .createQueryBuilder("posMaster")
posMasterNoPrefix: posMaster.posMasterNoPrefix, .leftJoin("posMaster.positions", "position")
posMasterNo: posMaster.posMasterNo, .leftJoin("position.posType", "posType")
posMasterNoSuffix: posMaster.posMasterNoSuffix, .leftJoin("position.posLevel", "posLevel")
positions: positions.map((position) => ({ .andWhere("posMaster.posMasterNoPrefix LIKE :keyword", { keyword: `%${body.keyword}%` })
id: position.id, .orWhere("position.positionName LIKE :keyword", { keyword: `%${body.keyword}%` })
positionName: position.positionName, .orWhere("posType.posTypeName LIKE :keyword", { keyword: `%${body.keyword}%` })
positionField: position.positionField, .orWhere("posLevel.posLevelName LIKE :keyword", { keyword: `%${body.keyword}%` })
posTypeId: position.posTypeId, .orderBy("posMaster.posMasterOrder", "ASC")
posTypeName: position.posType == null ? null : position.posType.posTypeName, .skip((body.page - 1) * body.pageSize)
posLevelId: position.posLevelId, .take(body.pageSize);
posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName,
posExecutiveId: position.posExecutiveId, if (body.type === 0) {
posExecutiveName: data = data.andWhere("posMaster.orgRootId = :orgRootId", { orgRootId: body.id });
position.posExecutive == null ? null : position.posExecutive.posExecutiveName, if (!body.isAll) {
positionExecutiveField: position.positionExecutiveField, data = data.andWhere("posMaster.orgChild1Id IS NULL");
positionArea: position.positionArea, }
positionIsSelected: position.positionIsSelected, } else if (body.type === 1) {
})), data = data.andWhere("posMaster.orgChild1Id = :orgChild1Id", { orgChild1Id: body.id });
}; if (!body.isAll) {
}), data = data.andWhere("posMaster.orgChild2Id IS NULL");
); }
return new HttpSuccess({ data: formattedData, total }); } else if (body.type === 2) {
data = data.andWhere("posMaster.orgChild2Id = :orgChild2Id", { orgChild2Id: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild3Id IS NULL");
}
} else if (body.type === 3) {
data = data.andWhere("posMaster.orgChild3Id = :orgChild3Id", { orgChild3Id: body.id });
if (!body.isAll) {
data = data.andWhere("posMaster.orgChild4Id IS NULL");
}
} else if (body.type === 4) {
data = data.andWhere("posMaster.orgChild4Id = :orgChild4Id", { orgChild4Id: body.id });
}
const [posMaster, total] = await data.getManyAndCount();
// const posMaster = await this.posMasterRepository.find({
// where: {
// ...typeCondition,
// ...checkChildConditions,
// },
// order: { posMasterOrder: "ASC" },
// skip: (body.page - 1) * body.pageSize,
// take: body.pageSize,
// });
// const total = posMaster.length;
const formattedData = await Promise.all(
posMaster.map(async (posMaster) => {
const positions = await this.positionRepository.find({
where: {
posMasterId: posMaster.id,
},
relations: ["posLevel", "posType", "posExecutive"],
});
return {
id: posMaster.id,
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
positions: positions.map((position) => ({
id: position.id,
positionName: position.positionName,
positionField: position.positionField,
posTypeId: position.posTypeId,
posTypeName: position.posType == null ? null : position.posType.posTypeName,
posLevelId: position.posLevelId,
posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName,
posExecutiveId: position.posExecutiveId,
posExecutiveName:
position.posExecutive == null ? null : position.posExecutive.posExecutiveName,
positionExecutiveField: position.positionExecutiveField,
positionArea: position.positionArea,
positionIsSelected: position.positionIsSelected,
})),
};
}),
);
return new HttpSuccess({ data: formattedData, total });
} catch (error) { } catch (error) {
return error; return error;
} }
@ -1000,27 +1038,27 @@ export class PositionController extends Controller {
* @summary ORG_041 - (ADMIN) #44 * @summary ORG_041 - (ADMIN) #44
* *
*/ */
@Post("executive") @Post("executive")
async createPosExecutive( async createPosExecutive(
@Body() @Body()
requestBody: CreatePosMaster, requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> } @Request() request: { user: Record<string, any> },
) { ) {
const posExecutive = Object.assign(new PosExecutive(), requestBody); const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) { if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
} }
try { try {
posExecutive.createdUserId = request.user.sub; posExecutive.createdUserId = request.user.sub;
posExecutive.createdFullName = request.user.name; posExecutive.createdFullName = request.user.name;
posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name; posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive); await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;
}
} }
}
/** /**
* API * API
@ -1029,28 +1067,27 @@ export class PositionController extends Controller {
* *
* @param {string} id Id * @param {string} id Id
*/ */
@Put("executive/{id}") @Put("executive/{id}")
async updatePosExecutive( async updatePosExecutive(
@Path() id: string, @Path() id: string,
@Body() @Body()
requestBody: CreatePosMaster, requestBody: CreatePosMaster,
@Request() request: { user: Record<string, any> } @Request() request: { user: Record<string, any> },
) { ) {
const posExecutive = Object.assign(new PosExecutive(), requestBody); const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) { if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
} }
try { try {
posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name; posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive); await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;
}
} }
}
/** /**
* API * API
* *
@ -1074,5 +1111,4 @@ export class PositionController extends Controller {
return error; return error;
} }
} }
} }