Skip to content
Snippets Groups Projects
Commit 4ab26cc7 authored by kelly.nguyen's avatar kelly.nguyen
Browse files

add command user list and user change role

parent d4dd88c0
Branches
No related tags found
Loading
Pipeline #29746 failed
import CommanderCommand from '../CommanderCommand';
import UserChangeRoleCommand from './subcommands/UserChangeRoleCommand';
import UserListCommand from './subcommands/UserListCommand';
class UserCommand extends CommanderCommand {
protected commandName: string = 'user';
protected defineCommand() {
this.command
.description('manage user');
}
protected defineSubCommands() {
UserListCommand.registerOnCommand(this.command);
UserChangeRoleCommand.registerOnCommand(this.command);
}
protected async commandAction(): Promise<void> { }
}
export default new UserCommand();
\ No newline at end of file
import chalk from "chalk";
import CommanderCommand from "../../CommanderCommand";
import DojoBackendManager from "../../../managers/DojoBackendManager";
import ora from "ora";
import { StatusCodes } from "http-status-codes";
class UserChangeRoleCommand extends CommanderCommand {
protected commandName : string = 'role';
protected defineCommand(): void {
this.command
.description('change the role of an user')
.arguments('<userId> <newRole>', )
.action(this.commandAction.bind(this));
}
protected async commandAction(id: string, newRole : string): Promise<void> {
{
// if ( !await AccessesHelper.checkTeachingStaff() ) {
// return;
// }
console.log(chalk.cyan('Please wait while we are changing the role of the user...'));
// TODO : newRole -> type UserRole
const upd = await DojoBackendManager.updUserRole(id, newRole);
if (!upd) {
return;
}
const oraInfo = (message: string) => {
ora({
text: message,
indent: 4
}).start().info();
};
if (upd.status == StatusCodes.OK) {
oraInfo(`${chalk.green('Modification success')}`);
}
}
}
}
export default new UserChangeRoleCommand();
\ No newline at end of file
import chalk from "chalk";
import CommanderCommand from "../../CommanderCommand";
import DojoBackendManager from "../../../managers/DojoBackendManager";
import User from "../../../sharedByClients/models/User";
import ora from "ora";
class UserListCommand extends CommanderCommand {
protected commandName : string = 'list';
protected defineCommand(): void {
this.command
.description('list all the user')
.option('-v, --verbose', 'verbose mode - display principal container output in live')
.action(this.commandAction.bind(this));
}
protected async commandAction(): Promise<void> {
let users : User[] = [];
// Retrieve data
{
console.log(chalk.cyan('Please wait while we are retrieving the users...'));
users = await DojoBackendManager.getAllUsers();
users.forEach(user => {
const oraInfo = (message: string) => {
ora({
text: message,
indent: 4
}).start().info();
};
oraInfo(`${chalk.magenta('Id:')} ${user.id}`);
oraInfo(`${chalk.magenta('Mail:')} ${user.gitlabUsername}`);
oraInfo(`${chalk.magenta('Role:')} ${user.role}`);
});
}
}
}
export default new UserListCommand();
\ No newline at end of file
...@@ -29,7 +29,6 @@ class DojoBackendManager { ...@@ -29,7 +29,6 @@ class DojoBackendManager {
} }
} }
public async refreshTokens(refreshToken: string): Promise<GitlabToken> { public async refreshTokens(refreshToken: string): Promise<GitlabToken> {
return (await axios.post<DojoBackendResponse<GitlabToken>>(this.getApiUrl(ApiRoute.REFRESH_TOKENS), { return (await axios.post<DojoBackendResponse<GitlabToken>>(this.getApiUrl(ApiRoute.REFRESH_TOKENS), {
refreshToken: refreshToken refreshToken: refreshToken
...@@ -45,6 +44,20 @@ class DojoBackendManager { ...@@ -45,6 +44,20 @@ class DojoBackendManager {
} }
} }
public async getUserAssignments(id : string) : Promise<User> {
return (await axios.get<DojoBackendResponse<User>>(this.getApiUrl(ApiRoute.USER_ASSIGNMENTS).replace('{{userId}}', id))).data.data;
}
public async updUserRole(id: string, newRole : string) {
return await axios.patch<DojoBackendResponse<User>>(this.getApiUrl(ApiRoute.USER_CHANGE_ROLE).replace('{{userId}}', id), {
newRole: newRole
});
}
public async delAssignment() : Promise<Assignment[]> {
return (await axios.get<DojoBackendResponse<Assignment[]>>(this.getApiUrl(ApiRoute.ASSIGNMENT_DEL))).data.data;
}
public async checkTemplateAccess(idOrNamespace: string, verbose: boolean = true): Promise<boolean> { public async checkTemplateAccess(idOrNamespace: string, verbose: boolean = true): Promise<boolean> {
const spinner: ora.Ora = ora('Checking template access'); const spinner: ora.Ora = ora('Checking template access');
...@@ -165,74 +178,44 @@ class DojoBackendManager { ...@@ -165,74 +178,44 @@ class DojoBackendManager {
} }
} }
public async changeAssignmentPublishedStatus(assignment: Assignment, publish: boolean, verbose: boolean = true) { public async deleteExercise(verbose: boolean = true) {
const spinner: ora.Ora = ora('Changing published status...'); const spinner: ora.Ora = ora('Deleting exercise...');
if ( verbose ) { if ( verbose ) {
spinner.start(); spinner.start();
} }
try {
await axios.patch<DojoBackendResponse<null>>(this.getApiUrl(publish ? ApiRoute.ASSIGNMENT_PUBLISH : ApiRoute.ASSIGNMENT_UNPUBLISH).replace('{{nameOrUrl}}', encodeURIComponent(assignment.name)), {});
if ( verbose ) {
spinner.succeed(`Assignment ${ assignment.name } successfully ${ publish ? 'published' : 'unpublished' }`);
} }
return; public async getAllUsers(): Promise<User[]> {
} catch ( error ) { return (await axios.get<DojoBackendResponse<User[]>>(this.getApiUrl(ApiRoute.USER_GET))).data.data;
if ( verbose ) {
if ( error instanceof AxiosError && error.response ) {
spinner.fail(`Assignment visibility change error: ${ error.response.statusText }`);
} else {
spinner.fail(`Assignment visibility change error: unknown error`);
}
} }
throw error;
}
}
public async linkUpdateCorrection(exerciseIdOrUrl: string, assignment: Assignment, isUpdate: boolean, verbose: boolean = true): Promise<boolean> { public async changeAssignmentPublishedStatus(assignment: Assignment, publish: boolean, verbose: boolean = true) {
const spinner: ora.Ora = ora(`${ isUpdate ? 'Updating' : 'Linking' } correction`); const spinner: ora.Ora = ora('Changing published status...');
if ( verbose ) { if ( verbose ) {
spinner.start(); spinner.start();
} }
try { try {
const axiosFunction = isUpdate ? axios.patch : axios.post; await axios.patch<DojoBackendResponse<null>>(this.getApiUrl(publish ? ApiRoute.ASSIGNMENT_PUBLISH : ApiRoute.ASSIGNMENT_UNPUBLISH).replace('{{nameOrUrl}}', encodeURIComponent(assignment.name)), {});
const route = isUpdate ? ApiRoute.ASSIGNMENT_CORRECTION_UPDATE : ApiRoute.ASSIGNMENT_CORRECTION_LINK;
await axiosFunction(this.getApiUrl(route).replace('{{assignmentNameOrUrl}}', encodeURIComponent(assignment.name)).replace('{{exerciseIdOrUrl}}', encodeURIComponent(exerciseIdOrUrl)), {
exerciseIdOrUrl: exerciseIdOrUrl
});
if ( verbose ) { if ( verbose ) {
spinner.succeed(`Correction ${ isUpdate ? 'updated' : 'linked' }`); spinner.succeed(`Assignment ${ assignment.name } successfully ${ publish ? 'published' : 'unpublished' }`);
} }
return true; return;
} catch ( error ) { } catch ( error ) {
if ( verbose ) { if ( verbose ) {
if ( error instanceof AxiosError ) { if ( error instanceof AxiosError && error.response ) {
if ( error.response?.data ) { spinner.fail(`Assignment visibility change error: ${ error.response.statusText }`);
if ( error.response.data.code === DojoStatusCode.ASSIGNMENT_EXERCISE_NOT_RELATED ) {
spinner.fail(`The exercise does not belong to the assignment.`);
} else if ( error.response.data.code === DojoStatusCode.EXERCISE_CORRECTION_ALREADY_EXIST ) {
spinner.fail(`This exercise is already labelled as a correction. If you want to update it, please use the update command.`);
} else if ( error.response.data.code === DojoStatusCode.EXERCISE_CORRECTION_NOT_EXIST ) {
spinner.fail(`The exercise is not labelled as a correction so it's not possible to update it.`);
}
} else {
spinner.fail(`Correction ${ isUpdate ? 'update' : 'link' } error: ${ error.response?.statusText }`);
}
} else { } else {
spinner.fail(`Correction ${ isUpdate ? 'update' : 'link' } error: ${ error }`); spinner.fail(`Assignment visibility change error: unknown error`);
} }
} }
return false; throw error;
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment