เพิ่้มและลบ 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

@ -74,16 +74,24 @@ export class ProfileFamilyCoupleEmployeeController extends Controller {
})
public async getFamilyCouple(@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 familyCouple = await this.ProfileFamilyCouple.findOne({
select: [
"id", "couplePrefix", "coupleFirstName", "coupleLastName", "coupleLastNameOld",
"coupleCareer", "coupleCitizenId", "coupleLive", "relationship", "profileEmployeeId",
"id",
"couplePrefix",
"coupleFirstName",
"coupleLastName",
"coupleLastNameOld",
"coupleCareer",
"coupleCitizenId",
"coupleLive",
"relationship",
"profileEmployeeId",
],
where: { profileEmployeeId },
order: { lastUpdatedAt: "DESC" },
@ -92,6 +100,51 @@ export class ProfileFamilyCoupleEmployeeController extends Controller {
return new HttpSuccess(familyCouple);
}
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async familyCoupleHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const familyCouple = await this.ProfileFamilyCouple.find({
relations: ["histories"],
order: { lastUpdatedAt: "DESC" },
where: {
profileEmployeeId: profile.id,
},
});
const mapData = familyCouple
.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,
couplePrefix: y.couplePrefix,
coupleFirstName: y.coupleFirstName,
coupleLastName: y.coupleLastName,
coupleLastNameOld: y.coupleLastNameOld,
coupleCareer: y.coupleCareer,
coupleCitizenId: y.coupleCitizenId,
coupleLive: y.coupleLive,
relationship: y.relationship,
profileFamilyCoupleId: y.profileFamilyCoupleId,
profileEmployeeId: profile.id,
}));
return new HttpSuccess(mapData);
}
@Get("history/{profileEmployeeId}")
@Example({
status: 200,
@ -115,13 +168,13 @@ export class ProfileFamilyCoupleEmployeeController extends Controller {
relationship: "string",
profileFamilyCoupleId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
}
},
],
})
public async familyCoupleHistory(@Path() profileEmployeeId: string) {
const profile = await this.profileRepo.findOne({
where: { id: profileEmployeeId }
})
where: { id: profileEmployeeId },
});
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
@ -129,28 +182,30 @@ export class ProfileFamilyCoupleEmployeeController extends Controller {
const familyCouple = await this.ProfileFamilyCouple.find({
relations: ["histories"],
order: { lastUpdatedAt: "DESC" },
where: { profileEmployeeId: profileEmployeeId},
where: { profileEmployeeId: profileEmployeeId },
});
const mapData = familyCouple.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,
couplePrefix: y.couplePrefix,
coupleFirstName: y.coupleFirstName,
coupleLastName: y.coupleLastName,
coupleLastNameOld: y.coupleLastNameOld,
coupleCareer: y.coupleCareer,
coupleCitizenId: y.coupleCitizenId,
coupleLive: y.coupleLive,
relationship: y.relationship,
profileFamilyCoupleId: y.profileFamilyCoupleId,
profileEmployeeId: profileEmployeeId,
}));
const mapData = familyCouple
.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,
couplePrefix: y.couplePrefix,
coupleFirstName: y.coupleFirstName,
coupleLastName: y.coupleLastName,
coupleLastNameOld: y.coupleLastNameOld,
coupleCareer: y.coupleCareer,
coupleCitizenId: y.coupleCitizenId,
coupleLive: y.coupleLive,
relationship: y.relationship,
profileFamilyCoupleId: y.profileFamilyCoupleId,
profileEmployeeId: profileEmployeeId,
}));
return new HttpSuccess(mapData);
}
@ -168,7 +223,7 @@ export class ProfileFamilyCoupleEmployeeController extends Controller {
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId));
familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId));
familyCouple.createdUserId = req.user.sub;
familyCouple.createdFullName = req.user.name;
familyCouple.lastUpdateUserId = req.user.sub;
@ -176,7 +231,7 @@ export class ProfileFamilyCoupleEmployeeController extends Controller {
await this.ProfileFamilyCouple.save(familyCouple);
if (familyCouple) {
profile.relationship = familyCouple.relationship //update profileEmployee.relationship
profile.relationship = familyCouple.relationship; //update profileEmployee.relationship
const history: ProfileFamilyCoupleHistory = Object.assign(new ProfileFamilyCoupleHistory(), {
profileFamilyCoupleId: familyCouple.id,
couplePrefix: familyCouple.couplePrefix,
@ -194,7 +249,7 @@ export class ProfileFamilyCoupleEmployeeController extends Controller {
});
await Promise.all([
this.profileRepo.save(profile),
this.ProfileFamilyCoupleHistory.save(history)
this.ProfileFamilyCoupleHistory.save(history),
]);
}
return new HttpSuccess(familyCouple.id);
@ -206,26 +261,28 @@ export class ProfileFamilyCoupleEmployeeController extends Controller {
@Body() body: UpdateProfileFamilyCouple,
@Path() profileEmployeeId: string,
) {
const familyCouple = await this.ProfileFamilyCouple.findOneBy({ profileEmployeeId: profileEmployeeId });
const familyCouple = await this.ProfileFamilyCouple.findOneBy({
profileEmployeeId: profileEmployeeId,
});
if (!familyCouple) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileFamilyCoupleHistory();
Object.assign(history, { ...familyCouple, id: undefined });
Object.assign(familyCouple, body);
familyCouple.lastUpdateUserId = req.user.sub,
familyCouple.lastUpdateFullName = req.user.name;
familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId));
(familyCouple.lastUpdateUserId = req.user.sub),
(familyCouple.lastUpdateFullName = req.user.name);
familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId));
history.profileFamilyCoupleId = familyCouple.id;
history.couplePrefix = familyCouple.couplePrefix,
history.coupleFirstName = familyCouple.coupleFirstName,
history.coupleLastName = familyCouple.coupleLastName,
history.coupleLastNameOld = familyCouple.coupleLastNameOld,
history.coupleCareer = familyCouple.coupleCareer,
history.coupleCitizenId = familyCouple.coupleCitizenId,
history.coupleLive = familyCouple.coupleLive,
history.relationship = familyCouple.relationship,
history.lastUpdateUserId = req.user.sub,
history.lastUpdateFullName = req.user.name;
(history.couplePrefix = familyCouple.couplePrefix),
(history.coupleFirstName = familyCouple.coupleFirstName),
(history.coupleLastName = familyCouple.coupleLastName),
(history.coupleLastNameOld = familyCouple.coupleLastNameOld),
(history.coupleCareer = familyCouple.coupleCareer),
(history.coupleCitizenId = familyCouple.coupleCitizenId),
(history.coupleLive = familyCouple.coupleLive),
(history.relationship = familyCouple.relationship),
(history.lastUpdateUserId = req.user.sub),
(history.lastUpdateFullName = req.user.name);
await Promise.all([
this.ProfileFamilyCouple.save(familyCouple),
@ -234,5 +291,4 @@ export class ProfileFamilyCoupleEmployeeController extends Controller {
return new HttpSuccess();
}
}