boards.js 41 KB

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