API checkRole and history/user

This commit is contained in:
AdisakKanthawilang 2024-05-27 11:36:55 +07:00
parent 38871bcc80
commit 3167fe271e
7 changed files with 142 additions and 6 deletions

View file

@ -79,12 +79,17 @@ export class ProfileAbilityController extends Controller {
return new HttpSuccess(getProfileAbilityId);
}
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async getProfileAbilityHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.profileAbilityHistoryRepo.find({
where: {
histories: {

View file

@ -81,6 +81,11 @@ export class ProfileAbilityEmployeeController extends Controller {
return new HttpSuccess(getProfileAbilityId);
}
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async getProfileAbilityHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub });

View file

@ -87,6 +87,35 @@ export class ProfileAssessmentsController extends Controller {
return new HttpSuccess(getProfileAssessments);
}
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async getProfileAssessmentsHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.profileAssessmentsHistoryRepository.find({
relations: {
histories: true,
},
where: {
histories: {
profileId: profile.id,
},
},
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess(record);
}
@Get("history/{assessmentId}")
@Example({
status: 200,

View file

@ -36,7 +36,8 @@ import { RequestWithUser } from "../middlewares/user";
export class ProfileAssessmentsEmployeeController extends Controller {
private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee);
private profileAssessmentsRepository = AppDataSource.getRepository(ProfileAssessment);
private profileAssessmentsHistoryRepository = AppDataSource.getRepository(ProfileAssessmentHistory);
private profileAssessmentsHistoryRepository =
AppDataSource.getRepository(ProfileAssessmentHistory);
@Get("user")
public async detailProfileAssessmentsUser(@Request() request: { user: Record<string, any> }) {
@ -79,13 +80,44 @@ export class ProfileAssessmentsEmployeeController extends Controller {
],
})
public async detailProfileAssessments(@Path() profileEmployeeId: string) {
const getProfileAssessments = await this.profileAssessmentsRepository.findBy({ profileEmployeeId });
const getProfileAssessments = await this.profileAssessmentsRepository.findBy({
profileEmployeeId,
});
if (!getProfileAssessments) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess(getProfileAssessments);
}
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async getProfileAssessmentsHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.profileAssessmentsHistoryRepository.find({
relations: {
histories: true,
},
where: {
histories: {
profileEmployeeId: profile.id,
},
},
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess(record);
}
@Get("history/{assessmentId}")
@Example({
status: 200,

View file

@ -72,6 +72,27 @@ export class ProfileCertificateController extends Controller {
return new HttpSuccess(record);
}
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async certificateHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.certificateHistoryRepo.find({
where: {
histories: {
profileId: profile.id,
},
},
});
return new HttpSuccess(record);
}
@Get("history/{certificateId}")
@Example({
status: 200,

View file

@ -72,6 +72,27 @@ export class ProfileCertificateEmployeeController extends Controller {
return new HttpSuccess(record);
}
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async certificateHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.certificateHistoryRepo.find({
where: {
histories: {
profileEmployeeId: profile.id,
},
},
});
return new HttpSuccess(record);
}
@Get("history/{certificateId}")
@Example({
status: 200,

View file

@ -803,6 +803,29 @@ export class ProfileController extends Controller {
return new HttpSuccess(profile);
}
@Get("type")
async checkRole(@Request() request: RequestWithUser) {
let role: any;
const checkProfile = await this.profileRepo.findOne({
where: { keycloak: request.user.sub },
});
role = "OFFICER";
if (!checkProfile) {
const checkEmployee = await this.profileEmpRepo.findOne({
where: { keycloak: request.user.sub },
select: ["employeeClass"],
});
if (checkEmployee?.employeeClass === "PERM" || checkEmployee?.employeeClass === "TEMP") {
role = checkEmployee.employeeClass.toUpperCase();
} else {
role = "EMPLOYEE";
}
if (!checkProfile && !checkEmployee)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
}
return new HttpSuccess(role);
}
/**
* API
*