2024-03-20 16:24:36 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Example,
|
|
|
|
|
Get,
|
|
|
|
|
Patch,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import { AppDataSource } from "../database/data-source";
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
|
|
|
import HttpStatus from "../interfaces/http-status";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
|
|
|
import { Profile } from "../entities/Profile";
|
|
|
|
|
import {
|
|
|
|
|
CreateProfileGovernment,
|
|
|
|
|
ProfileGovernment,
|
|
|
|
|
UpdateProfileGovernment,
|
|
|
|
|
} from "../entities/ProfileGovernment";
|
|
|
|
|
|
2024-03-20 16:29:40 +07:00
|
|
|
@Route("api/v1/org/profile/government")
|
|
|
|
|
@Tags("ProfileGovernment")
|
2024-03-20 16:24:36 +07:00
|
|
|
@Security("bearerAuth")
|
2024-03-20 16:29:40 +07:00
|
|
|
export class ProfileGovernmentHistoryController extends Controller {
|
2024-03-20 16:24:36 +07:00
|
|
|
private profileRepo = AppDataSource.getRepository(Profile);
|
|
|
|
|
private govRepo = AppDataSource.getRepository(ProfileGovernment);
|
|
|
|
|
|
|
|
|
|
@Get("{profileId}")
|
|
|
|
|
@Example({})
|
|
|
|
|
public async getGovHistory(@Path() profileId: string) {
|
|
|
|
|
const record = await this.govRepo.find({
|
|
|
|
|
take: 1,
|
|
|
|
|
order: { createdAt: "DESC" },
|
|
|
|
|
where: { profileId },
|
|
|
|
|
});
|
|
|
|
|
return new HttpSuccess({ ...record[0] });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("history/{profileId}")
|
|
|
|
|
@Example({})
|
|
|
|
|
public async govHistory(@Path() profileId: string) {
|
|
|
|
|
const record = await this.govRepo.find({
|
|
|
|
|
order: { lastUpdatedAt: "DESC" },
|
|
|
|
|
where: { profileId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
record.pop();
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
2024-03-22 16:32:15 +07:00
|
|
|
public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileGovernment) {
|
2024-03-20 16:24:36 +07:00
|
|
|
if (!body.profileId) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
|
|
|
|
|
|
|
|
|
|
if (!profile) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = new ProfileGovernment();
|
|
|
|
|
|
|
|
|
|
const meta = {
|
|
|
|
|
createdUserId: req.user.sub,
|
|
|
|
|
createdFullName: req.user.name,
|
|
|
|
|
lastUpdateUserId: req.user.sub,
|
|
|
|
|
lastUpdateFullName: req.user.name,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Object.assign(data, { ...body, ...meta });
|
|
|
|
|
|
|
|
|
|
await this.govRepo.save(data);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch("{profileId}")
|
2024-03-22 16:32:15 +07:00
|
|
|
public async editGov(
|
2024-03-20 16:24:36 +07:00
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: UpdateProfileGovernment,
|
|
|
|
|
@Path() profileId: string,
|
|
|
|
|
) {
|
|
|
|
|
const record = (
|
|
|
|
|
await this.govRepo.find({
|
|
|
|
|
take: 1,
|
|
|
|
|
order: { lastUpdatedAt: "DESC" },
|
|
|
|
|
where: { profileId },
|
|
|
|
|
})
|
|
|
|
|
)[0];
|
|
|
|
|
|
|
|
|
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
|
|
|
|
|
const historyData = new ProfileGovernment();
|
|
|
|
|
|
|
|
|
|
Object.assign(historyData, { ...record, ...body, id: undefined });
|
|
|
|
|
historyData.lastUpdateFullName = req.user.name;
|
|
|
|
|
historyData.lastUpdateFullName = req.user.name;
|
|
|
|
|
|
|
|
|
|
await this.govRepo.save(historyData);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{profileId}")
|
2024-03-22 16:32:15 +07:00
|
|
|
public async deleteGov(@Path() profileId: string) {
|
2024-03-20 16:24:36 +07:00
|
|
|
const result = await this.govRepo.delete({ profileId: profileId });
|
|
|
|
|
|
|
|
|
|
if (result.affected && result.affected <= 0) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|