All files / src/lib/routes/admin-api user.ts

100% Statements 32/32
100% Branches 4/4
100% Functions 4/4
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102    59x   59x           59x                                                                     142x 142x 142x 142x 142x 142x   142x 142x 142x       4x 4x   4x 1x   3x   4x     4x   4x                   3x 3x 3x 2x 1x 1x   1x         1x 1x 1x       59x 59x  
import { Response } from 'express';
import { IAuthRequest } from '../unleash-types';
import Controller from '../controller';
import { AccessService } from '../../services/access-service';
import { IAuthType, IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import UserService from '../../services/user-service';
import SessionService from '../../services/session-service';
import UserFeedbackService from '../../services/user-feedback-service';
import UserSplashService from '../../services/user-splash-service';
import { ADMIN, NONE } from '../../types/permissions';
 
interface IChangeUserRequest {
    password: string;
    confirmPassword: string;
}
 
class UserController extends Controller {
    private accessService: AccessService;
 
    private userService: UserService;
 
    private userFeedbackService: UserFeedbackService;
 
    private sessionService: SessionService;
 
    private userSplashService: UserSplashService;
 
    constructor(
        config: IUnleashConfig,
        {
            accessService,
            userService,
            sessionService,
            userFeedbackService,
            userSplashService,
        }: Pick<
            IUnleashServices,
            | 'accessService'
            | 'userService'
            | 'sessionService'
            | 'userFeedbackService'
            | 'userSplashService'
        >,
    ) {
        super(config);
        this.accessService = accessService;
        this.userService = userService;
        this.sessionService = sessionService;
        this.userFeedbackService = userFeedbackService;
        this.userSplashService = userSplashService;
 
        this.get('/', this.getUser);
        this.post('/change-password', this.updateUserPass, NONE);
        this.get('/my-sessions', this.mySessions);
    }
 
    async getUser(req: IAuthRequest, res: Response): Promise<void> {
        res.setHeader('cache-control', 'no-store');
        const { user } = req;
        let permissions;
        if (this.config.authentication.type === IAuthType.NONE) {
            permissions = [{ permission: ADMIN }];
        } else {
            permissions = await this.accessService.getPermissionsForUser(user);
        }
        const feedback = await this.userFeedbackService.getAllUserFeedback(
            user,
        );
        const splash = await this.userSplashService.getAllUserSplashs(user);
 
        return res
            .status(200)
            .json({ user, permissions, feedback, splash })
            .end();
    }
 
    async updateUserPass(
        req: IAuthRequest<any, any, IChangeUserRequest, any>,
        res: Response,
    ): Promise<void> {
        const { user } = req;
        const { password, confirmPassword } = req.body;
        if (password === confirmPassword) {
            this.userService.validatePassword(password);
            await this.userService.changePassword(user.id, password);
            res.status(200).end();
        } else {
            res.status(400).end();
        }
    }
 
    async mySessions(req: IAuthRequest, res: Response): Promise<void> {
        const { user } = req;
        const sessions = await this.sessionService.getSessionsForUser(user.id);
        res.json(sessions);
    }
}
 
module.exports = UserController;
export default UserController;