แก้ไขบุตร

This commit is contained in:
Kittapath 2024-05-14 17:26:06 +07:00
parent 291d3199d2
commit 0114e41c3a
7 changed files with 511 additions and 106 deletions

View file

@ -0,0 +1,117 @@
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 { ProfileChildrenHistory } from "../entities/ProfileChildrenHistory";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import {
CreateProfileChildren,
ProfileChildren,
UpdateProfileChildren,
} from "../entities/ProfileChildren";
@Route("api/v1/org/profile/children")
@Tags("ProfileChildren")
@Security("bearerAuth")
export class ProfileChildrenController extends Controller {
private profileRepository = AppDataSource.getRepository(Profile);
private childrenRepository = AppDataSource.getRepository(ProfileChildren);
private childrenHistoryRepository = AppDataSource.getRepository(ProfileChildrenHistory);
@Get("{profileId}")
public async getChildren(@Path() profileId: string) {
const lists = await this.childrenRepository.find({
where: { profileId: profileId },
});
return new HttpSuccess(lists);
}
@Get("history/{childrenId}")
public async childrenHistory(@Path() childrenId: string) {
const record = await this.childrenHistoryRepository.find({
where: { profileChildrenId: childrenId },
order: { createdAt: "DESC" },
});
return new HttpSuccess(record);
}
@Post()
public async newChildren(@Request() req: RequestWithUser, @Body() body: CreateProfileChildren) {
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const data = new ProfileChildren();
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.childrenRepository.save(data);
return new HttpSuccess();
}
@Patch("{childrenId}")
public async editChildren(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileChildren,
@Path() childrenId: string,
) {
const record = await this.childrenRepository.findOneBy({ id: childrenId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileChildrenHistory();
Object.assign(history, { ...record, id: undefined });
Object.assign(record, body);
history.profileChildrenId = childrenId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([
this.childrenRepository.save(record),
this.childrenHistoryRepository.save(history),
]);
return new HttpSuccess();
}
@Delete("{childrenId}")
public async deleteTraning(@Path() childrenId: string) {
await this.childrenHistoryRepository.delete({
profileChildrenId: childrenId,
});
const result = await this.childrenRepository.delete({ id: childrenId });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}

View file

@ -0,0 +1,122 @@
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 { ProfileChildrenHistory } from "../entities/ProfileChildrenHistory";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import {
CreateProfileChildren,
CreateProfileChildrenEmployee,
ProfileChildren,
UpdateProfileChildren,
} from "../entities/ProfileChildren";
import { ProfileEmployee } from "../entities/ProfileEmployee";
@Route("api/v1/org/profile-employee/children")
@Tags("ProfileChildren")
@Security("bearerAuth")
export class ProfileChildrenEmployeeController extends Controller {
private profileRepository = AppDataSource.getRepository(ProfileEmployee);
private childrenRepository = AppDataSource.getRepository(ProfileChildren);
private childrenHistoryRepository = AppDataSource.getRepository(ProfileChildrenHistory);
@Get("{profileId}")
public async getChildren(@Path() profileId: string) {
const lists = await this.childrenRepository.find({
where: { profileEmployeeId: profileId },
});
return new HttpSuccess(lists);
}
@Get("history/{childrenId}")
public async childrenHistory(@Path() childrenId: string) {
const record = await this.childrenHistoryRepository.find({
where: { profileChildrenId: childrenId },
order: { createdAt: "DESC" },
});
return new HttpSuccess(record);
}
@Post()
public async newChildren(
@Request() req: RequestWithUser,
@Body() body: CreateProfileChildrenEmployee,
) {
const profile = await this.profileRepository.findOneBy({ id: body.profileEmployeeId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const data = new ProfileChildren();
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.childrenRepository.save(data);
return new HttpSuccess();
}
@Patch("{childrenId}")
public async editChildren(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileChildren,
@Path() childrenId: string,
) {
const record = await this.childrenRepository.findOneBy({ id: childrenId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileChildrenHistory();
Object.assign(history, { ...record, id: undefined });
Object.assign(record, body);
history.profileChildrenId = childrenId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([
this.childrenRepository.save(record),
this.childrenHistoryRepository.save(history),
]);
return new HttpSuccess();
}
@Delete("{childrenId}")
public async deleteTraning(@Path() childrenId: string) {
await this.childrenHistoryRepository.delete({
profileChildrenId: childrenId,
});
const result = await this.childrenRepository.delete({ id: childrenId });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}

View file

@ -16,12 +16,13 @@ import { ProfileAbility } from "./ProfileAbility";
import { ProfileDuty } from "./ProfileDuty";
import { ProfileNopaid } from "./ProfileNopaid";
import { ProfileOther } from "./ProfileOther";
import { ProfileChildren, ProfileFamilyHistory } from "./ProfileFamily";
import { ProfileFamilyHistory } from "./ProfileFamily";
import { ProfileGovernment } from "./ProfileGovernment";
import { Province } from "./Province";
import { SubDistrict } from "./SubDistrict";
import { District } from "./District";
import { ProfileAvatar } from "./ProfileAvatar";
import { ProfileChildren } from "./ProfileChildren";
@Entity("profile")
export class Profile extends EntityBase {
@ -309,7 +310,7 @@ export class Profile extends EntityBase {
profileFamily: ProfileFamilyHistory[];
@OneToMany(() => ProfileChildren, (profileChildren) => profileChildren.profile)
profileChildren: ProfileChildren[];
profileChildrens: ProfileChildren[];
@OneToMany(() => ProfileGovernment, (profileGovernment) => profileGovernment.profile)
profileGovernment: ProfileGovernment[];

View file

@ -0,0 +1,112 @@
import { Column, Entity, ManyToOne, OneToMany, JoinColumn } from "typeorm";
import { EntityBase } from "./base/Base";
import { Profile } from "./Profile";
import { ProfileEmployee } from "./ProfileEmployee";
import { ProfileChildrenHistory } from "./ProfileChildrenHistory";
@Entity("profileChildren")
export class ProfileChildren extends EntityBase {
@Column({
nullable: true,
default: null,
comment: "อาชีพบุตร",
})
childrenCareer: string;
@Column({
nullable: true,
default: null,
comment: "ชื่อบุตร",
})
childrenFirstName: string;
@Column({
nullable: true,
default: null,
comment: "นามสกุลบุตร",
})
childrenLastName: string;
@Column({
nullable: true,
default: null,
comment: "คำนำหน้าบุตร",
})
childrenPrefix: string;
@Column({
nullable: true,
default: null,
type: "boolean",
comment: "มีชีวิตบุตร",
})
childrenLive: boolean;
@Column({
nullable: true,
default: null,
comment: "เลขที่บัตรประชาชนบุตร",
})
childrenCitizenId: string;
@Column({
nullable: true,
length: 40,
type: "uuid",
comment: "คีย์นอก(FK) ของตาราง Profile",
default: null,
})
profileId: string;
@Column({
nullable: true,
length: 40,
comment: "คีย์นอก(FK)ของตาราง ProfileEmployee",
default: null,
})
profileEmployeeId: string;
@ManyToOne(() => Profile, (Profile) => Profile.profileChildrens)
@JoinColumn({ name: "profileId" })
profile: Profile;
@ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileChildrens)
@JoinColumn({ name: "profileEmployeeId" })
profileEmployee: ProfileEmployee;
@OneToMany(
() => ProfileChildrenHistory,
(profileChildrenHistory) => profileChildrenHistory.histories,
)
profileChildrenHistories: ProfileChildrenHistory[];
}
export type CreateProfileChildren = {
profileId: string;
childrenCareer: string;
childrenFirstName: string;
childrenLastName: string;
childrenPrefix: string;
childrenLive: boolean;
childrenCitizenId: string;
};
export type CreateProfileChildrenEmployee = {
profileEmployeeId: string;
childrenCareer: string;
childrenFirstName: string;
childrenLastName: string;
childrenPrefix: string;
childrenLive: boolean;
childrenCitizenId: string;
};
export type UpdateProfileChildren = {
id: string;
childrenCareer?: string | null;
childrenFirstName?: string | null;
childrenLastName?: string | null;
childrenPrefix?: string | null;
childrenLive?: boolean | null;
childrenCitizenId?: string | null;
};

View file

@ -0,0 +1,155 @@
import { Column, Entity, ManyToOne, OneToMany, JoinColumn } from "typeorm";
import { EntityBase } from "./base/Base";
import { Profile } from "./Profile";
import { ProfileEmployee } from "./ProfileEmployee";
import { ProfileChildren } from "./ProfileChildren";
@Entity("profileChildrenHistory")
export class ProfileChildrenHistory extends EntityBase {
@Column({
nullable: true,
default: null,
comment: "อาชีพบุตร",
})
childrenCareer: string;
@Column({
nullable: true,
default: null,
comment: "ชื่อบุตร",
})
childrenFirstName: string;
@Column({
nullable: true,
default: null,
comment: "นามสกุลบุตร",
})
childrenLastName: string;
@Column({
nullable: true,
default: null,
comment: "คำนำหน้าบุตร",
})
childrenPrefix: string;
@Column({
nullable: true,
default: null,
type: "boolean",
comment: "มีชีวิตบุตร",
})
childrenLive: boolean;
@Column({
nullable: true,
default: null,
comment: "เลขที่บัตรประชาชนบุตร",
})
childrenCitizenId: string;
@Column({
nullable: true,
length: 40,
comment: "คีย์นอก(FK)ของตาราง ProfileChildren",
default: null,
})
profileChildrenId: string;
@ManyToOne(() => ProfileChildren, (profileChildren) => profileChildren.profileChildrenHistories)
@JoinColumn({ name: "profileChildrenId" })
histories: ProfileChildren;
}
export type CreateChildren = {
childrenCareer: string;
childrenFirstName: string;
childrenLastName: string;
childrenPrefix: string;
childrenLive: boolean;
childrenCitizenId: string;
};
export type UpdateChildren = {
id: string;
childrenCareer?: string | null;
childrenFirstName?: string | null;
childrenLastName?: string | null;
childrenPrefix?: string | null;
childrenLive?: boolean | null;
childrenCitizenId?: string | null;
};
export type CreateProfileFamily = {
couple: boolean | null;
couplePrefix: string | null;
coupleFirstName: string | null;
coupleLastName: string | null;
coupleLastNameOld: string | null;
coupleCareer: string | null;
coupleCitizenId: string | null;
coupleLive: boolean | null;
fatherPrefix: string | null;
fatherFirstName: string | null;
fatherLastName: string | null;
fatherCareer: string | null;
fatherCitizenId: string | null;
fatherLive: boolean | null;
motherPrefix: string | null;
motherFirstName: string | null;
motherLastName: string | null;
motherCareer: string | null;
motherCitizenId: string | null;
motherLive: boolean | null;
profileId: string;
children: CreateChildren[];
};
export type CreateProfileFamilyEmployee = {
couple: boolean | null;
couplePrefix: string | null;
coupleFirstName: string | null;
coupleLastName: string | null;
coupleLastNameOld: string | null;
coupleCareer: string | null;
coupleCitizenId: string | null;
coupleLive: boolean | null;
fatherPrefix: string | null;
fatherFirstName: string | null;
fatherLastName: string | null;
fatherCareer: string | null;
fatherCitizenId: string | null;
fatherLive: boolean | null;
motherPrefix: string | null;
motherFirstName: string | null;
motherLastName: string | null;
motherCareer: string | null;
motherCitizenId: string | null;
motherLive: boolean | null;
profileEmployeeId: string | null;
children: CreateChildren[];
};
export type UpdateProfileFamily = {
couple?: boolean | null;
couplePrefix?: string | null;
coupleFirstName?: string | null;
coupleLastName?: string | null;
coupleLastNameOld?: string | null;
coupleCareer?: string | null;
coupleCitizenId?: string | null;
coupleLive?: boolean | null;
fatherPrefix?: string | null;
fatherFirstName?: string | null;
fatherLastName?: string | null;
fatherCareer?: string | null;
fatherCitizenId?: string | null;
fatherLive?: boolean | null;
motherPrefix?: string | null;
motherFirstName?: string | null;
motherLastName?: string | null;
motherCareer?: string | null;
motherCitizenId?: string | null;
motherLive?: boolean | null;
children: UpdateChildren[];
};

View file

@ -15,12 +15,13 @@ import { ProfileDuty } from "./ProfileDuty";
import { ProfileNopaid } from "./ProfileNopaid";
import { ProfileDiscipline } from "./ProfileDiscipline";
import { ProfileChangeName } from "./ProfileChangeName";
import { ProfileChildren, ProfileFamilyHistory } from "./ProfileFamily";
import { ProfileFamilyHistory } from "./ProfileFamily";
import { ProfileEducation } from "./ProfileEducation";
import { ProfileAbility } from "./ProfileAbility";
import { ProfileOther } from "./ProfileOther";
import { ProfileAvatar } from "./ProfileAvatar";
import { ProfileGovernment } from "./ProfileGovernment";
import { ProfileChildren } from "./ProfileChildren";
@Entity("profileEmployee")
export class ProfileEmployee extends EntityBase {
@Column({

View file

@ -167,114 +167,11 @@ export class ProfileFamilyHistory extends EntityBase {
@ManyToOne(() => Profile, (v) => v.profileFamily)
profile: Profile;
@OneToMany(() => ProfileChildrenHistory, (v) => v.profileFamilyHistory)
profileChildrenHistories: ProfileChildrenHistory[];
@ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileFamilys)
@JoinColumn({ name: "profileEmployeeId" })
profileEmployee: ProfileEmployee;
}
@Entity("profileChildren")
export class ProfileChildren extends EntityBase {
@Column({
nullable: true,
default: null,
comment: "อาชีพบุตร",
})
childrenCareer: string;
@Column({
nullable: true,
default: null,
comment: "ชื่อบุตร",
})
childrenFirstName: string;
@Column({
nullable: true,
default: null,
comment: "นามสกุลบุตร",
})
childrenLastName: string;
@Column({
nullable: true,
default: null,
comment: "คำนำหน้าบุตร",
})
childrenPrefix: string;
@Column({
nullable: true,
default: null,
type: "boolean",
comment: "มีชีวิตบุตร",
})
childrenLive: boolean;
@Column({
nullable: true,
default: null,
comment: "เลขที่บัตรประชาชนบุตร",
})
childrenCitizenId: string;
@Column({
nullable: true,
length: 40,
type: "uuid",
comment: "คีย์นอก(FK) ของตาราง Profile",
default: null,
})
profileId: string;
@Column({
nullable: true,
length: 40,
comment: "คีย์นอก(FK)ของตาราง ProfileEmployee",
default: null,
})
profileEmployeeId: string;
@ManyToOne(() => Profile, (v) => v.profileFamily, { onDelete: "CASCADE" })
profile: Profile;
@OneToMany(() => ProfileChildrenHistory, (v) => v.profileChildren)
histories: Profile;
@ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileChildrens)
@JoinColumn({ name: "profileEmployeeId" })
profileEmployee: ProfileEmployee;
}
@Entity("profileChildrenHistory")
export class ProfileChildrenHistory extends ProfileChildren {
@Column({
nullable: true,
length: 40,
type: "uuid",
comment: "คีย์นอก(FK) ของตาราง Profile",
default: null,
})
profileFamilyHistoryId: string;
@ManyToOne(() => ProfileFamilyHistory, (v) => v.profileChildrenHistories, { onDelete: "CASCADE" })
profileFamilyHistory: ProfileFamilyHistory;
@Column({
nullable: true,
length: 40,
type: "uuid",
comment: "คีย์นอก(FK) ของตาราง Profile",
default: null,
})
profileChildrenId: string;
@ManyToOne(() => ProfileChildren, (v) => v.histories, { onDelete: "CASCADE" })
profileChildren: ProfileChildren;
}
export type CreateChildren = {
childrenCareer: string;
childrenFirstName: string;