แก้apiข้อมูลหลัก
This commit is contained in:
parent
73e07dfed6
commit
6b78a365fa
26 changed files with 1816 additions and 988 deletions
128
src/controllers/ProfileAddressController.ts
Normal file
128
src/controllers/ProfileAddressController.ts
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
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";
|
||||
|
||||
@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);
|
||||
|
||||
/**
|
||||
*
|
||||
* @summary ข้อมูลที่อยู่
|
||||
*
|
||||
*/
|
||||
@Get("{profileId}")
|
||||
public async detailProfileAddress(@Path() profileId: string) {
|
||||
const getProfileAddress = await this.profileRepo.findOne({
|
||||
where: { id: profileId },
|
||||
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("history/{addressId}")
|
||||
public async getProfileAddressHistory(@Path() addressId: string) {
|
||||
const record = await this.profileAddressHistoryRepo.find({
|
||||
where: {
|
||||
id: addressId,
|
||||
},
|
||||
select: [
|
||||
"registrationAddress",
|
||||
"registrationProvinceId",
|
||||
"registrationDistrictId",
|
||||
"registrationSubDistrictId",
|
||||
"registrationZipCode",
|
||||
"currentAddress",
|
||||
"currentProvinceId",
|
||||
"currentDistrictId",
|
||||
"currentSubDistrictId",
|
||||
"currentZipCode",
|
||||
"createdFullName",
|
||||
"createdAt",
|
||||
],
|
||||
});
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
return new HttpSuccess(record);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @summary แก้ไขที่อยู่
|
||||
*
|
||||
*/
|
||||
@Patch("{addressId}")
|
||||
public async editProfileAddress(
|
||||
@Body() requestBody: UpdateProfileAddress,
|
||||
@Request() req: RequestWithUser,
|
||||
@Path() addressId: string,
|
||||
) {
|
||||
const record = await this.profileRepo.findOneBy({ id: addressId });
|
||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
|
||||
const history = new ProfileAddressHistory();
|
||||
|
||||
Object.assign(history, { ...record, id: undefined });
|
||||
Object.assign(record, requestBody);
|
||||
|
||||
history.profileId = addressId;
|
||||
history.lastUpdateFullName = req.user.name;
|
||||
record.lastUpdateFullName = req.user.name;
|
||||
|
||||
await Promise.all([
|
||||
this.profileRepo.save(record),
|
||||
this.profileAddressHistoryRepo.save(history),
|
||||
]);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue