Skip to content

Commit 0fcd188

Browse files
authored
Merge pull request #522 from vaivk369/vk-late
Build failed
2 parents 2386182 + d8ff592 commit 0fcd188

2 files changed

Lines changed: 1 addition & 92 deletions

File tree

src/common/services/oblf.service.ts

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@ import { InjectRepository } from "@nestjs/typeorm";
1313
import { DataSource, ILike, In, Repository } from "typeorm";
1414
import { User } from "../../user/entities/user-entity";
1515
import { CohortMembers, MemberStatus } from "src/cohortMembers/entities/cohort-member.entity";
16-
import {
17-
getKeycloakAdminToken,
18-
createUserInKeyCloak,
19-
updateUserInKeyCloak,
20-
checkIfUsernameExistsInKeycloak,
21-
checkIfEmailExistsInKeycloak,
22-
assignRoleToUserInKeycloak
23-
} from "../../common/utils/keycloak.adapter.util";
16+
2417
@Injectable()
2518
export class OblfService {
2619
constructor(
@@ -371,67 +364,6 @@ constructor(
371364
}
372365
}
373366

374-
async syncAllUsersToKeycloak() {
375-
// Fetch all users with the given status
376-
const users = await this.usersRepository.find();
377-
if (users.length === 0) {
378-
console.log('No users found to sync.');
379-
return { message: 'No users found to sync.' };
380-
}
381-
console.log(`Found ${users.length} users to sync.`);
382-
// Get Keycloak admin token
383-
const token = await getKeycloakAdminToken();
384-
385-
for (const user of users) {
386-
user['id'] = user.userId;
387-
user['password'] = user.username + '@oblf'; // Set a default password or generate one
388-
if (!user.firstName || user.firstName.trim() === '' || user.firstName === 'NULL') {
389-
user.firstName = user.name ? user.name.split(' ')[0] : '';
390-
user.lastName = user.name ? user.name.split(' ')[1] : '';
391-
}
392-
393-
// Remove fields set to null from user object
394-
Object.keys(user).forEach(key => {
395-
if (user[key] === "NULL" || user[key] === null || user[key] === "null" || user[key] === "undefined") {
396-
delete user[key];
397-
}
398-
});
399-
400-
// Check if user already exists in Keycloak (optional)
401-
const exists = await checkIfUsernameExistsInKeycloak(
402-
user.username,
403-
token.data.access_token
404-
);
405-
if (exists?.data?.length > 0) {
406-
console.log(`User ${user.username} already exists in Keycloak.`);
407-
continue; // Skip to the next user
408-
}
409-
else {
410-
// Create user in Keycloak
411-
const resKeycloak = await createUserInKeyCloak(
412-
user,
413-
token.data.access_token,
414-
'teacher'
415-
);
416-
if (resKeycloak?.statusCode !== 201) {
417-
console.error(`Failed to create user ${user.username} in Keycloak. Message: ${resKeycloak.message}`);
418-
continue; // Skip to the next user
419-
} else {
420-
// Optionally assign role to user in Keycloak
421-
const roleRes = await assignRoleToUserInKeycloak(resKeycloak.userId, token.data.access_token);
422-
console.log(`User ${user.username} created in Keycloak.`);
423-
// Update id of the user in Users table with new Keycloak id
424-
await this.usersRepository.update(
425-
{ userId: user.userId },
426-
{ userId: resKeycloak.userId }
427-
);
428-
console.log(`Updated Users table: set userId = ${resKeycloak.userId} for username = ${user.username}`);
429-
}
430-
}
431-
}
432-
console.log(`All users synced to Keycloak with teacher role.`);
433-
return { message: 'All users synced to Keycloak with teacher role.' };
434-
}
435367

436368
async assignTeacherToClass(req, response, academicYearId, tenantId, authBearer) {
437369
try {

src/user/user.controller.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -386,29 +386,6 @@ export class UserController {
386386
return this.oblfservice.findEligibleUsers(request, response, dto, academicYearId, tenantId);
387387
}
388388

389-
390-
// GET /users/eligible
391-
@Post('syncAllUsersToKeycloak')
392-
@ApiHeader({ name: "academicyearid"})
393-
@ApiHeader({ name: "tenantid" })
394-
395-
async syncAllUsersToKeycloak(
396-
@Headers() headers,
397-
@Res() response,
398-
@Req() request: Request,
399-
400-
@Body() dto: {
401-
cohortId: string;
402-
page?: number;
403-
limit?: number;
404-
filters?: any;
405-
}) {
406-
const academicYearId = headers["academicyearid"];
407-
const tenantId = headers["tenantid"];
408-
409-
return this.oblfservice.syncAllUsersToKeycloak();
410-
}
411-
412389
@Post('addMembersByfilter')
413390
@ApiHeader({ name: "academicyearid"})
414391
@ApiHeader({ name: "tenantid" })

0 commit comments

Comments
 (0)