%PDF- %PDF-
Direktori : /home/opti/workflow/src/routes/ |
Current File : /home/opti/workflow/src/routes/organization.js |
var express = require('express'); var router = express.Router(); var bcrypt = require('bcrypt') var secured = require('../middleware/login'); var db = require('../models'); router.get('/', secured, function (req, res) { var user = req.user; if (user.roleId === 'isSuperAdmin') { return db.Organization.findAll().then(function (allOrganizations) { if (allOrganizations === null) { res.render('organization/index', { title: "Organizations", allOrganizations: [], user, scripts: 'index' }); } else { console.log("All organizations: ", allOrganizations) res.render('organization/index', { title: "Organizations", allOrganizations, user, scripts: 'index' }); } }); } db.Organization.findAll({ where: { id: user.orgId } }).then(function (allOrganizations) { if (allOrganizations === null) { res.render('organization/index', { title: "Organizations", allOrganizations: [], user, scripts: 'index' }); } else { console.log("All organizations: ", allOrganizations) res.render('organization/index', { title: "Organizations", allOrganizations, user, scripts: 'index' }); } }) }) router.post('/add', secured, function (req, res) { var user = req.user; var name = req.body.name, description = req.body.description, address = req.body.address, city = req.body.city, state = req.body.state, country = req.body.country, postalCode = req.body.postalCode, email = req.body.email, phone = req.body.phone, createdBy = user.id; var organization = { name, description, address, city, state, country, postalCode, email, phone, createdBy }; db.Organization.find({ where: { name: organization.name, id: user.orgId } }).then(function (oldOrganization) { if (oldOrganization === null) { db.Organization.create(organization).then(function (organization) { if (!organization) { req.flash('info', 'Error occured while saving organization') return res.redirect('/organization'); } // TODO create the admin and login var adminFirstName = req.body.adminFirstName, adminLastName = req.body.adminLastName, adminEmail = req.body.adminEmail, adminPassword = req.body.adminPassword; var salt = bcrypt.genSaltSync(10); adminPassword = bcrypt.hashSync(adminPassword, salt); var newUser = { orgId: organization.id, firstName: adminFirstName, lastName: adminLastName, email: adminEmail, password: adminPassword, roleId: 'isAdmin' } db.User.create(newUser).then(function (user) { if (!user) { req.flash('info', 'Error creating admin'); return res.redirect('/organization') } req.flash('info', 'Organization and admin created successfully ') res.redirect('/organization'); }) }) } else { req.flash('info', 'An organization with the same name is already in the system') return res.redirect('/organization'); } }); }) router.post('/delete', secured, function (req, res) { var orgId = req.body.id; var user = req.user; db.Organization.find({ where: { id: orgId } }).then(function (organization) { if (organization == null) { return res.status(404); } if (user.roleId == 'isSuperAdmin' ) { deleteOrganizationUsers(organization.id) organization.destroy().then(function () { console.log("Organization deleted successfully") req.flash('info', 'Organization successfully removed.') res.redirect('/organization'); }) } else { req.flash('info', 'You do not have permission to remove this organization') res.redirect('/organization'); } }) }) router.post('/update', secured, function (req, res) { console.log("BODY: ", req.body) var orgId = req.body.id; var user = req.user; var name = req.body.name, description = req.body.description, address = req.body.address, city = req.body.city, state = req.body.state, country = req.body.country, postalCode = req.body.postalCode, email = req.body.email, phone = req.body.phone, updatedBy = user.id; var organization = { name, description, address, city, state, country, postalCode, email, phone, updatedBy }; db.Organization.update(organization, { where: { id: orgId } }) .then(function () { console.log("Organization updated successfully") res.redirect('/organization') }) }) function deleteOrganizationUsers(orgId) { db.User.findAll({ where: { orgId: orgId } }).then(function (users) { users.forEach(function (user) { user.destroy().then(function () { console.log("Organization Employee deleted") }) }) }) } module.exports = router;