diff --git a/models/db.js b/models/db.js index e5169a1..70e8f19 100644 --- a/models/db.js +++ b/models/db.js @@ -93,6 +93,8 @@ module.exports = { space_id: Sequelize.STRING, user_id: Sequelize.STRING, role: Sequelize.STRING, + code: Sequelize.STRING, + state: {type: Sequelize.STRING, defaultValue: "pending"}, created_at: {type: Sequelize.DATE, defaultValue: Sequelize.NOW}, updated_at: {type: Sequelize.DATE, defaultValue: Sequelize.NOW} }), @@ -211,6 +213,20 @@ module.exports = { }, as: 'creator' }); + + Membership.belongsTo(User, { + foreignKey: { + name: 'user_id' + }, + as: 'user' + }); + + Membership.belongsTo(Space, { + foreignKey: { + name: 'space_id' + }, + as: 'space' + }); Artifact.belongsTo(User, { foreignKey: { diff --git a/routes/api/memberships.js b/routes/api/memberships.js index e91360e..f25cd1d 100644 --- a/routes/api/memberships.js +++ b/routes/api/memberships.js @@ -1,12 +1,8 @@ "use strict"; var config = require('config'); -require('../../models/db'); -var fs = require('fs'); -var _ = require("underscore"); var async = require('async'); -var request = require('request'); var url = require("url"); var path = require("path"); var crypto = require('crypto'); @@ -15,38 +11,30 @@ var glob = require('glob'); var express = require('express'); var router = express.Router(); -var userMapping = { '_id': 1, 'nickname': 1, 'email': 1}; -var spaceMapping = { '_id': 1, name: 1}; +const db = require('../../models/db'); +const Sequelize = require('sequelize'); +const Op = Sequelize.Op; +const uuidv4 = require('uuid/v4'); router.get('/:membership_id/accept', function(req, res, next) { if (req.user) { - Membership.findOne({ + db.Membership.findOne({where:{ _id: req.params.membership_id, - state: "pending", - code: req.query.code, - user: { "$exists": false } - }).populate('space').exec((err,mem) => { - if (err) res.sendStatus(400); - else { - if (mem) { - if(!mem.user) { - mem.code = null; - mem.state = "active"; - mem.user = req.user; - - mem.save(function(err){ - if (err) res.status(400).json(err); - else { - console.log(mem); - res.status(200).json(mem); - } - }); - } else { - res.status(400).json({"error": "already_used"}); - } + code: req.query.code + }, include: ['space']}).then((mem) => { + if (mem) { + if (!mem.user) { + mem.state = "active"; + mem.user_id = req.user._id; + + mem.save().then(function() { + res.status(200).json(mem); + }); } else { - res.status(404).json({"error": "not found"}); + res.status(200).json(mem); } + } else { + res.status(404).json({"error": "not found"}); } }); } else { diff --git a/routes/api/space_memberships.js b/routes/api/space_memberships.js index 6df21d7..2b88dfa 100644 --- a/routes/api/space_memberships.js +++ b/routes/api/space_memberships.js @@ -15,37 +15,16 @@ var request = require('request'); var url = require("url"); var path = require("path"); var glob = require('glob'); +var crypto = require('crypto'); var express = require('express'); var router = express.Router({mergeParams: true}); -// JSON MAPPINGS -var userMapping = { - _id: 1, - nickname: 1, - email: 1, - avatar_thumb_uri: 1 -}; - -var spaceMapping = { - _id: 1, - name: 1, - thumbnail_url: 1 -}; - -var roleMapping = { - "none": 0, - "viewer": 1, - "editor": 2, - "admin": 3 -} - router.get('/', function(req, res, next) { db.Membership .findAll({where: { space_id: req.space._id - }}) - //.populate("user") + }, include: ['user']}) .then(memberships => { res.status(200).json(memberships); }); @@ -54,8 +33,8 @@ router.get('/', function(req, res, next) { router.post('/', function(req, res, next) { if (req.spaceRole == "admin") { var attrs = req.body; - attrs['space'] = req.space._id; - attrs['state'] = "pending"; + attrs.space_id = req.space._id; + attrs.state = "pending"; attrs._id = uuidv4(); var membership = attrs; @@ -64,10 +43,10 @@ router.post('/', function(req, res, next) { if (membership.email_invited != req.user.email) { db.User.findOne({where:{ "email": membership.email_invited - }}, function(user) { + }}).then(function(user) { if (user) { - membership.user = user; + membership.user_id = user._id; membership.state = "active"; } else { membership.code = crypto.randomBytes(64).toString('hex').substring(0, 12); @@ -119,21 +98,15 @@ router.post('/', function(req, res, next) { router.put('/:membership_id', function(req, res, next) { if (req.user) { if (req.spaceRole == "admin") { - Membership.findOne({ + db.Membership.findOne({ where: { _id: req.params.membership_id - }, function(err, mem) { - if (err) res.sendStatus(400); - else { - if (mem) { - var attrs = req.body; - mem.role = attrs.role; - mem.save(function(err) { - if (err) res.sendStatus(400); - else { - res.status(201).json(mem); - } - }); - } + }}).then(function(mem) { + if (mem) { + var attrs = req.body; + mem.role = attrs.role; + mem.save(function() { + res.status(201).json(mem); + }); } }); } else { @@ -146,20 +119,12 @@ router.put('/:membership_id', function(req, res, next) { router.delete('/:membership_id', function(req, res, next) { if (req.user) { - Membership.findOne({ + db.Membership.findOne({ where: { _id: req.params.membership_id - }, function(err, mem) { - if (err) res.sendStatus(400); - else { - mem.remove(function(err) { - if (err) { - res.status(400).json(err); - } else { - // FIXME might need to delete the user? - res.sendStatus(204); - } - }); - } + }}).then(function(mem) { + mem.destroy().then(function() { + res.sendStatus(204); + }); }); } else { res.sendStatus(403); diff --git a/views/partials/tool/zones.html b/views/partials/tool/zones.html index 8bfec42..57f873f 100644 --- a/views/partials/tool/zones.html +++ b/views/partials/tool/zones.html @@ -9,7 +9,7 @@ -