feat: profile gov endpoints (incomplete)
This commit is contained in:
parent
232211bcee
commit
0b69570738
1 changed files with 126 additions and 0 deletions
126
src/controllers/ProfileGovernmentController.ts
Normal file
126
src/controllers/ProfileGovernmentController.ts
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
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";
|
||||
|
||||
@Route("api/v1/org/profile/familyHistory")
|
||||
@Tags("ProfileFamilyHistory")
|
||||
@Security("bearerAuth")
|
||||
export class ProfileFamilyHistoryController extends Controller {
|
||||
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()
|
||||
public async newFamilyHistory(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: CreateProfileGovernment,
|
||||
) {
|
||||
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}")
|
||||
public async editFamilyHistory(
|
||||
@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}")
|
||||
public async deleteGovHistory(@Path() profileId: string) {
|
||||
const result = await this.govRepo.delete({ profileId: profileId });
|
||||
|
||||
if (result.affected && result.affected <= 0) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue