boards.js 24 KB

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