refactor: CRUD profileInformation
This commit is contained in:
parent
2ab0032236
commit
5cd4beebed
1 changed files with 190 additions and 0 deletions
190
src/controllers/ProfileInformationController.ts
Normal file
190
src/controllers/ProfileInformationController.ts
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
Body,
|
||||
Path,
|
||||
Request,
|
||||
SuccessResponse,
|
||||
Response,
|
||||
Get,
|
||||
Query,
|
||||
Patch,
|
||||
Example,
|
||||
} from "tsoa";
|
||||
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
|
||||
import {
|
||||
ProfileInformationHistory,
|
||||
ProfileInformation,
|
||||
CreateProfileInformation,
|
||||
UpdateProfileInformation,
|
||||
} from "../entities/ProfileInformation";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
import { Profile } from "../entities/Profile";
|
||||
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
|
||||
@Route("api/v1/org/profile/information")
|
||||
@Tags("ProfileInformation")
|
||||
@Security("bearerAuth")
|
||||
export class ProfileInformationController extends Controller {
|
||||
private profileRepo = AppDataSource.getRepository(Profile);
|
||||
private profileInformationRepo = AppDataSource.getRepository(ProfileInformation);
|
||||
private profileInformationHistoryRepo = AppDataSource.getRepository(ProfileInformationHistory);
|
||||
|
||||
@Get("{profileId}")
|
||||
@Example({
|
||||
status: 200,
|
||||
message: "สำเร็จ",
|
||||
result: [
|
||||
{
|
||||
id: "42deba79-0725-403f-898d-6c142b2842a5",
|
||||
createdAt: "2024-03-19T19:47:26.512Z",
|
||||
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
|
||||
lastUpdatedAt: "2024-03-19T20:01:15.000Z",
|
||||
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
|
||||
createdFullName: "สาวิตรี ศรีสมัย",
|
||||
lastUpdateFullName: "สาวิตรี ศรีสมัย",
|
||||
citizenId: "1849900687228",
|
||||
prefix: "นาย",
|
||||
firstName: "ธนพนธ์",
|
||||
lastName: "แสงจันทร์",
|
||||
birthDate: "2024-03-20T02:59:27.000Z",
|
||||
ethnicity: "ไทย",
|
||||
religion: "-",
|
||||
telephoneNumber: "0639195701",
|
||||
genderId: "74ec022c-b961-47f4-985e-2d9cbb10984c",
|
||||
relationshipId: "5872f993-6dc3-44c7-85d3-f56825abd96d",
|
||||
bloodGroupId: "fab11ded-8177-4e23-a791-78a10bc92333",
|
||||
profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
||||
},
|
||||
],
|
||||
})
|
||||
public async detailProfileInformation(@Path() profileId: string) {
|
||||
const getProfileInformation = await this.profileInformationRepo.findBy({ profileId });
|
||||
if (!getProfileInformation) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
return new HttpSuccess(getProfileInformation);
|
||||
}
|
||||
|
||||
@Get("history/{informationId}")
|
||||
@Example({
|
||||
status: 200,
|
||||
message: "สำเร็จ",
|
||||
result: [
|
||||
{
|
||||
id: "9abba9df-5fa0-4056-847d-4b3680b61ee5",
|
||||
createdAt: "2024-03-19T20:07:24.320Z",
|
||||
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
|
||||
lastUpdatedAt: "2024-03-19T20:07:24.320Z",
|
||||
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
|
||||
createdFullName: "string",
|
||||
lastUpdateFullName: "สาวิตรี ศรีสมัย",
|
||||
citizenId: "1849900687228",
|
||||
prefix: "นาย",
|
||||
firstName: "ธนพนธ์",
|
||||
lastName: "แสงจันทร์",
|
||||
birthDate: "2024-03-20T03:05:41.000Z",
|
||||
ethnicity: "ไทย",
|
||||
religion: "-",
|
||||
telephoneNumber: "0639195701",
|
||||
genderId: "74ec022c-b961-47f4-985e-2d9cbb10984c",
|
||||
relationshipId: "5872f993-6dc3-44c7-85d3-f56825abd96d",
|
||||
bloodGroupId: "fab11ded-8177-4e23-a791-78a10bc92333",
|
||||
profileId: null,
|
||||
profileInformationId: "42deba79-0725-403f-898d-6c142b2842a5",
|
||||
},
|
||||
],
|
||||
})
|
||||
public async getProfileInformationHistory(@Path() informationId: string) {
|
||||
const record = await this.profileInformationHistoryRepo.findBy({
|
||||
profileInformationId: informationId,
|
||||
});
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
return new HttpSuccess(record);
|
||||
}
|
||||
|
||||
@Post()
|
||||
public async newProfileInformation(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: CreateProfileInformation,
|
||||
) {
|
||||
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 ProfileInformation();
|
||||
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.profileInformationRepo.save(data);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Patch("{informationId}")
|
||||
public async editProfileInformation(
|
||||
@Body() requestBody: UpdateProfileInformation,
|
||||
@Request() req: RequestWithUser,
|
||||
@Path() informationId: string,
|
||||
) {
|
||||
const record = await this.profileInformationRepo.findOneBy({ id: informationId });
|
||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
|
||||
const history = new ProfileInformationHistory();
|
||||
|
||||
console.log(requestBody);
|
||||
|
||||
Object.assign(record, requestBody);
|
||||
Object.assign(history, { ...requestBody, id: undefined });
|
||||
|
||||
history.profileInformationId = informationId;
|
||||
history.lastUpdateFullName = req.user.name;
|
||||
history.createdUserId = req.user.sub;
|
||||
history.lastUpdateUserId = req.user.sub;
|
||||
record.lastUpdateFullName = req.user.name;
|
||||
|
||||
await Promise.all([
|
||||
this.profileInformationRepo.save(record),
|
||||
this.profileInformationHistoryRepo.save(history),
|
||||
]);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Delete("{informationId}")
|
||||
public async deleteProfileInformation(@Path() informationId: string) {
|
||||
await this.profileInformationHistoryRepo.delete({
|
||||
profileInformationId: informationId,
|
||||
});
|
||||
|
||||
const result = await this.profileInformationRepo.delete({ id: informationId });
|
||||
|
||||
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