Merge branch 'develop' of github.com:Frappet/bma-ehr-kpi into develop

This commit is contained in:
Kittapath 2024-05-07 14:44:56 +07:00
commit bea28e0cc1
5 changed files with 355 additions and 13 deletions

View file

@ -22,7 +22,8 @@ import HttpStatusCode from "../interfaces/http-status";
import { KpiPlan, createKpiPlan, updateKpiPlan } from "../entities/kpiPlan";
import CallAPI from "../interfaces/call-api";
import { KpiPeriod } from "../entities/kpiPeriod";
import { Brackets } from "typeorm";
import { Brackets, IsNull, Not } from "typeorm";
import { KpiPlanHistory } from "../entities/kpiPlanHistory";
@Route("api/v1/kpi/plan")
@Tags("kpiPlan")
@ -34,6 +35,7 @@ import { Brackets } from "typeorm";
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class kpiPlanController extends Controller {
private kpiPlanRepository = AppDataSource.getRepository(KpiPlan);
private kpiPlanHistoryRepository = AppDataSource.getRepository(KpiPlanHistory);
private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod);
/**
*
@ -96,11 +98,78 @@ export class kpiPlanController extends Controller {
})
.catch((x) => {});
let maxIncludingPlan: any;
if (requestBody.node == 0) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.rootId = :rootId AND kpiPlan.child1Id IS NULL", {
rootId: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 1) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.child1Id = :child1Id AND kpiPlan.child2Id IS NULL", {
child1Id: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 2) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.child2Id = :child2Id AND kpiPlan.child3Id IS NULL", {
child2Id: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 3) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.child3Id = :child3Id AND kpiPlan.child4Id IS NULL", {
child3Id: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 4) {
maxIncludingPlan = await this.kpiPlanRepository
.createQueryBuilder("kpiPlan")
.select("MAX(kpiPlan.including)", "maxIncluding")
.where("kpiPlan.child4Id = :child4Id", {
child4Id: requestBody.nodeId,
})
.andWhere("kpiPlan.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
}
kpiPlan.including = maxIncludingPlan.maxIncluding + 1;
kpiPlan.createdUserId = request.user.sub;
kpiPlan.createdFullName = request.user.name;
kpiPlan.lastUpdateUserId = request.user.sub;
kpiPlan.lastUpdateFullName = request.user.name;
await this.kpiPlanRepository.save(kpiPlan);
const history = new KpiPlanHistory();
history.kpiPlanId = kpiPlan.id;
history.createdUserId = request.user.sub;
history.createdFullName = request.user.name;
history.lastUpdateUserId = request.user.sub;
history.lastUpdateFullName = request.user.name;
await this.kpiPlanHistoryRepository.save(history);
return new HttpSuccess(kpiPlan.id);
}
@ -178,6 +247,15 @@ export class kpiPlanController extends Controller {
kpiPlan.lastUpdateUserId = request.user.sub;
kpiPlan.lastUpdateFullName = request.user.name;
await this.kpiPlanRepository.save(kpiPlan);
const history = new KpiPlanHistory();
history.kpiPlanId = kpiPlan.id;
history.createdUserId = request.user.sub;
history.createdFullName = request.user.name;
history.lastUpdateUserId = request.user.sub;
history.lastUpdateFullName = request.user.name;
await this.kpiPlanHistoryRepository.save(history);
return new HttpSuccess(id);
}
@ -357,7 +435,77 @@ export class kpiPlanController extends Controller {
if (!kpiPlan) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามแผนฯนี้");
}
let type = 0;
if (kpiPlan.child1Id != null) {
type = 1;
} else if (kpiPlan.child2Id != null) {
type = 2;
} else if (kpiPlan.child3Id != null) {
type = 3;
} else if (kpiPlan.child4Id != null) {
type = 4;
}
await this.kpiPlanHistoryRepository.delete({ kpiPlanId: id });
await this.kpiPlanRepository.remove(kpiPlan);
if (kpiPlan) {
let remainingKpiPlans: any;
if (type == 0) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
rootId: kpiPlan.rootId,
child1Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 1) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
child1Id: kpiPlan.child1Id,
child2Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 2) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
child2Id: kpiPlan.child2Id,
child3Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 3) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
child3Id: kpiPlan.child3Id,
child4Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 4) {
remainingKpiPlans = await this.kpiPlanRepository.find({
where: {
child4Id: kpiPlan.child4Id,
},
order: {
including: "ASC",
},
});
}
remainingKpiPlans.forEach((kpiPlan: any, index: any) => {
kpiPlan.including = index + 1;
});
await this.kpiPlanRepository.save(remainingKpiPlans);
}
return new HttpSuccess();
}
}

View file

@ -22,7 +22,8 @@ import HttpStatusCode from "../interfaces/http-status";
import { KpiRole, createKpiRole, updateKpiRole } from "../entities/kpiRole";
import CallAPI from "../interfaces/call-api";
import { KpiPeriod } from "../entities/kpiPeriod";
import { Brackets } from "typeorm";
import { Brackets, IsNull } from "typeorm";
import { KpiRoleHistory } from "../entities/kpiRoleHistory";
@Route("api/v1/kpi/role")
@Tags("kpiRole")
@ -34,6 +35,7 @@ import { Brackets } from "typeorm";
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class kpiRoleController extends Controller {
private kpiRoleRepository = AppDataSource.getRepository(KpiRole);
private kpiRoleHistoryRepository = AppDataSource.getRepository(KpiRoleHistory);
private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod);
/**
*
@ -77,11 +79,79 @@ export class kpiRoleController extends Controller {
kpiRole.child4ShortName = requestBody.node <= 3 ? null : x.child4ShortName;
})
.catch((x) => {});
let maxIncludingRole: any;
if (requestBody.node == 0) {
maxIncludingRole = await this.kpiRoleRepository
.createQueryBuilder("kpiRole")
.select("MAX(kpiRole.including)", "maxIncluding")
.where("kpiRole.rootId = :rootId AND kpiRole.child1Id IS NULL", {
rootId: requestBody.nodeId,
})
.andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 1) {
maxIncludingRole = await this.kpiRoleRepository
.createQueryBuilder("kpiRole")
.select("MAX(kpiRole.including)", "maxIncluding")
.where("kpiRole.child1Id = :child1Id AND kpiRole.child2Id IS NULL", {
child1Id: requestBody.nodeId,
})
.andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 2) {
maxIncludingRole = await this.kpiRoleRepository
.createQueryBuilder("kpiRole")
.select("MAX(kpiRole.including)", "maxIncluding")
.where("kpiRole.child2Id = :child2Id AND kpiRole.child3Id IS NULL", {
child2Id: requestBody.nodeId,
})
.andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 3) {
maxIncludingRole = await this.kpiRoleRepository
.createQueryBuilder("kpiRole")
.select("MAX(kpiRole.including)", "maxIncluding")
.where("kpiRole.child3Id = :child3Id AND kpiRole.child4Id IS NULL", {
child3Id: requestBody.nodeId,
})
.andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
} else if (requestBody.node == 4) {
maxIncludingRole = await this.kpiRoleRepository
.createQueryBuilder("kpiRole")
.select("MAX(kpiRole.including)", "maxIncluding")
.where("kpiRole.child4Id = :child4Id", {
child4Id: requestBody.nodeId,
})
.andWhere("kpiRole.kpiPeriodId = :kpiPeriodId", {
kpiPeriodId: requestBody.kpiPeriodId,
})
.getRawOne();
}
kpiRole.including = maxIncludingRole.maxIncluding + 1;
kpiRole.createdUserId = request.user.sub;
kpiRole.createdFullName = request.user.name;
kpiRole.lastUpdateUserId = request.user.sub;
kpiRole.lastUpdateFullName = request.user.name;
await this.kpiRoleRepository.save(kpiRole);
const history = new KpiRoleHistory();
history.kpiRoleId = kpiRole.id;
history.createdUserId = request.user.sub;
history.createdFullName = request.user.name;
history.lastUpdateUserId = request.user.sub;
history.lastUpdateFullName = request.user.name;
await this.kpiRoleHistoryRepository.save(history);
return new HttpSuccess(kpiRole.id);
}
@ -141,6 +211,15 @@ export class kpiRoleController extends Controller {
kpiRole.lastUpdateUserId = request.user.sub;
kpiRole.lastUpdateFullName = request.user.name;
await this.kpiRoleRepository.save(kpiRole);
const history = new KpiRoleHistory();
history.kpiRoleId = kpiRole.id;
history.createdUserId = request.user.sub;
history.createdFullName = request.user.name;
history.lastUpdateUserId = request.user.sub;
history.lastUpdateFullName = request.user.name;
await this.kpiRoleHistoryRepository.save(history);
return new HttpSuccess(id);
}
@ -227,6 +306,7 @@ export class kpiRoleController extends Controller {
@Query("node") node?: number | null,
@Query("keyword") keyword?: string,
@Query("position") position?: string,
@Query("isAll") isAll: boolean = false,
) {
const [kpiRole, total] = await AppDataSource.getRepository(KpiRole)
.createQueryBuilder("kpiRole")
@ -294,7 +374,77 @@ export class kpiRoleController extends Controller {
if (!kpiRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัดตามตำแหน่งนี้");
}
let type = 0;
if (kpiRole.child1Id != null) {
type = 1;
} else if (kpiRole.child2Id != null) {
type = 2;
} else if (kpiRole.child3Id != null) {
type = 3;
} else if (kpiRole.child4Id != null) {
type = 4;
}
await this.kpiRoleHistoryRepository.delete({ kpiRoleId: id });
await this.kpiRoleRepository.remove(kpiRole);
if (kpiRole) {
let remainingKpiRoles: any;
if (type == 0) {
remainingKpiRoles = await this.kpiRoleRepository.find({
where: {
rootId: kpiRole.rootId,
child1Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 1) {
remainingKpiRoles = await this.kpiRoleRepository.find({
where: {
child1Id: kpiRole.child1Id,
child2Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 2) {
remainingKpiRoles = await this.kpiRoleRepository.find({
where: {
child2Id: kpiRole.child2Id,
child3Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 3) {
remainingKpiRoles = await this.kpiRoleRepository.find({
where: {
child3Id: kpiRole.child3Id,
child4Id: IsNull(),
},
order: {
including: "ASC",
},
});
} else if (type == 4) {
remainingKpiRoles = await this.kpiRoleRepository.find({
where: {
child4Id: kpiRole.child4Id,
},
order: {
including: "ASC",
},
});
}
remainingKpiRoles.forEach((kpiRole: any, index: any) => {
kpiRole.including = index + 1;
});
await this.kpiRoleRepository.save(remainingKpiRoles);
}
return new HttpSuccess();
}
}

View file

@ -321,6 +321,10 @@ export class createKpiPlan {
// @Column()
// including: number | null;
@Column()
period: string | null;
@Column()
year: string | null;
@Column()
includingName: string | null;
@Column()
target: string | null;
@ -354,14 +358,18 @@ export class createKpiPlan {
strategyId: string | null;
@Column()
kpiPeriodId: string | null;
@Column()
documentInfoEvidence: string | null;
// @Column()
// documentInfoEvidence: string | null;
}
export class updateKpiPlan {
// @Column()
// including: number | null;
@Column()
period: string | null;
@Column()
year: string | null;
@Column()
includingName: string | null;
@Column()
target: string | null;
@ -395,6 +403,6 @@ export class updateKpiPlan {
strategyId: string | null;
@Column()
kpiPeriodId: string | null;
@Column()
documentInfoEvidence: string | null;
// @Column()
// documentInfoEvidence: string | null;
}

View file

@ -32,7 +32,7 @@ export class KpiRole extends EntityBase {
comment: "ลำดับ/รหัสตัวชี้วัด",
default: null,
})
including: string;
including: number;
@Column({
nullable: true,
@ -252,7 +252,11 @@ export class createKpiRole {
@Column()
position: string | null;
// @Column()
// including: string | null;
// including: number | null;
@Column()
period: string | null;
@Column()
year: string | null;
@Column()
includingName: string | null;
@Column()
@ -283,15 +287,19 @@ export class createKpiRole {
formula: string | null;
@Column()
kpiPeriodId: string | null;
@Column()
documentInfoEvidence: string | null;
// @Column()
// documentInfoEvidence: string | null;
}
export class updateKpiRole {
@Column()
position: string | null;
// @Column()
// including: string | null;
// including: number | null;
@Column()
period: string | null;
@Column()
year: string | null;
@Column()
includingName: string | null;
@Column()
@ -322,6 +330,6 @@ export class updateKpiRole {
formula: string | null;
@Column()
kpiPeriodId: string | null;
@Column()
documentInfoEvidence: string | null;
// @Column()
// documentInfoEvidence: string | null;
}

View file

@ -0,0 +1,28 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class UpdateTableKpiPlanAddDate1715067815719 implements MigrationInterface {
name = 'UpdateTableKpiPlanAddDate1715067815719'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`kpiUserPlanned\` ADD \`documentInfoEvidence\` varchar(255) NULL COMMENT 'ข้อมูลเอกสารหลักฐาน'`);
await queryRunner.query(`ALTER TABLE \`kpiUserPlanned\` ADD \`startDate\` datetime NULL COMMENT 'ช่วงเวลาเริ่มเก็บข้อมูล'`);
await queryRunner.query(`ALTER TABLE \`kpiUserPlanned\` ADD \`endDate\` datetime NULL COMMENT 'ช่วงเวลาสิ้นสุดเก็บข้อมูล'`);
await queryRunner.query(`ALTER TABLE \`kpiUserRole\` ADD \`documentInfoEvidence\` varchar(255) NULL COMMENT 'ข้อมูลเอกสารหลักฐาน'`);
await queryRunner.query(`ALTER TABLE \`kpiUserRole\` ADD \`startDate\` datetime NULL COMMENT 'ช่วงเวลาเริ่มเก็บข้อมูล'`);
await queryRunner.query(`ALTER TABLE \`kpiUserRole\` ADD \`endDate\` datetime NULL COMMENT 'ช่วงเวลาสิ้นสุดเก็บข้อมูล'`);
await queryRunner.query(`ALTER TABLE \`kpiRole\` DROP COLUMN \`including\``);
await queryRunner.query(`ALTER TABLE \`kpiRole\` ADD \`including\` int NULL COMMENT 'ลำดับ/รหัสตัวชี้วัด'`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`kpiRole\` DROP COLUMN \`including\``);
await queryRunner.query(`ALTER TABLE \`kpiRole\` ADD \`including\` varchar(255) NULL COMMENT 'ลำดับ/รหัสตัวชี้วัด'`);
await queryRunner.query(`ALTER TABLE \`kpiUserRole\` DROP COLUMN \`endDate\``);
await queryRunner.query(`ALTER TABLE \`kpiUserRole\` DROP COLUMN \`startDate\``);
await queryRunner.query(`ALTER TABLE \`kpiUserRole\` DROP COLUMN \`documentInfoEvidence\``);
await queryRunner.query(`ALTER TABLE \`kpiUserPlanned\` DROP COLUMN \`endDate\``);
await queryRunner.query(`ALTER TABLE \`kpiUserPlanned\` DROP COLUMN \`startDate\``);
await queryRunner.query(`ALTER TABLE \`kpiUserPlanned\` DROP COLUMN \`documentInfoEvidence\``);
}
}