changeName [notTest]

This commit is contained in:
AdisakKanthawilang 2024-03-19 17:52:36 +07:00
parent 9c7072c128
commit 25734e3f2d
3 changed files with 371 additions and 0 deletions

View file

@ -0,0 +1,175 @@
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 { ProfileChangeNameHistory } from "../entities/ProfileChangeNameHistory";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import {
CreateProfileChangeName,
ProfileChangeName,
UpdateProfileChangeName,
} from "../entities/ProfileChangeName";
@Route("api/v1/org/profile/changeName")
@Tags("ProfileChangeName")
@Security("bearerAuth")
export class ProfileChangeNameController extends Controller {
private profileRepository = AppDataSource.getRepository(Profile);
private changeNameRepository = AppDataSource.getRepository(ProfileChangeName);
private changeNameHistoryRepository = AppDataSource.getRepository(ProfileChangeNameHistory);
@Get("{profileId}")
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
isActive: true,
prefix: "string",
firstName: "string",
lastName: "string",
status: "string",
},
],
})
public async getChangeName(@Path() profileId: string) {
const lists = await this.changeNameRepository.find({
where: { profileId: profileId },
select: ["id", "isActive", "prefix", "firstName", "lastName", "status"],
});
return new HttpSuccess(lists);
}
@Get("history/{changeNameId}")
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
isActive: true,
prefix: "string",
firstName: "string",
lastName: "string",
status: "string",
createdFullName: "string",
createdAt: "string",
},
{
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
isActive: true,
prefix: "string",
firstName: "string",
lastName: "string",
status: "string",
createdFullName: "string",
createdAt: "string",
},
],
})
public async changeNameHistory(@Path() changeNameId: string) {
const record = await this.changeNameHistoryRepository.find({
where: { profileChangeNameId: changeNameId },
select: [
"id",
"isActive",
"prefix",
"firstName",
"lastName",
"status",
"createdFullName",
"createdAt",
],
order: { createdAt: "DESC" },
});
return new HttpSuccess(record);
}
@Post()
public async newChangeName(
@Request() req: RequestWithUser,
@Body() body: CreateProfileChangeName,
) {
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 ProfileChangeName();
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.changeNameRepository.save(data);
return new HttpSuccess();
}
@Patch("{changeNameId}")
public async editChangeName(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileChangeName,
@Path() changeNameId: string,
) {
const record = await this.changeNameRepository.findOneBy({ id: changeNameId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileChangeNameHistory();
Object.assign(history, { ...record, id: undefined });
Object.assign(record, body);
history.profileChangeNameId = changeNameId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([
this.changeNameRepository.save(record),
this.changeNameHistoryRepository.save(history),
]);
return new HttpSuccess();
}
@Delete("{changeNameId}")
public async deleteTraning(@Path() changeNameId: string) {
await this.changeNameHistoryRepository.delete({
profileChangeNameId: changeNameId,
});
const result = await this.changeNameRepository.delete({ id: changeNameId });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}

View file

@ -0,0 +1,99 @@
import { Entity, Column, OneToMany, ManyToOne, JoinColumn } from "typeorm";
import { EntityBase } from "./base/Base";
import { Profile } from "./Profile";
import { ProfileChangeNameHistory } from "./ProfileChangeNameHistory";
// import { ProfileChangeNameHistory } from "./ProfileChangeNameHistory";
@Entity("profileChangeName")
export class ProfileChangeName extends EntityBase {
@Column({
nullable: true,
length: 40,
comment: "คีย์นอก(FK)ของตาราง Profile",
default: null,
})
profileId: string;
@Column({
comment: "สถานะการใช้งาน",
default: false,
})
isActive: boolean;
@Column({
nullable: true,
length: 40,
comment: "Id คำนำหน้า",
default: null,
})
prefixId: string;
@Column({
nullable: true,
length: 40,
comment: "คำนำหน้า",
default: null,
})
prefix: string;
@Column({
nullable: true,
length: 100,
comment: "ชื่อ",
default: null,
})
firstName: string;
@Column({
nullable: true,
length: 100,
comment: "นามสกุล",
default: null,
})
lastName: string;
@Column({
nullable: true,
length: 100,
comment: "สถานะ",
default: null,
})
status: string;
@Column({
nullable: true,
length: 40,
comment: "",
default: null,
})
documentId: string;
@OneToMany(() => ProfileChangeNameHistory, (profileChangeNameHistory) => profileChangeNameHistory.histories)
profileChangeNameHistories: ProfileChangeNameHistory[];
// @ManyToOne(() => Profile, (profile) => profile.profileChangeName)
// @JoinColumn({ name: "profileId" })
// profile: Profile;
}
export class CreateProfileChangeName {
profileId: string | null;
isActive: boolean;
prefixId: string | null;
prefix: string | null;
firstName: string | null;
lastName: string | null;
status: string | null;
documentId: string | null;
}
export type UpdateProfileChangeName = {
profileId?: string | null;
isActive?: boolean;
prefixId?: string | null;
prefix?: string | null;
firstName?: string | null;
lastName?: string | null;
status?: string | null;
documentId?: string | null;
};

View file

@ -0,0 +1,97 @@
import { Entity, Column, OneToMany, ManyToOne, JoinColumn } from "typeorm";
import { EntityBase } from "./base/Base";
import { Profile } from "./Profile";
import { ProfileChangeName } from "./ProfileChangeName";
@Entity("profileChangeNameHistory")
export class ProfileChangeNameHistory extends EntityBase {
@Column({
comment: "สถานะการใช้งาน",
default: false,
})
isActive: boolean;
@Column({
nullable: true,
length: 40,
comment: "Id คำนำหน้า",
default: null,
})
prefixId: string;
@Column({
nullable: true,
length: 40,
comment: "คำนำหน้า",
default: null,
})
prefix: string;
@Column({
nullable: true,
length: 100,
comment: "ชื่อ",
default: null,
})
firstName: string;
@Column({
nullable: true,
length: 100,
comment: "นามสกุล",
default: null,
})
lastName: string;
@Column({
nullable: true,
length: 100,
comment: "สถานะ",
default: null,
})
status: string;
@Column({
nullable: true,
length: 40,
comment: "",
default: null,
})
documentId: string;
@Column({
nullable: true,
length: 40,
comment: "คีย์นอก(FK)ของตาราง ProfileChangeName",
default: null,
})
profileChangeNameId: string;
@ManyToOne(() => ProfileChangeName, (profileChangeName) => profileChangeName.profileChangeNameHistories)
@JoinColumn({ name: "profileChangeNameId" })
histories: ProfileChangeName;
}
export class CreateProfileChangeNameHistory {
profileChangeNameId: string | null;
isActive: boolean;
prefixId: string | null;
prefix: string | null;
firstName: string | null;
lastName: string | null;
status: string | null;
documentId: string | null;
}
export type UpdateProfileChangeNameHistory = {
profileChangeNameId?: string | null;
isActive?: boolean;
prefixId?: string | null;
prefix?: string | null;
firstName?: string | null;
lastName?: string | null;
status?: string | null;
documentId?: string | null;
};