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

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

@ -31,6 +31,7 @@ import { Profile } from "../entities/Profile";
import { RequestWithUser } from "../middlewares/user";
import permission from "../interfaces/permission";
import { PermissionOrg } from "../entities/PermissionOrg";
import FunctionMain from "../interfaces/functionMain";
@Route("api/v1/org")
@Tags("Organization")
@ -120,7 +121,7 @@ export class OrganizationController extends Controller {
@Post("draft")
async CreateOrgRevision(
@Body() requestBody: CreateOrgRevision,
@Request() request: { user: Record<string, any> },
@Request() request: RequestWithUser,
) {
//new main revision
const revision = Object.assign(new OrgRevision(), requestBody) as OrgRevision;
@ -2261,7 +2262,7 @@ export class OrganizationController extends Controller {
id: string;
type: number;
},
@Request() request: { user: Record<string, any> },
@Request() request: RequestWithUser,
) {
if (requestBody.type == 1) {
const orgChild1 = await this.child1Repository.findOne({
@ -2515,7 +2516,7 @@ export class OrganizationController extends Controller {
* @param {string} id Id revison
*/
@Get("get/publish")
async runPublish(@Request() request: { user: Record<string, any> }) {
async runPublish(@Request() request: RequestWithUser) {
const today = new Date();
today.setHours(0, 0, 0, 0); // Set time to the beginning of the day
const orgRevisionPublish = await this.orgRevisionRepository
@ -2589,7 +2590,7 @@ export class OrganizationController extends Controller {
: item != null && item?.orgRoot != null
? `${item.orgRoot.orgRootShortName}${item.posMasterNo}`
: null;
await new CallAPI().PostData(request, "/org/profile/salary", {
await new FunctionMain().newSalaryFunction(request, {
profileId: item.next_holderId,
date: new Date(),
amount: profileSalary?.amount ?? null,
@ -4733,7 +4734,7 @@ export class OrganizationController extends Controller {
* @param {string} id
*/
@Get("approver/{id}")
async getUserRootOrg(@Path() id: string, @Request() request: { user: Record<string, any> }) {
async getUserRootOrg(@Path() id: string, @Request() request: RequestWithUser) {
if (id == "00000000-0000-0000-0000-000000000000") {
const maps = {
id: "00000000-0000-0000-0000-000000000000",

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;