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 { RequestWithUser } from "../middlewares/user"; import { Profile, ProfileAddressHistory, UpdateProfileAddress } from "../entities/Profile"; import { AppDataSource } from "../database/data-source"; import { Province } from "../entities/Province"; import { District } from "../entities/District"; import { SubDistrict } from "../entities/SubDistrict"; import { ProfileEmployee, UpdateProfileAddressEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; @Route("api/v1/org/profile-employee/address") @Tags("ProfileAddressEmployee") @Security("bearerAuth") export class ProfileAddressEmployeeController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private profileAddressHistoryRepo = AppDataSource.getRepository(ProfileAddressHistory); @Get("user") public async detailProfileAddressUser(@Request() request: { user: Record }) { const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const getProfileAddress = await this.profileEmployeeRepo.findOne({ where: { id: profile.id }, select: [ "id", "registrationAddress", "registrationProvinceId", "registrationDistrictId", "registrationSubDistrictId", "registrationZipCode", "currentAddress", "currentProvinceId", "currentDistrictId", "currentSubDistrictId", "currentZipCode", ], }); if (!getProfileAddress) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(getProfileAddress); } /** * * @summary ข้อมูลที่อยู่ * */ @Get("{profileEmployeeId}") public async detailProfileAddress(@Path() profileEmployeeId: string) { const getProfileAddress = await this.profileEmployeeRepo.findOne({ where: { id: profileEmployeeId }, select: [ "id", "registrationAddress", "registrationProvinceId", "registrationDistrictId", "registrationSubDistrictId", "registrationZipCode", "currentAddress", "currentProvinceId", "currentDistrictId", "currentSubDistrictId", "currentZipCode", ], }); if (!getProfileAddress) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(getProfileAddress); } /** * * @summary ประวัติแก้ไขที่อยู่ by keycloak * */ @Get("history/user") public async getProfileAddressHistoryUser(@Request() request: RequestWithUser) { const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const record = await this.profileAddressHistoryRepo.find({ where: { profileEmployeeId: profile.id}, relations: { registrationProvince: true, registrationDistrict: true, registrationSubDistrict: true, currentProvince: true, currentDistrict: true, currentSubDistrict: true, }, select: [ "registrationAddress", "registrationProvinceId", "registrationDistrictId", "registrationSubDistrictId", "registrationZipCode", "currentAddress", "currentProvinceId", "currentDistrictId", "currentSubDistrictId", "currentZipCode", "lastUpdateFullName", "lastUpdatedAt", ], }); if (!record) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(record); } /** * * @summary ประวัติแก้ไขที่อยู่ * */ @Get("history/{profileId}") public async getProfileAddressHistory(@Path() profileId: string) { const record = await this.profileAddressHistoryRepo.find({ where: { profileEmployeeId: profileId }, relations: { registrationProvince: true, registrationDistrict: true, registrationSubDistrict: true, currentProvince: true, currentDistrict: true, currentSubDistrict: true, }, select: [ "registrationAddress", "registrationProvinceId", "registrationDistrictId", "registrationSubDistrictId", "registrationZipCode", "currentAddress", "currentProvinceId", "currentDistrictId", "currentSubDistrictId", "currentZipCode", "lastUpdateFullName", "lastUpdatedAt", ], }); if (!record) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(record); } /** * * @summary แก้ไขที่อยู่ * */ @Patch("{profileId}") public async editProfileAddress( @Body() requestBody: UpdateProfileAddressEmployee, @Request() req: RequestWithUser, @Path() profileId: string, ) { await new permission().PermissionUpdate(req,"SYS_REGISTRY_EMP"); const record = await this.profileEmployeeRepo.findOneBy({ id: profileId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileAddressHistory(); Object.assign(history, { ...record, id: undefined }); Object.assign(record, requestBody); history.profileEmployeeId = profileId; history.lastUpdateFullName = req.user.name; record.lastUpdateFullName = req.user.name; await Promise.all([ this.profileEmployeeRepo.save(record), this.profileAddressHistoryRepo.save(history), ]); return new HttpSuccess(); } }