2
0

boards.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. }));
  164. Boards.helpers({
  165. /**
  166. * Is supplied user authorized to view this board?
  167. */
  168. isVisibleBy(user) {
  169. if (this.isPublic()) {
  170. // public boards are visible to everyone
  171. return true;
  172. } else {
  173. // otherwise you have to be logged-in and active member
  174. return user && this.isActiveMember(user._id);
  175. }
  176. },
  177. /**
  178. * Is the user one of the active members of the board?
  179. *
  180. * @param userId
  181. * @returns {boolean} the member that matches, or undefined/false
  182. */
  183. isActiveMember(userId) {
  184. if (userId) {
  185. return this.members.find((member) => (member.userId === userId && member.isActive));
  186. } else {
  187. return false;
  188. }
  189. },
  190. isPublic() {
  191. return this.permission === 'public';
  192. },
  193. lists() {
  194. return Lists.find({ boardId: this._id, archived: false }, { sort: { sort: 1 } });
  195. },
  196. swimlanes() {
  197. return Swimlanes.find({ boardId: this._id, archived: false }, { sort: { sort: 1 } });
  198. },
  199. hasOvertimeCards(){
  200. const card = Cards.findOne({isOvertime: true, boardId: this._id, archived: false} );
  201. return card !== undefined;
  202. },
  203. hasSpentTimeCards(){
  204. const card = Cards.findOne({spentTime: { $gt: 0 }, boardId: this._id, archived: false} );
  205. return card !== undefined;
  206. },
  207. activities() {
  208. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 } });
  209. },
  210. activeMembers() {
  211. return _.where(this.members, { isActive: true });
  212. },
  213. activeAdmins() {
  214. return _.where(this.members, { isActive: true, isAdmin: true });
  215. },
  216. memberUsers() {
  217. return Users.find({ _id: { $in: _.pluck(this.members, 'userId') } });
  218. },
  219. getLabel(name, color) {
  220. return _.findWhere(this.labels, { name, color });
  221. },
  222. labelIndex(labelId) {
  223. return _.pluck(this.labels, '_id').indexOf(labelId);
  224. },
  225. memberIndex(memberId) {
  226. return _.pluck(this.members, 'userId').indexOf(memberId);
  227. },
  228. hasMember(memberId) {
  229. return !!_.findWhere(this.members, { userId: memberId, isActive: true });
  230. },
  231. hasAdmin(memberId) {
  232. return !!_.findWhere(this.members, { userId: memberId, isActive: true, isAdmin: true });
  233. },
  234. hasCommentOnly(memberId) {
  235. return !!_.findWhere(this.members, { userId: memberId, isActive: true, isAdmin: false, isCommentOnly: true });
  236. },
  237. absoluteUrl() {
  238. return FlowRouter.url('board', { id: this._id, slug: this.slug });
  239. },
  240. colorClass() {
  241. return `board-color-${this.color}`;
  242. },
  243. customFields() {
  244. return CustomFields.find({ boardId: this._id }, { sort: { name: 1 } });
  245. },
  246. // XXX currently mutations return no value so we have an issue when using addLabel in import
  247. // XXX waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  248. pushLabel(name, color) {
  249. const _id = Random.id(6);
  250. Boards.direct.update(this._id, { $push: { labels: { _id, name, color } } });
  251. return _id;
  252. },
  253. searchCards(term) {
  254. check(term, Match.OneOf(String, null, undefined));
  255. let query = { boardId: this._id };
  256. const projection = { limit: 10, sort: { createdAt: -1 } };
  257. if (term) {
  258. const regex = new RegExp(term, 'i');
  259. query = {
  260. boardId: this._id,
  261. $or: [
  262. { title: regex },
  263. { description: regex },
  264. ],
  265. };
  266. }
  267. return Cards.find(query, projection);
  268. },
  269. // A board alwasy has another board where it deposits subtasks of thasks
  270. // that belong to itself.
  271. getDefaultSubtasksBoardId() {
  272. if ((this.subtasksDefaultBoardId === null) || (this.subtasksDefaultBoardId === undefined)) {
  273. this.subtasksDefaultBoardId = Boards.insert({
  274. title: `^${this.title}^`,
  275. permission: this.permission,
  276. members: this.members,
  277. color: this.color,
  278. description: TAPi18n.__('default-subtasks-board', {board: this.title}),
  279. });
  280. Swimlanes.insert({
  281. title: TAPi18n.__('default'),
  282. boardId: this.subtasksDefaultBoardId,
  283. });
  284. Boards.update(this._id, {$set: {
  285. subtasksDefaultBoardId: this.subtasksDefaultBoardId,
  286. }});
  287. }
  288. return this.subtasksDefaultBoardId;
  289. },
  290. getDefaultSubtasksBoard() {
  291. return Boards.findOne(this.getDefaultSubtasksBoardId());
  292. },
  293. getDefaultSubtasksListId() {
  294. if ((this.subtasksDefaultListId === null) || (this.subtasksDefaultListId === undefined)) {
  295. this.subtasksDefaultListId = Lists.insert({
  296. title: TAPi18n.__('queue'),
  297. boardId: this._id,
  298. });
  299. Boards.update(this._id, {$set: {
  300. subtasksDefaultListId: this.subtasksDefaultListId,
  301. }});
  302. }
  303. return this.subtasksDefaultListId;
  304. },
  305. getDefaultSubtasksList() {
  306. return Lists.findOne(this.getDefaultSubtasksListId());
  307. },
  308. getDefaultSwimline() {
  309. let result = Swimlanes.findOne({boardId: this._id});
  310. if (result === undefined) {
  311. Swimlanes.insert({
  312. title: TAPi18n.__('default'),
  313. boardId: this._id,
  314. });
  315. result = Swimlanes.findOne({boardId: this._id});
  316. }
  317. return result;
  318. },
  319. });
  320. Boards.mutations({
  321. archive() {
  322. return { $set: { archived: true } };
  323. },
  324. restore() {
  325. return { $set: { archived: false } };
  326. },
  327. rename(title) {
  328. return { $set: { title } };
  329. },
  330. setDescription(description) {
  331. return { $set: { description } };
  332. },
  333. setColor(color) {
  334. return { $set: { color } };
  335. },
  336. setVisibility(visibility) {
  337. return { $set: { permission: visibility } };
  338. },
  339. addLabel(name, color) {
  340. // If label with the same name and color already exists we don't want to
  341. // create another one because they would be indistinguishable in the UI
  342. // (they would still have different `_id` but that is not exposed to the
  343. // user).
  344. if (!this.getLabel(name, color)) {
  345. const _id = Random.id(6);
  346. return { $push: { labels: { _id, name, color } } };
  347. }
  348. return {};
  349. },
  350. editLabel(labelId, name, color) {
  351. if (!this.getLabel(name, color)) {
  352. const labelIndex = this.labelIndex(labelId);
  353. return {
  354. $set: {
  355. [`labels.${labelIndex}.name`]: name,
  356. [`labels.${labelIndex}.color`]: color,
  357. },
  358. };
  359. }
  360. return {};
  361. },
  362. removeLabel(labelId) {
  363. return { $pull: { labels: { _id: labelId } } };
  364. },
  365. changeOwnership(fromId, toId) {
  366. const memberIndex = this.memberIndex(fromId);
  367. return {
  368. $set: {
  369. [`members.${memberIndex}.userId`]: toId,
  370. },
  371. };
  372. },
  373. addMember(memberId) {
  374. const memberIndex = this.memberIndex(memberId);
  375. if (memberIndex >= 0) {
  376. return {
  377. $set: {
  378. [`members.${memberIndex}.isActive`]: true,
  379. },
  380. };
  381. }
  382. return {
  383. $push: {
  384. members: {
  385. userId: memberId,
  386. isAdmin: false,
  387. isActive: true,
  388. isCommentOnly: false,
  389. },
  390. },
  391. };
  392. },
  393. removeMember(memberId) {
  394. const memberIndex = this.memberIndex(memberId);
  395. // we do not allow the only one admin to be removed
  396. const allowRemove = (!this.members[memberIndex].isAdmin) || (this.activeAdmins().length > 1);
  397. if (!allowRemove) {
  398. return {
  399. $set: {
  400. [`members.${memberIndex}.isActive`]: true,
  401. },
  402. };
  403. }
  404. return {
  405. $set: {
  406. [`members.${memberIndex}.isActive`]: false,
  407. [`members.${memberIndex}.isAdmin`]: false,
  408. },
  409. };
  410. },
  411. setMemberPermission(memberId, isAdmin, isCommentOnly) {
  412. const memberIndex = this.memberIndex(memberId);
  413. // do not allow change permission of self
  414. if (memberId === Meteor.userId()) {
  415. isAdmin = this.members[memberIndex].isAdmin;
  416. }
  417. return {
  418. $set: {
  419. [`members.${memberIndex}.isAdmin`]: isAdmin,
  420. [`members.${memberIndex}.isCommentOnly`]: isCommentOnly,
  421. },
  422. };
  423. },
  424. });
  425. if (Meteor.isServer) {
  426. Boards.allow({
  427. insert: Meteor.userId,
  428. update: allowIsBoardAdmin,
  429. remove: allowIsBoardAdmin,
  430. fetch: ['members'],
  431. });
  432. // The number of users that have starred this board is managed by trusted code
  433. // and the user is not allowed to update it
  434. Boards.deny({
  435. update(userId, board, fieldNames) {
  436. return _.contains(fieldNames, 'stars');
  437. },
  438. fetch: [],
  439. });
  440. // We can't remove a member if it is the last administrator
  441. Boards.deny({
  442. update(userId, doc, fieldNames, modifier) {
  443. if (!_.contains(fieldNames, 'members'))
  444. return false;
  445. // We only care in case of a $pull operation, ie remove a member
  446. if (!_.isObject(modifier.$pull && modifier.$pull.members))
  447. return false;
  448. // If there is more than one admin, it's ok to remove anyone
  449. const nbAdmins = _.where(doc.members, { isActive: true, isAdmin: true }).length;
  450. if (nbAdmins > 1)
  451. return false;
  452. // If all the previous conditions were verified, we can't remove
  453. // a user if it's an admin
  454. const removedMemberId = modifier.$pull.members.userId;
  455. return Boolean(_.findWhere(doc.members, {
  456. userId: removedMemberId,
  457. isAdmin: true,
  458. }));
  459. },
  460. fetch: ['members'],
  461. });
  462. Meteor.methods({
  463. quitBoard(boardId) {
  464. check(boardId, String);
  465. const board = Boards.findOne(boardId);
  466. if (board) {
  467. const userId = Meteor.userId();
  468. const index = board.memberIndex(userId);
  469. if (index >= 0) {
  470. board.removeMember(userId);
  471. return true;
  472. } else throw new Meteor.Error('error-board-notAMember');
  473. } else throw new Meteor.Error('error-board-doesNotExist');
  474. },
  475. });
  476. }
  477. if (Meteor.isServer) {
  478. // Let MongoDB ensure that a member is not included twice in the same board
  479. Meteor.startup(() => {
  480. Boards._collection._ensureIndex({
  481. _id: 1,
  482. 'members.userId': 1,
  483. }, { unique: true });
  484. Boards._collection._ensureIndex({ 'members.userId': 1 });
  485. });
  486. // Genesis: the first activity of the newly created board
  487. Boards.after.insert((userId, doc) => {
  488. Activities.insert({
  489. userId,
  490. type: 'board',
  491. activityTypeId: doc._id,
  492. activityType: 'createBoard',
  493. boardId: doc._id,
  494. });
  495. });
  496. // If the user remove one label from a board, we cant to remove reference of
  497. // this label in any card of this board.
  498. Boards.after.update((userId, doc, fieldNames, modifier) => {
  499. if (!_.contains(fieldNames, 'labels') ||
  500. !modifier.$pull ||
  501. !modifier.$pull.labels ||
  502. !modifier.$pull.labels._id) {
  503. return;
  504. }
  505. const removedLabelId = modifier.$pull.labels._id;
  506. Cards.update(
  507. { boardId: doc._id },
  508. {
  509. $pull: {
  510. labelIds: removedLabelId,
  511. },
  512. },
  513. { multi: true }
  514. );
  515. });
  516. const foreachRemovedMember = (doc, modifier, callback) => {
  517. Object.keys(modifier).forEach((set) => {
  518. if (modifier[set] !== false) {
  519. return;
  520. }
  521. const parts = set.split('.');
  522. if (parts.length === 3 && parts[0] === 'members' && parts[2] === 'isActive') {
  523. callback(doc.members[parts[1]].userId);
  524. }
  525. });
  526. };
  527. // Remove a member from all objects of the board before leaving the board
  528. Boards.before.update((userId, doc, fieldNames, modifier) => {
  529. if (!_.contains(fieldNames, 'members')) {
  530. return;
  531. }
  532. if (modifier.$set) {
  533. const boardId = doc._id;
  534. foreachRemovedMember(doc, modifier.$set, (memberId) => {
  535. Cards.update(
  536. { boardId },
  537. {
  538. $pull: {
  539. members: memberId,
  540. watchers: memberId,
  541. },
  542. },
  543. { multi: true }
  544. );
  545. Lists.update(
  546. { boardId },
  547. {
  548. $pull: {
  549. watchers: memberId,
  550. },
  551. },
  552. { multi: true }
  553. );
  554. const board = Boards._transform(doc);
  555. board.setWatcher(memberId, false);
  556. // Remove board from users starred list
  557. if (!board.isPublic()) {
  558. Users.update(
  559. memberId,
  560. {
  561. $pull: {
  562. 'profile.starredBoards': boardId,
  563. },
  564. }
  565. );
  566. }
  567. });
  568. }
  569. });
  570. // Add a new activity if we add or remove a member to the board
  571. Boards.after.update((userId, doc, fieldNames, modifier) => {
  572. if (!_.contains(fieldNames, 'members')) {
  573. return;
  574. }
  575. // Say hello to the new member
  576. if (modifier.$push && modifier.$push.members) {
  577. const memberId = modifier.$push.members.userId;
  578. Activities.insert({
  579. userId,
  580. memberId,
  581. type: 'member',
  582. activityType: 'addBoardMember',
  583. boardId: doc._id,
  584. });
  585. }
  586. // Say goodbye to the former member
  587. if (modifier.$set) {
  588. foreachRemovedMember(doc, modifier.$set, (memberId) => {
  589. Activities.insert({
  590. userId,
  591. memberId,
  592. type: 'member',
  593. activityType: 'removeBoardMember',
  594. boardId: doc._id,
  595. });
  596. });
  597. }
  598. });
  599. }
  600. //BOARDS REST API
  601. if (Meteor.isServer) {
  602. JsonRoutes.add('GET', '/api/users/:userId/boards', function (req, res) {
  603. try {
  604. Authentication.checkLoggedIn(req.userId);
  605. const paramUserId = req.params.userId;
  606. // A normal user should be able to see their own boards,
  607. // admins can access boards of any user
  608. Authentication.checkAdminOrCondition(req.userId, req.userId === paramUserId);
  609. const data = Boards.find({
  610. archived: false,
  611. 'members.userId': paramUserId,
  612. }, {
  613. sort: ['title'],
  614. }).map(function(board) {
  615. return {
  616. _id: board._id,
  617. title: board.title,
  618. };
  619. });
  620. JsonRoutes.sendResult(res, {code: 200, data});
  621. }
  622. catch (error) {
  623. JsonRoutes.sendResult(res, {
  624. code: 200,
  625. data: error,
  626. });
  627. }
  628. });
  629. JsonRoutes.add('GET', '/api/boards', function (req, res) {
  630. try {
  631. Authentication.checkUserId(req.userId);
  632. JsonRoutes.sendResult(res, {
  633. code: 200,
  634. data: Boards.find({ permission: 'public' }).map(function (doc) {
  635. return {
  636. _id: doc._id,
  637. title: doc.title,
  638. };
  639. }),
  640. });
  641. }
  642. catch (error) {
  643. JsonRoutes.sendResult(res, {
  644. code: 200,
  645. data: error,
  646. });
  647. }
  648. });
  649. JsonRoutes.add('GET', '/api/boards/:id', function (req, res) {
  650. try {
  651. const id = req.params.id;
  652. Authentication.checkBoardAccess(req.userId, id);
  653. JsonRoutes.sendResult(res, {
  654. code: 200,
  655. data: Boards.findOne({ _id: id }),
  656. });
  657. }
  658. catch (error) {
  659. JsonRoutes.sendResult(res, {
  660. code: 200,
  661. data: error,
  662. });
  663. }
  664. });
  665. JsonRoutes.add('POST', '/api/boards', function (req, res) {
  666. try {
  667. Authentication.checkUserId(req.userId);
  668. const id = Boards.insert({
  669. title: req.body.title,
  670. members: [
  671. {
  672. userId: req.body.owner,
  673. isAdmin: true,
  674. isActive: true,
  675. isCommentOnly: false,
  676. },
  677. ],
  678. permission: 'public',
  679. color: 'belize',
  680. });
  681. JsonRoutes.sendResult(res, {
  682. code: 200,
  683. data: {
  684. _id: id,
  685. },
  686. });
  687. }
  688. catch (error) {
  689. JsonRoutes.sendResult(res, {
  690. code: 200,
  691. data: error,
  692. });
  693. }
  694. });
  695. JsonRoutes.add('DELETE', '/api/boards/:id', function (req, res) {
  696. try {
  697. Authentication.checkUserId(req.userId);
  698. const id = req.params.id;
  699. Boards.remove({ _id: id });
  700. JsonRoutes.sendResult(res, {
  701. code: 200,
  702. data:{
  703. _id: id,
  704. },
  705. });
  706. }
  707. catch (error) {
  708. JsonRoutes.sendResult(res, {
  709. code: 200,
  710. data: error,
  711. });
  712. }
  713. });
  714. JsonRoutes.add('PUT', '/api/boards/:id/labels', function (req, res) {
  715. Authentication.checkUserId(req.userId);
  716. const id = req.params.id;
  717. try {
  718. if (req.body.hasOwnProperty('label')) {
  719. const board = Boards.findOne({ _id: id });
  720. const color = req.body.label.color;
  721. const name = req.body.label.name;
  722. const labelId = Random.id(6);
  723. if (!board.getLabel(name, color)) {
  724. Boards.direct.update({ _id: id }, { $push: { labels: { _id: labelId, name, color } } });
  725. JsonRoutes.sendResult(res, {
  726. code: 200,
  727. data: labelId,
  728. });
  729. } else {
  730. JsonRoutes.sendResult(res, {
  731. code: 200,
  732. });
  733. }
  734. }
  735. }
  736. catch (error) {
  737. JsonRoutes.sendResult(res, {
  738. data: error,
  739. });
  740. }
  741. });
  742. }