|
5 | 5 | * @description :: Manipulate tags (add / delete / assign to projects/tasks)
|
6 | 6 | */
|
7 | 7 |
|
| 8 | +var _ = require('underscore'); |
| 9 | +var async = require('async'); |
| 10 | +var projUtils = require('../services/projectUtils'); |
| 11 | + |
8 | 12 | module.exports = {
|
9 | 13 |
|
| 14 | + findAllByProjectId: function (req, res) { |
| 15 | + Tag.findByProjectId(req.params.id, function (err, tags) { |
| 16 | + var entities = {}; |
| 17 | + var tagIds = []; |
| 18 | + |
| 19 | + // Helper function for async to look up each tag entitiy |
| 20 | + var getTags = function (tagId, done) { |
| 21 | + if (entities[tagId]) { return done(); } |
| 22 | + TagEntity.findOne(tagId, function (err, t) { |
| 23 | + entities[tagId] = t; |
| 24 | + done(err); |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + // Get all the tag ids |
| 29 | + for (var i = 0; i < tags.length; i++) { |
| 30 | + tagIds.push(tags[i].id); |
| 31 | + } |
| 32 | + |
| 33 | + // Get the tag entities for each id |
| 34 | + async.each(tagIds, getTags, function(err) { |
| 35 | + if (err) { return res.send(400, { message: "Error looking up tags"}); } |
| 36 | + // Attach the tag entity to the tag |
| 37 | + for (var i = 0; i < tags.length; i++) { |
| 38 | + tags[i].tag = entities[tags[i].id]; |
| 39 | + return res.send(tags); |
| 40 | + } |
| 41 | + }); |
| 42 | + }); |
| 43 | + }, |
| 44 | + |
| 45 | + // Add a new tag entity to the system (alias for TagEntity.create) |
| 46 | + add: function (req, res) { |
| 47 | + if (req.route.method != 'post') { return res.send(400, { message: 'Unsupported operation.' } ); } |
| 48 | + var tag = _.extend(req.body || {}, req.params); |
| 49 | + TagEntity.findOne({where: { name: tag.name, type: tag.type }}, function (err, existingTag) { |
| 50 | + if (err) { return res.send(400, { message: 'Error looking up tag' }); } |
| 51 | + if (existingTag) { return res.send(existingTag); } |
| 52 | + TagEntity.create(tag, function (err, tag) { |
| 53 | + if (err) { return res.send(400, { message: 'Error creating tag' }); } |
| 54 | + return res.send(tag); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }, |
| 58 | + |
| 59 | + // Override default create to check parameters and |
| 60 | + // ensure duplicate tags are not created. |
| 61 | + create: function (req, res) { |
| 62 | + if (req.route.method != 'post') { return res.send(400, { message: 'Unsupported operation.' } ); } |
| 63 | + var tag = _.extend(req.body || {}, req.params); |
| 64 | + if (!tag.projectId) { tag.projectId = null; } |
| 65 | + if (!tag.taskId) { tag.taskId = null; } |
| 66 | + if (!tag.tagId) { return res.send(400, { message: "Must specify a tag id" }); } |
| 67 | + if (!tag.projectId && !tag.taskId) { return res.send(400, { message: "Must specify either a project or task" }); } |
| 68 | + // check if the tag already exists |
| 69 | + Tag.findOne( |
| 70 | + { where: { projectId: tag.projectId, taskId: tag.taskId, tagId: tag.tagId }}, |
| 71 | + function (err, existingTag) { |
| 72 | + if (err) { return res.send(400, { message: 'Error looking up tag' }); } |
| 73 | + if (existingTag) { return res.send(existingTag); } |
| 74 | + // Create tag if it doesn't exist |
| 75 | + Tag.create(tag, function (err, tag) { |
| 76 | + if (err) { return res.send(400, { message: 'Error creating tag' }); } |
| 77 | + return res.send(tag); |
| 78 | + }); |
| 79 | + } |
| 80 | + ); |
| 81 | + }, |
| 82 | + |
| 83 | + // Override destroy to ensure owner has access to project |
| 84 | + destroy: function (req, res) { |
| 85 | + if (req.route.method != 'delete') { return res.send(400, { message: 'Unsupported operation.' } ); } |
| 86 | + Tag.findOneById( req.params.id, function (err, tag) { |
| 87 | + if (err) { return res.send(400, { message: 'Error looking up tag' }); } |
| 88 | + if (!tag) { return res.send(404, { message: 'Tag not found'}); } |
| 89 | + // check if this user is authorized |
| 90 | + projUtils.authorized(tag.projectId, req.user[0].id, function (err, proj) { |
| 91 | + if (err) { return res.send(400, { message: err }); } |
| 92 | + if (!err && !proj) { return res.send(403, { message: 'Not authorized.'}); } |
| 93 | + tag.destroy(function (err) { |
| 94 | + if (err) { return res.send(400, { message: 'Error destroying tag mapping' }); } |
| 95 | + return res.send(tag); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
10 | 101 | };
|
0 commit comments