diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index f98d02e7..75bd5d27 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -20,6 +20,7 @@ import HttpError from "../interfaces/http-error"; import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory"; import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; +import { LessThan, MoreThan } from "typeorm"; @Route("api/v1/org/profile/salary") @Tags("ProfileSalary") @@ -59,7 +60,10 @@ export class ProfileSalaryController extends Controller { ], }) public async getSalary(@Path() profileId: string) { - const record = await this.salaryRepo.findBy({ profileId }); + const record = await this.salaryRepo.find({ + where: { profileId: profileId }, + order: { order: "ASC" }, + }); return new HttpSuccess(record); } @@ -133,9 +137,15 @@ export class ProfileSalaryController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: body.profileId }, + order: { order: "DESC" }, + }); + const data = new ProfileSalary(); const meta = { + order: dest_item == null ? 1 : dest_item.order, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, @@ -186,4 +196,37 @@ export class ProfileSalaryController extends Controller { return new HttpSuccess(); } + + @Get("swap/{direction}/{salaryId}") + public async swapSalary(@Path() direction: string, salaryId: string) { + const source_item = await this.salaryRepo.findOne({ where: { id: salaryId } }); + if (source_item == null) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const sourceOrder = source_item.order; + if (direction.trim().toUpperCase() == "UP") { + console.log("xxxxxxxxxxxxxxxxxxxxxxxxx"); + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: source_item.profileId, order: LessThan(sourceOrder) }, + order: { order: "DESC" }, + }); + console.log("vvvvvvvvvvvvvvvvvvvvvvv"); + if (dest_item == null) return new HttpSuccess(); + console.log("ccccccccccccccccccccccccc"); + var destOrder = dest_item.order; + dest_item.order = sourceOrder; + source_item.order = destOrder; + console.log("zzzzzzzzzzzzzzzzzzzzz"); + await Promise.all([this.salaryRepo.save(source_item), this.salaryRepo.save(dest_item)]); + } else { + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: source_item.profileId, order: MoreThan(sourceOrder) }, + order: { order: "ASC" }, + }); + if (dest_item == null) return new HttpSuccess(); + var destOrder = dest_item.order; + dest_item.order = sourceOrder; + source_item.order = destOrder; + await Promise.all([this.salaryRepo.save(source_item), this.salaryRepo.save(dest_item)]); + } + return new HttpSuccess(); + } } diff --git a/src/entities/ProfileSalary.ts b/src/entities/ProfileSalary.ts index c7ed0662..657ae14a 100644 --- a/src/entities/ProfileSalary.ts +++ b/src/entities/ProfileSalary.ts @@ -124,6 +124,13 @@ export class ProfileSalary extends EntityBase { }) templateDoc: string; + @Column({ + nullable: true, + comment: "ลำดับตำแหน่ง", + default: null, + }) + order: number; + @OneToMany(() => ProfileSalaryHistory, (profileSalaryHistory) => profileSalaryHistory.histories) profileSalaryHistories: ProfileSalaryHistory[]; diff --git a/src/entities/ProfileSalaryHistory.ts b/src/entities/ProfileSalaryHistory.ts index 2c38c47b..b1101d07 100644 --- a/src/entities/ProfileSalaryHistory.ts +++ b/src/entities/ProfileSalaryHistory.ts @@ -130,6 +130,13 @@ export class ProfileSalaryHistory extends EntityBase { }) profileSalaryId: string; + @Column({ + nullable: true, + comment: "ลำดับตำแหน่ง", + default: null, + }) + order: number; + @ManyToOne(() => ProfileSalary, (profileSalary) => profileSalary.profileSalaryHistories) @JoinColumn({ name: "profileSalaryId" }) histories: ProfileSalary; diff --git a/src/migration/1711599588075-update_table_profilesalary_add_order.ts b/src/migration/1711599588075-update_table_profilesalary_add_order.ts new file mode 100644 index 00000000..051326b4 --- /dev/null +++ b/src/migration/1711599588075-update_table_profilesalary_add_order.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfilesalaryAddOrder1711599588075 implements MigrationInterface { + name = 'UpdateTableProfilesalaryAddOrder1711599588075' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileSalaryHistory\` ADD \`order\` int NULL COMMENT 'ลำดับตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD \`order\` int NULL COMMENT 'ลำดับตำแหน่ง'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP COLUMN \`order\``); + await queryRunner.query(`ALTER TABLE \`profileSalaryHistory\` DROP COLUMN \`order\``); + } + +}