get ข้อมูล by keycloak

This commit is contained in:
Kittapath 2024-05-23 16:44:37 +07:00
parent f6bcd35e14
commit ff0be7af48
22 changed files with 633 additions and 137 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 { Profile } from "../entities/Profile";
import { ProfileFamilyCouple, CreateProfileFamilyCouple, UpdateProfileFamilyCouple } from "../entities/ProfileFamilyCouple";
import {
ProfileFamilyCouple,
CreateProfileFamilyCouple,
UpdateProfileFamilyCouple,
} from "../entities/ProfileFamilyCouple";
import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory";
import Extension from "../interfaces/extension";
@Route("api/v1/org/profile/family/couple")
@ -29,6 +33,32 @@ export class ProfileFamilyCoupleController extends Controller {
private ProfileFamilyCouple = AppDataSource.getRepository(ProfileFamilyCouple);
private ProfileFamilyCoupleHistory = AppDataSource.getRepository(ProfileFamilyCoupleHistory);
@Get("user")
public async getFamilyCoupleUser(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
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",
"profileId",
],
where: { id: profile.id },
order: { lastUpdatedAt: "DESC" },
});
return new HttpSuccess(familyCouple);
}
@Get("{profileId}")
@Example({
status: 200,
@ -48,16 +78,24 @@ export class ProfileFamilyCoupleController extends Controller {
})
public async getFamilyCouple(@Path() profileId: string) {
const profile = await this.profileRepo.findOne({
where: { id: profileId }
})
where: { id: profileId },
});
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", "profileId",
"id",
"couplePrefix",
"coupleFirstName",
"coupleLastName",
"coupleLastNameOld",
"coupleCareer",
"coupleCitizenId",
"coupleLive",
"relationship",
"profileId",
],
where: { profileId },
order: { lastUpdatedAt: "DESC" },
@ -89,13 +127,13 @@ export class ProfileFamilyCoupleController extends Controller {
relationship: "string",
profileFamilyCoupleId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
}
},
],
})
public async familyCoupleHistory(@Path() profileId: string) {
const profile = await this.profileRepo.findOne({
where: { id: profileId }
})
where: { id: profileId },
});
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
@ -103,28 +141,30 @@ export class ProfileFamilyCoupleController extends Controller {
const familyCouple = await this.ProfileFamilyCouple.find({
relations: ["histories"],
order: { lastUpdatedAt: "DESC" },
where: { profileId: profileId},
where: { profileId: profileId },
});
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,
profileId: profileId,
}));
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,
profileId: profileId,
}));
return new HttpSuccess(mapData);
}
@ -142,7 +182,7 @@ export class ProfileFamilyCoupleController 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;
@ -150,7 +190,7 @@ export class ProfileFamilyCoupleController extends Controller {
await this.ProfileFamilyCouple.save(familyCouple);
if (familyCouple) {
profile.relationship = familyCouple.relationship //update profile.relationship
profile.relationship = familyCouple.relationship; //update profile.relationship
const history: ProfileFamilyCoupleHistory = Object.assign(new ProfileFamilyCoupleHistory(), {
profileFamilyCoupleId: familyCouple.id,
couplePrefix: familyCouple.couplePrefix,
@ -168,7 +208,7 @@ export class ProfileFamilyCoupleController extends Controller {
});
await Promise.all([
this.profileRepo.save(profile),
this.ProfileFamilyCoupleHistory.save(history)
this.ProfileFamilyCoupleHistory.save(history),
]);
}
return new HttpSuccess(familyCouple.id);
@ -186,20 +226,20 @@ export class ProfileFamilyCoupleController extends Controller {
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),
@ -208,5 +248,4 @@ export class ProfileFamilyCoupleController extends Controller {
return new HttpSuccess();
}
}