// admin/user.js // Copyright (C) 2021 Digital Telepresence, LLC // License: Apache-2.0 'use strict'; const DTP_COMPONENT_NAME = 'admin:user'; const express = require('express'); const { /*SiteError,*/ SiteController } = require('../../../lib/site-lib'); class UserController extends SiteController { constructor (dtp) { super(dtp, DTP_COMPONENT_NAME); } async start ( ) { const router = express.Router(); router.use(async (req, res, next) => { res.locals.currentView = 'admin'; res.locals.adminView = 'user'; return next(); }); router.param('userId', this.populateUserId.bind(this)); router.post('/:userId', this.postUpdateUser.bind(this)); router.get('/:userId', this.getUserView.bind(this)); router.get('/', this.getHomeView.bind(this)); return router; } async populateUserId (req, res, next, userId) { const { user: userService } = this.dtp.services; try { res.locals.userAccount = await userService.getUserAccount(userId); return next(); } catch (error) { return next(error); } } async postUpdateUser (req, res, next) { const { user: userService } = this.dtp.services; try { await userService.update(res.locals.userAccount, req.body); res.redirect('/admin/user'); } catch (error) { return next(error); } } async getUserView (req, res) { res.render('admin/user/form'); } async getHomeView (req, res, next) { const { user: userService } = this.dtp.services; try { res.locals.pagination = this.getPaginationParameters(req, 10); res.locals.userAccounts = await userService.getUserAccounts(res.locals.pagination, req.query.u); res.locals.totalUserCount = await userService.getTotalCount(); res.render('admin/user/index'); } catch (error) { return next(error); } } } module.exports = async (dtp) => { let controller = new UserController(dtp); return controller; };