boards.js 22 KB

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