fix and add api(/search-personal-no-keycloak)

This commit is contained in:
AdisakKanthawilang 2024-06-13 14:49:50 +07:00
parent b7ce71d017
commit 8a1f2506bb
3 changed files with 360 additions and 7 deletions

View file

@ -30,6 +30,9 @@ import {
getRoleMappings,
getUserCount,
} from "../keycloak";
import { AppDataSource } from "../database/data-source";
import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee";
// import * as io from "../lib/websocket";
// import elasticsearch from "../elasticsearch";
// import { StorageFolder } from "../interfaces/storage-fs";
@ -38,7 +41,6 @@ import {
// if (!process.env.ELASTICSEARCH_INDEX) throw Error("Default ElasticSearch index must be specified.");
// const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX;
function stripLeadingSlash(str: string) {
return str.replace(/^\//, "");
}
@ -47,6 +49,10 @@ function stripLeadingSlash(str: string) {
@Tags("Single-Sign On")
@Security("bearerAuth")
export class KeycloakController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee);
@Get("user/{id}")
async getUser(@Path("id") id: string) {
const userData = await getUser(id);
@ -80,6 +86,7 @@ export class KeycloakController extends Controller {
lastName?: string;
email?: string;
roles?: string[];
profileId?: string;
},
) {
const userId = await createUser(body.username, body.password, {
@ -133,6 +140,17 @@ export class KeycloakController extends Controller {
role: body.roles || [],
};
const addRole = await this.addRole(userId, _roles);
const profile = await this.profileRepo.findOne({
where: {
id: body.profileId,
},
});
if (profile) {
profile.keycloak = userId;
await this.profileRepo.save(profile);
}
return userId;
}