2026-01-28 09:22:52 +07:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
Body,
|
|
|
|
|
Path,
|
|
|
|
|
Request,
|
|
|
|
|
Response,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
|
|
|
import { AppDataSource } from "../database/data-source";
|
|
|
|
|
import { Issues, CreateIssueRequest, UpdateIssueRequest } from "../entities/Issues";
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
2026-01-28 10:57:25 +07:00
|
|
|
import { RequestWithUser } from "../middlewares/user";
|
2026-01-28 09:22:52 +07:00
|
|
|
|
|
|
|
|
@Route("api/v1/org/issues")
|
|
|
|
|
@Tags("issues")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
export class IssuesController extends Controller {
|
|
|
|
|
private issuesRepository = AppDataSource.getRepository(Issues);
|
|
|
|
|
|
|
|
|
|
@Get("lists")
|
|
|
|
|
async getIssues() {
|
|
|
|
|
const issues = await this.issuesRepository.find({
|
|
|
|
|
order: {
|
|
|
|
|
createdAt: "DESC",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return new HttpSuccess(issues);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post("")
|
2026-01-28 10:57:25 +07:00
|
|
|
async createIssue(@Body() requestBody: CreateIssueRequest, @Request() request: RequestWithUser) {
|
2026-01-28 09:22:52 +07:00
|
|
|
let issue = this.issuesRepository.create(requestBody);
|
2026-01-28 10:57:25 +07:00
|
|
|
issue.createdUserId = request.user.sub;
|
|
|
|
|
issue.createdFullName = request.user.name;
|
|
|
|
|
issue.createdAt = new Date();
|
|
|
|
|
issue.lastUpdateUserId = "";
|
|
|
|
|
issue.lastUpdateFullName = "";
|
2026-01-28 09:22:52 +07:00
|
|
|
await this.issuesRepository.save(issue);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess(issue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Put("{id}")
|
2026-01-28 10:57:25 +07:00
|
|
|
async updateIssue(
|
|
|
|
|
@Path("id") id: string,
|
|
|
|
|
@Body() requestBody: Partial<UpdateIssueRequest>,
|
|
|
|
|
@Request() request: RequestWithUser,
|
|
|
|
|
) {
|
2026-01-28 09:22:52 +07:00
|
|
|
let issue = await this.issuesRepository.findOneBy({ id });
|
|
|
|
|
if (!issue) {
|
|
|
|
|
this.setStatus(HttpStatusCode.NOT_FOUND);
|
|
|
|
|
return { message: "ไม่พบข้อมูลที่ต้องการแก้ไข" };
|
|
|
|
|
}
|
|
|
|
|
Object.assign(issue, requestBody);
|
2026-01-28 10:57:25 +07:00
|
|
|
issue.lastUpdateUserId = request.user.sub;
|
|
|
|
|
issue.lastUpdateFullName = request.user.name;
|
|
|
|
|
issue.lastUpdatedAt = new Date();
|
2026-01-28 09:22:52 +07:00
|
|
|
await this.issuesRepository.save(issue);
|
|
|
|
|
return new HttpSuccess(issue);
|
|
|
|
|
}
|
|
|
|
|
}
|