boards.js 24 KB

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