boards.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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. allowsDescriptionTitle: {
  327. /**
  328. * Does the board allows description title?
  329. */
  330. type: Boolean,
  331. defaultValue: true,
  332. },
  333. allowsDescriptionText: {
  334. /**
  335. * Does the board allows description text?
  336. */
  337. type: Boolean,
  338. defaultValue: true,
  339. },
  340. allowsActivities: {
  341. /**
  342. * Does the board allows comments?
  343. */
  344. type: Boolean,
  345. defaultValue: true,
  346. },
  347. allowsLabels: {
  348. /**
  349. * Does the board allows labels?
  350. */
  351. type: Boolean,
  352. defaultValue: true,
  353. },
  354. allowsAssignee: {
  355. /**
  356. * Does the board allows assignee?
  357. */
  358. type: Boolean,
  359. defaultValue: true,
  360. },
  361. allowsMembers: {
  362. /**
  363. * Does the board allows members?
  364. */
  365. type: Boolean,
  366. defaultValue: true,
  367. },
  368. allowsRequestedBy: {
  369. /**
  370. * Does the board allows requested by?
  371. */
  372. type: Boolean,
  373. defaultValue: true,
  374. },
  375. allowsAssignedBy: {
  376. /**
  377. * Does the board allows requested by?
  378. */
  379. type: Boolean,
  380. defaultValue: true,
  381. },
  382. allowsReceivedDate: {
  383. /**
  384. * Does the board allows received date?
  385. */
  386. type: Boolean,
  387. defaultValue: true,
  388. },
  389. allowsStartDate: {
  390. /**
  391. * Does the board allows start date?
  392. */
  393. type: Boolean,
  394. defaultValue: true,
  395. },
  396. allowsEndDate: {
  397. /**
  398. * Does the board allows end date?
  399. */
  400. type: Boolean,
  401. defaultValue: true,
  402. },
  403. allowsDueDate: {
  404. /**
  405. * Does the board allows due date?
  406. */
  407. type: Boolean,
  408. defaultValue: true,
  409. },
  410. presentParentTask: {
  411. /**
  412. * Controls how to present the parent task:
  413. *
  414. * - `prefix-with-full-path`: add a prefix with the full path
  415. * - `prefix-with-parent`: add a prefisx with the parent name
  416. * - `subtext-with-full-path`: add a subtext with the full path
  417. * - `subtext-with-parent`: add a subtext with the parent name
  418. * - `no-parent`: does not show the parent at all
  419. */
  420. type: String,
  421. allowedValues: [
  422. 'prefix-with-full-path',
  423. 'prefix-with-parent',
  424. 'subtext-with-full-path',
  425. 'subtext-with-parent',
  426. 'no-parent',
  427. ],
  428. optional: true,
  429. defaultValue: 'no-parent',
  430. },
  431. startAt: {
  432. /**
  433. * Starting date of the board.
  434. */
  435. type: Date,
  436. optional: true,
  437. },
  438. dueAt: {
  439. /**
  440. * Due date of the board.
  441. */
  442. type: Date,
  443. optional: true,
  444. },
  445. endAt: {
  446. /**
  447. * End date of the board.
  448. */
  449. type: Date,
  450. optional: true,
  451. },
  452. spentTime: {
  453. /**
  454. * Time spent in the board.
  455. */
  456. type: Number,
  457. decimal: true,
  458. optional: true,
  459. },
  460. isOvertime: {
  461. /**
  462. * Is the board overtimed?
  463. */
  464. type: Boolean,
  465. defaultValue: false,
  466. optional: true,
  467. },
  468. type: {
  469. /**
  470. * The type of board
  471. */
  472. type: String,
  473. defaultValue: 'board',
  474. },
  475. sort: {
  476. /**
  477. * Sort value
  478. */
  479. type: Number,
  480. decimal: true,
  481. defaultValue: -1,
  482. },
  483. }),
  484. );
  485. Boards.helpers({
  486. copy() {
  487. const oldId = this._id;
  488. delete this._id;
  489. const _id = Boards.insert(this);
  490. // Copy all swimlanes in board
  491. Swimlanes.find({
  492. boardId: oldId,
  493. archived: false,
  494. }).forEach(swimlane => {
  495. swimlane.type = 'swimlane';
  496. swimlane.copy(_id);
  497. });
  498. },
  499. /**
  500. * Is supplied user authorized to view this board?
  501. */
  502. isVisibleBy(user) {
  503. if (this.isPublic()) {
  504. // public boards are visible to everyone
  505. return true;
  506. } else {
  507. // otherwise you have to be logged-in and active member
  508. return user && this.isActiveMember(user._id);
  509. }
  510. },
  511. /**
  512. * Is the user one of the active members of the board?
  513. *
  514. * @param userId
  515. * @returns {boolean} the member that matches, or undefined/false
  516. */
  517. isActiveMember(userId) {
  518. if (userId) {
  519. return this.members.find(
  520. member => member.userId === userId && member.isActive,
  521. );
  522. } else {
  523. return false;
  524. }
  525. },
  526. isPublic() {
  527. return this.permission === 'public';
  528. },
  529. cards() {
  530. return Cards.find(
  531. { boardId: this._id, archived: false },
  532. { sort: { title: 1 } },
  533. );
  534. },
  535. lists() {
  536. //currentUser = Meteor.user();
  537. //if (currentUser) {
  538. // enabled = Meteor.user().hasSortBy();
  539. //}
  540. //return enabled ? this.newestLists() : this.draggableLists();
  541. return this.draggableLists();
  542. },
  543. newestLists() {
  544. // sorted lists from newest to the oldest, by its creation date or its cards' last modification date
  545. const value = Meteor.user()._getListSortBy();
  546. const sortKey = { starred: -1, [value[0]]: value[1] }; // [["starred",-1],value];
  547. return Lists.find(
  548. {
  549. boardId: this._id,
  550. archived: false,
  551. },
  552. { sort: sortKey },
  553. );
  554. },
  555. draggableLists() {
  556. return Lists.find({ boardId: this._id }, { sort: { sort: 1 } });
  557. },
  558. nullSortLists() {
  559. return Lists.find({
  560. boardId: this._id,
  561. archived: false,
  562. sort: { $eq: null },
  563. });
  564. },
  565. swimlanes() {
  566. return Swimlanes.find(
  567. { boardId: this._id, archived: false },
  568. { sort: { sort: 1 } },
  569. );
  570. },
  571. nextSwimlane(swimlane) {
  572. return Swimlanes.findOne(
  573. {
  574. boardId: this._id,
  575. archived: false,
  576. sort: { $gte: swimlane.sort },
  577. _id: { $ne: swimlane._id },
  578. },
  579. {
  580. sort: { sort: 1 },
  581. },
  582. );
  583. },
  584. nullSortSwimlanes() {
  585. return Swimlanes.find({
  586. boardId: this._id,
  587. archived: false,
  588. sort: { $eq: null },
  589. });
  590. },
  591. hasOvertimeCards() {
  592. const card = Cards.findOne({
  593. isOvertime: true,
  594. boardId: this._id,
  595. archived: false,
  596. });
  597. return card !== undefined;
  598. },
  599. hasSpentTimeCards() {
  600. const card = Cards.findOne({
  601. spentTime: { $gt: 0 },
  602. boardId: this._id,
  603. archived: false,
  604. });
  605. return card !== undefined;
  606. },
  607. activities() {
  608. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 } });
  609. },
  610. activeMembers() {
  611. return _.where(this.members, { isActive: true });
  612. },
  613. activeAdmins() {
  614. return _.where(this.members, { isActive: true, isAdmin: true });
  615. },
  616. memberUsers() {
  617. return Users.find({ _id: { $in: _.pluck(this.members, 'userId') } });
  618. },
  619. getLabel(name, color) {
  620. return _.findWhere(this.labels, { name, color });
  621. },
  622. getLabelById(labelId) {
  623. return _.findWhere(this.labels, { _id: labelId });
  624. },
  625. labelIndex(labelId) {
  626. return _.pluck(this.labels, '_id').indexOf(labelId);
  627. },
  628. memberIndex(memberId) {
  629. return _.pluck(this.members, 'userId').indexOf(memberId);
  630. },
  631. hasMember(memberId) {
  632. return !!_.findWhere(this.members, { userId: memberId, isActive: true });
  633. },
  634. hasAdmin(memberId) {
  635. return !!_.findWhere(this.members, {
  636. userId: memberId,
  637. isActive: true,
  638. isAdmin: true,
  639. });
  640. },
  641. hasNoComments(memberId) {
  642. return !!_.findWhere(this.members, {
  643. userId: memberId,
  644. isActive: true,
  645. isAdmin: false,
  646. isNoComments: true,
  647. isWorker: false,
  648. });
  649. },
  650. hasCommentOnly(memberId) {
  651. return !!_.findWhere(this.members, {
  652. userId: memberId,
  653. isActive: true,
  654. isAdmin: false,
  655. isCommentOnly: true,
  656. isWorker: false,
  657. });
  658. },
  659. hasWorker(memberId) {
  660. return !!_.findWhere(this.members, {
  661. userId: memberId,
  662. isActive: true,
  663. isAdmin: false,
  664. isCommentOnly: false,
  665. isWorker: true,
  666. });
  667. },
  668. absoluteUrl() {
  669. return FlowRouter.url('board', { id: this._id, slug: this.slug });
  670. },
  671. colorClass() {
  672. return `board-color-${this.color}`;
  673. },
  674. customFields() {
  675. return CustomFields.find(
  676. { boardIds: { $in: [this._id] } },
  677. { sort: { name: 1 } },
  678. );
  679. },
  680. // XXX currently mutations return no value so we have an issue when using addLabel in import
  681. // XXX waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  682. pushLabel(name, color) {
  683. const _id = Random.id(6);
  684. Boards.direct.update(this._id, { $push: { labels: { _id, name, color } } });
  685. return _id;
  686. },
  687. searchBoards(term) {
  688. check(term, Match.OneOf(String, null, undefined));
  689. const query = { boardId: this._id };
  690. query.type = 'cardType-linkedBoard';
  691. query.archived = false;
  692. const projection = { limit: 10, sort: { createdAt: -1 } };
  693. if (term) {
  694. const regex = new RegExp(term, 'i');
  695. query.$or = [{ title: regex }, { description: regex }];
  696. }
  697. return Cards.find(query, projection);
  698. },
  699. searchSwimlanes(term) {
  700. check(term, Match.OneOf(String, null, undefined));
  701. const query = { boardId: this._id };
  702. if (this.isTemplatesBoard()) {
  703. query.type = 'template-swimlane';
  704. query.archived = false;
  705. } else {
  706. query.type = { $nin: ['template-swimlane'] };
  707. }
  708. const projection = { limit: 10, sort: { createdAt: -1 } };
  709. if (term) {
  710. const regex = new RegExp(term, 'i');
  711. query.$or = [{ title: regex }, { description: regex }];
  712. }
  713. return Swimlanes.find(query, projection);
  714. },
  715. searchLists(term) {
  716. check(term, Match.OneOf(String, null, undefined));
  717. const query = { boardId: this._id };
  718. if (this.isTemplatesBoard()) {
  719. query.type = 'template-list';
  720. query.archived = false;
  721. } else {
  722. query.type = { $nin: ['template-list'] };
  723. }
  724. const projection = { limit: 10, sort: { createdAt: -1 } };
  725. if (term) {
  726. const regex = new RegExp(term, 'i');
  727. query.$or = [{ title: regex }, { description: regex }];
  728. }
  729. return Lists.find(query, projection);
  730. },
  731. searchCards(term, excludeLinked) {
  732. check(term, Match.OneOf(String, null, undefined));
  733. const query = { boardId: this._id };
  734. if (excludeLinked) {
  735. query.linkedId = null;
  736. }
  737. if (this.isTemplatesBoard()) {
  738. query.type = 'template-card';
  739. query.archived = false;
  740. } else {
  741. query.type = { $nin: ['template-card'] };
  742. }
  743. const projection = { limit: 10, sort: { createdAt: -1 } };
  744. if (term) {
  745. const regex = new RegExp(term, 'i');
  746. query.$or = [
  747. { title: regex },
  748. { description: regex },
  749. { customFields: { $elemMatch: { value: regex } } },
  750. ];
  751. }
  752. return Cards.find(query, projection);
  753. },
  754. // A board alwasy has another board where it deposits subtasks of thasks
  755. // that belong to itself.
  756. getDefaultSubtasksBoardId() {
  757. if (
  758. this.subtasksDefaultBoardId === null ||
  759. this.subtasksDefaultBoardId === undefined
  760. ) {
  761. this.subtasksDefaultBoardId = Boards.insert({
  762. title: `^${this.title}^`,
  763. permission: this.permission,
  764. members: this.members,
  765. color: this.color,
  766. description: TAPi18n.__('default-subtasks-board', {
  767. board: this.title,
  768. }),
  769. });
  770. Swimlanes.insert({
  771. title: TAPi18n.__('default'),
  772. boardId: this.subtasksDefaultBoardId,
  773. });
  774. Boards.update(this._id, {
  775. $set: {
  776. subtasksDefaultBoardId: this.subtasksDefaultBoardId,
  777. },
  778. });
  779. }
  780. return this.subtasksDefaultBoardId;
  781. },
  782. getDefaultSubtasksBoard() {
  783. return Boards.findOne(this.getDefaultSubtasksBoardId());
  784. },
  785. //Date Settings option such as received date, start date and so on.
  786. getDefaultDateSettingsBoardId() {
  787. if (
  788. this.dateSettingsDefaultBoardId === null ||
  789. this.dateSettingsDefaultBoardId === undefined
  790. ) {
  791. this.dateSettingsDefaultBoardId = Boards.insert({
  792. title: `^${this.title}^`,
  793. permission: this.permission,
  794. members: this.members,
  795. color: this.color,
  796. description: TAPi18n.__('default-dates-board', {
  797. board: this.title,
  798. }),
  799. });
  800. Swimlanes.insert({
  801. title: TAPi18n.__('default'),
  802. boardId: this.dateSettingsDefaultBoardId,
  803. });
  804. Boards.update(this._id, {
  805. $set: {
  806. dateSettingsDefaultBoardId: this.dateSettingsDefaultBoardId,
  807. },
  808. });
  809. }
  810. return this.dateSettingsDefaultBoardId;
  811. },
  812. getDefaultDateSettingsBoard() {
  813. return Boards.findOne(this.getDefaultDateSettingsBoardId());
  814. },
  815. getDefaultSubtasksListId() {
  816. if (
  817. this.subtasksDefaultListId === null ||
  818. this.subtasksDefaultListId === undefined
  819. ) {
  820. this.subtasksDefaultListId = Lists.insert({
  821. title: TAPi18n.__('queue'),
  822. boardId: this._id,
  823. });
  824. this.setSubtasksDefaultListId(this.subtasksDefaultListId);
  825. }
  826. return this.subtasksDefaultListId;
  827. },
  828. getDefaultSubtasksList() {
  829. return Lists.findOne(this.getDefaultSubtasksListId());
  830. },
  831. getDefaultDateSettingsListId() {
  832. if (
  833. this.dateSettingsDefaultListId === null ||
  834. this.dateSettingsDefaultListId === undefined
  835. ) {
  836. this.dateSettingsDefaultListId = Lists.insert({
  837. title: TAPi18n.__('queue'),
  838. boardId: this._id,
  839. });
  840. this.setDateSettingsDefaultListId(this.dateSettingsDefaultListId);
  841. }
  842. return this.dateSettingsDefaultListId;
  843. },
  844. getDefaultDateSettingsList() {
  845. return Lists.findOne(this.getDefaultDateSettingsListId());
  846. },
  847. getDefaultSwimline() {
  848. let result = Swimlanes.findOne({ boardId: this._id });
  849. if (result === undefined) {
  850. Swimlanes.insert({
  851. title: TAPi18n.__('default'),
  852. boardId: this._id,
  853. });
  854. result = Swimlanes.findOne({ boardId: this._id });
  855. }
  856. return result;
  857. },
  858. cardsDueInBetween(start, end) {
  859. return Cards.find({
  860. boardId: this._id,
  861. dueAt: { $gte: start, $lte: end },
  862. });
  863. },
  864. cardsInInterval(start, end) {
  865. return Cards.find({
  866. boardId: this._id,
  867. $or: [
  868. {
  869. startAt: {
  870. $lte: start,
  871. },
  872. endAt: {
  873. $gte: start,
  874. },
  875. },
  876. {
  877. startAt: {
  878. $lte: end,
  879. },
  880. endAt: {
  881. $gte: end,
  882. },
  883. },
  884. {
  885. startAt: {
  886. $gte: start,
  887. },
  888. endAt: {
  889. $lte: end,
  890. },
  891. },
  892. ],
  893. });
  894. },
  895. isTemplateBoard() {
  896. return this.type === 'template-board';
  897. },
  898. isTemplatesBoard() {
  899. return this.type === 'template-container';
  900. },
  901. });
  902. Boards.mutations({
  903. archive() {
  904. return { $set: { archived: true } };
  905. },
  906. restore() {
  907. return { $set: { archived: false } };
  908. },
  909. rename(title) {
  910. return { $set: { title } };
  911. },
  912. setDescription(description) {
  913. return { $set: { description } };
  914. },
  915. setColor(color) {
  916. return { $set: { color } };
  917. },
  918. setVisibility(visibility) {
  919. return { $set: { permission: visibility } };
  920. },
  921. addLabel(name, color) {
  922. // If label with the same name and color already exists we don't want to
  923. // create another one because they would be indistinguishable in the UI
  924. // (they would still have different `_id` but that is not exposed to the
  925. // user).
  926. if (!this.getLabel(name, color)) {
  927. const _id = Random.id(6);
  928. return { $push: { labels: { _id, name, color } } };
  929. }
  930. return {};
  931. },
  932. editLabel(labelId, name, color) {
  933. if (!this.getLabel(name, color)) {
  934. const labelIndex = this.labelIndex(labelId);
  935. return {
  936. $set: {
  937. [`labels.${labelIndex}.name`]: name,
  938. [`labels.${labelIndex}.color`]: color,
  939. },
  940. };
  941. }
  942. return {};
  943. },
  944. removeLabel(labelId) {
  945. return { $pull: { labels: { _id: labelId } } };
  946. },
  947. changeOwnership(fromId, toId) {
  948. const memberIndex = this.memberIndex(fromId);
  949. return {
  950. $set: {
  951. [`members.${memberIndex}.userId`]: toId,
  952. },
  953. };
  954. },
  955. addMember(memberId) {
  956. const memberIndex = this.memberIndex(memberId);
  957. if (memberIndex >= 0) {
  958. return {
  959. $set: {
  960. [`members.${memberIndex}.isActive`]: true,
  961. },
  962. };
  963. }
  964. return {
  965. $push: {
  966. members: {
  967. userId: memberId,
  968. isAdmin: false,
  969. isActive: true,
  970. isNoComments: false,
  971. isCommentOnly: false,
  972. isWorker: false,
  973. },
  974. },
  975. };
  976. },
  977. removeMember(memberId) {
  978. const memberIndex = this.memberIndex(memberId);
  979. // we do not allow the only one admin to be removed
  980. const allowRemove =
  981. !this.members[memberIndex].isAdmin || this.activeAdmins().length > 1;
  982. if (!allowRemove) {
  983. return {
  984. $set: {
  985. [`members.${memberIndex}.isActive`]: true,
  986. },
  987. };
  988. }
  989. return {
  990. $set: {
  991. [`members.${memberIndex}.isActive`]: false,
  992. [`members.${memberIndex}.isAdmin`]: false,
  993. },
  994. };
  995. },
  996. setMemberPermission(
  997. memberId,
  998. isAdmin,
  999. isNoComments,
  1000. isCommentOnly,
  1001. isWorker,
  1002. currentUserId = Meteor.userId(),
  1003. ) {
  1004. const memberIndex = this.memberIndex(memberId);
  1005. // do not allow change permission of self
  1006. if (memberId === currentUserId) {
  1007. isAdmin = this.members[memberIndex].isAdmin;
  1008. }
  1009. return {
  1010. $set: {
  1011. [`members.${memberIndex}.isAdmin`]: isAdmin,
  1012. [`members.${memberIndex}.isNoComments`]: isNoComments,
  1013. [`members.${memberIndex}.isCommentOnly`]: isCommentOnly,
  1014. [`members.${memberIndex}.isWorker`]: isWorker,
  1015. },
  1016. };
  1017. },
  1018. setAllowsSubtasks(allowsSubtasks) {
  1019. return { $set: { allowsSubtasks } };
  1020. },
  1021. setAllowsMembers(allowsMembers) {
  1022. return { $set: { allowsMembers } };
  1023. },
  1024. setAllowsChecklists(allowsChecklists) {
  1025. return { $set: { allowsChecklists } };
  1026. },
  1027. setAllowsAssignee(allowsAssignee) {
  1028. return { $set: { allowsAssignee } };
  1029. },
  1030. setAllowsAssignedBy(allowsAssignedBy) {
  1031. return { $set: { allowsAssignedBy } };
  1032. },
  1033. setAllowsRequestedBy(allowsRequestedBy) {
  1034. return { $set: { allowsRequestedBy } };
  1035. },
  1036. setAllowsAttachments(allowsAttachments) {
  1037. return { $set: { allowsAttachments } };
  1038. },
  1039. setAllowsLabels(allowsLabels) {
  1040. return { $set: { allowsLabels } };
  1041. },
  1042. setAllowsComments(allowsComments) {
  1043. return { $set: { allowsComments } };
  1044. },
  1045. setAllowsDescriptionTitle(allowsDescriptionTitle) {
  1046. return { $set: { allowsDescriptionTitle } };
  1047. },
  1048. setAllowsDescriptionText(allowsDescriptionText) {
  1049. return { $set: { allowsDescriptionText } };
  1050. },
  1051. setAllowsActivities(allowsActivities) {
  1052. return { $set: { allowsActivities } };
  1053. },
  1054. setAllowsReceivedDate(allowsReceivedDate) {
  1055. return { $set: { allowsReceivedDate } };
  1056. },
  1057. setAllowsStartDate(allowsStartDate) {
  1058. return { $set: { allowsStartDate } };
  1059. },
  1060. setAllowsEndDate(allowsEndDate) {
  1061. return { $set: { allowsEndDate } };
  1062. },
  1063. setAllowsDueDate(allowsDueDate) {
  1064. return { $set: { allowsDueDate } };
  1065. },
  1066. setSubtasksDefaultBoardId(subtasksDefaultBoardId) {
  1067. return { $set: { subtasksDefaultBoardId } };
  1068. },
  1069. setSubtasksDefaultListId(subtasksDefaultListId) {
  1070. return { $set: { subtasksDefaultListId } };
  1071. },
  1072. setPresentParentTask(presentParentTask) {
  1073. return { $set: { presentParentTask } };
  1074. },
  1075. move(sortIndex) {
  1076. return { $set: { sort: sortIndex } };
  1077. },
  1078. });
  1079. function boardRemover(userId, doc) {
  1080. [Cards, Lists, Swimlanes, Integrations, Rules, Activities].forEach(
  1081. element => {
  1082. element.remove({ boardId: doc._id });
  1083. },
  1084. );
  1085. }
  1086. if (Meteor.isServer) {
  1087. Boards.allow({
  1088. insert: Meteor.userId,
  1089. update: allowIsBoardAdmin,
  1090. remove: allowIsBoardAdmin,
  1091. fetch: ['members'],
  1092. });
  1093. // The number of users that have starred this board is managed by trusted code
  1094. // and the user is not allowed to update it
  1095. Boards.deny({
  1096. update(userId, board, fieldNames) {
  1097. return _.contains(fieldNames, 'stars');
  1098. },
  1099. fetch: [],
  1100. });
  1101. // We can't remove a member if it is the last administrator
  1102. Boards.deny({
  1103. update(userId, doc, fieldNames, modifier) {
  1104. if (!_.contains(fieldNames, 'members')) return false;
  1105. // We only care in case of a $pull operation, ie remove a member
  1106. if (!_.isObject(modifier.$pull && modifier.$pull.members)) return false;
  1107. // If there is more than one admin, it's ok to remove anyone
  1108. const nbAdmins = _.where(doc.members, { isActive: true, isAdmin: true })
  1109. .length;
  1110. if (nbAdmins > 1) return false;
  1111. // If all the previous conditions were verified, we can't remove
  1112. // a user if it's an admin
  1113. const removedMemberId = modifier.$pull.members.userId;
  1114. return Boolean(
  1115. _.findWhere(doc.members, {
  1116. userId: removedMemberId,
  1117. isAdmin: true,
  1118. }),
  1119. );
  1120. },
  1121. fetch: ['members'],
  1122. });
  1123. Meteor.methods({
  1124. quitBoard(boardId) {
  1125. check(boardId, String);
  1126. const board = Boards.findOne(boardId);
  1127. if (board) {
  1128. const userId = Meteor.userId();
  1129. const index = board.memberIndex(userId);
  1130. if (index >= 0) {
  1131. board.removeMember(userId);
  1132. return true;
  1133. } else throw new Meteor.Error('error-board-notAMember');
  1134. } else throw new Meteor.Error('error-board-doesNotExist');
  1135. },
  1136. acceptInvite(boardId) {
  1137. check(boardId, String);
  1138. const board = Boards.findOne(boardId);
  1139. if (!board) {
  1140. throw new Meteor.Error('error-board-doesNotExist');
  1141. }
  1142. Meteor.users.update(Meteor.userId(), {
  1143. $pull: {
  1144. 'profile.invitedBoards': boardId,
  1145. },
  1146. });
  1147. },
  1148. });
  1149. Meteor.methods({
  1150. archiveBoard(boardId) {
  1151. check(boardId, String);
  1152. const board = Boards.findOne(boardId);
  1153. if (board) {
  1154. const userId = Meteor.userId();
  1155. const index = board.memberIndex(userId);
  1156. if (index >= 0) {
  1157. board.archive();
  1158. return true;
  1159. } else throw new Meteor.Error('error-board-notAMember');
  1160. } else throw new Meteor.Error('error-board-doesNotExist');
  1161. },
  1162. });
  1163. }
  1164. // Insert new board at last position in sort order.
  1165. Boards.before.insert((userId, doc) => {
  1166. const lastBoard = Boards.findOne({ sort: { $exists: true } }, { sort: { sort: -1 } });
  1167. if (lastBoard && typeof lastBoard.sort !== 'undefined') {
  1168. doc.sort = lastBoard.sort + 1;
  1169. }
  1170. });
  1171. if (Meteor.isServer) {
  1172. // Let MongoDB ensure that a member is not included twice in the same board
  1173. Meteor.startup(() => {
  1174. Boards._collection._ensureIndex({ modifiedAt: -1 });
  1175. Boards._collection._ensureIndex(
  1176. {
  1177. _id: 1,
  1178. 'members.userId': 1,
  1179. },
  1180. { unique: true },
  1181. );
  1182. Boards._collection._ensureIndex({ 'members.userId': 1 });
  1183. });
  1184. // Genesis: the first activity of the newly created board
  1185. Boards.after.insert((userId, doc) => {
  1186. Activities.insert({
  1187. userId,
  1188. type: 'board',
  1189. activityTypeId: doc._id,
  1190. activityType: 'createBoard',
  1191. boardId: doc._id,
  1192. });
  1193. });
  1194. // If the user remove one label from a board, we cant to remove reference of
  1195. // this label in any card of this board.
  1196. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1197. if (
  1198. !_.contains(fieldNames, 'labels') ||
  1199. !modifier.$pull ||
  1200. !modifier.$pull.labels ||
  1201. !modifier.$pull.labels._id
  1202. ) {
  1203. return;
  1204. }
  1205. const removedLabelId = modifier.$pull.labels._id;
  1206. Cards.update(
  1207. { boardId: doc._id },
  1208. {
  1209. $pull: {
  1210. labelIds: removedLabelId,
  1211. },
  1212. },
  1213. { multi: true },
  1214. );
  1215. });
  1216. const foreachRemovedMember = (doc, modifier, callback) => {
  1217. Object.keys(modifier).forEach(set => {
  1218. if (modifier[set] !== false) {
  1219. return;
  1220. }
  1221. const parts = set.split('.');
  1222. if (
  1223. parts.length === 3 &&
  1224. parts[0] === 'members' &&
  1225. parts[2] === 'isActive'
  1226. ) {
  1227. callback(doc.members[parts[1]].userId);
  1228. }
  1229. });
  1230. };
  1231. // Remove a member from all objects of the board before leaving the board
  1232. Boards.before.update((userId, doc, fieldNames, modifier) => {
  1233. if (!_.contains(fieldNames, 'members')) {
  1234. return;
  1235. }
  1236. if (modifier.$set) {
  1237. const boardId = doc._id;
  1238. foreachRemovedMember(doc, modifier.$set, memberId => {
  1239. Cards.update(
  1240. { boardId },
  1241. {
  1242. $pull: {
  1243. members: memberId,
  1244. watchers: memberId,
  1245. },
  1246. },
  1247. { multi: true },
  1248. );
  1249. Lists.update(
  1250. { boardId },
  1251. {
  1252. $pull: {
  1253. watchers: memberId,
  1254. },
  1255. },
  1256. { multi: true },
  1257. );
  1258. const board = Boards._transform(doc);
  1259. board.setWatcher(memberId, false);
  1260. // Remove board from users starred list
  1261. if (!board.isPublic()) {
  1262. Users.update(memberId, {
  1263. $pull: {
  1264. 'profile.starredBoards': boardId,
  1265. },
  1266. });
  1267. }
  1268. });
  1269. }
  1270. });
  1271. Boards.before.remove((userId, doc) => {
  1272. boardRemover(userId, doc);
  1273. // Add removeBoard activity to keep it
  1274. Activities.insert({
  1275. userId,
  1276. type: 'board',
  1277. activityTypeId: doc._id,
  1278. activityType: 'removeBoard',
  1279. boardId: doc._id,
  1280. });
  1281. });
  1282. // Add a new activity if we add or remove a member to the board
  1283. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1284. if (!_.contains(fieldNames, 'members')) {
  1285. return;
  1286. }
  1287. // Say hello to the new member
  1288. if (modifier.$push && modifier.$push.members) {
  1289. const memberId = modifier.$push.members.userId;
  1290. Activities.insert({
  1291. userId,
  1292. memberId,
  1293. type: 'member',
  1294. activityType: 'addBoardMember',
  1295. boardId: doc._id,
  1296. });
  1297. }
  1298. // Say goodbye to the former member
  1299. if (modifier.$set) {
  1300. foreachRemovedMember(doc, modifier.$set, memberId => {
  1301. Activities.insert({
  1302. userId,
  1303. memberId,
  1304. type: 'member',
  1305. activityType: 'removeBoardMember',
  1306. boardId: doc._id,
  1307. });
  1308. });
  1309. }
  1310. });
  1311. }
  1312. //BOARDS REST API
  1313. if (Meteor.isServer) {
  1314. /**
  1315. * @operation get_boards_from_user
  1316. * @summary Get all boards attached to a user
  1317. *
  1318. * @param {string} userId the ID of the user to retrieve the data
  1319. * @return_type [{_id: string,
  1320. title: string}]
  1321. */
  1322. JsonRoutes.add('GET', '/api/users/:userId/boards', function(req, res) {
  1323. try {
  1324. Authentication.checkLoggedIn(req.userId);
  1325. const paramUserId = req.params.userId;
  1326. // A normal user should be able to see their own boards,
  1327. // admins can access boards of any user
  1328. Authentication.checkAdminOrCondition(
  1329. req.userId,
  1330. req.userId === paramUserId,
  1331. );
  1332. const data = Boards.find(
  1333. {
  1334. archived: false,
  1335. 'members.userId': paramUserId,
  1336. },
  1337. {
  1338. sort: { sort: 1 /* boards default sorting */ },
  1339. },
  1340. ).map(function(board) {
  1341. return {
  1342. _id: board._id,
  1343. title: board.title,
  1344. };
  1345. });
  1346. JsonRoutes.sendResult(res, { code: 200, data });
  1347. } catch (error) {
  1348. JsonRoutes.sendResult(res, {
  1349. code: 200,
  1350. data: error,
  1351. });
  1352. }
  1353. });
  1354. /**
  1355. * @operation get_public_boards
  1356. * @summary Get all public boards
  1357. *
  1358. * @return_type [{_id: string,
  1359. title: string}]
  1360. */
  1361. JsonRoutes.add('GET', '/api/boards', function(req, res) {
  1362. try {
  1363. Authentication.checkUserId(req.userId);
  1364. JsonRoutes.sendResult(res, {
  1365. code: 200,
  1366. data: Boards.find(
  1367. { permission: 'public' },
  1368. {
  1369. sort: { sort: 1 /* boards default sorting */ },
  1370. },
  1371. ).map(function(doc) {
  1372. return {
  1373. _id: doc._id,
  1374. title: doc.title,
  1375. };
  1376. }),
  1377. });
  1378. } catch (error) {
  1379. JsonRoutes.sendResult(res, {
  1380. code: 200,
  1381. data: error,
  1382. });
  1383. }
  1384. });
  1385. /**
  1386. * @operation get_board
  1387. * @summary Get the board with that particular ID
  1388. *
  1389. * @param {string} boardId the ID of the board to retrieve the data
  1390. * @return_type Boards
  1391. */
  1392. JsonRoutes.add('GET', '/api/boards/:boardId', function(req, res) {
  1393. try {
  1394. const id = req.params.boardId;
  1395. Authentication.checkBoardAccess(req.userId, id);
  1396. JsonRoutes.sendResult(res, {
  1397. code: 200,
  1398. data: Boards.findOne({ _id: id }),
  1399. });
  1400. } catch (error) {
  1401. JsonRoutes.sendResult(res, {
  1402. code: 200,
  1403. data: error,
  1404. });
  1405. }
  1406. });
  1407. /**
  1408. * @operation new_board
  1409. * @summary Create a board
  1410. *
  1411. * @description This allows to create a board.
  1412. *
  1413. * The color has to be chosen between `belize`, `nephritis`, `pomegranate`,
  1414. * `pumpkin`, `wisteria`, `moderatepink`, `strongcyan`,
  1415. * `limegreen`, `midnight`, `dark`, `relax`, `corteza`:
  1416. *
  1417. * <img src="https://wekan.github.io/board-colors.png" width="40%" alt="Wekan logo" />
  1418. *
  1419. * @param {string} title the new title of the board
  1420. * @param {string} owner "ABCDE12345" <= User ID in Wekan.
  1421. * (Not username or email)
  1422. * @param {boolean} [isAdmin] is the owner an admin of the board (default true)
  1423. * @param {boolean} [isActive] is the board active (default true)
  1424. * @param {boolean} [isNoComments] disable comments (default false)
  1425. * @param {boolean} [isCommentOnly] only enable comments (default false)
  1426. * @param {boolean} [isWorker] only move cards, assign himself to card and comment (default false)
  1427. * @param {string} [permission] "private" board <== Set to "public" if you
  1428. * want public Wekan board
  1429. * @param {string} [color] the color of the board
  1430. *
  1431. * @return_type {_id: string,
  1432. defaultSwimlaneId: string}
  1433. */
  1434. JsonRoutes.add('POST', '/api/boards', function(req, res) {
  1435. try {
  1436. Authentication.checkUserId(req.userId);
  1437. const id = Boards.insert({
  1438. title: req.body.title,
  1439. members: [
  1440. {
  1441. userId: req.body.owner,
  1442. isAdmin: req.body.isAdmin || true,
  1443. isActive: req.body.isActive || true,
  1444. isNoComments: req.body.isNoComments || false,
  1445. isCommentOnly: req.body.isCommentOnly || false,
  1446. isWorker: req.body.isWorker || false,
  1447. },
  1448. ],
  1449. permission: req.body.permission || 'private',
  1450. color: req.body.color || 'belize',
  1451. });
  1452. const swimlaneId = Swimlanes.insert({
  1453. title: TAPi18n.__('default'),
  1454. boardId: id,
  1455. });
  1456. JsonRoutes.sendResult(res, {
  1457. code: 200,
  1458. data: {
  1459. _id: id,
  1460. defaultSwimlaneId: swimlaneId,
  1461. },
  1462. });
  1463. } catch (error) {
  1464. JsonRoutes.sendResult(res, {
  1465. code: 200,
  1466. data: error,
  1467. });
  1468. }
  1469. });
  1470. /**
  1471. * @operation delete_board
  1472. * @summary Delete a board
  1473. *
  1474. * @param {string} boardId the ID of the board
  1475. */
  1476. JsonRoutes.add('DELETE', '/api/boards/:boardId', function(req, res) {
  1477. try {
  1478. Authentication.checkUserId(req.userId);
  1479. const id = req.params.boardId;
  1480. Boards.remove({ _id: id });
  1481. JsonRoutes.sendResult(res, {
  1482. code: 200,
  1483. data: {
  1484. _id: id,
  1485. },
  1486. });
  1487. } catch (error) {
  1488. JsonRoutes.sendResult(res, {
  1489. code: 200,
  1490. data: error,
  1491. });
  1492. }
  1493. });
  1494. /**
  1495. * @operation add_board_label
  1496. * @summary Add a label to a board
  1497. *
  1498. * @description If the board doesn't have the name/color label, this function
  1499. * adds the label to the board.
  1500. *
  1501. * @param {string} boardId the board
  1502. * @param {string} color the color of the new label
  1503. * @param {string} name the name of the new label
  1504. *
  1505. * @return_type string
  1506. */
  1507. JsonRoutes.add('PUT', '/api/boards/:boardId/labels', function(req, res) {
  1508. Authentication.checkUserId(req.userId);
  1509. const id = req.params.boardId;
  1510. try {
  1511. if (req.body.hasOwnProperty('label')) {
  1512. const board = Boards.findOne({ _id: id });
  1513. const color = req.body.label.color;
  1514. const name = req.body.label.name;
  1515. const labelId = Random.id(6);
  1516. if (!board.getLabel(name, color)) {
  1517. Boards.direct.update(
  1518. { _id: id },
  1519. { $push: { labels: { _id: labelId, name, color } } },
  1520. );
  1521. JsonRoutes.sendResult(res, {
  1522. code: 200,
  1523. data: labelId,
  1524. });
  1525. } else {
  1526. JsonRoutes.sendResult(res, {
  1527. code: 200,
  1528. });
  1529. }
  1530. }
  1531. } catch (error) {
  1532. JsonRoutes.sendResult(res, {
  1533. data: error,
  1534. });
  1535. }
  1536. });
  1537. /**
  1538. * @operation set_board_member_permission
  1539. * @tag Users
  1540. * @summary Change the permission of a member of a board
  1541. *
  1542. * @param {string} boardId the ID of the board that we are changing
  1543. * @param {string} memberId the ID of the user to change permissions
  1544. * @param {boolean} isAdmin admin capability
  1545. * @param {boolean} isNoComments NoComments capability
  1546. * @param {boolean} isCommentOnly CommentsOnly capability
  1547. * @param {boolean} isWorker Worker capability
  1548. */
  1549. JsonRoutes.add('POST', '/api/boards/:boardId/members/:memberId', function(
  1550. req,
  1551. res,
  1552. ) {
  1553. try {
  1554. const boardId = req.params.boardId;
  1555. const memberId = req.params.memberId;
  1556. const { isAdmin, isNoComments, isCommentOnly, isWorker } = req.body;
  1557. Authentication.checkBoardAccess(req.userId, boardId);
  1558. const board = Boards.findOne({ _id: boardId });
  1559. function isTrue(data) {
  1560. try {
  1561. return data.toLowerCase() === 'true';
  1562. } catch (error) {
  1563. return data;
  1564. }
  1565. }
  1566. const query = board.setMemberPermission(
  1567. memberId,
  1568. isTrue(isAdmin),
  1569. isTrue(isNoComments),
  1570. isTrue(isCommentOnly),
  1571. isTrue(isWorker),
  1572. req.userId,
  1573. );
  1574. JsonRoutes.sendResult(res, {
  1575. code: 200,
  1576. data: query,
  1577. });
  1578. } catch (error) {
  1579. JsonRoutes.sendResult(res, {
  1580. code: 200,
  1581. data: error,
  1582. });
  1583. }
  1584. });
  1585. }
  1586. export default Boards;