hrms-api-org/src/controllers/ProfileInsigniaController.ts

109 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-03-12 16:48:44 +07:00
import {
Body,
Controller,
Delete,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import {
CreateProfileInsignia,
ProfileInsignia,
UpdateProfileInsignia,
} from "../entities/ProfileInsignia";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { ProfileInsigniaHistory } from "../entities/ProfileInsigniaHistory";
import { RequestWithUser } from "../middlewares/user";
@Route("api/v1/org/profile/insignia")
@Tags("ProfileInsignia")
@Security("bearerAuth")
export class ProfileInsigniaController extends Controller {
private insigniaRepo = AppDataSource.getRepository(ProfileInsignia);
private insigniaHistoryRepo = AppDataSource.getRepository(ProfileInsigniaHistory);
@Get("{profileId}")
public async getInsignia(@Path() profileId: string) {
const record = await this.insigniaRepo.findBy({ profileId });
return new HttpSuccess(record);
}
@Get("history/{InsigniaId}")
public async getInsigniaHistory(@Path() InsigniaId: string) {
const record = await this.insigniaHistoryRepo.findBy({
profileInsigniaId: InsigniaId,
});
2024-03-12 17:01:02 +07:00
return new HttpSuccess(record);
2024-03-12 16:48:44 +07:00
}
@Post()
public async newInsignia(@Request() req: RequestWithUser, @Body() body: CreateProfileInsignia) {
const data = new ProfileInsignia();
const history = new ProfileInsigniaHistory();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
Object.assign(history, { ...body, ...meta });
const result = await this.insigniaRepo.save(data);
history.profileInsigniaId = result.id;
await this.insigniaHistoryRepo.save(data);
return new HttpSuccess();
}
@Patch("{insigniaId}")
public async editInsignia(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileInsignia,
@Path() insigniaId: string,
) {
const record = await this.insigniaRepo.findOneBy({ id: insigniaId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileInsigniaHistory();
Object.assign(record, body);
Object.assign(history, { ...body, id: undefined });
history.profileInsigniaId = insigniaId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([this.insigniaRepo.save(record), this.insigniaHistoryRepo.save(history)]);
return new HttpSuccess();
}
@Delete("{insigniaId}")
public async deleteInsignia(@Path() insigniaId: string) {
await this.insigniaHistoryRepo.delete({
profileInsigniaId: insigniaId,
});
const result = await this.insigniaRepo.delete({ id: insigniaId });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}