boards.js 22 KB

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