diff --git a/app/controllers/adminController.js b/app/controllers/adminController.js index 79927ea..8af7761 100644 --- a/app/controllers/adminController.js +++ b/app/controllers/adminController.js @@ -46,6 +46,20 @@ exports.getUsers = (userId) => { }); }; +exports.addUser = (userId, first_name, last_name, email, organization, note, is_paying, is_active, is_admin, is_verified, password) => { + logger.trace(); + return new promise((resolve, reject) => { + db.user.add(first_name, last_name, email, organization, note, is_paying, is_active, is_admin, is_verified, password) + .then(data => { + db.user.logAction(userId, 'addUser'); + resolve(data); + }) + .catch(err => { + reject(err); + }); + }); +}; + exports.getUser = (userId, id) => { logger.trace(); return new promise((resolve, reject) => { diff --git a/app/db/user.js b/app/db/user.js index cae13ed..0e1d01a 100644 --- a/app/db/user.js +++ b/app/db/user.js @@ -183,4 +183,34 @@ exports.getSingleUser = (id) => { }); }); }); +}; + +exports.add = (first_name, last_name, email, organization, note, is_paying, is_active, is_admin, is_verified, password) => { + logger.trace(); + return new promise((resolve, reject) => { + db.one('INSERT INTO users(first_name, last_name, email, organization, note, is_paying, is_active, is_admin, is_verified, password) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, crypt($10, gen_salt(\'bf\'))) RETURNING user_id, verification_code', + [first_name, last_name, email.toLowerCase(), organization, note, is_paying, is_active, is_admin, is_verified, password]) + .then(data => { + logger.trace(); + if (data) { + resolve(data); // data + } + else { + reject({ + state: 'failure', + reason: 'No user_id returned', + extra: null + }); + } + }) + .catch(error => { + logger.trace(); + logger.error(error); + reject({ + state: 'failure', + reason: 'Database error', + extra: error + }); + }); + }); }; \ No newline at end of file diff --git a/app/routes/adminRoutes.js b/app/routes/adminRoutes.js index 1ab8ed9..0594495 100644 --- a/app/routes/adminRoutes.js +++ b/app/routes/adminRoutes.js @@ -77,6 +77,45 @@ router.get('/admin/users', function (req, res, next) { res.render('users', {user: user, users_active: true}); }); + +router.get('/admin/add-user', function (req, res, next) { + logger.trace(); + let user = req.session.user; + res.render('add-user', {user: user, users_active: true}); +}); + +router.post('/admin/add-user', function (req, res, next) { + logger.trace(); + let user = req.session.user; + let first_name = req.body.first_name; + let last_name = req.body.last_name; + let email = req.body.email; + let organization = req.body.organization; + let note = req.body.note; + let is_paying = (typeof req.body.is_paying !== 'undefined'); + let is_active = (typeof req.body.is_active !== 'undefined'); + let is_admin = (typeof req.body.is_admin !== 'undefined'); + let is_verified = true; + let password = (Math.random().toString(36).slice(2)+Math.random().toString(36).slice(2).toUpperCase()).split('').sort(function(){return 0.5-Math.random()}).join('');; + if(!first_name || !last_name || !email ) { + logger.trace(); + res.render('/admin/add-user', {users_active: true, error: 'All the fields are required'}); + } + logger.info("ADDING USER", first_name, last_name, email, organization, note, is_paying, is_active, is_admin); + adminController.addUser(user.user_id, first_name, last_name, email, organization, note, is_paying, is_active, is_admin, is_verified, password) + .then(data => { + logger.trace(); + res.render("users", {user: user, users_active: true, message: 'User added successfully'}); + }) + .catch(err => { + logger.trace(); + if(err.extra.constraint=='services_name_key' || err.extra.constraint=='services_prefix_key') { + res.render('add-service', {user: user, services_active: true, error: 'A service with this name or prefix already exists'}); + } + }); +}); + + router.get('/admin/user/:userId', function (req, res, next) { logger.trace(); let user = req.session.user; diff --git a/app/views/add-user.pug b/app/views/add-user.pug new file mode 100644 index 0000000..d9ab683 --- /dev/null +++ b/app/views/add-user.pug @@ -0,0 +1,92 @@ +extends dashboard.pug + +block title + title Login Billing: Dashboard + +block content + .container + .row + .col.s12 + h5 Add New User + if error + .red-text + strong= error + if message + .green-text + strong= message + .divider + .section + form(action="admin/add-user" method="post" enctype="application/x-www-form-urlencoding") + .row + .input-field.col.s12 + i.material-icons.prefix person + input#first_name.validate(name="first_name" type="text" required="" aria-required="true") + label(for="first_name") First Name + span.helper-text(data-error="Required") + .row + .input-field.col.s12 + i.material-icons.prefix person + input#last_name.validate(name="last_name" type="text" required="" aria-required="true") + label(for="last_name") Last Name + span.helper-text(data-error="Required") + .row + .input-field.col.s12 + i.material-icons.prefix mail_outline + input#email.validate(name="email" type="text" required="" aria-required="true") + label(for="email") Email Address + span.helper-text(data-error="Invalid Email") + .row + .input-field.col.s12 + i.material-icons.prefix account_balance + input#organization.validate(name="organization" type="text" ) + label(for="organization") Organization + .row + .input-field.col.s12 + i.material-icons.prefix description + textarea.materialize-textarea#note.validate(name="note" type="text") + label(for="note") Note + .row + .col.s1 + i.material-icons.prefix attach_money + .col.s3 + .switch + label + span No + input.is_paying(name="is_paying" type="checkbox") + span.lever + span Yes + .col.s3 + span Is Paying? + .row + .col.s1 + i.material-icons.prefix directions_run + .col.s3 + .switch + label + span No + input.is_active(name="is_active" type="checkbox" checked="") + span.lever + span Yes + .col.s3 + span Is Active? + .row + .col.s1 + i.material-icons.prefix settings + .col.s3 + .switch + label + span No + input.is_active(name="is_admin" type="checkbox") + span.lever + span Yes + .col.s3 + span Is Admin? + .row + .input-field.col.s6 + button.btn.grey.waves-effect.col.s12.white-text(type="submit") + strong Add User + .input-field.col.s6 + a.btn.red.lighten-2.waves-effect.col.s12.white-text(href="admin/users") + strong Cancel + .section +