boards.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. Boards = new Mongo.Collection('boards');
  2. /**
  3. * This is a Board.
  4. */
  5. Boards.attachSchema(
  6. new SimpleSchema({
  7. title: {
  8. /**
  9. * The title of the board
  10. */
  11. type: String,
  12. },
  13. slug: {
  14. /**
  15. * The title slugified.
  16. */
  17. type: String,
  18. // eslint-disable-next-line consistent-return
  19. autoValue() {
  20. // XXX We need to improve slug management. Only the id should be necessary
  21. // to identify a board in the code.
  22. // XXX If the board title is updated, the slug should also be updated.
  23. // In some cases (Chinese and Japanese for instance) the `getSlug` function
  24. // return an empty string. This is causes bugs in our application so we set
  25. // a default slug in this case.
  26. if (this.isInsert && !this.isSet) {
  27. let slug = 'board';
  28. const title = this.field('title');
  29. if (title.isSet) {
  30. slug = getSlug(title.value) || slug;
  31. }
  32. return slug;
  33. }
  34. },
  35. },
  36. archived: {
  37. /**
  38. * Is the board archived?
  39. */
  40. type: Boolean,
  41. // eslint-disable-next-line consistent-return
  42. autoValue() {
  43. if (this.isInsert && !this.isSet) {
  44. return false;
  45. }
  46. },
  47. },
  48. createdAt: {
  49. /**
  50. * Creation time of the board
  51. */
  52. type: Date,
  53. // eslint-disable-next-line consistent-return
  54. autoValue() {
  55. if (this.isInsert) {
  56. return new Date();
  57. } else if (this.isUpsert) {
  58. return { $setOnInsert: new Date() };
  59. } else {
  60. this.unset();
  61. }
  62. },
  63. },
  64. // XXX Inconsistent field naming
  65. modifiedAt: {
  66. /**
  67. * Last modification time of the board
  68. */
  69. type: Date,
  70. optional: true,
  71. // eslint-disable-next-line consistent-return
  72. autoValue() {
  73. if (this.isInsert || this.isUpsert || this.isUpdate) {
  74. return new Date();
  75. } else {
  76. this.unset();
  77. }
  78. },
  79. },
  80. // De-normalized number of users that have starred this board
  81. stars: {
  82. /**
  83. * How many stars the board has
  84. */
  85. type: Number,
  86. // eslint-disable-next-line consistent-return
  87. autoValue() {
  88. if (this.isInsert) {
  89. return 0;
  90. }
  91. },
  92. },
  93. // De-normalized label system
  94. labels: {
  95. /**
  96. * List of labels attached to a board
  97. */
  98. type: [Object],
  99. // eslint-disable-next-line consistent-return
  100. autoValue() {
  101. if (this.isInsert && !this.isSet) {
  102. const colors = Boards.simpleSchema()._schema['labels.$.color']
  103. .allowedValues;
  104. const defaultLabelsColors = _.clone(colors).splice(0, 6);
  105. return defaultLabelsColors.map(color => ({
  106. color,
  107. _id: Random.id(6),
  108. name: '',
  109. }));
  110. }
  111. },
  112. },
  113. 'labels.$._id': {
  114. /**
  115. * Unique id of a label
  116. */
  117. // We don't specify that this field must be unique in the board because that
  118. // will cause performance penalties and is not necessary since this field is
  119. // always set on the server.
  120. // XXX Actually if we create a new label, the `_id` is set on the client
  121. // without being overwritten by the server, could it be a problem?
  122. type: String,
  123. },
  124. 'labels.$.name': {
  125. /**
  126. * Name of a label
  127. */
  128. type: String,
  129. optional: true,
  130. },
  131. 'labels.$.color': {
  132. /**
  133. * color of a label.
  134. *
  135. * Can be amongst `green`, `yellow`, `orange`, `red`, `purple`,
  136. * `blue`, `sky`, `lime`, `pink`, `black`,
  137. * `silver`, `peachpuff`, `crimson`, `plum`, `darkgreen`,
  138. * `slateblue`, `magenta`, `gold`, `navy`, `gray`,
  139. * `saddlebrown`, `paleturquoise`, `mistyrose`, `indigo`
  140. */
  141. type: String,
  142. allowedValues: [
  143. 'green',
  144. 'yellow',
  145. 'orange',
  146. 'red',
  147. 'purple',
  148. 'blue',
  149. 'sky',
  150. 'lime',
  151. 'pink',
  152. 'black',
  153. 'silver',
  154. 'peachpuff',
  155. 'crimson',
  156. 'plum',
  157. 'darkgreen',
  158. 'slateblue',
  159. 'magenta',
  160. 'gold',
  161. 'navy',
  162. 'gray',
  163. 'saddlebrown',
  164. 'paleturquoise',
  165. 'mistyrose',
  166. 'indigo',
  167. ],
  168. },
  169. // XXX We might want to maintain more informations under the member sub-
  170. // documents like de-normalized meta-data (the date the member joined the
  171. // board, the number of contributions, etc.).
  172. members: {
  173. /**
  174. * List of members of a board
  175. */
  176. type: [Object],
  177. // eslint-disable-next-line consistent-return
  178. autoValue() {
  179. if (this.isInsert && !this.isSet) {
  180. return [
  181. {
  182. userId: this.userId,
  183. isAdmin: true,
  184. isActive: true,
  185. isNoComments: false,
  186. isCommentOnly: false,
  187. isWorker: false,
  188. },
  189. ];
  190. }
  191. },
  192. },
  193. 'members.$.userId': {
  194. /**
  195. * The uniq ID of the member
  196. */
  197. type: String,
  198. },
  199. 'members.$.isAdmin': {
  200. /**
  201. * Is the member an admin of the board?
  202. */
  203. type: Boolean,
  204. },
  205. 'members.$.isActive': {
  206. /**
  207. * Is the member active?
  208. */
  209. type: Boolean,
  210. },
  211. 'members.$.isNoComments': {
  212. /**
  213. * Is the member not allowed to make comments
  214. */
  215. type: Boolean,
  216. optional: true,
  217. },
  218. 'members.$.isCommentOnly': {
  219. /**
  220. * Is the member only allowed to comment on the board
  221. */
  222. type: Boolean,
  223. optional: true,
  224. },
  225. 'members.$.isWorker': {
  226. /**
  227. * Is the member only allowed to move card, assign himself to card and comment
  228. */
  229. type: Boolean,
  230. optional: true,
  231. },
  232. permission: {
  233. /**
  234. * visibility of the board
  235. */
  236. type: String,
  237. allowedValues: ['public', 'private'],
  238. },
  239. color: {
  240. /**
  241. * The color of the board.
  242. */
  243. type: String,
  244. allowedValues: [
  245. 'belize',
  246. 'nephritis',
  247. 'pomegranate',
  248. 'pumpkin',
  249. 'wisteria',
  250. 'moderatepink',
  251. 'strongcyan',
  252. 'limegreen',
  253. 'midnight',
  254. 'dark',
  255. 'relax',
  256. 'corteza',
  257. ],
  258. // eslint-disable-next-line consistent-return
  259. autoValue() {
  260. if (this.isInsert && !this.isSet) {
  261. return Boards.simpleSchema()._schema.color.allowedValues[0];
  262. }
  263. },
  264. },
  265. description: {
  266. /**
  267. * The description of the board
  268. */
  269. type: String,
  270. optional: true,
  271. },
  272. subtasksDefaultBoardId: {
  273. /**
  274. * The default board ID assigned to subtasks.
  275. */
  276. type: String,
  277. optional: true,
  278. defaultValue: null,
  279. },
  280. subtasksDefaultListId: {
  281. /**
  282. * The default List ID assigned to subtasks.
  283. */
  284. type: String,
  285. optional: true,
  286. defaultValue: null,
  287. },
  288. dateSettingsDefaultBoardId: {
  289. type: String,
  290. optional: true,
  291. defaultValue: null,
  292. },
  293. dateSettingsDefaultListId: {
  294. type: String,
  295. optional: true,
  296. defaultValue: null,
  297. },
  298. allowsSubtasks: {
  299. /**
  300. * Does the board allows subtasks?
  301. */
  302. type: Boolean,
  303. defaultValue: true,
  304. },
  305. allowsAttachments: {
  306. /**
  307. * Does the board allows attachments?
  308. */
  309. type: Boolean,
  310. defaultValue: true,
  311. },
  312. allowsChecklists: {
  313. /**
  314. * Does the board allows checklists?
  315. */
  316. type: Boolean,
  317. defaultValue: true,
  318. },
  319. allowsComments: {
  320. /**
  321. * Does the board allows comments?
  322. */
  323. type: Boolean,
  324. defaultValue: true,
  325. },
  326. allowsLabels: {
  327. /**
  328. * Does the board allows labels?
  329. */
  330. type: Boolean,
  331. defaultValue: true,
  332. },
  333. allowsAssignee: {
  334. /**
  335. * Does the board allows assignee?
  336. */
  337. type: Boolean,
  338. defaultValue: true,
  339. },
  340. allowsMembers: {
  341. /**
  342. * Does the board allows members?
  343. */
  344. type: Boolean,
  345. defaultValue: true,
  346. },
  347. allowsRequestedBy: {
  348. /**
  349. * Does the board allows requested by?
  350. */
  351. type: Boolean,
  352. defaultValue: true,
  353. },
  354. allowsAssignedBy: {
  355. /**
  356. * Does the board allows requested by?
  357. */
  358. type: Boolean,
  359. defaultValue: true,
  360. },
  361. allowsReceivedDate: {
  362. /**
  363. * Does the board allows received date?
  364. */
  365. type: Boolean,
  366. defaultValue: true,
  367. },
  368. allowsStartDate: {
  369. /**
  370. * Does the board allows start date?
  371. */
  372. type: Boolean,
  373. defaultValue: true,
  374. },
  375. allowsEndDate: {
  376. /**
  377. * Does the board allows end date?
  378. */
  379. type: Boolean,
  380. defaultValue: true,
  381. },
  382. allowsDueDate: {
  383. /**
  384. * Does the board allows due date?
  385. */
  386. type: Boolean,
  387. defaultValue: true,
  388. },
  389. presentParentTask: {
  390. /**
  391. * Controls how to present the parent task:
  392. *
  393. * - `prefix-with-full-path`: add a prefix with the full path
  394. * - `prefix-with-parent`: add a prefisx with the parent name
  395. * - `subtext-with-full-path`: add a subtext with the full path
  396. * - `subtext-with-parent`: add a subtext with the parent name
  397. * - `no-parent`: does not show the parent at all
  398. */
  399. type: String,
  400. allowedValues: [
  401. 'prefix-with-full-path',
  402. 'prefix-with-parent',
  403. 'subtext-with-full-path',
  404. 'subtext-with-parent',
  405. 'no-parent',
  406. ],
  407. optional: true,
  408. defaultValue: 'no-parent',
  409. },
  410. startAt: {
  411. /**
  412. * Starting date of the board.
  413. */
  414. type: Date,
  415. optional: true,
  416. },
  417. dueAt: {
  418. /**
  419. * Due date of the board.
  420. */
  421. type: Date,
  422. optional: true,
  423. },
  424. endAt: {
  425. /**
  426. * End date of the board.
  427. */
  428. type: Date,
  429. optional: true,
  430. },
  431. spentTime: {
  432. /**
  433. * Time spent in the board.
  434. */
  435. type: Number,
  436. decimal: true,
  437. optional: true,
  438. },
  439. isOvertime: {
  440. /**
  441. * Is the board overtimed?
  442. */
  443. type: Boolean,
  444. defaultValue: false,
  445. optional: true,
  446. },
  447. type: {
  448. /**
  449. * The type of board
  450. */
  451. type: String,
  452. defaultValue: 'board',
  453. },
  454. }),
  455. );
  456. Boards.helpers({
  457. copy() {
  458. const oldId = this._id;
  459. delete this._id;
  460. const _id = Boards.insert(this);
  461. // Copy all swimlanes in board
  462. Swimlanes.find({
  463. boardId: oldId,
  464. archived: false,
  465. }).forEach(swimlane => {
  466. swimlane.type = 'swimlane';
  467. swimlane.copy(_id);
  468. });
  469. },
  470. /**
  471. * Is supplied user authorized to view this board?
  472. */
  473. isVisibleBy(user) {
  474. if (this.isPublic()) {
  475. // public boards are visible to everyone
  476. return true;
  477. } else {
  478. // otherwise you have to be logged-in and active member
  479. return user && this.isActiveMember(user._id);
  480. }
  481. },
  482. /**
  483. * Is the user one of the active members of the board?
  484. *
  485. * @param userId
  486. * @returns {boolean} the member that matches, or undefined/false
  487. */
  488. isActiveMember(userId) {
  489. if (userId) {
  490. return this.members.find(
  491. member => member.userId === userId && member.isActive,
  492. );
  493. } else {
  494. return false;
  495. }
  496. },
  497. isPublic() {
  498. return this.permission === 'public';
  499. },
  500. cards() {
  501. return Cards.find(
  502. { boardId: this._id, archived: false },
  503. { sort: { title: 1 } },
  504. );
  505. },
  506. lists() {
  507. //currentUser = Meteor.user();
  508. //if (currentUser) {
  509. // enabled = Meteor.user().hasSortBy();
  510. //}
  511. //return enabled ? this.newestLists() : this.draggableLists();
  512. return this.draggableLists();
  513. },
  514. newestLists() {
  515. // sorted lists from newest to the oldest, by its creation date or its cards' last modification date
  516. const value = Meteor.user()._getListSortBy();
  517. const sortKey = { starred: -1, [value[0]]: value[1] }; // [["starred",-1],value];
  518. return Lists.find(
  519. {
  520. boardId: this._id,
  521. archived: false,
  522. },
  523. { sort: sortKey },
  524. );
  525. },
  526. draggableLists() {
  527. return Lists.find({ boardId: this._id }, { sort: { sort: 1 } });
  528. },
  529. nullSortLists() {
  530. return Lists.find({
  531. boardId: this._id,
  532. archived: false,
  533. sort: { $eq: null },
  534. });
  535. },
  536. swimlanes() {
  537. return Swimlanes.find(
  538. { boardId: this._id, archived: false },
  539. { sort: { sort: 1 } },
  540. );
  541. },
  542. nextSwimlane(swimlane) {
  543. return Swimlanes.findOne(
  544. {
  545. boardId: this._id,
  546. archived: false,
  547. sort: { $gte: swimlane.sort },
  548. _id: { $ne: swimlane._id },
  549. },
  550. {
  551. sort: { sort: 1 },
  552. },
  553. );
  554. },
  555. nullSortSwimlanes() {
  556. return Swimlanes.find({
  557. boardId: this._id,
  558. archived: false,
  559. sort: { $eq: null },
  560. });
  561. },
  562. hasOvertimeCards() {
  563. const card = Cards.findOne({
  564. isOvertime: true,
  565. boardId: this._id,
  566. archived: false,
  567. });
  568. return card !== undefined;
  569. },
  570. hasSpentTimeCards() {
  571. const card = Cards.findOne({
  572. spentTime: { $gt: 0 },
  573. boardId: this._id,
  574. archived: false,
  575. });
  576. return card !== undefined;
  577. },
  578. activities() {
  579. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 } });
  580. },
  581. activeMembers() {
  582. return _.where(this.members, { isActive: true });
  583. },
  584. activeAdmins() {
  585. return _.where(this.members, { isActive: true, isAdmin: true });
  586. },
  587. memberUsers() {
  588. return Users.find({ _id: { $in: _.pluck(this.members, 'userId') } });
  589. },
  590. getLabel(name, color) {
  591. return _.findWhere(this.labels, { name, color });
  592. },
  593. getLabelById(labelId) {
  594. return _.findWhere(this.labels, { _id: labelId });
  595. },
  596. labelIndex(labelId) {
  597. return _.pluck(this.labels, '_id').indexOf(labelId);
  598. },
  599. memberIndex(memberId) {
  600. return _.pluck(this.members, 'userId').indexOf(memberId);
  601. },
  602. hasMember(memberId) {
  603. return !!_.findWhere(this.members, { userId: memberId, isActive: true });
  604. },
  605. hasAdmin(memberId) {
  606. return !!_.findWhere(this.members, {
  607. userId: memberId,
  608. isActive: true,
  609. isAdmin: true,
  610. });
  611. },
  612. hasNoComments(memberId) {
  613. return !!_.findWhere(this.members, {
  614. userId: memberId,
  615. isActive: true,
  616. isAdmin: false,
  617. isNoComments: true,
  618. isWorker: false,
  619. });
  620. },
  621. hasCommentOnly(memberId) {
  622. return !!_.findWhere(this.members, {
  623. userId: memberId,
  624. isActive: true,
  625. isAdmin: false,
  626. isCommentOnly: true,
  627. isWorker: false,
  628. });
  629. },
  630. hasWorker(memberId) {
  631. return !!_.findWhere(this.members, {
  632. userId: memberId,
  633. isActive: true,
  634. isAdmin: false,
  635. isCommentOnly: false,
  636. isWorker: true,
  637. });
  638. },
  639. absoluteUrl() {
  640. return FlowRouter.url('board', { id: this._id, slug: this.slug });
  641. },
  642. colorClass() {
  643. return `board-color-${this.color}`;
  644. },
  645. customFields() {
  646. return CustomFields.find(
  647. { boardIds: { $in: [this._id] } },
  648. { sort: { name: 1 } },
  649. );
  650. },
  651. // XXX currently mutations return no value so we have an issue when using addLabel in import
  652. // XXX waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  653. pushLabel(name, color) {
  654. const _id = Random.id(6);
  655. Boards.direct.update(this._id, { $push: { labels: { _id, name, color } } });
  656. return _id;
  657. },
  658. searchBoards(term) {
  659. check(term, Match.OneOf(String, null, undefined));
  660. const query = { boardId: this._id };
  661. query.type = 'cardType-linkedBoard';
  662. query.archived = false;
  663. const projection = { limit: 10, sort: { createdAt: -1 } };
  664. if (term) {
  665. const regex = new RegExp(term, 'i');
  666. query.$or = [{ title: regex }, { description: regex }];
  667. }
  668. return Cards.find(query, projection);
  669. },
  670. searchSwimlanes(term) {
  671. check(term, Match.OneOf(String, null, undefined));
  672. const query = { boardId: this._id };
  673. if (this.isTemplatesBoard()) {
  674. query.type = 'template-swimlane';
  675. query.archived = false;
  676. } else {
  677. query.type = { $nin: ['template-swimlane'] };
  678. }
  679. const projection = { limit: 10, sort: { createdAt: -1 } };
  680. if (term) {
  681. const regex = new RegExp(term, 'i');
  682. query.$or = [{ title: regex }, { description: regex }];
  683. }
  684. return Swimlanes.find(query, projection);
  685. },
  686. searchLists(term) {
  687. check(term, Match.OneOf(String, null, undefined));
  688. const query = { boardId: this._id };
  689. if (this.isTemplatesBoard()) {
  690. query.type = 'template-list';
  691. query.archived = false;
  692. } else {
  693. query.type = { $nin: ['template-list'] };
  694. }
  695. const projection = { limit: 10, sort: { createdAt: -1 } };
  696. if (term) {
  697. const regex = new RegExp(term, 'i');
  698. query.$or = [{ title: regex }, { description: regex }];
  699. }
  700. return Lists.find(query, projection);
  701. },
  702. searchCards(term, excludeLinked) {
  703. check(term, Match.OneOf(String, null, undefined));
  704. const query = { boardId: this._id };
  705. if (excludeLinked) {
  706. query.linkedId = null;
  707. }
  708. if (this.isTemplatesBoard()) {
  709. query.type = 'template-card';
  710. query.archived = false;
  711. } else {
  712. query.type = { $nin: ['template-card'] };
  713. }
  714. const projection = { limit: 10, sort: { createdAt: -1 } };
  715. if (term) {
  716. const regex = new RegExp(term, 'i');
  717. query.$or = [{ title: regex }, { description: regex }];
  718. }
  719. return Cards.find(query, projection);
  720. },
  721. // A board alwasy has another board where it deposits subtasks of thasks
  722. // that belong to itself.
  723. getDefaultSubtasksBoardId() {
  724. if (
  725. this.subtasksDefaultBoardId === null ||
  726. this.subtasksDefaultBoardId === undefined
  727. ) {
  728. this.subtasksDefaultBoardId = Boards.insert({
  729. title: `^${this.title}^`,
  730. permission: this.permission,
  731. members: this.members,
  732. color: this.color,
  733. description: TAPi18n.__('default-subtasks-board', {
  734. board: this.title,
  735. }),
  736. });
  737. Swimlanes.insert({
  738. title: TAPi18n.__('default'),
  739. boardId: this.subtasksDefaultBoardId,
  740. });
  741. Boards.update(this._id, {
  742. $set: {
  743. subtasksDefaultBoardId: this.subtasksDefaultBoardId,
  744. },
  745. });
  746. }
  747. return this.subtasksDefaultBoardId;
  748. },
  749. getDefaultSubtasksBoard() {
  750. return Boards.findOne(this.getDefaultSubtasksBoardId());
  751. },
  752. //Date Settings option such as received date, start date and so on.
  753. getDefaultDateSettingsBoardId() {
  754. if (
  755. this.dateSettingsDefaultBoardId === null ||
  756. this.dateSettingsDefaultBoardId === undefined
  757. ) {
  758. this.dateSettingsDefaultBoardId = Boards.insert({
  759. title: `^${this.title}^`,
  760. permission: this.permission,
  761. members: this.members,
  762. color: this.color,
  763. description: TAPi18n.__('default-dates-board', {
  764. board: this.title,
  765. }),
  766. });
  767. Swimlanes.insert({
  768. title: TAPi18n.__('default'),
  769. boardId: this.dateSettingsDefaultBoardId,
  770. });
  771. Boards.update(this._id, {
  772. $set: {
  773. dateSettingsDefaultBoardId: this.dateSettingsDefaultBoardId,
  774. },
  775. });
  776. }
  777. return this.dateSettingsDefaultBoardId;
  778. },
  779. getDefaultDateSettingsBoard() {
  780. return Boards.findOne(this.getDefaultDateSettingsBoardId());
  781. },
  782. getDefaultSubtasksListId() {
  783. if (
  784. this.subtasksDefaultListId === null ||
  785. this.subtasksDefaultListId === undefined
  786. ) {
  787. this.subtasksDefaultListId = Lists.insert({
  788. title: TAPi18n.__('queue'),
  789. boardId: this._id,
  790. });
  791. this.setSubtasksDefaultListId(this.subtasksDefaultListId);
  792. }
  793. return this.subtasksDefaultListId;
  794. },
  795. getDefaultSubtasksList() {
  796. return Lists.findOne(this.getDefaultSubtasksListId());
  797. },
  798. getDefaultDateSettingsListId() {
  799. if (
  800. this.dateSettingsDefaultListId === null ||
  801. this.dateSettingsDefaultListId === undefined
  802. ) {
  803. this.dateSettingsDefaultListId = Lists.insert({
  804. title: TAPi18n.__('queue'),
  805. boardId: this._id,
  806. });
  807. this.setDateSettingsDefaultListId(this.dateSettingsDefaultListId);
  808. }
  809. return this.dateSettingsDefaultListId;
  810. },
  811. getDefaultDateSettingsList() {
  812. return Lists.findOne(this.getDefaultDateSettingsListId());
  813. },
  814. getDefaultSwimline() {
  815. let result = Swimlanes.findOne({ boardId: this._id });
  816. if (result === undefined) {
  817. Swimlanes.insert({
  818. title: TAPi18n.__('default'),
  819. boardId: this._id,
  820. });
  821. result = Swimlanes.findOne({ boardId: this._id });
  822. }
  823. return result;
  824. },
  825. cardsDueInBetween(start, end) {
  826. return Cards.find({
  827. boardId: this._id,
  828. dueAt: { $gte: start, $lte: end },
  829. });
  830. },
  831. cardsInInterval(start, end) {
  832. return Cards.find({
  833. boardId: this._id,
  834. $or: [
  835. {
  836. startAt: {
  837. $lte: start,
  838. },
  839. endAt: {
  840. $gte: start,
  841. },
  842. },
  843. {
  844. startAt: {
  845. $lte: end,
  846. },
  847. endAt: {
  848. $gte: end,
  849. },
  850. },
  851. {
  852. startAt: {
  853. $gte: start,
  854. },
  855. endAt: {
  856. $lte: end,
  857. },
  858. },
  859. ],
  860. });
  861. },
  862. isTemplateBoard() {
  863. return this.type === 'template-board';
  864. },
  865. isTemplatesBoard() {
  866. return this.type === 'template-container';
  867. },
  868. });
  869. Boards.mutations({
  870. archive() {
  871. return { $set: { archived: true } };
  872. },
  873. restore() {
  874. return { $set: { archived: false } };
  875. },
  876. rename(title) {
  877. return { $set: { title } };
  878. },
  879. setDescription(description) {
  880. return { $set: { description } };
  881. },
  882. setColor(color) {
  883. return { $set: { color } };
  884. },
  885. setVisibility(visibility) {
  886. return { $set: { permission: visibility } };
  887. },
  888. addLabel(name, color) {
  889. // If label with the same name and color already exists we don't want to
  890. // create another one because they would be indistinguishable in the UI
  891. // (they would still have different `_id` but that is not exposed to the
  892. // user).
  893. if (!this.getLabel(name, color)) {
  894. const _id = Random.id(6);
  895. return { $push: { labels: { _id, name, color } } };
  896. }
  897. return {};
  898. },
  899. editLabel(labelId, name, color) {
  900. if (!this.getLabel(name, color)) {
  901. const labelIndex = this.labelIndex(labelId);
  902. return {
  903. $set: {
  904. [`labels.${labelIndex}.name`]: name,
  905. [`labels.${labelIndex}.color`]: color,
  906. },
  907. };
  908. }
  909. return {};
  910. },
  911. removeLabel(labelId) {
  912. return { $pull: { labels: { _id: labelId } } };
  913. },
  914. changeOwnership(fromId, toId) {
  915. const memberIndex = this.memberIndex(fromId);
  916. return {
  917. $set: {
  918. [`members.${memberIndex}.userId`]: toId,
  919. },
  920. };
  921. },
  922. addMember(memberId) {
  923. const memberIndex = this.memberIndex(memberId);
  924. if (memberIndex >= 0) {
  925. return {
  926. $set: {
  927. [`members.${memberIndex}.isActive`]: true,
  928. },
  929. };
  930. }
  931. return {
  932. $push: {
  933. members: {
  934. userId: memberId,
  935. isAdmin: false,
  936. isActive: true,
  937. isNoComments: false,
  938. isCommentOnly: false,
  939. isWorker: false,
  940. },
  941. },
  942. };
  943. },
  944. removeMember(memberId) {
  945. const memberIndex = this.memberIndex(memberId);
  946. // we do not allow the only one admin to be removed
  947. const allowRemove =
  948. !this.members[memberIndex].isAdmin || this.activeAdmins().length > 1;
  949. if (!allowRemove) {
  950. return {
  951. $set: {
  952. [`members.${memberIndex}.isActive`]: true,
  953. },
  954. };
  955. }
  956. return {
  957. $set: {
  958. [`members.${memberIndex}.isActive`]: false,
  959. [`members.${memberIndex}.isAdmin`]: false,
  960. },
  961. };
  962. },
  963. setMemberPermission(
  964. memberId,
  965. isAdmin,
  966. isNoComments,
  967. isCommentOnly,
  968. isWorker,
  969. currentUserId = Meteor.userId(),
  970. ) {
  971. const memberIndex = this.memberIndex(memberId);
  972. // do not allow change permission of self
  973. if (memberId === currentUserId) {
  974. isAdmin = this.members[memberIndex].isAdmin;
  975. }
  976. return {
  977. $set: {
  978. [`members.${memberIndex}.isAdmin`]: isAdmin,
  979. [`members.${memberIndex}.isNoComments`]: isNoComments,
  980. [`members.${memberIndex}.isCommentOnly`]: isCommentOnly,
  981. [`members.${memberIndex}.isWorker`]: isWorker,
  982. },
  983. };
  984. },
  985. setAllowsSubtasks(allowsSubtasks) {
  986. return { $set: { allowsSubtasks } };
  987. },
  988. setAllowsMembers(allowsMembers) {
  989. return { $set: { allowsMembers } };
  990. },
  991. setAllowsChecklists(allowsChecklists) {
  992. return { $set: { allowsChecklists } };
  993. },
  994. setAllowsAssignee(allowsComments) {
  995. return { $set: { allowsComments } };
  996. },
  997. setAllowsAssignedBy(allowsAssignedBy) {
  998. return { $set: { allowsAssignedBy } };
  999. },
  1000. setAllowsRequestedBy(allowsRequestedBy) {
  1001. return { $set: { allowsRequestedBy } };
  1002. },
  1003. setAllowsAttachments(allowsAttachments) {
  1004. return { $set: { allowsAttachments } };
  1005. },
  1006. setAllowsLabels(allowsLabels) {
  1007. return { $set: { allowsLabels } };
  1008. },
  1009. setAllowsReceivedDate(allowsReceivedDate) {
  1010. return { $set: { allowsReceivedDate } };
  1011. },
  1012. setAllowsStartDate(allowsStartDate) {
  1013. return { $set: { allowsStartDate } };
  1014. },
  1015. setAllowsEndDate(allowsEndDate) {
  1016. return { $set: { allowsEndDate } };
  1017. },
  1018. setAllowsDueDate(allowsDueDate) {
  1019. return { $set: { allowsDueDate } };
  1020. },
  1021. setSubtasksDefaultBoardId(subtasksDefaultBoardId) {
  1022. return { $set: { subtasksDefaultBoardId } };
  1023. },
  1024. setSubtasksDefaultListId(subtasksDefaultListId) {
  1025. return { $set: { subtasksDefaultListId } };
  1026. },
  1027. setPresentParentTask(presentParentTask) {
  1028. return { $set: { presentParentTask } };
  1029. },
  1030. });
  1031. function boardRemover(userId, doc) {
  1032. [Cards, Lists, Swimlanes, Integrations, Rules, Activities].forEach(
  1033. element => {
  1034. element.remove({ boardId: doc._id });
  1035. },
  1036. );
  1037. }
  1038. if (Meteor.isServer) {
  1039. Boards.allow({
  1040. insert: Meteor.userId,
  1041. update: allowIsBoardAdmin,
  1042. remove: allowIsBoardAdmin,
  1043. fetch: ['members'],
  1044. });
  1045. // The number of users that have starred this board is managed by trusted code
  1046. // and the user is not allowed to update it
  1047. Boards.deny({
  1048. update(userId, board, fieldNames) {
  1049. return _.contains(fieldNames, 'stars');
  1050. },
  1051. fetch: [],
  1052. });
  1053. // We can't remove a member if it is the last administrator
  1054. Boards.deny({
  1055. update(userId, doc, fieldNames, modifier) {
  1056. if (!_.contains(fieldNames, 'members')) return false;
  1057. // We only care in case of a $pull operation, ie remove a member
  1058. if (!_.isObject(modifier.$pull && modifier.$pull.members)) return false;
  1059. // If there is more than one admin, it's ok to remove anyone
  1060. const nbAdmins = _.where(doc.members, { isActive: true, isAdmin: true })
  1061. .length;
  1062. if (nbAdmins > 1) return false;
  1063. // If all the previous conditions were verified, we can't remove
  1064. // a user if it's an admin
  1065. const removedMemberId = modifier.$pull.members.userId;
  1066. return Boolean(
  1067. _.findWhere(doc.members, {
  1068. userId: removedMemberId,
  1069. isAdmin: true,
  1070. }),
  1071. );
  1072. },
  1073. fetch: ['members'],
  1074. });
  1075. Meteor.methods({
  1076. quitBoard(boardId) {
  1077. check(boardId, String);
  1078. const board = Boards.findOne(boardId);
  1079. if (board) {
  1080. const userId = Meteor.userId();
  1081. const index = board.memberIndex(userId);
  1082. if (index >= 0) {
  1083. board.removeMember(userId);
  1084. return true;
  1085. } else throw new Meteor.Error('error-board-notAMember');
  1086. } else throw new Meteor.Error('error-board-doesNotExist');
  1087. },
  1088. acceptInvite(boardId) {
  1089. check(boardId, String);
  1090. const board = Boards.findOne(boardId);
  1091. if (!board) {
  1092. throw new Meteor.Error('error-board-doesNotExist');
  1093. }
  1094. Meteor.users.update(Meteor.userId(), {
  1095. $pull: {
  1096. 'profile.invitedBoards': boardId,
  1097. },
  1098. });
  1099. },
  1100. });
  1101. Meteor.methods({
  1102. archiveBoard(boardId) {
  1103. check(boardId, String);
  1104. const board = Boards.findOne(boardId);
  1105. if (board) {
  1106. const userId = Meteor.userId();
  1107. const index = board.memberIndex(userId);
  1108. if (index >= 0) {
  1109. board.archive();
  1110. return true;
  1111. } else throw new Meteor.Error('error-board-notAMember');
  1112. } else throw new Meteor.Error('error-board-doesNotExist');
  1113. },
  1114. });
  1115. }
  1116. if (Meteor.isServer) {
  1117. // Let MongoDB ensure that a member is not included twice in the same board
  1118. Meteor.startup(() => {
  1119. Boards._collection._ensureIndex({ modifiedAt: -1 });
  1120. Boards._collection._ensureIndex(
  1121. {
  1122. _id: 1,
  1123. 'members.userId': 1,
  1124. },
  1125. { unique: true },
  1126. );
  1127. Boards._collection._ensureIndex({ 'members.userId': 1 });
  1128. });
  1129. // Genesis: the first activity of the newly created board
  1130. Boards.after.insert((userId, doc) => {
  1131. Activities.insert({
  1132. userId,
  1133. type: 'board',
  1134. activityTypeId: doc._id,
  1135. activityType: 'createBoard',
  1136. boardId: doc._id,
  1137. });
  1138. });
  1139. // If the user remove one label from a board, we cant to remove reference of
  1140. // this label in any card of this board.
  1141. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1142. if (
  1143. !_.contains(fieldNames, 'labels') ||
  1144. !modifier.$pull ||
  1145. !modifier.$pull.labels ||
  1146. !modifier.$pull.labels._id
  1147. ) {
  1148. return;
  1149. }
  1150. const removedLabelId = modifier.$pull.labels._id;
  1151. Cards.update(
  1152. { boardId: doc._id },
  1153. {
  1154. $pull: {
  1155. labelIds: removedLabelId,
  1156. },
  1157. },
  1158. { multi: true },
  1159. );
  1160. });
  1161. const foreachRemovedMember = (doc, modifier, callback) => {
  1162. Object.keys(modifier).forEach(set => {
  1163. if (modifier[set] !== false) {
  1164. return;
  1165. }
  1166. const parts = set.split('.');
  1167. if (
  1168. parts.length === 3 &&
  1169. parts[0] === 'members' &&
  1170. parts[2] === 'isActive'
  1171. ) {
  1172. callback(doc.members[parts[1]].userId);
  1173. }
  1174. });
  1175. };
  1176. // Remove a member from all objects of the board before leaving the board
  1177. Boards.before.update((userId, doc, fieldNames, modifier) => {
  1178. if (!_.contains(fieldNames, 'members')) {
  1179. return;
  1180. }
  1181. if (modifier.$set) {
  1182. const boardId = doc._id;
  1183. foreachRemovedMember(doc, modifier.$set, memberId => {
  1184. Cards.update(
  1185. { boardId },
  1186. {
  1187. $pull: {
  1188. members: memberId,
  1189. watchers: memberId,
  1190. },
  1191. },
  1192. { multi: true },
  1193. );
  1194. Lists.update(
  1195. { boardId },
  1196. {
  1197. $pull: {
  1198. watchers: memberId,
  1199. },
  1200. },
  1201. { multi: true },
  1202. );
  1203. const board = Boards._transform(doc);
  1204. board.setWatcher(memberId, false);
  1205. // Remove board from users starred list
  1206. if (!board.isPublic()) {
  1207. Users.update(memberId, {
  1208. $pull: {
  1209. 'profile.starredBoards': boardId,
  1210. },
  1211. });
  1212. }
  1213. });
  1214. }
  1215. });
  1216. Boards.before.remove((userId, doc) => {
  1217. boardRemover(userId, doc);
  1218. // Add removeBoard activity to keep it
  1219. Activities.insert({
  1220. userId,
  1221. type: 'board',
  1222. activityTypeId: doc._id,
  1223. activityType: 'removeBoard',
  1224. boardId: doc._id,
  1225. });
  1226. });
  1227. // Add a new activity if we add or remove a member to the board
  1228. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1229. if (!_.contains(fieldNames, 'members')) {
  1230. return;
  1231. }
  1232. // Say hello to the new member
  1233. if (modifier.$push && modifier.$push.members) {
  1234. const memberId = modifier.$push.members.userId;
  1235. Activities.insert({
  1236. userId,
  1237. memberId,
  1238. type: 'member',
  1239. activityType: 'addBoardMember',
  1240. boardId: doc._id,
  1241. });
  1242. }
  1243. // Say goodbye to the former member
  1244. if (modifier.$set) {
  1245. foreachRemovedMember(doc, modifier.$set, memberId => {
  1246. Activities.insert({
  1247. userId,
  1248. memberId,
  1249. type: 'member',
  1250. activityType: 'removeBoardMember',
  1251. boardId: doc._id,
  1252. });
  1253. });
  1254. }
  1255. });
  1256. }
  1257. //BOARDS REST API
  1258. if (Meteor.isServer) {
  1259. /**
  1260. * @operation get_boards_from_user
  1261. * @summary Get all boards attached to a user
  1262. *
  1263. * @param {string} userId the ID of the user to retrieve the data
  1264. * @return_type [{_id: string,
  1265. title: string}]
  1266. */
  1267. JsonRoutes.add('GET', '/api/users/:userId/boards', function(req, res) {
  1268. try {
  1269. Authentication.checkLoggedIn(req.userId);
  1270. const paramUserId = req.params.userId;
  1271. // A normal user should be able to see their own boards,
  1272. // admins can access boards of any user
  1273. Authentication.checkAdminOrCondition(
  1274. req.userId,
  1275. req.userId === paramUserId,
  1276. );
  1277. const data = Boards.find(
  1278. {
  1279. archived: false,
  1280. 'members.userId': paramUserId,
  1281. },
  1282. {
  1283. sort: ['title'],
  1284. },
  1285. ).map(function(board) {
  1286. return {
  1287. _id: board._id,
  1288. title: board.title,
  1289. };
  1290. });
  1291. JsonRoutes.sendResult(res, { code: 200, data });
  1292. } catch (error) {
  1293. JsonRoutes.sendResult(res, {
  1294. code: 200,
  1295. data: error,
  1296. });
  1297. }
  1298. });
  1299. /**
  1300. * @operation get_public_boards
  1301. * @summary Get all public boards
  1302. *
  1303. * @return_type [{_id: string,
  1304. title: string}]
  1305. */
  1306. JsonRoutes.add('GET', '/api/boards', function(req, res) {
  1307. try {
  1308. Authentication.checkUserId(req.userId);
  1309. JsonRoutes.sendResult(res, {
  1310. code: 200,
  1311. data: Boards.find({ permission: 'public' }).map(function(doc) {
  1312. return {
  1313. _id: doc._id,
  1314. title: doc.title,
  1315. };
  1316. }),
  1317. });
  1318. } catch (error) {
  1319. JsonRoutes.sendResult(res, {
  1320. code: 200,
  1321. data: error,
  1322. });
  1323. }
  1324. });
  1325. /**
  1326. * @operation get_board
  1327. * @summary Get the board with that particular ID
  1328. *
  1329. * @param {string} boardId the ID of the board to retrieve the data
  1330. * @return_type Boards
  1331. */
  1332. JsonRoutes.add('GET', '/api/boards/:boardId', function(req, res) {
  1333. try {
  1334. const id = req.params.boardId;
  1335. Authentication.checkBoardAccess(req.userId, id);
  1336. JsonRoutes.sendResult(res, {
  1337. code: 200,
  1338. data: Boards.findOne({ _id: id }),
  1339. });
  1340. } catch (error) {
  1341. JsonRoutes.sendResult(res, {
  1342. code: 200,
  1343. data: error,
  1344. });
  1345. }
  1346. });
  1347. /**
  1348. * @operation new_board
  1349. * @summary Create a board
  1350. *
  1351. * @description This allows to create a board.
  1352. *
  1353. * The color has to be chosen between `belize`, `nephritis`, `pomegranate`,
  1354. * `pumpkin`, `wisteria`, `moderatepink`, `strongcyan`,
  1355. * `limegreen`, `midnight`, `dark`, `relax`, `corteza`:
  1356. *
  1357. * <img src="https://wekan.github.io/board-colors.png" width="40%" alt="Wekan logo" />
  1358. *
  1359. * @param {string} title the new title of the board
  1360. * @param {string} owner "ABCDE12345" <= User ID in Wekan.
  1361. * (Not username or email)
  1362. * @param {boolean} [isAdmin] is the owner an admin of the board (default true)
  1363. * @param {boolean} [isActive] is the board active (default true)
  1364. * @param {boolean} [isNoComments] disable comments (default false)
  1365. * @param {boolean} [isCommentOnly] only enable comments (default false)
  1366. * @param {boolean} [isWorker] only move cards, assign himself to card and comment (default false)
  1367. * @param {string} [permission] "private" board <== Set to "public" if you
  1368. * want public Wekan board
  1369. * @param {string} [color] the color of the board
  1370. *
  1371. * @return_type {_id: string,
  1372. defaultSwimlaneId: string}
  1373. */
  1374. JsonRoutes.add('POST', '/api/boards', function(req, res) {
  1375. try {
  1376. Authentication.checkUserId(req.userId);
  1377. const id = Boards.insert({
  1378. title: req.body.title,
  1379. members: [
  1380. {
  1381. userId: req.body.owner,
  1382. isAdmin: req.body.isAdmin || true,
  1383. isActive: req.body.isActive || true,
  1384. isNoComments: req.body.isNoComments || false,
  1385. isCommentOnly: req.body.isCommentOnly || false,
  1386. isWorker: req.body.isWorker || false,
  1387. },
  1388. ],
  1389. permission: req.body.permission || 'private',
  1390. color: req.body.color || 'belize',
  1391. });
  1392. const swimlaneId = Swimlanes.insert({
  1393. title: TAPi18n.__('default'),
  1394. boardId: id,
  1395. });
  1396. JsonRoutes.sendResult(res, {
  1397. code: 200,
  1398. data: {
  1399. _id: id,
  1400. defaultSwimlaneId: swimlaneId,
  1401. },
  1402. });
  1403. } catch (error) {
  1404. JsonRoutes.sendResult(res, {
  1405. code: 200,
  1406. data: error,
  1407. });
  1408. }
  1409. });
  1410. /**
  1411. * @operation delete_board
  1412. * @summary Delete a board
  1413. *
  1414. * @param {string} boardId the ID of the board
  1415. */
  1416. JsonRoutes.add('DELETE', '/api/boards/:boardId', function(req, res) {
  1417. try {
  1418. Authentication.checkUserId(req.userId);
  1419. const id = req.params.boardId;
  1420. Boards.remove({ _id: id });
  1421. JsonRoutes.sendResult(res, {
  1422. code: 200,
  1423. data: {
  1424. _id: id,
  1425. },
  1426. });
  1427. } catch (error) {
  1428. JsonRoutes.sendResult(res, {
  1429. code: 200,
  1430. data: error,
  1431. });
  1432. }
  1433. });
  1434. /**
  1435. * @operation add_board_label
  1436. * @summary Add a label to a board
  1437. *
  1438. * @description If the board doesn't have the name/color label, this function
  1439. * adds the label to the board.
  1440. *
  1441. * @param {string} boardId the board
  1442. * @param {string} color the color of the new label
  1443. * @param {string} name the name of the new label
  1444. *
  1445. * @return_type string
  1446. */
  1447. JsonRoutes.add('PUT', '/api/boards/:boardId/labels', function(req, res) {
  1448. Authentication.checkUserId(req.userId);
  1449. const id = req.params.boardId;
  1450. try {
  1451. if (req.body.hasOwnProperty('label')) {
  1452. const board = Boards.findOne({ _id: id });
  1453. const color = req.body.label.color;
  1454. const name = req.body.label.name;
  1455. const labelId = Random.id(6);
  1456. if (!board.getLabel(name, color)) {
  1457. Boards.direct.update(
  1458. { _id: id },
  1459. { $push: { labels: { _id: labelId, name, color } } },
  1460. );
  1461. JsonRoutes.sendResult(res, {
  1462. code: 200,
  1463. data: labelId,
  1464. });
  1465. } else {
  1466. JsonRoutes.sendResult(res, {
  1467. code: 200,
  1468. });
  1469. }
  1470. }
  1471. } catch (error) {
  1472. JsonRoutes.sendResult(res, {
  1473. data: error,
  1474. });
  1475. }
  1476. });
  1477. /**
  1478. * @operation set_board_member_permission
  1479. * @tag Users
  1480. * @summary Change the permission of a member of a board
  1481. *
  1482. * @param {string} boardId the ID of the board that we are changing
  1483. * @param {string} memberId the ID of the user to change permissions
  1484. * @param {boolean} isAdmin admin capability
  1485. * @param {boolean} isNoComments NoComments capability
  1486. * @param {boolean} isCommentOnly CommentsOnly capability
  1487. * @param {boolean} isWorker Worker capability
  1488. */
  1489. JsonRoutes.add('POST', '/api/boards/:boardId/members/:memberId', function(
  1490. req,
  1491. res,
  1492. ) {
  1493. try {
  1494. const boardId = req.params.boardId;
  1495. const memberId = req.params.memberId;
  1496. const { isAdmin, isNoComments, isCommentOnly, isWorker } = req.body;
  1497. Authentication.checkBoardAccess(req.userId, boardId);
  1498. const board = Boards.findOne({ _id: boardId });
  1499. function isTrue(data) {
  1500. try {
  1501. return data.toLowerCase() === 'true';
  1502. } catch (error) {
  1503. return data;
  1504. }
  1505. }
  1506. const query = board.setMemberPermission(
  1507. memberId,
  1508. isTrue(isAdmin),
  1509. isTrue(isNoComments),
  1510. isTrue(isCommentOnly),
  1511. isTrue(isWorker),
  1512. req.userId,
  1513. );
  1514. JsonRoutes.sendResult(res, {
  1515. code: 200,
  1516. data: query,
  1517. });
  1518. } catch (error) {
  1519. JsonRoutes.sendResult(res, {
  1520. code: 200,
  1521. data: error,
  1522. });
  1523. }
  1524. });
  1525. }
  1526. export default Boards;