Merge branch 'dev/methapon' into develop

This commit is contained in:
Methapon2001 2024-03-12 16:50:30 +07:00
commit 51beb3615d
5 changed files with 437 additions and 0 deletions

View file

@ -0,0 +1,116 @@
import {
Body,
Controller,
Delete,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import {
CreateProfileCertificate,
ProfileCertificate,
UpdateProfileCertificate,
} from "../entities/ProfileCertificate";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory";
import { RequestWithUser } from "../middlewares/user";
@Route("api/v1/org/profile/certificate")
@Tags("ProfileCertificate")
@Security("bearerAuth")
export class ProfileCertificateController extends Controller {
private certificateRepo = AppDataSource.getRepository(ProfileCertificate);
private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory);
@Get("{profileId}")
public async getCertificate(@Path() profileId: string) {
const record = await this.certificateRepo.findBy({ profileId });
return new HttpSuccess(record);
}
@Get("history/{certificateId}")
public async certificateHistory(@Path() certificateId: string) {
const record = await this.certificateHistoryRepo.findBy({
profileCertificateId: certificateId,
});
return record;
}
@Post()
public async newCertificate(
@Request() req: RequestWithUser,
@Body() body: CreateProfileCertificate,
) {
const data = new ProfileCertificate();
const history = new ProfileCertificateHistory();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
Object.assign(history, { ...body, ...meta });
const result = await this.certificateRepo.save(data);
history.profileCertificateId = result.id;
await this.certificateHistoryRepo.save(history);
return new HttpSuccess();
}
@Patch("{certificateId}")
public async editCertificate(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileCertificate,
@Path() certificateId: string,
) {
const record = await this.certificateRepo.findOneBy({ id: certificateId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileCertificateHistory();
Object.assign(record, body);
Object.assign(history, { ...body, id: undefined });
history.profileCertificateId = certificateId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([
this.certificateRepo.save(record),
this.certificateHistoryRepo.save(history),
]);
return new HttpSuccess();
}
@Delete("{certificateId}")
public async deleteCertificate(@Path() certificateId: string) {
await this.certificateHistoryRepo.delete({
profileCertificateId: certificateId,
});
const certificateResult = await this.certificateRepo.delete({
id: certificateId,
});
if (certificateResult.affected && certificateResult.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}

View file

@ -0,0 +1,104 @@
import {
Body,
Controller,
Delete,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { CreateProfileHonor, ProfileHonor, UpdateProfileHonor } from "../entities/ProfileHonor";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { ProfileHonorHistory } from "../entities/ProfileHonorHistory";
import { RequestWithUser } from "../middlewares/user";
@Route("api/v1/org/profile/honor")
@Tags("ProfileHonor")
@Security("bearerAuth")
export class ProfileHonorController extends Controller {
private honorRepo = AppDataSource.getRepository(ProfileHonor);
private honorHistoryRepo = AppDataSource.getRepository(ProfileHonorHistory);
@Get("{profileId}")
public async getHonor(@Path() profileId: string) {
const record = await this.honorRepo.findBy({ profileId });
return new HttpSuccess(record);
}
@Get("history/{honorId}")
public async honorHistory(@Path() honorId: string) {
const record = await this.honorHistoryRepo.findBy({
profileHonorId: honorId,
});
return record;
}
@Post()
public async newHonor(@Request() req: RequestWithUser, @Body() body: CreateProfileHonor) {
const data = new ProfileHonor();
const history = new ProfileHonorHistory();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
Object.assign(history, { ...body, ...meta });
const result = await this.honorRepo.save(data);
history.profileHonorId = result.id;
await this.honorHistoryRepo.save(history);
return new HttpSuccess();
}
@Patch("{honorId}")
public async editHonor(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileHonor,
@Path() honorId: string,
) {
const record = await this.honorRepo.findOneBy({ id: honorId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileHonorHistory();
Object.assign(record, body);
Object.assign(history, { ...body, id: undefined });
history.profileHonorId = honorId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]);
return new HttpSuccess();
}
@Delete("{honorId}")
public async deleteTraning(@Path() honorId: string) {
await this.honorHistoryRepo.delete({
profileHonorId: honorId,
});
const result = await this.honorRepo.delete({ id: honorId });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}

View file

@ -0,0 +1,108 @@
import {
Body,
Controller,
Delete,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import {
CreateProfileInsignia,
ProfileInsignia,
UpdateProfileInsignia,
} from "../entities/ProfileInsignia";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { ProfileInsigniaHistory } from "../entities/ProfileInsigniaHistory";
import { RequestWithUser } from "../middlewares/user";
@Route("api/v1/org/profile/insignia")
@Tags("ProfileInsignia")
@Security("bearerAuth")
export class ProfileInsigniaController extends Controller {
private insigniaRepo = AppDataSource.getRepository(ProfileInsignia);
private insigniaHistoryRepo = AppDataSource.getRepository(ProfileInsigniaHistory);
@Get("{profileId}")
public async getInsignia(@Path() profileId: string) {
const record = await this.insigniaRepo.findBy({ profileId });
return new HttpSuccess(record);
}
@Get("history/{InsigniaId}")
public async getInsigniaHistory(@Path() InsigniaId: string) {
const record = await this.insigniaHistoryRepo.findBy({
profileInsigniaId: InsigniaId,
});
return record;
}
@Post()
public async newInsignia(@Request() req: RequestWithUser, @Body() body: CreateProfileInsignia) {
const data = new ProfileInsignia();
const history = new ProfileInsigniaHistory();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
Object.assign(history, { ...body, ...meta });
const result = await this.insigniaRepo.save(data);
history.profileInsigniaId = result.id;
await this.insigniaHistoryRepo.save(data);
return new HttpSuccess();
}
@Patch("{insigniaId}")
public async editInsignia(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileInsignia,
@Path() insigniaId: string,
) {
const record = await this.insigniaRepo.findOneBy({ id: insigniaId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileInsigniaHistory();
Object.assign(record, body);
Object.assign(history, { ...body, id: undefined });
history.profileInsigniaId = insigniaId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([this.insigniaRepo.save(record), this.insigniaHistoryRepo.save(history)]);
return new HttpSuccess();
}
@Delete("{insigniaId}")
public async deleteInsignia(@Path() insigniaId: string) {
await this.insigniaHistoryRepo.delete({
profileInsigniaId: insigniaId,
});
const result = await this.insigniaRepo.delete({ id: insigniaId });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}

View file

@ -0,0 +1,108 @@
import {
Body,
Controller,
Delete,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import {
CreateProfileTraining,
ProfileTraining,
UpdateProfileTraining,
} from "../entities/ProfileTraining";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory";
import { RequestWithUser } from "../middlewares/user";
@Route("api/v1/org/profile/training")
@Tags("ProfileTraining")
@Security("bearerAuth")
export class ProfileTrainingController extends Controller {
private trainingRepo = AppDataSource.getRepository(ProfileTraining);
private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory);
@Get("{profileId}")
public async getTraining(@Path() profileId: string) {
const record = await this.trainingRepo.findBy({ profileId });
return new HttpSuccess(record);
}
@Get("history/{trainingId}")
public async trainingHistory(@Path() trainingId: string) {
const record = await this.trainingHistoryRepo.findBy({
profileTrainingId: trainingId,
});
return record;
}
@Post()
public async newTraining(@Request() req: RequestWithUser, @Body() body: CreateProfileTraining) {
const data = new ProfileTraining();
const history = new ProfileTrainingHistory();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
Object.assign(history, { ...body, ...meta });
const result = await this.trainingRepo.save(data);
history.profileTrainingId = result.id;
await this.trainingHistoryRepo.save(history);
return new HttpSuccess();
}
@Patch("{trainingId}")
public async editTraining(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileTraining,
@Path() trainingId: string,
) {
const record = await this.trainingRepo.findOneBy({ id: trainingId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileTrainingHistory();
Object.assign(record, body);
Object.assign(history, { ...body, id: undefined });
history.profileTrainingId = trainingId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]);
return new HttpSuccess();
}
@Delete("{trainingId}")
public async deleteTraining(@Path() trainingId: string) {
await this.trainingHistoryRepo.delete({
profileTrainingId: trainingId,
});
const result = await this.trainingRepo.delete({ id: trainingId });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}

View file

@ -2,6 +2,7 @@ import type { Request } from "express";
export type RequestWithUser = Request & {
user: {
sub: string;
name: string;
given_name: string;
familiy_name: string;