boards.js 22 KB

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