hrms-api-org/src/controllers/ProfileAddressEmployeeController.ts

136 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-05-16 10:32:51 +07:00
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";
@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);
/**
*
* @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
*
*/
2024-05-16 15:22:30 +07:00
@Get("history/{profileId}")
public async getProfileAddressHistory(@Path() profileId: string) {
2024-05-16 10:32:51 +07:00
const record = await this.profileAddressHistoryRepo.find({
2024-05-16 15:22:30 +07:00
where: { profileEmployeeId: profileId },
2024-05-16 10:32:51 +07:00
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
*
*/
2024-05-16 15:22:30 +07:00
@Patch("{profileId}")
2024-05-16 10:32:51 +07:00
public async editProfileAddress(
@Body() requestBody: UpdateProfileAddressEmployee,
@Request() req: RequestWithUser,
2024-05-16 15:22:30 +07:00
@Path() profileId: string,
2024-05-16 10:32:51 +07:00
) {
2024-05-16 15:22:30 +07:00
const record = await this.profileEmployeeRepo.findOneBy({ id: profileId });
2024-05-16 10:32:51 +07:00
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileAddressHistory();
Object.assign(history, { ...record, id: undefined });
Object.assign(record, requestBody);
2024-05-16 15:22:30 +07:00
history.profileEmployeeId = profileId;
2024-05-16 10:32:51 +07:00
history.lastUpdateFullName = req.user.name;
record.lastUpdateFullName = req.user.name;
await Promise.all([
this.profileEmployeeRepo.save(record),
this.profileAddressHistoryRepo.save(history),
]);
return new HttpSuccess();
}
}