เพิ่้มและลบ api history/user

This commit is contained in:
AdisakKanthawilang 2024-05-28 09:30:14 +07:00
parent abc4aee8a4
commit 8d1de6365a
22 changed files with 512 additions and 466 deletions

View file

@ -18,7 +18,11 @@ import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { RequestWithUser } from "../middlewares/user";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { ProfileFamilyFather, CreateProfileEmployeeFamilyFather, UpdateProfileFamilyFather } from "../entities/ProfileFamilyFather";
import {
ProfileFamilyFather,
CreateProfileEmployeeFamilyFather,
UpdateProfileFamilyFather,
} from "../entities/ProfileFamilyFather";
import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory";
import Extension from "../interfaces/extension";
@Route("api/v1/org/profile-employee/family/father")
@ -70,16 +74,22 @@ export class ProfileFamilyFatherEmployeeController extends Controller {
})
public async getFamilyFather(@Path() profileEmployeeId: string) {
const profile = await this.profileRepo.findOne({
where: { id: profileEmployeeId }
})
where: { id: profileEmployeeId },
});
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const familyFather = await this.ProfileFamilyFather.findOne({
select: [
"id", "fatherPrefix", "fatherFirstName", "fatherLastName",
"fatherCareer", "fatherCitizenId", "fatherLive", "profileEmployeeId",
"id",
"fatherPrefix",
"fatherFirstName",
"fatherLastName",
"fatherCareer",
"fatherCitizenId",
"fatherLive",
"profileEmployeeId",
],
where: { profileEmployeeId },
order: { lastUpdatedAt: "DESC" },
@ -88,6 +98,47 @@ export class ProfileFamilyFatherEmployeeController extends Controller {
return new HttpSuccess(familyFather);
}
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async familyFatherHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const familyFather = await this.ProfileFamilyFather.find({
relations: ["histories"],
order: { lastUpdatedAt: "DESC" },
where: { profileEmployeeId: profile.id },
});
const mapData = familyFather
.flatMap((x) => x.histories)
.map((y) => ({
id: y.id,
createdAt: y.createdAt,
createdUserId: y.createdUserId,
lastUpdatedAt: y.lastUpdatedAt,
lastUpdateUserId: y.lastUpdateUserId,
createdFullName: y.createdFullName,
lastUpdateFullName: y.lastUpdateFullName,
fatherPrefix: y.fatherPrefix,
fatherFirstName: y.fatherFirstName,
fatherLastName: y.fatherLastName,
fatherCareer: y.fatherCareer,
fatherCitizenId: y.fatherCitizenId,
fatherLive: y.fatherLive,
profileFamilyFatherId: y.profileFamilyFatherId,
profileEmployeeId: profile.id,
}));
return new HttpSuccess(mapData);
}
@Get("history/{profileEmployeeId}")
@Example({
status: 200,
@ -109,13 +160,13 @@ export class ProfileFamilyFatherEmployeeController extends Controller {
fatherLive: true,
profileFamilyFatherId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
}
},
],
})
public async familyFatherHistory(@Path() profileEmployeeId: string) {
const profile = await this.profileRepo.findOne({
where: { id: profileEmployeeId }
})
where: { id: profileEmployeeId },
});
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
@ -123,26 +174,28 @@ export class ProfileFamilyFatherEmployeeController extends Controller {
const familyFather = await this.ProfileFamilyFather.find({
relations: ["histories"],
order: { lastUpdatedAt: "DESC" },
where: { profileEmployeeId: profileEmployeeId},
where: { profileEmployeeId: profileEmployeeId },
});
const mapData = familyFather.flatMap((x) => x.histories).map((y) => ({
id: y.id,
createdAt: y.createdAt,
createdUserId: y.createdUserId,
lastUpdatedAt: y.lastUpdatedAt,
lastUpdateUserId: y.lastUpdateUserId,
createdFullName: y.createdFullName,
lastUpdateFullName: y.lastUpdateFullName,
fatherPrefix: y.fatherPrefix,
fatherFirstName: y.fatherFirstName,
fatherLastName: y.fatherLastName,
fatherCareer: y.fatherCareer,
fatherCitizenId: y.fatherCitizenId,
fatherLive: y.fatherLive,
profileFamilyFatherId: y.profileFamilyFatherId,
profileEmployeeId: profileEmployeeId,
}));
const mapData = familyFather
.flatMap((x) => x.histories)
.map((y) => ({
id: y.id,
createdAt: y.createdAt,
createdUserId: y.createdUserId,
lastUpdatedAt: y.lastUpdatedAt,
lastUpdateUserId: y.lastUpdateUserId,
createdFullName: y.createdFullName,
lastUpdateFullName: y.lastUpdateFullName,
fatherPrefix: y.fatherPrefix,
fatherFirstName: y.fatherFirstName,
fatherLastName: y.fatherLastName,
fatherCareer: y.fatherCareer,
fatherCitizenId: y.fatherCitizenId,
fatherLive: y.fatherLive,
profileFamilyFatherId: y.profileFamilyFatherId,
profileEmployeeId: profileEmployeeId,
}));
return new HttpSuccess(mapData);
}
@ -160,7 +213,7 @@ export class ProfileFamilyFatherEmployeeController extends Controller {
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId));
familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId));
familyFather.createdUserId = req.user.sub;
familyFather.createdFullName = req.user.name;
familyFather.lastUpdateUserId = req.user.sub;
@ -192,24 +245,26 @@ export class ProfileFamilyFatherEmployeeController extends Controller {
@Body() body: UpdateProfileFamilyFather,
@Path() profileEmployeeId: string,
) {
const familyFather = await this.ProfileFamilyFather.findOneBy({ profileEmployeeId: profileEmployeeId });
const familyFather = await this.ProfileFamilyFather.findOneBy({
profileEmployeeId: profileEmployeeId,
});
if (!familyFather) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileFamilyFatherHistory();
Object.assign(history, { ...familyFather, id: undefined });
Object.assign(familyFather, body);
familyFather.lastUpdateUserId = req.user.sub,
familyFather.lastUpdateFullName = req.user.name;
familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId));
(familyFather.lastUpdateUserId = req.user.sub),
(familyFather.lastUpdateFullName = req.user.name);
familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId));
history.profileFamilyFatherId = familyFather.id;
history.fatherPrefix = familyFather.fatherPrefix,
history.fatherFirstName = familyFather.fatherFirstName,
history.fatherLastName = familyFather.fatherLastName,
history.fatherCareer = familyFather.fatherCareer,
history.fatherCitizenId = familyFather.fatherCitizenId,
history.fatherLive = familyFather.fatherLive,
history.lastUpdateUserId = req.user.sub,
history.lastUpdateFullName = req.user.name;
(history.fatherPrefix = familyFather.fatherPrefix),
(history.fatherFirstName = familyFather.fatherFirstName),
(history.fatherLastName = familyFather.fatherLastName),
(history.fatherCareer = familyFather.fatherCareer),
(history.fatherCitizenId = familyFather.fatherCitizenId),
(history.fatherLive = familyFather.fatherLive),
(history.lastUpdateUserId = req.user.sub),
(history.lastUpdateFullName = req.user.name);
await Promise.all([
this.ProfileFamilyFather.save(familyFather),
@ -218,5 +273,4 @@ export class ProfileFamilyFatherEmployeeController extends Controller {
return new HttpSuccess();
}
}