สร้างฟังก์ชันกลาง

This commit is contained in:
kittapath 2024-09-27 23:44:47 +07:00
parent 90100e3460
commit 32b50c905c
3 changed files with 77 additions and 10 deletions

View file

@ -12,16 +12,28 @@ import {
Path,
} from "tsoa";
import axios from "axios";
import HttpError from "./http-error";
import { CreateProfileSalary, ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { RequestWithUser } from "../middlewares/user";
import HttpStatus from "./http-status";
import HttpSuccess from "./http-success";
import permission from "./permission";
import { AppDataSource } from "../database/data-source";
import { Profile } from "../entities/Profile";
class CallAPI {
private profileRepo = AppDataSource.getRepository(Profile);
private salaryRepo = AppDataSource.getRepository(ProfileSalary);
private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory);
//Get
public async GetData(request: any, @Path() path: any) {
const token = request.headers.authorization;
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
const url = process.env.API_URL + path;
try {
const response = await axios.get(url, {
headers: {
Authorization: `${token}`,
Authorization: `${token}`.includes("Bearer "),
"Content-Type": "application/json",
api_key: process.env.API_KEY,
},
@ -33,12 +45,12 @@ class CallAPI {
}
//Get (response.data)
public async GetData2(request: any, @Path() path: any) {
const token = request.headers.authorization;
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
const url = process.env.API_URL + path;
try {
const response = await axios.get(url, {
headers: {
Authorization: `${token}`,
Authorization: `${token}`.includes("Bearer "),
"Content-Type": "application/json",
api_key: process.env.API_KEY,
},
@ -50,7 +62,7 @@ class CallAPI {
}
//Post
public async PostData(request: any, @Path() path: any, sendData: any) {
const token = request.headers.authorization;
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
const url = process.env.API_URL + path;
try {
const response = await axios.post(url, sendData, {
@ -62,6 +74,7 @@ class CallAPI {
});
return response.data.result;
} catch (error) {
console.log(error);
throw error;
}
}

View file

@ -0,0 +1,53 @@
import { CreateProfileSalary, ProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { RequestWithUser } from "../middlewares/user";
import { AppDataSource } from "../database/data-source";
import { Profile } from "../entities/Profile";
class FunctionMain {
private profileRepo = AppDataSource.getRepository(Profile);
private salaryRepo = AppDataSource.getRepository(ProfileSalary);
private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory);
public async newSalaryFunction(req: RequestWithUser, body: CreateProfileSalary) {
if (!body.profileId) {
// throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
return;
}
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
if (!profile) {
// throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
return;
}
const dest_item = await this.salaryRepo.findOne({
where: { profileId: body.profileId },
order: { order: "DESC" },
});
const data = new ProfileSalary();
const meta = {
order: dest_item == null ? 1 : dest_item.order + 1,
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(data, { ...body, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...data, id: undefined });
await this.salaryRepo.save(data);
history.profileSalaryId = data.id;
await this.salaryHistoryRepo.save(history);
return;
}
}
export default FunctionMain;