boards.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. Boards = new Mongo.Collection('boards');
  2. Boards.attachSchema(new SimpleSchema({
  3. title: {
  4. type: String,
  5. },
  6. slug: {
  7. type: String,
  8. autoValue() { // eslint-disable-line consistent-return
  9. // XXX We need to improve slug management. Only the id should be necessary
  10. // to identify a board in the code.
  11. // XXX If the board title is updated, the slug should also be updated.
  12. // In some cases (Chinese and Japanese for instance) the `getSlug` function
  13. // return an empty string. This is causes bugs in our application so we set
  14. // a default slug in this case.
  15. if (this.isInsert && !this.isSet) {
  16. let slug = 'board';
  17. const title = this.field('title');
  18. if (title.isSet) {
  19. slug = getSlug(title.value) || slug;
  20. }
  21. return slug;
  22. }
  23. },
  24. },
  25. archived: {
  26. type: Boolean,
  27. autoValue() { // eslint-disable-line consistent-return
  28. if (this.isInsert && !this.isSet) {
  29. return false;
  30. }
  31. },
  32. },
  33. createdAt: {
  34. type: Date,
  35. autoValue() { // eslint-disable-line consistent-return
  36. if (this.isInsert) {
  37. return new Date();
  38. } else {
  39. this.unset();
  40. }
  41. },
  42. },
  43. // XXX Inconsistent field naming
  44. modifiedAt: {
  45. type: Date,
  46. optional: true,
  47. autoValue() { // eslint-disable-line consistent-return
  48. if (this.isUpdate) {
  49. return new Date();
  50. } else {
  51. this.unset();
  52. }
  53. },
  54. },
  55. // De-normalized number of users that have starred this board
  56. stars: {
  57. type: Number,
  58. autoValue() { // eslint-disable-line consistent-return
  59. if (this.isInsert) {
  60. return 0;
  61. }
  62. },
  63. },
  64. // De-normalized label system
  65. 'labels': {
  66. type: [Object],
  67. autoValue() { // eslint-disable-line consistent-return
  68. if (this.isInsert && !this.isSet) {
  69. const colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
  70. const defaultLabelsColors = _.clone(colors).splice(0, 6);
  71. return defaultLabelsColors.map((color) => ({
  72. color,
  73. _id: Random.id(6),
  74. name: '',
  75. }));
  76. }
  77. },
  78. },
  79. 'labels.$._id': {
  80. // We don't specify that this field must be unique in the board because that
  81. // will cause performance penalties and is not necessary since this field is
  82. // always set on the server.
  83. // XXX Actually if we create a new label, the `_id` is set on the client
  84. // without being overwritten by the server, could it be a problem?
  85. type: String,
  86. },
  87. 'labels.$.name': {
  88. type: String,
  89. optional: true,
  90. },
  91. 'labels.$.color': {
  92. type: String,
  93. allowedValues: [
  94. 'green', 'yellow', 'orange', 'red', 'purple',
  95. 'blue', 'sky', 'lime', 'pink', 'black',
  96. 'silver', 'peachpuff', 'crimson', 'plum', 'darkgreen',
  97. 'slateblue', 'magenta', 'gold', 'navy', 'gray',
  98. 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo',
  99. ],
  100. },
  101. // XXX We might want to maintain more informations under the member sub-
  102. // documents like de-normalized meta-data (the date the member joined the
  103. // board, the number of contributions, etc.).
  104. 'members': {
  105. type: [Object],
  106. autoValue() { // eslint-disable-line consistent-return
  107. if (this.isInsert && !this.isSet) {
  108. return [{
  109. userId: this.userId,
  110. isAdmin: true,
  111. isActive: true,
  112. isCommentOnly: false,
  113. }];
  114. }
  115. },
  116. },
  117. 'members.$.userId': {
  118. type: String,
  119. },
  120. 'members.$.isAdmin': {
  121. type: Boolean,
  122. },
  123. 'members.$.isActive': {
  124. type: Boolean,
  125. },
  126. 'members.$.isCommentOnly': {
  127. type: Boolean,
  128. },
  129. permission: {
  130. type: String,
  131. allowedValues: ['public', 'private'],
  132. },
  133. color: {
  134. type: String,
  135. allowedValues: [
  136. 'belize',
  137. 'nephritis',
  138. 'pomegranate',
  139. 'pumpkin',
  140. 'wisteria',
  141. 'midnight',
  142. ],
  143. autoValue() { // eslint-disable-line consistent-return
  144. if (this.isInsert && !this.isSet) {
  145. return Boards.simpleSchema()._schema.color.allowedValues[0];
  146. }
  147. },
  148. },
  149. description: {
  150. type: String,
  151. optional: true,
  152. },
  153. subtasksDefaultBoardId: {
  154. type: String,
  155. optional: true,
  156. defaultValue: null,
  157. },
  158. subtasksDefaultListId: {
  159. type: String,
  160. optional: true,
  161. defaultValue: null,
  162. },
  163. allowsSubtasks: {
  164. type: Boolean,
  165. defaultValue: true,
  166. },
  167. presentParentTask: {
  168. type: String,
  169. allowedValues: [
  170. 'prefix-with-full-path',
  171. 'prefix-with-parent',
  172. 'subtext-with-full-path',
  173. 'subtext-with-parent',
  174. 'no-parent',
  175. ],
  176. optional: true,
  177. defaultValue: 'no-parent',
  178. },
  179. }));
  180. Boards.helpers({
  181. /**
  182. * Is supplied user authorized to view this board?
  183. */
  184. isVisibleBy(user) {
  185. if (this.isPublic()) {
  186. // public boards are visible to everyone
  187. return true;
  188. } else {
  189. // otherwise you have to be logged-in and active member
  190. return user && this.isActiveMember(user._id);
  191. }
  192. },
  193. /**
  194. * Is the user one of the active members of the board?
  195. *
  196. * @param userId
  197. * @returns {boolean} the member that matches, or undefined/false
  198. */
  199. isActiveMember(userId) {
  200. if (userId) {
  201. return this.members.find((member) => (member.userId === userId && member.isActive));
  202. } else {
  203. return false;
  204. }
  205. },
  206. isPublic() {
  207. return this.permission === 'public';
  208. },
  209. lists() {
  210. return Lists.find({ boardId: this._id, archived: false }, { sort: { sort: 1 } });
  211. },
  212. swimlanes() {
  213. return Swimlanes.find({ boardId: this._id, archived: false }, { sort: { sort: 1 } });
  214. },
  215. hasOvertimeCards(){
  216. const card = Cards.findOne({isOvertime: true, boardId: this._id, archived: false} );
  217. return card !== undefined;
  218. },
  219. hasSpentTimeCards(){
  220. const card = Cards.findOne({spentTime: { $gt: 0 }, boardId: this._id, archived: false} );
  221. return card !== undefined;
  222. },
  223. activities() {
  224. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 } });
  225. },
  226. activeMembers() {
  227. return _.where(this.members, { isActive: true });
  228. },
  229. activeAdmins() {
  230. return _.where(this.members, { isActive: true, isAdmin: true });
  231. },
  232. memberUsers() {
  233. return Users.find({ _id: { $in: _.pluck(this.members, 'userId') } });
  234. },
  235. getLabel(name, color) {
  236. return _.findWhere(this.labels, { name, color });
  237. },
  238. labelIndex(labelId) {
  239. return _.pluck(this.labels, '_id').indexOf(labelId);
  240. },
  241. memberIndex(memberId) {
  242. return _.pluck(this.members, 'userId').indexOf(memberId);
  243. },
  244. hasMember(memberId) {
  245. return !!_.findWhere(this.members, { userId: memberId, isActive: true });
  246. },
  247. hasAdmin(memberId) {
  248. return !!_.findWhere(this.members, { userId: memberId, isActive: true, isAdmin: true });
  249. },
  250. hasCommentOnly(memberId) {
  251. return !!_.findWhere(this.members, { userId: memberId, isActive: true, isAdmin: false, isCommentOnly: true });
  252. },
  253. absoluteUrl() {
  254. return FlowRouter.url('board', { id: this._id, slug: this.slug });
  255. },
  256. colorClass() {
  257. return `board-color-${this.color}`;
  258. },
  259. customFields() {
  260. return CustomFields.find({ boardId: this._id }, { sort: { name: 1 } });
  261. },
  262. // XXX currently mutations return no value so we have an issue when using addLabel in import
  263. // XXX waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  264. pushLabel(name, color) {
  265. const _id = Random.id(6);
  266. Boards.direct.update(this._id, { $push: { labels: { _id, name, color } } });
  267. return _id;
  268. },
  269. searchCards(term) {
  270. check(term, Match.OneOf(String, null, undefined));
  271. let query = { boardId: this._id };
  272. const projection = { limit: 10, sort: { createdAt: -1 } };
  273. if (term) {
  274. const regex = new RegExp(term, 'i');
  275. query = {
  276. boardId: this._id,
  277. $or: [
  278. { title: regex },
  279. { description: regex },
  280. ],
  281. };
  282. }
  283. return Cards.find(query, projection);
  284. },
  285. // A board alwasy has another board where it deposits subtasks of thasks
  286. // that belong to itself.
  287. getDefaultSubtasksBoardId() {
  288. if ((this.subtasksDefaultBoardId === null) || (this.subtasksDefaultBoardId === undefined)) {
  289. this.subtasksDefaultBoardId = Boards.insert({
  290. title: `^${this.title}^`,
  291. permission: this.permission,
  292. members: this.members,
  293. color: this.color,
  294. description: TAPi18n.__('default-subtasks-board', {board: this.title}),
  295. });
  296. Swimlanes.insert({
  297. title: TAPi18n.__('default'),
  298. boardId: this.subtasksDefaultBoardId,
  299. });
  300. Boards.update(this._id, {$set: {
  301. subtasksDefaultBoardId: this.subtasksDefaultBoardId,
  302. }});
  303. }
  304. return this.subtasksDefaultBoardId;
  305. },
  306. getDefaultSubtasksBoard() {
  307. return Boards.findOne(this.getDefaultSubtasksBoardId());
  308. },
  309. getDefaultSubtasksListId() {
  310. if ((this.subtasksDefaultListId === null) || (this.subtasksDefaultListId === undefined)) {
  311. this.subtasksDefaultListId = Lists.insert({
  312. title: TAPi18n.__('queue'),
  313. boardId: this._id,
  314. });
  315. Boards.update(this._id, {$set: {
  316. subtasksDefaultListId: this.subtasksDefaultListId,
  317. }});
  318. }
  319. return this.subtasksDefaultListId;
  320. },
  321. getDefaultSubtasksList() {
  322. return Lists.findOne(this.getDefaultSubtasksListId());
  323. },
  324. getDefaultSwimline() {
  325. let result = Swimlanes.findOne({boardId: this._id});
  326. if (result === undefined) {
  327. Swimlanes.insert({
  328. title: TAPi18n.__('default'),
  329. boardId: this._id,
  330. });
  331. result = Swimlanes.findOne({boardId: this._id});
  332. }
  333. return result;
  334. },
  335. });
  336. Boards.mutations({
  337. archive() {
  338. return { $set: { archived: true } };
  339. },
  340. restore() {
  341. return { $set: { archived: false } };
  342. },
  343. rename(title) {
  344. return { $set: { title } };
  345. },
  346. setDescription(description) {
  347. return { $set: { description } };
  348. },
  349. setColor(color) {
  350. return { $set: { color } };
  351. },
  352. setVisibility(visibility) {
  353. return { $set: { permission: visibility } };
  354. },
  355. addLabel(name, color) {
  356. // If label with the same name and color already exists we don't want to
  357. // create another one because they would be indistinguishable in the UI
  358. // (they would still have different `_id` but that is not exposed to the
  359. // user).
  360. if (!this.getLabel(name, color)) {
  361. const _id = Random.id(6);
  362. return { $push: { labels: { _id, name, color } } };
  363. }
  364. return {};
  365. },
  366. editLabel(labelId, name, color) {
  367. if (!this.getLabel(name, color)) {
  368. const labelIndex = this.labelIndex(labelId);
  369. return {
  370. $set: {
  371. [`labels.${labelIndex}.name`]: name,
  372. [`labels.${labelIndex}.color`]: color,
  373. },
  374. };
  375. }
  376. return {};
  377. },
  378. removeLabel(labelId) {
  379. return { $pull: { labels: { _id: labelId } } };
  380. },
  381. changeOwnership(fromId, toId) {
  382. const memberIndex = this.memberIndex(fromId);
  383. return {
  384. $set: {
  385. [`members.${memberIndex}.userId`]: toId,
  386. },
  387. };
  388. },
  389. addMember(memberId) {
  390. const memberIndex = this.memberIndex(memberId);
  391. if (memberIndex >= 0) {
  392. return {
  393. $set: {
  394. [`members.${memberIndex}.isActive`]: true,
  395. },
  396. };
  397. }
  398. return {
  399. $push: {
  400. members: {
  401. userId: memberId,
  402. isAdmin: false,
  403. isActive: true,
  404. isCommentOnly: false,
  405. },
  406. },
  407. };
  408. },
  409. removeMember(memberId) {
  410. const memberIndex = this.memberIndex(memberId);
  411. // we do not allow the only one admin to be removed
  412. const allowRemove = (!this.members[memberIndex].isAdmin) || (this.activeAdmins().length > 1);
  413. if (!allowRemove) {
  414. return {
  415. $set: {
  416. [`members.${memberIndex}.isActive`]: true,
  417. },
  418. };
  419. }
  420. return {
  421. $set: {
  422. [`members.${memberIndex}.isActive`]: false,
  423. [`members.${memberIndex}.isAdmin`]: false,
  424. },
  425. };
  426. },
  427. setMemberPermission(memberId, isAdmin, isCommentOnly) {
  428. const memberIndex = this.memberIndex(memberId);
  429. // do not allow change permission of self
  430. if (memberId === Meteor.userId()) {
  431. isAdmin = this.members[memberIndex].isAdmin;
  432. }
  433. return {
  434. $set: {
  435. [`members.${memberIndex}.isAdmin`]: isAdmin,
  436. [`members.${memberIndex}.isCommentOnly`]: isCommentOnly,
  437. },
  438. };
  439. },
  440. setAllowsSubtasks(allowsSubtasks) {
  441. return { $set: { allowsSubtasks } };
  442. },
  443. setSubtasksDefaultBoardId(subtasksDefaultBoardId) {
  444. return { $set: { subtasksDefaultBoardId } };
  445. },
  446. setSubtasksDefaultListId(subtasksDefaultListId) {
  447. return { $set: { subtasksDefaultListId } };
  448. },
  449. setPresentParentTask(presentParentTask) {
  450. return { $set: { presentParentTask } };
  451. },
  452. });
  453. if (Meteor.isServer) {
  454. Boards.allow({
  455. insert: Meteor.userId,
  456. update: allowIsBoardAdmin,
  457. remove: allowIsBoardAdmin,
  458. fetch: ['members'],
  459. });
  460. // The number of users that have starred this board is managed by trusted code
  461. // and the user is not allowed to update it
  462. Boards.deny({
  463. update(userId, board, fieldNames) {
  464. return _.contains(fieldNames, 'stars');
  465. },
  466. fetch: [],
  467. });
  468. // We can't remove a member if it is the last administrator
  469. Boards.deny({
  470. update(userId, doc, fieldNames, modifier) {
  471. if (!_.contains(fieldNames, 'members'))
  472. return false;
  473. // We only care in case of a $pull operation, ie remove a member
  474. if (!_.isObject(modifier.$pull && modifier.$pull.members))
  475. return false;
  476. // If there is more than one admin, it's ok to remove anyone
  477. const nbAdmins = _.where(doc.members, { isActive: true, isAdmin: true }).length;
  478. if (nbAdmins > 1)
  479. return false;
  480. // If all the previous conditions were verified, we can't remove
  481. // a user if it's an admin
  482. const removedMemberId = modifier.$pull.members.userId;
  483. return Boolean(_.findWhere(doc.members, {
  484. userId: removedMemberId,
  485. isAdmin: true,
  486. }));
  487. },
  488. fetch: ['members'],
  489. });
  490. Meteor.methods({
  491. quitBoard(boardId) {
  492. check(boardId, String);
  493. const board = Boards.findOne(boardId);
  494. if (board) {
  495. const userId = Meteor.userId();
  496. const index = board.memberIndex(userId);
  497. if (index >= 0) {
  498. board.removeMember(userId);
  499. return true;
  500. } else throw new Meteor.Error('error-board-notAMember');
  501. } else throw new Meteor.Error('error-board-doesNotExist');
  502. },
  503. });
  504. }
  505. if (Meteor.isServer) {
  506. // Let MongoDB ensure that a member is not included twice in the same board
  507. Meteor.startup(() => {
  508. Boards._collection._ensureIndex({
  509. _id: 1,
  510. 'members.userId': 1,
  511. }, { unique: true });
  512. Boards._collection._ensureIndex({ 'members.userId': 1 });
  513. });
  514. // Genesis: the first activity of the newly created board
  515. Boards.after.insert((userId, doc) => {
  516. Activities.insert({
  517. userId,
  518. type: 'board',
  519. activityTypeId: doc._id,
  520. activityType: 'createBoard',
  521. boardId: doc._id,
  522. });
  523. });
  524. // If the user remove one label from a board, we cant to remove reference of
  525. // this label in any card of this board.
  526. Boards.after.update((userId, doc, fieldNames, modifier) => {
  527. if (!_.contains(fieldNames, 'labels') ||
  528. !modifier.$pull ||
  529. !modifier.$pull.labels ||
  530. !modifier.$pull.labels._id) {
  531. return;
  532. }
  533. const removedLabelId = modifier.$pull.labels._id;
  534. Cards.update(
  535. { boardId: doc._id },
  536. {
  537. $pull: {
  538. labelIds: removedLabelId,
  539. },
  540. },
  541. { multi: true }
  542. );
  543. });
  544. const foreachRemovedMember = (doc, modifier, callback) => {
  545. Object.keys(modifier).forEach((set) => {
  546. if (modifier[set] !== false) {
  547. return;
  548. }
  549. const parts = set.split('.');
  550. if (parts.length === 3 && parts[0] === 'members' && parts[2] === 'isActive') {
  551. callback(doc.members[parts[1]].userId);
  552. }
  553. });
  554. };
  555. // Remove a member from all objects of the board before leaving the board
  556. Boards.before.update((userId, doc, fieldNames, modifier) => {
  557. if (!_.contains(fieldNames, 'members')) {
  558. return;
  559. }
  560. if (modifier.$set) {
  561. const boardId = doc._id;
  562. foreachRemovedMember(doc, modifier.$set, (memberId) => {
  563. Cards.update(
  564. { boardId },
  565. {
  566. $pull: {
  567. members: memberId,
  568. watchers: memberId,
  569. },
  570. },
  571. { multi: true }
  572. );
  573. Lists.update(
  574. { boardId },
  575. {
  576. $pull: {
  577. watchers: memberId,
  578. },
  579. },
  580. { multi: true }
  581. );
  582. const board = Boards._transform(doc);
  583. board.setWatcher(memberId, false);
  584. // Remove board from users starred list
  585. if (!board.isPublic()) {
  586. Users.update(
  587. memberId,
  588. {
  589. $pull: {
  590. 'profile.starredBoards': boardId,
  591. },
  592. }
  593. );
  594. }
  595. });
  596. }
  597. });
  598. // Add a new activity if we add or remove a member to the board
  599. Boards.after.update((userId, doc, fieldNames, modifier) => {
  600. if (!_.contains(fieldNames, 'members')) {
  601. return;
  602. }
  603. // Say hello to the new member
  604. if (modifier.$push && modifier.$push.members) {
  605. const memberId = modifier.$push.members.userId;
  606. Activities.insert({
  607. userId,
  608. memberId,
  609. type: 'member',
  610. activityType: 'addBoardMember',
  611. boardId: doc._id,
  612. });
  613. }
  614. // Say goodbye to the former member
  615. if (modifier.$set) {
  616. foreachRemovedMember(doc, modifier.$set, (memberId) => {
  617. Activities.insert({
  618. userId,
  619. memberId,
  620. type: 'member',
  621. activityType: 'removeBoardMember',
  622. boardId: doc._id,
  623. });
  624. });
  625. }
  626. });
  627. }
  628. //BOARDS REST API
  629. if (Meteor.isServer) {
  630. JsonRoutes.add('GET', '/api/users/:userId/boards', function (req, res) {
  631. try {
  632. Authentication.checkLoggedIn(req.userId);
  633. const paramUserId = req.params.userId;
  634. // A normal user should be able to see their own boards,
  635. // admins can access boards of any user
  636. Authentication.checkAdminOrCondition(req.userId, req.userId === paramUserId);
  637. const data = Boards.find({
  638. archived: false,
  639. 'members.userId': paramUserId,
  640. }, {
  641. sort: ['title'],
  642. }).map(function(board) {
  643. return {
  644. _id: board._id,
  645. title: board.title,
  646. };
  647. });
  648. JsonRoutes.sendResult(res, {code: 200, data});
  649. }
  650. catch (error) {
  651. JsonRoutes.sendResult(res, {
  652. code: 200,
  653. data: error,
  654. });
  655. }
  656. });
  657. JsonRoutes.add('GET', '/api/boards', function (req, res) {
  658. try {
  659. Authentication.checkUserId(req.userId);
  660. JsonRoutes.sendResult(res, {
  661. code: 200,
  662. data: Boards.find({ permission: 'public' }).map(function (doc) {
  663. return {
  664. _id: doc._id,
  665. title: doc.title,
  666. };
  667. }),
  668. });
  669. }
  670. catch (error) {
  671. JsonRoutes.sendResult(res, {
  672. code: 200,
  673. data: error,
  674. });
  675. }
  676. });
  677. JsonRoutes.add('GET', '/api/boards/:id', function (req, res) {
  678. try {
  679. const id = req.params.id;
  680. Authentication.checkBoardAccess(req.userId, id);
  681. JsonRoutes.sendResult(res, {
  682. code: 200,
  683. data: Boards.findOne({ _id: id }),
  684. });
  685. }
  686. catch (error) {
  687. JsonRoutes.sendResult(res, {
  688. code: 200,
  689. data: error,
  690. });
  691. }
  692. });
  693. JsonRoutes.add('POST', '/api/boards', function (req, res) {
  694. try {
  695. Authentication.checkUserId(req.userId);
  696. const id = Boards.insert({
  697. title: req.body.title,
  698. members: [
  699. {
  700. userId: req.body.owner,
  701. isAdmin: true,
  702. isActive: true,
  703. isCommentOnly: false,
  704. },
  705. ],
  706. permission: 'public',
  707. color: 'belize',
  708. });
  709. JsonRoutes.sendResult(res, {
  710. code: 200,
  711. data: {
  712. _id: id,
  713. },
  714. });
  715. }
  716. catch (error) {
  717. JsonRoutes.sendResult(res, {
  718. code: 200,
  719. data: error,
  720. });
  721. }
  722. });
  723. JsonRoutes.add('DELETE', '/api/boards/:id', function (req, res) {
  724. try {
  725. Authentication.checkUserId(req.userId);
  726. const id = req.params.id;
  727. Boards.remove({ _id: id });
  728. JsonRoutes.sendResult(res, {
  729. code: 200,
  730. data:{
  731. _id: id,
  732. },
  733. });
  734. }
  735. catch (error) {
  736. JsonRoutes.sendResult(res, {
  737. code: 200,
  738. data: error,
  739. });
  740. }
  741. });
  742. JsonRoutes.add('PUT', '/api/boards/:id/labels', function (req, res) {
  743. Authentication.checkUserId(req.userId);
  744. const id = req.params.id;
  745. try {
  746. if (req.body.hasOwnProperty('label')) {
  747. const board = Boards.findOne({ _id: id });
  748. const color = req.body.label.color;
  749. const name = req.body.label.name;
  750. const labelId = Random.id(6);
  751. if (!board.getLabel(name, color)) {
  752. Boards.direct.update({ _id: id }, { $push: { labels: { _id: labelId, name, color } } });
  753. JsonRoutes.sendResult(res, {
  754. code: 200,
  755. data: labelId,
  756. });
  757. } else {
  758. JsonRoutes.sendResult(res, {
  759. code: 200,
  760. });
  761. }
  762. }
  763. }
  764. catch (error) {
  765. JsonRoutes.sendResult(res, {
  766. data: error,
  767. });
  768. }
  769. });
  770. }