2024-08-30 18:02:34 +07:00
|
|
|
import { Controller, Route, Security, Tags, Body, Path, Request, Get, Patch } from "tsoa";
|
2024-03-26 23:07:55 +07:00
|
|
|
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import HttpStatus from "../interfaces/http-status";
|
|
|
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
|
|
|
import { Profile, ProfileAddressHistory, UpdateProfileAddress } from "../entities/Profile";
|
|
|
|
|
import { AppDataSource } from "../database/data-source";
|
2024-08-08 17:15:21 +07:00
|
|
|
import permission from "../interfaces/permission";
|
2024-03-26 23:07:55 +07:00
|
|
|
@Route("api/v1/org/profile/address")
|
|
|
|
|
@Tags("ProfileAddress")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
export class ProfileAddressController extends Controller {
|
|
|
|
|
private profileRepo = AppDataSource.getRepository(Profile);
|
|
|
|
|
private profileAddressHistoryRepo = AppDataSource.getRepository(ProfileAddressHistory);
|
|
|
|
|
|
2024-05-23 16:44:37 +07:00
|
|
|
@Get("user")
|
|
|
|
|
public async detailProfileAddressUser(@Request() request: { user: Record<string, any> }) {
|
|
|
|
|
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
|
|
|
|
|
if (!profile) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
|
|
|
}
|
|
|
|
|
const getProfileAddress = await this.profileRepo.findOne({
|
2024-05-23 17:20:15 +07:00
|
|
|
where: { id: profile.id },
|
2024-05-23 16:44:37 +07:00
|
|
|
select: [
|
|
|
|
|
"id",
|
|
|
|
|
"registrationAddress",
|
|
|
|
|
"registrationProvinceId",
|
|
|
|
|
"registrationDistrictId",
|
|
|
|
|
"registrationSubDistrictId",
|
|
|
|
|
"registrationZipCode",
|
|
|
|
|
"currentAddress",
|
|
|
|
|
"currentProvinceId",
|
|
|
|
|
"currentDistrictId",
|
|
|
|
|
"currentSubDistrictId",
|
|
|
|
|
"currentZipCode",
|
2024-08-30 18:02:34 +07:00
|
|
|
"createdAt",
|
2024-05-23 16:44:37 +07:00
|
|
|
],
|
2024-08-30 18:02:34 +07:00
|
|
|
order: { createdAt: "ASC" },
|
2024-05-23 16:44:37 +07:00
|
|
|
});
|
|
|
|
|
if (!getProfileAddress) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
return new HttpSuccess(getProfileAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 23:07:55 +07:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @summary ข้อมูลที่อยู่
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get("{profileId}")
|
2024-08-22 17:25:25 +07:00
|
|
|
public async detailProfileAddress(@Path() profileId: string, @Request() req: RequestWithUser) {
|
|
|
|
|
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
|
2024-03-26 23:07:55 +07:00
|
|
|
const getProfileAddress = await this.profileRepo.findOne({
|
|
|
|
|
where: { id: profileId },
|
|
|
|
|
select: [
|
|
|
|
|
"id",
|
|
|
|
|
"registrationAddress",
|
|
|
|
|
"registrationProvinceId",
|
|
|
|
|
"registrationDistrictId",
|
|
|
|
|
"registrationSubDistrictId",
|
|
|
|
|
"registrationZipCode",
|
|
|
|
|
"currentAddress",
|
|
|
|
|
"currentProvinceId",
|
|
|
|
|
"currentDistrictId",
|
|
|
|
|
"currentSubDistrictId",
|
|
|
|
|
"currentZipCode",
|
2024-08-30 18:02:34 +07:00
|
|
|
"createdAt",
|
2024-03-26 23:07:55 +07:00
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
if (!getProfileAddress) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
return new HttpSuccess(getProfileAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 18:06:54 +07:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @summary ประวัติแก้ไขที่อยู่ by keycloak
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get("history/user")
|
|
|
|
|
public async getProfileAddressHistoryUser(@Request() request: RequestWithUser) {
|
|
|
|
|
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
|
|
|
|
|
if (!profile) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
|
|
|
}
|
|
|
|
|
const record = await this.profileAddressHistoryRepo.find({
|
|
|
|
|
where: { profileId: profile.id },
|
2024-08-30 18:02:34 +07:00
|
|
|
order: { createdAt: "DESC" },
|
2024-05-24 18:06:54 +07:00
|
|
|
});
|
|
|
|
|
if (!record) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
return new HttpSuccess(record);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 23:07:55 +07:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @summary ประวัติแก้ไขที่อยู่
|
|
|
|
|
*
|
|
|
|
|
*/
|
2024-05-16 15:22:30 +07:00
|
|
|
@Get("history/{profileId}")
|
2024-08-30 18:02:34 +07:00
|
|
|
public async getProfileAddressHistory(
|
|
|
|
|
@Path() profileId: string,
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
) {
|
2024-08-22 17:25:25 +07:00
|
|
|
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
|
2024-03-26 23:07:55 +07:00
|
|
|
const record = await this.profileAddressHistoryRepo.find({
|
2024-05-16 15:22:30 +07:00
|
|
|
where: { profileId: profileId },
|
2024-08-30 18:02:34 +07:00
|
|
|
order: { createdAt: "DESC" },
|
2024-03-26 23:07:55 +07:00
|
|
|
});
|
|
|
|
|
if (!record) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
return new HttpSuccess(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @summary แก้ไขที่อยู่
|
|
|
|
|
*
|
|
|
|
|
*/
|
2024-05-16 15:22:30 +07:00
|
|
|
@Patch("{profileId}")
|
2024-03-26 23:07:55 +07:00
|
|
|
public async editProfileAddress(
|
2024-08-13 17:43:30 +07:00
|
|
|
@Body() body: UpdateProfileAddress,
|
2024-03-26 23:07:55 +07:00
|
|
|
@Request() req: RequestWithUser,
|
2024-05-16 15:22:30 +07:00
|
|
|
@Path() profileId: string,
|
2024-03-26 23:07:55 +07:00
|
|
|
) {
|
2024-08-22 17:25:25 +07:00
|
|
|
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profileId);
|
2024-05-16 15:22:30 +07:00
|
|
|
const record = await this.profileRepo.findOneBy({ id: profileId });
|
2024-03-26 23:07:55 +07:00
|
|
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
|
|
|
|
|
const history = new ProfileAddressHistory();
|
|
|
|
|
|
2024-08-13 17:43:30 +07:00
|
|
|
Object.assign(record, body);
|
2024-08-30 18:02:34 +07:00
|
|
|
Object.assign(history, { ...body, id: undefined });
|
2024-03-26 23:07:55 +07:00
|
|
|
|
2024-05-16 15:22:30 +07:00
|
|
|
history.profileId = profileId;
|
2024-08-13 17:43:30 +07:00
|
|
|
record.lastUpdateUserId = req.user.sub;
|
2024-03-26 23:07:55 +07:00
|
|
|
record.lastUpdateFullName = req.user.name;
|
2024-08-30 18:02:34 +07:00
|
|
|
record.lastUpdatedAt = new Date();
|
2024-08-13 17:43:30 +07:00
|
|
|
history.lastUpdateUserId = req.user.sub;
|
|
|
|
|
history.lastUpdateFullName = req.user.name;
|
|
|
|
|
history.createdUserId = req.user.sub;
|
|
|
|
|
history.createdFullName = req.user.name;
|
2024-08-30 18:02:34 +07:00
|
|
|
history.createdAt = new Date();
|
|
|
|
|
history.lastUpdatedAt = new Date();
|
2024-03-26 23:07:55 +07:00
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
this.profileRepo.save(record),
|
|
|
|
|
this.profileAddressHistoryRepo.save(history),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|