crud profile discip,duty,nopaid,other
This commit is contained in:
parent
1d48bb06ee
commit
13fc7945e5
9 changed files with 706 additions and 5 deletions
186
src/controllers/ProfileDisciplineController.ts
Normal file
186
src/controllers/ProfileDisciplineController.ts
Normal file
|
|
@ -0,0 +1,186 @@
|
||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Delete,
|
||||||
|
Example,
|
||||||
|
Get,
|
||||||
|
Patch,
|
||||||
|
Path,
|
||||||
|
Post,
|
||||||
|
Request,
|
||||||
|
Route,
|
||||||
|
Security,
|
||||||
|
Tags,
|
||||||
|
} from "tsoa";
|
||||||
|
import { AppDataSource } from "../database/data-source";
|
||||||
|
import HttpSuccess from "../interfaces/http-success";
|
||||||
|
import HttpStatus from "../interfaces/http-status";
|
||||||
|
import HttpError from "../interfaces/http-error";
|
||||||
|
import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory";
|
||||||
|
import { RequestWithUser } from "../middlewares/user";
|
||||||
|
import { Profile } from "../entities/Profile";
|
||||||
|
import {
|
||||||
|
CreateProfileDiscipline,
|
||||||
|
ProfileDiscipline,
|
||||||
|
UpdateProfileDiscipline,
|
||||||
|
} from "../entities/ProfileDiscipline";
|
||||||
|
|
||||||
|
@Route("api/v1/org/profile/disipline")
|
||||||
|
@Tags("ProfileDiscipline")
|
||||||
|
@Security("bearerAuth")
|
||||||
|
export class ProfileDisciplineController extends Controller {
|
||||||
|
private profileRepository = AppDataSource.getRepository(Profile);
|
||||||
|
private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline);
|
||||||
|
private disciplineHistoryRepository = AppDataSource.getRepository(ProfileDisciplineHistory);
|
||||||
|
|
||||||
|
@Get("{profileId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: [
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
date: "2024-03-12T10:09:47.000Z",
|
||||||
|
level: "string",
|
||||||
|
detail: "string",
|
||||||
|
unStigma: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
public async getDiscipline(@Path() profileId: string) {
|
||||||
|
const lists = await this.disciplineRepository.find({
|
||||||
|
where: { profileId: profileId },
|
||||||
|
select: [
|
||||||
|
"id",
|
||||||
|
"isActive",
|
||||||
|
"date",
|
||||||
|
"level",
|
||||||
|
"detail",
|
||||||
|
"unStigma",
|
||||||
|
"refCommandNo",
|
||||||
|
"refCommandDate",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
return new HttpSuccess(lists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get("history/{disciplineId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: [
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
date: "2024-03-12T10:09:47.000Z",
|
||||||
|
level: "string",
|
||||||
|
detail: "string",
|
||||||
|
unStigma: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "ba0e2f82-014e-46c6-8b82-a7c28eb5325f",
|
||||||
|
isActive: true,
|
||||||
|
date: "2024-03-12T10:09:47.000Z",
|
||||||
|
level: "string",
|
||||||
|
detail: "string",
|
||||||
|
unStigma: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
public async disciplineHistory(@Path() disciplineId: string) {
|
||||||
|
const record = await this.disciplineHistoryRepository.find({
|
||||||
|
where: { profileDisciplineId: disciplineId },
|
||||||
|
select: [
|
||||||
|
"id",
|
||||||
|
"isActive",
|
||||||
|
"date",
|
||||||
|
"level",
|
||||||
|
"detail",
|
||||||
|
"unStigma",
|
||||||
|
"refCommandNo",
|
||||||
|
"refCommandDate",
|
||||||
|
],
|
||||||
|
order: { createdAt: "DESC" }
|
||||||
|
});
|
||||||
|
return new HttpSuccess(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
public async newDiscipline(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Body() body: CreateProfileDiscipline,
|
||||||
|
) {
|
||||||
|
if (!body.profileId) {
|
||||||
|
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
|
||||||
|
|
||||||
|
if (!profile) {
|
||||||
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = new ProfileDiscipline();
|
||||||
|
|
||||||
|
const meta = {
|
||||||
|
createdUserId: req.user.sub,
|
||||||
|
createdFullName: req.user.name,
|
||||||
|
lastUpdateUserId: req.user.sub,
|
||||||
|
lastUpdateFullName: req.user.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.assign(data, { ...body, ...meta });
|
||||||
|
|
||||||
|
await this.disciplineRepository.save(data);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch("{disciplineId}")
|
||||||
|
public async editDiscipline(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Body() body: UpdateProfileDiscipline,
|
||||||
|
@Path() disciplineId: string,
|
||||||
|
) {
|
||||||
|
const record = await this.disciplineRepository.findOneBy({ id: disciplineId });
|
||||||
|
|
||||||
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
|
const history = new ProfileDisciplineHistory();
|
||||||
|
|
||||||
|
Object.assign(history, { ...record, id: undefined });
|
||||||
|
Object.assign(record, body);
|
||||||
|
history.profileDisciplineId = disciplineId;
|
||||||
|
record.lastUpdateFullName = req.user.name;
|
||||||
|
history.lastUpdateFullName = req.user.name;
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
this.disciplineRepository.save(record),
|
||||||
|
this.disciplineHistoryRepository.save(history),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete("{disciplineId}")
|
||||||
|
public async deleteTraning(@Path() disciplineId: string) {
|
||||||
|
await this.disciplineHistoryRepository.delete({
|
||||||
|
profileDisciplineId: disciplineId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await this.disciplineRepository.delete({ id: disciplineId });
|
||||||
|
|
||||||
|
if (result.affected && result.affected <= 0) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
}
|
||||||
176
src/controllers/ProfileDutyController.ts
Normal file
176
src/controllers/ProfileDutyController.ts
Normal file
|
|
@ -0,0 +1,176 @@
|
||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Delete,
|
||||||
|
Example,
|
||||||
|
Get,
|
||||||
|
Patch,
|
||||||
|
Path,
|
||||||
|
Post,
|
||||||
|
Request,
|
||||||
|
Route,
|
||||||
|
Security,
|
||||||
|
Tags,
|
||||||
|
} from "tsoa";
|
||||||
|
import { AppDataSource } from "../database/data-source";
|
||||||
|
import HttpSuccess from "../interfaces/http-success";
|
||||||
|
import HttpStatus from "../interfaces/http-status";
|
||||||
|
import HttpError from "../interfaces/http-error";
|
||||||
|
import { ProfileDutyHistory } from "../entities/ProfileDutyHistory";
|
||||||
|
import { RequestWithUser } from "../middlewares/user";
|
||||||
|
import { Profile } from "../entities/Profile";
|
||||||
|
import { CreateProfileDuty, ProfileDuty, UpdateProfileDuty } from "../entities/ProfileDuty";
|
||||||
|
|
||||||
|
@Route("api/v1/org/profile/duty")
|
||||||
|
@Tags("ProfileDuty")
|
||||||
|
@Security("bearerAuth")
|
||||||
|
export class ProfileDutyController extends Controller {
|
||||||
|
private profileRepository = AppDataSource.getRepository(Profile);
|
||||||
|
private dutyRepository = AppDataSource.getRepository(ProfileDuty);
|
||||||
|
private dutyHistoryRepository = AppDataSource.getRepository(ProfileDutyHistory);
|
||||||
|
|
||||||
|
@Get("{profileId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: [
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
dateStart: "2024-03-12T10:09:47.000Z",
|
||||||
|
dateEnd: "string",
|
||||||
|
reference: "string",
|
||||||
|
detail: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
public async getDuty(@Path() profileId: string) {
|
||||||
|
const lists = await this.dutyRepository.find({
|
||||||
|
where: { profileId: profileId },
|
||||||
|
select: [
|
||||||
|
"id",
|
||||||
|
"isActive",
|
||||||
|
"dateStart",
|
||||||
|
"dateEnd",
|
||||||
|
"reference",
|
||||||
|
"detail",
|
||||||
|
"refCommandNo",
|
||||||
|
"refCommandDate",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
return new HttpSuccess(lists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get("history/{dutyId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: [
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
dateStart: "2024-03-12T10:09:47.000Z",
|
||||||
|
dateEnd: "string",
|
||||||
|
reference: "string",
|
||||||
|
detail: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
dateStart: "2024-03-12T10:09:47.000Z",
|
||||||
|
dateEnd: "string",
|
||||||
|
reference: "string",
|
||||||
|
detail: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
public async dutyHistory(@Path() dutyId: string) {
|
||||||
|
const record = await this.dutyHistoryRepository.find({
|
||||||
|
where: { profileDutyId: dutyId },
|
||||||
|
select: [
|
||||||
|
"id",
|
||||||
|
"isActive",
|
||||||
|
"dateStart",
|
||||||
|
"dateEnd",
|
||||||
|
"reference",
|
||||||
|
"detail",
|
||||||
|
"refCommandNo",
|
||||||
|
"refCommandDate",
|
||||||
|
],
|
||||||
|
order: { createdAt: "DESC" },
|
||||||
|
});
|
||||||
|
return new HttpSuccess(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
public async newDuty(@Request() req: RequestWithUser, @Body() body: CreateProfileDuty) {
|
||||||
|
if (!body.profileId) {
|
||||||
|
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
|
||||||
|
|
||||||
|
if (!profile) {
|
||||||
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = new ProfileDuty();
|
||||||
|
|
||||||
|
const meta = {
|
||||||
|
createdUserId: req.user.sub,
|
||||||
|
createdFullName: req.user.name,
|
||||||
|
lastUpdateUserId: req.user.sub,
|
||||||
|
lastUpdateFullName: req.user.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.assign(data, { ...body, ...meta });
|
||||||
|
|
||||||
|
await this.dutyRepository.save(data);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch("{dutyId}")
|
||||||
|
public async editDuty(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Body() body: UpdateProfileDuty,
|
||||||
|
@Path() dutyId: string,
|
||||||
|
) {
|
||||||
|
const record = await this.dutyRepository.findOneBy({ id: dutyId });
|
||||||
|
|
||||||
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
|
const history = new ProfileDutyHistory();
|
||||||
|
|
||||||
|
Object.assign(history, { ...record, id: undefined });
|
||||||
|
Object.assign(record, body);
|
||||||
|
history.profileDutyId = dutyId;
|
||||||
|
record.lastUpdateFullName = req.user.name;
|
||||||
|
history.lastUpdateFullName = req.user.name;
|
||||||
|
|
||||||
|
await Promise.all([this.dutyRepository.save(record), this.dutyHistoryRepository.save(history)]);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete("{dutyId}")
|
||||||
|
public async deleteTraning(@Path() dutyId: string) {
|
||||||
|
await this.dutyHistoryRepository.delete({
|
||||||
|
profileDutyId: dutyId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await this.dutyRepository.delete({ id: dutyId });
|
||||||
|
|
||||||
|
if (result.affected && result.affected <= 0) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
}
|
||||||
158
src/controllers/ProfileNopaidController.ts
Normal file
158
src/controllers/ProfileNopaidController.ts
Normal file
|
|
@ -0,0 +1,158 @@
|
||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Delete,
|
||||||
|
Example,
|
||||||
|
Get,
|
||||||
|
Patch,
|
||||||
|
Path,
|
||||||
|
Post,
|
||||||
|
Request,
|
||||||
|
Route,
|
||||||
|
Security,
|
||||||
|
Tags,
|
||||||
|
} from "tsoa";
|
||||||
|
import { AppDataSource } from "../database/data-source";
|
||||||
|
import HttpSuccess from "../interfaces/http-success";
|
||||||
|
import HttpStatus from "../interfaces/http-status";
|
||||||
|
import HttpError from "../interfaces/http-error";
|
||||||
|
import { ProfileNopaidHistory } from "../entities/ProfileNopaidHistory";
|
||||||
|
import { RequestWithUser } from "../middlewares/user";
|
||||||
|
import { Profile } from "../entities/Profile";
|
||||||
|
import { CreateProfileNopaid, ProfileNopaid, UpdateProfileNopaid } from "../entities/ProfileNopaid";
|
||||||
|
|
||||||
|
@Route("api/v1/org/profile/nopaid")
|
||||||
|
@Tags("ProfileNopaid")
|
||||||
|
@Security("bearerAuth")
|
||||||
|
export class ProfileNopaidController extends Controller {
|
||||||
|
private profileRepository = AppDataSource.getRepository(Profile);
|
||||||
|
private nopaidRepository = AppDataSource.getRepository(ProfileNopaid);
|
||||||
|
private nopaidHistoryRepository = AppDataSource.getRepository(ProfileNopaidHistory);
|
||||||
|
|
||||||
|
@Get("{profileId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: [
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
date: "2024-03-12T10:09:47.000Z",
|
||||||
|
reference: "string",
|
||||||
|
detail: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
public async getNopaid(@Path() profileId: string) {
|
||||||
|
const lists = await this.nopaidRepository.find({
|
||||||
|
where: { profileId: profileId },
|
||||||
|
select: ["id", "isActive", "date", "reference", "detail", "refCommandNo", "refCommandDate"],
|
||||||
|
});
|
||||||
|
return new HttpSuccess(lists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get("history/{nopaidId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: [
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
date: "2024-03-12T10:09:47.000Z",
|
||||||
|
reference: "string",
|
||||||
|
detail: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
date: "2024-03-12T10:09:47.000Z",
|
||||||
|
reference: "string",
|
||||||
|
detail: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
public async nopaidHistory(@Path() nopaidId: string) {
|
||||||
|
const record = await this.nopaidHistoryRepository.find({
|
||||||
|
where: { profileNopaidId: nopaidId },
|
||||||
|
select: ["id", "isActive", "date", "reference", "detail", "refCommandNo", "refCommandDate"],
|
||||||
|
order: { createdAt: "DESC" },
|
||||||
|
});
|
||||||
|
return new HttpSuccess(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
public async newNopaid(@Request() req: RequestWithUser, @Body() body: CreateProfileNopaid) {
|
||||||
|
if (!body.profileId) {
|
||||||
|
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
|
||||||
|
|
||||||
|
if (!profile) {
|
||||||
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = new ProfileNopaid();
|
||||||
|
|
||||||
|
const meta = {
|
||||||
|
createdUserId: req.user.sub,
|
||||||
|
createdFullName: req.user.name,
|
||||||
|
lastUpdateUserId: req.user.sub,
|
||||||
|
lastUpdateFullName: req.user.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.assign(data, { ...body, ...meta });
|
||||||
|
|
||||||
|
await this.nopaidRepository.save(data);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch("{nopaidId}")
|
||||||
|
public async editNopaid(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Body() body: UpdateProfileNopaid,
|
||||||
|
@Path() nopaidId: string,
|
||||||
|
) {
|
||||||
|
const record = await this.nopaidRepository.findOneBy({ id: nopaidId });
|
||||||
|
|
||||||
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
|
const history = new ProfileNopaidHistory();
|
||||||
|
|
||||||
|
Object.assign(history, { ...record, id: undefined });
|
||||||
|
Object.assign(record, body);
|
||||||
|
history.profileNopaidId = nopaidId;
|
||||||
|
record.lastUpdateFullName = req.user.name;
|
||||||
|
history.lastUpdateFullName = req.user.name;
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
this.nopaidRepository.save(record),
|
||||||
|
this.nopaidHistoryRepository.save(history),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete("{nopaidId}")
|
||||||
|
public async deleteTraning(@Path() nopaidId: string) {
|
||||||
|
await this.nopaidHistoryRepository.delete({
|
||||||
|
profileNopaidId: nopaidId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await this.nopaidRepository.delete({ id: nopaidId });
|
||||||
|
|
||||||
|
if (result.affected && result.affected <= 0) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
}
|
||||||
152
src/controllers/ProfileOtherContorller.ts
Normal file
152
src/controllers/ProfileOtherContorller.ts
Normal file
|
|
@ -0,0 +1,152 @@
|
||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Delete,
|
||||||
|
Example,
|
||||||
|
Get,
|
||||||
|
Patch,
|
||||||
|
Path,
|
||||||
|
Post,
|
||||||
|
Request,
|
||||||
|
Route,
|
||||||
|
Security,
|
||||||
|
Tags,
|
||||||
|
} from "tsoa";
|
||||||
|
import { AppDataSource } from "../database/data-source";
|
||||||
|
import HttpSuccess from "../interfaces/http-success";
|
||||||
|
import HttpStatus from "../interfaces/http-status";
|
||||||
|
import HttpError from "../interfaces/http-error";
|
||||||
|
import { ProfileOtherHistory } from "../entities/ProfileOtherHistory";
|
||||||
|
import { RequestWithUser } from "../middlewares/user";
|
||||||
|
import { Profile } from "../entities/Profile";
|
||||||
|
import { CreateProfileOther, ProfileOther, UpdateProfileOther } from "../entities/ProfileOther";
|
||||||
|
|
||||||
|
@Route("api/v1/org/profile/other")
|
||||||
|
@Tags("ProfileOther")
|
||||||
|
@Security("bearerAuth")
|
||||||
|
export class ProfileOtherController extends Controller {
|
||||||
|
private profileRepository = AppDataSource.getRepository(Profile);
|
||||||
|
private otherRepository = AppDataSource.getRepository(ProfileOther);
|
||||||
|
private otherHistoryRepository = AppDataSource.getRepository(ProfileOtherHistory);
|
||||||
|
|
||||||
|
@Get("{profileId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: [
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
date: "2024-03-12T10:09:47.000Z",
|
||||||
|
reference: "string",
|
||||||
|
detail: "string",
|
||||||
|
refCommandNo: "string",
|
||||||
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
public async getOther(@Path() profileId: string) {
|
||||||
|
const lists = await this.otherRepository.find({
|
||||||
|
where: { profileId: profileId },
|
||||||
|
select: ["id", "isActive", "date", "detail"],
|
||||||
|
});
|
||||||
|
return new HttpSuccess(lists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get("history/{otherId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: [
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
date: "2024-03-12T10:09:47.000Z",
|
||||||
|
detail: "string",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||||
|
isActive: true,
|
||||||
|
date: "2024-03-12T10:09:47.000Z",
|
||||||
|
detail: "string",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
public async otherHistory(@Path() otherId: string) {
|
||||||
|
const record = await this.otherHistoryRepository.find({
|
||||||
|
where: { profileOtherId: otherId },
|
||||||
|
select: ["id", "isActive", "date", "detail"],
|
||||||
|
order: { createdAt: "DESC" },
|
||||||
|
});
|
||||||
|
return new HttpSuccess(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
public async newOther(@Request() req: RequestWithUser, @Body() body: CreateProfileOther) {
|
||||||
|
if (!body.profileId) {
|
||||||
|
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
|
||||||
|
|
||||||
|
if (!profile) {
|
||||||
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = new ProfileOther();
|
||||||
|
|
||||||
|
const meta = {
|
||||||
|
createdUserId: req.user.sub,
|
||||||
|
createdFullName: req.user.name,
|
||||||
|
lastUpdateUserId: req.user.sub,
|
||||||
|
lastUpdateFullName: req.user.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.assign(data, { ...body, ...meta });
|
||||||
|
|
||||||
|
await this.otherRepository.save(data);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch("{otherId}")
|
||||||
|
public async editOther(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Body() body: UpdateProfileOther,
|
||||||
|
@Path() otherId: string,
|
||||||
|
) {
|
||||||
|
const record = await this.otherRepository.findOneBy({ id: otherId });
|
||||||
|
|
||||||
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
|
const history = new ProfileOtherHistory();
|
||||||
|
|
||||||
|
Object.assign(history, { ...record, id: undefined });
|
||||||
|
Object.assign(record, body);
|
||||||
|
history.profileOtherId = otherId;
|
||||||
|
record.lastUpdateFullName = req.user.name;
|
||||||
|
history.lastUpdateFullName = req.user.name;
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
this.otherRepository.save(record),
|
||||||
|
this.otherHistoryRepository.save(history),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete("{otherId}")
|
||||||
|
public async deleteTraning(@Path() otherId: string) {
|
||||||
|
await this.otherHistoryRepository.delete({
|
||||||
|
profileOtherId: otherId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await this.otherRepository.delete({ id: otherId });
|
||||||
|
|
||||||
|
if (result.affected && result.affected <= 0) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,7 +13,7 @@ import { ProfileHonor } from "./ProfileHonor";
|
||||||
import { ProfileAssessment } from "./ProfileAssessment";
|
import { ProfileAssessment } from "./ProfileAssessment";
|
||||||
import { ProfileLeave } from "./ProfileLeave";
|
import { ProfileLeave } from "./ProfileLeave";
|
||||||
import { ProfileAbility } from "./ProfileAbility";
|
import { ProfileAbility } from "./ProfileAbility";
|
||||||
import { ProfileDuty } from "./ProfileDutys";
|
import { ProfileDuty } from "./ProfileDuty";
|
||||||
import { ProfileNopaid } from "./ProfileNopaid";
|
import { ProfileNopaid } from "./ProfileNopaid";
|
||||||
import { ProfileOther } from "./ProfileOther";
|
import { ProfileOther } from "./ProfileOther";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,3 +73,32 @@ export class ProfileDiscipline extends EntityBase {
|
||||||
@JoinColumn({ name: "profileId" })
|
@JoinColumn({ name: "profileId" })
|
||||||
profile: Profile;
|
profile: Profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class CreateProfileDiscipline {
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
date: Date | null;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
profileId: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
isActive: boolean | null;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
level: string | null;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
detail: string | null;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
refCommandDate: Date | null;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
refCommandNo: string | null;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
unStigma: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type UpdateProfileDiscipline = Partial<CreateProfileDiscipline>;
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { Entity, Column, ManyToOne, JoinColumn } from "typeorm";
|
import { Entity, Column, ManyToOne, JoinColumn } from "typeorm";
|
||||||
import { EntityBase } from "./base/Base";
|
import { EntityBase } from "./base/Base";
|
||||||
import { Profile } from "./Profile";
|
import { Profile } from "./Profile";
|
||||||
import { ProfileDuty } from "./ProfileDutys";
|
import { ProfileDuty } from "./ProfileDuty";
|
||||||
|
|
||||||
@Entity("profileDutyHistory")
|
@Entity("profileDutyHistory")
|
||||||
export class ProfileDutyHistory extends EntityBase {
|
export class ProfileDutyHistory extends EntityBase {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ export class ProfileOtherHistory extends EntityBase {
|
||||||
comment: "คีย์นอก(FK)ของตาราง Profile",
|
comment: "คีย์นอก(FK)ของตาราง Profile",
|
||||||
default: null,
|
default: null,
|
||||||
})
|
})
|
||||||
profileId: string;
|
profileOtherId: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
comment: "สถานะการใช้งาน",
|
comment: "สถานะการใช้งาน",
|
||||||
|
|
@ -35,13 +35,13 @@ export class ProfileOtherHistory extends EntityBase {
|
||||||
date: Date;
|
date: Date;
|
||||||
|
|
||||||
@ManyToOne(() => ProfileOther, (profileOther) => profileOther.profileOtherHistories)
|
@ManyToOne(() => ProfileOther, (profileOther) => profileOther.profileOtherHistories)
|
||||||
@JoinColumn({ name: "profileId" })
|
@JoinColumn({ name: "profileOtherId" })
|
||||||
histories: ProfileOther;
|
histories: ProfileOther;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateProfileOtherHistory {
|
export class CreateProfileOtherHistory {
|
||||||
@Column("uuid")
|
@Column("uuid")
|
||||||
profileId: string | null;
|
profileOtherId: string | null;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue