สลับตำแหน่งเงินเดือน

This commit is contained in:
Kittapath 2024-03-28 11:44:16 +07:00
parent ab365257de
commit 021ee8cd6e
4 changed files with 74 additions and 1 deletions

View file

@ -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();
}
}