boards.js 22 KB

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