boards.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import escapeForRegex from 'escape-string-regexp';
  3. import { TAPi18n } from '/imports/i18n';
  4. import {
  5. ALLOWED_BOARD_COLORS,
  6. ALLOWED_COLORS,
  7. TYPE_BOARD,
  8. TYPE_TEMPLATE_BOARD,
  9. TYPE_TEMPLATE_CONTAINER,
  10. } from '/config/const';
  11. import Users from "./users";
  12. // const escapeForRegex = require('escape-string-regexp');
  13. Boards = new Mongo.Collection('boards');
  14. /**
  15. * This is a Board.
  16. */
  17. Boards.attachSchema(
  18. new SimpleSchema({
  19. title: {
  20. /**
  21. * The title of the board
  22. */
  23. type: String,
  24. },
  25. slug: {
  26. /**
  27. * The title slugified.
  28. */
  29. type: String,
  30. // eslint-disable-next-line consistent-return
  31. autoValue() {
  32. // In some cases (Chinese and Japanese for instance) the `getSlug` function
  33. // return an empty string. This is causes bugs in our application so we set
  34. // a default slug in this case.
  35. // Improvment would be to change client URL after slug is changed
  36. const title = this.field('title');
  37. if (title.isSet && !this.isSet) {
  38. let slug = 'board';
  39. slug = getSlug(title.value) || slug;
  40. return slug;
  41. }
  42. },
  43. },
  44. archived: {
  45. /**
  46. * Is the board archived?
  47. */
  48. type: Boolean,
  49. // eslint-disable-next-line consistent-return
  50. autoValue() {
  51. if (this.isInsert && !this.isSet) {
  52. return false;
  53. }
  54. },
  55. },
  56. archivedAt: {
  57. /**
  58. * Latest archiving time of the board
  59. */
  60. type: Date,
  61. optional: true,
  62. },
  63. createdAt: {
  64. /**
  65. * Creation time of the board
  66. */
  67. type: Date,
  68. // eslint-disable-next-line consistent-return
  69. autoValue() {
  70. if (this.isInsert) {
  71. return new Date();
  72. } else if (this.isUpsert) {
  73. return { $setOnInsert: new Date() };
  74. } else {
  75. this.unset();
  76. }
  77. },
  78. },
  79. // XXX Inconsistent field naming
  80. modifiedAt: {
  81. /**
  82. * Last modification time of the board
  83. */
  84. type: Date,
  85. optional: true,
  86. // eslint-disable-next-line consistent-return
  87. autoValue() {
  88. if (this.isInsert || this.isUpsert || this.isUpdate) {
  89. return new Date();
  90. } else {
  91. this.unset();
  92. }
  93. },
  94. },
  95. // De-normalized number of users that have starred this board
  96. stars: {
  97. /**
  98. * How many stars the board has
  99. */
  100. type: Number,
  101. // eslint-disable-next-line consistent-return
  102. autoValue() {
  103. if (this.isInsert) {
  104. return 0;
  105. }
  106. },
  107. },
  108. // De-normalized label system
  109. labels: {
  110. /**
  111. * List of labels attached to a board
  112. */
  113. type: [Object],
  114. optional: true,
  115. /* Commented out, so does not create labels to new boards.
  116. // eslint-disable-next-line consistent-return
  117. autoValue() {
  118. if (this.isInsert && !this.isSet) {
  119. const colors = Boards.simpleSchema()._schema['labels.$.color']
  120. .allowedValues;
  121. const defaultLabelsColors = _.clone(colors).splice(0, 6);
  122. return defaultLabelsColors.map(color => ({
  123. color,
  124. _id: Random.id(6),
  125. name: '',
  126. }));
  127. }
  128. },
  129. */
  130. },
  131. 'labels.$._id': {
  132. /**
  133. * Unique id of a label
  134. */
  135. // We don't specify that this field must be unique in the board because that
  136. // will cause performance penalties and is not necessary since this field is
  137. // always set on the server.
  138. // XXX Actually if we create a new label, the `_id` is set on the client
  139. // without being overwritten by the server, could it be a problem?
  140. type: String,
  141. },
  142. 'labels.$.name': {
  143. /**
  144. * Name of a label
  145. */
  146. type: String,
  147. optional: true,
  148. },
  149. 'labels.$.color': {
  150. /**
  151. * color of a label.
  152. *
  153. * Can be amongst `green`, `yellow`, `orange`, `red`, `purple`,
  154. * `blue`, `sky`, `lime`, `pink`, `black`,
  155. * `silver`, `peachpuff`, `crimson`, `plum`, `darkgreen`,
  156. * `slateblue`, `magenta`, `gold`, `navy`, `gray`,
  157. * `saddlebrown`, `paleturquoise`, `mistyrose`, `indigo`
  158. */
  159. type: String,
  160. allowedValues: ALLOWED_COLORS,
  161. },
  162. // XXX We might want to maintain more informations under the member sub-
  163. // documents like de-normalized meta-data (the date the member joined the
  164. // board, the number of contributions, etc.).
  165. members: {
  166. /**
  167. * List of members of a board
  168. */
  169. type: [Object],
  170. // eslint-disable-next-line consistent-return
  171. autoValue() {
  172. if (this.isInsert && !this.isSet) {
  173. return [
  174. {
  175. userId: this.userId,
  176. isAdmin: true,
  177. isActive: true,
  178. isNoComments: false,
  179. isCommentOnly: false,
  180. isWorker: false,
  181. },
  182. ];
  183. }
  184. },
  185. },
  186. 'members.$.userId': {
  187. /**
  188. * The uniq ID of the member
  189. */
  190. type: String,
  191. },
  192. 'members.$.isAdmin': {
  193. /**
  194. * Is the member an admin of the board?
  195. */
  196. type: Boolean,
  197. },
  198. 'members.$.isActive': {
  199. /**
  200. * Is the member active?
  201. */
  202. type: Boolean,
  203. },
  204. 'members.$.isNoComments': {
  205. /**
  206. * Is the member not allowed to make comments
  207. */
  208. type: Boolean,
  209. optional: true,
  210. },
  211. 'members.$.isCommentOnly': {
  212. /**
  213. * Is the member only allowed to comment on the board
  214. */
  215. type: Boolean,
  216. optional: true,
  217. },
  218. 'members.$.isWorker': {
  219. /**
  220. * Is the member only allowed to move card, assign himself to card and comment
  221. */
  222. type: Boolean,
  223. optional: true,
  224. },
  225. permission: {
  226. /**
  227. * visibility of the board
  228. */
  229. type: String,
  230. allowedValues: ['public', 'private'],
  231. },
  232. orgs: {
  233. /**
  234. * the list of organizations that a board belongs to
  235. */
  236. type: [Object],
  237. optional: true,
  238. },
  239. 'orgs.$.orgId':{
  240. /**
  241. * The uniq ID of the organization
  242. */
  243. type: String,
  244. },
  245. 'orgs.$.orgDisplayName':{
  246. /**
  247. * The display name of the organization
  248. */
  249. type: String,
  250. },
  251. 'orgs.$.isActive': {
  252. /**
  253. * Is the organization active?
  254. */
  255. type: Boolean,
  256. },
  257. teams: {
  258. /**
  259. * the list of teams that a board belongs to
  260. */
  261. type: [Object],
  262. optional: true,
  263. },
  264. 'teams.$.teamId':{
  265. /**
  266. * The uniq ID of the team
  267. */
  268. type: String,
  269. },
  270. 'teams.$.teamDisplayName':{
  271. /**
  272. * The display name of the team
  273. */
  274. type: String,
  275. },
  276. 'teams.$.isActive': {
  277. /**
  278. * Is the team active?
  279. */
  280. type: Boolean,
  281. },
  282. color: {
  283. /**
  284. * The color of the board.
  285. */
  286. type: String,
  287. allowedValues: ALLOWED_BOARD_COLORS,
  288. // eslint-disable-next-line consistent-return
  289. autoValue() {
  290. if (this.isInsert && !this.isSet) {
  291. return ALLOWED_BOARD_COLORS[0];
  292. }
  293. },
  294. },
  295. backgroundImageURL: {
  296. /**
  297. * The background image URL of the board.
  298. */
  299. type: String,
  300. optional: true,
  301. },
  302. allowsCardCounterList: {
  303. /**
  304. * Show card counter per list
  305. */
  306. type: Boolean,
  307. defaultValue: false,
  308. },
  309. allowsBoardMemberList: {
  310. /**
  311. * Show board member list
  312. */
  313. type: Boolean,
  314. defaultValue: false,
  315. },
  316. description: {
  317. /**
  318. * The description of the board
  319. */
  320. type: String,
  321. optional: true,
  322. },
  323. subtasksDefaultBoardId: {
  324. /**
  325. * The default board ID assigned to subtasks.
  326. */
  327. type: String,
  328. optional: true,
  329. defaultValue: null,
  330. },
  331. subtasksDefaultListId: {
  332. /**
  333. * The default List ID assigned to subtasks.
  334. */
  335. type: String,
  336. optional: true,
  337. defaultValue: null,
  338. },
  339. dateSettingsDefaultBoardId: {
  340. type: String,
  341. optional: true,
  342. defaultValue: null,
  343. },
  344. dateSettingsDefaultListId: {
  345. type: String,
  346. optional: true,
  347. defaultValue: null,
  348. },
  349. allowsSubtasks: {
  350. /**
  351. * Does the board allows subtasks?
  352. */
  353. type: Boolean,
  354. defaultValue: true,
  355. },
  356. allowsAttachments: {
  357. /**
  358. * Does the board allows attachments?
  359. */
  360. type: Boolean,
  361. defaultValue: true,
  362. },
  363. allowsChecklists: {
  364. /**
  365. * Does the board allows checklists?
  366. */
  367. type: Boolean,
  368. defaultValue: true,
  369. },
  370. allowsComments: {
  371. /**
  372. * Does the board allows comments?
  373. */
  374. type: Boolean,
  375. defaultValue: true,
  376. },
  377. allowsDescriptionTitle: {
  378. /**
  379. * Does the board allows description title?
  380. */
  381. type: Boolean,
  382. defaultValue: true,
  383. },
  384. allowsDescriptionText: {
  385. /**
  386. * Does the board allows description text?
  387. */
  388. type: Boolean,
  389. defaultValue: true,
  390. },
  391. allowsDescriptionTextOnMinicard: {
  392. /**
  393. * Does the board allows description text on minicard?
  394. */
  395. type: Boolean,
  396. defaultValue: false,
  397. },
  398. allowsCoverAttachmentOnMinicard: {
  399. /**
  400. * Does the board allows cover attachment on minicard?
  401. */
  402. type: Boolean,
  403. defaultValue: false,
  404. },
  405. allowsBadgeAttachmentOnMinicard: {
  406. /**
  407. * Does the board allows badge attachment on minicard?
  408. */
  409. type: Boolean,
  410. defaultValue: false,
  411. },
  412. allowsCardSortingByNumberOnMinicard: {
  413. /**
  414. * Does the board allows card sorting by number on minicard?
  415. */
  416. type: Boolean,
  417. defaultValue: false,
  418. },
  419. allowsCardNumber: {
  420. /**
  421. * Does the board allows card numbers?
  422. */
  423. type: Boolean,
  424. defaultValue: false,
  425. },
  426. allowsActivities: {
  427. /**
  428. * Does the board allows comments?
  429. */
  430. type: Boolean,
  431. defaultValue: true,
  432. },
  433. allowsLabels: {
  434. /**
  435. * Does the board allows labels?
  436. */
  437. type: Boolean,
  438. defaultValue: true,
  439. },
  440. allowsCreator: {
  441. /**
  442. * Does the board allow creator?
  443. */
  444. type: Boolean,
  445. defaultValue: true,
  446. },
  447. allowsCreatorOnMinicard: {
  448. /**
  449. * Does the board allow creator?
  450. */
  451. type: Boolean,
  452. defaultValue: false,
  453. },
  454. allowsAssignee: {
  455. /**
  456. * Does the board allows assignee?
  457. */
  458. type: Boolean,
  459. defaultValue: true,
  460. },
  461. allowsMembers: {
  462. /**
  463. * Does the board allows members?
  464. */
  465. type: Boolean,
  466. defaultValue: true,
  467. },
  468. allowsRequestedBy: {
  469. /**
  470. * Does the board allows requested by?
  471. */
  472. type: Boolean,
  473. defaultValue: true,
  474. },
  475. allowsCardSortingByNumber: {
  476. /**
  477. * Does the board allows card sorting by number?
  478. */
  479. type: Boolean,
  480. defaultValue: true,
  481. },
  482. allowsShowLists: {
  483. /**
  484. * Does the board allows show lists on the card?
  485. */
  486. type: Boolean,
  487. defaultValue: true,
  488. },
  489. allowsAssignedBy: {
  490. /**
  491. * Does the board allows requested by?
  492. */
  493. type: Boolean,
  494. defaultValue: true,
  495. },
  496. allowsReceivedDate: {
  497. /**
  498. * Does the board allows received date?
  499. */
  500. type: Boolean,
  501. defaultValue: true,
  502. },
  503. allowsStartDate: {
  504. /**
  505. * Does the board allows start date?
  506. */
  507. type: Boolean,
  508. defaultValue: true,
  509. },
  510. allowsEndDate: {
  511. /**
  512. * Does the board allows end date?
  513. */
  514. type: Boolean,
  515. defaultValue: true,
  516. },
  517. allowsDueDate: {
  518. /**
  519. * Does the board allows due date?
  520. */
  521. type: Boolean,
  522. defaultValue: true,
  523. },
  524. presentParentTask: {
  525. /**
  526. * Controls how to present the parent task:
  527. *
  528. * - `prefix-with-full-path`: add a prefix with the full path
  529. * - `prefix-with-parent`: add a prefisx with the parent name
  530. * - `subtext-with-full-path`: add a subtext with the full path
  531. * - `subtext-with-parent`: add a subtext with the parent name
  532. * - `no-parent`: does not show the parent at all
  533. */
  534. type: String,
  535. allowedValues: [
  536. 'prefix-with-full-path',
  537. 'prefix-with-parent',
  538. 'subtext-with-full-path',
  539. 'subtext-with-parent',
  540. 'no-parent',
  541. ],
  542. optional: true,
  543. defaultValue: 'no-parent',
  544. },
  545. receivedAt: {
  546. /**
  547. * Date the card was received
  548. */
  549. type: Date,
  550. optional: true,
  551. },
  552. startAt: {
  553. /**
  554. * Starting date of the board.
  555. */
  556. type: Date,
  557. optional: true,
  558. },
  559. dueAt: {
  560. /**
  561. * Due date of the board.
  562. */
  563. type: Date,
  564. optional: true,
  565. },
  566. endAt: {
  567. /**
  568. * End date of the board.
  569. */
  570. type: Date,
  571. optional: true,
  572. },
  573. spentTime: {
  574. /**
  575. * Time spent in the board.
  576. */
  577. type: Number,
  578. decimal: true,
  579. optional: true,
  580. },
  581. isOvertime: {
  582. /**
  583. * Is the board overtimed?
  584. */
  585. type: Boolean,
  586. defaultValue: false,
  587. optional: true,
  588. },
  589. type: {
  590. /**
  591. * The type of board
  592. * possible values: board, template-board, template-container
  593. */
  594. type: String,
  595. defaultValue: TYPE_BOARD,
  596. allowedValues: [TYPE_BOARD, TYPE_TEMPLATE_BOARD, TYPE_TEMPLATE_CONTAINER],
  597. },
  598. sort: {
  599. /**
  600. * Sort value
  601. */
  602. type: Number,
  603. decimal: true,
  604. defaultValue: -1,
  605. },
  606. }),
  607. );
  608. Boards.helpers({
  609. copy() {
  610. const oldId = this._id;
  611. delete this._id;
  612. delete this.slug;
  613. this.title = this.copyTitle();
  614. const _id = Boards.insert(this);
  615. // Copy all swimlanes in board
  616. ReactiveCache.getSwimlanes({
  617. boardId: oldId,
  618. archived: false,
  619. }).forEach(swimlane => {
  620. swimlane.type = 'swimlane';
  621. swimlane.copy(_id);
  622. });
  623. // copy custom field definitions
  624. const cfMap = {};
  625. ReactiveCache.getCustomFields({ boardIds: oldId }).forEach(cf => {
  626. const id = cf._id;
  627. delete cf._id;
  628. cf.boardIds = [_id];
  629. cfMap[id] = CustomFields.insert(cf);
  630. });
  631. ReactiveCache.getCards({ boardId: _id }).forEach(card => {
  632. Cards.update(card._id, {
  633. $set: {
  634. customFields: card.customFields.map(cf => {
  635. cf._id = cfMap[cf._id];
  636. return cf;
  637. }),
  638. },
  639. });
  640. });
  641. // copy rules, actions, and triggers
  642. const actionsMap = {};
  643. ReactiveCache.getActions({ boardId: oldId }).forEach(action => {
  644. const id = action._id;
  645. delete action._id;
  646. action.boardId = _id;
  647. actionsMap[id] = Actions.insert(action);
  648. });
  649. const triggersMap = {};
  650. ReactiveCache.getTriggers({ boardId: oldId }).forEach(trigger => {
  651. const id = trigger._id;
  652. delete trigger._id;
  653. trigger.boardId = _id;
  654. triggersMap[id] = Triggers.insert(trigger);
  655. });
  656. ReactiveCache.getRules({ boardId: oldId }).forEach(rule => {
  657. delete rule._id;
  658. rule.boardId = _id;
  659. rule.actionId = actionsMap[rule.actionId];
  660. rule.triggerId = triggersMap[rule.triggerId];
  661. Rules.insert(rule);
  662. });
  663. return _id;
  664. },
  665. /**
  666. * Return a unique title based on the current title
  667. *
  668. * @returns {string|null}
  669. */
  670. copyTitle() {
  671. return Boards.uniqueTitle(this.title);
  672. },
  673. /**
  674. * Is supplied user authorized to view this board?
  675. */
  676. isVisibleBy(user) {
  677. if (this.isPublic()) {
  678. // public boards are visible to everyone
  679. return true;
  680. } else {
  681. // otherwise you have to be logged-in and active member
  682. return user && this.isActiveMember(user._id);
  683. }
  684. },
  685. /**
  686. * Is the user one of the active members of the board?
  687. *
  688. * @param userId
  689. * @returns {boolean} the member that matches, or undefined/false
  690. */
  691. isActiveMember(userId) {
  692. if (userId) {
  693. return this.members.find(
  694. member => member.userId === userId && member.isActive,
  695. );
  696. } else {
  697. return false;
  698. }
  699. },
  700. isPublic() {
  701. return this.permission === 'public';
  702. },
  703. cards() {
  704. const ret = ReactiveCache.getCards(
  705. { boardId: this._id, archived: false },
  706. { sort: { title: 1 } },
  707. );
  708. return ret;
  709. },
  710. lists() {
  711. return this.draggableLists();
  712. },
  713. newestLists() {
  714. // sorted lists from newest to the oldest, by its creation date or its cards' last modification date
  715. const value = ReactiveCache.getCurrentUser()._getListSortBy();
  716. const sortKey = { starred: -1, [value[0]]: value[1] }; // [["starred",-1],value];
  717. return ReactiveCache.getLists(
  718. {
  719. boardId: this._id,
  720. archived: false,
  721. },
  722. { sort: sortKey },
  723. );
  724. },
  725. draggableLists() {
  726. return ReactiveCache.getLists({ boardId: this._id }, { sort: { sort: 1 } });
  727. },
  728. /** returns the last list
  729. * @returns Document the last list
  730. */
  731. getLastList() {
  732. const ret = ReactiveCache.getList({ boardId: this._id }, { sort: { sort: 'desc' } });
  733. return ret;
  734. },
  735. nullSortLists() {
  736. return ReactiveCache.getLists({
  737. boardId: this._id,
  738. archived: false,
  739. sort: { $eq: null },
  740. });
  741. },
  742. swimlanes() {
  743. return ReactiveCache.getSwimlanes(
  744. { boardId: this._id, archived: false },
  745. { sort: { sort: 1 } },
  746. );
  747. },
  748. nextSwimlane(swimlane) {
  749. return ReactiveCache.getSwimlane(
  750. {
  751. boardId: this._id,
  752. archived: false,
  753. sort: { $gte: swimlane.sort },
  754. _id: { $ne: swimlane._id },
  755. },
  756. {
  757. sort: { sort: 1 },
  758. },
  759. );
  760. },
  761. nullSortSwimlanes() {
  762. return ReactiveCache.getSwimlanes({
  763. boardId: this._id,
  764. archived: false,
  765. sort: { $eq: null },
  766. });
  767. },
  768. hasOvertimeCards() {
  769. const card = ReactiveCache.getCard({
  770. isOvertime: true,
  771. boardId: this._id,
  772. archived: false,
  773. });
  774. return card !== undefined;
  775. },
  776. hasSpentTimeCards() {
  777. const card = ReactiveCache.getCard({
  778. spentTime: { $gt: 0 },
  779. boardId: this._id,
  780. archived: false,
  781. });
  782. return card !== undefined;
  783. },
  784. activities() {
  785. let linkedBoardId = [this._id];
  786. ReactiveCache.getCards({
  787. "type": "cardType-linkedBoard",
  788. "boardId": this._id}
  789. ).forEach(card => {
  790. linkedBoardId.push(card.linkedId);
  791. });
  792. const ret = ReactiveCache.getActivities({ boardId: { $in: linkedBoardId } }, { sort: { createdAt: -1 } });
  793. return ret;
  794. },
  795. activeMembers(){
  796. const members = _.where(this.members, { isActive: true });
  797. return _.sortBy(members, 'username');
  798. },
  799. activeOrgs() {
  800. return _.where(this.orgs, { isActive: true });
  801. },
  802. // hasNotAnyOrg(){
  803. // return this.orgs === undefined || this.orgs.length <= 0;
  804. // },
  805. activeTeams() {
  806. return _.where(this.teams, { isActive: true });
  807. },
  808. // hasNotAnyTeam(){
  809. // return this.teams === undefined || this.teams.length <= 0;
  810. // },
  811. activeAdmins() {
  812. return _.where(this.members, { isActive: true, isAdmin: true });
  813. },
  814. memberUsers() {
  815. return ReactiveCache.getUsers({ _id: { $in: _.pluck(this.members, 'userId') } });
  816. },
  817. getLabel(name, color) {
  818. return _.findWhere(this.labels, { name, color });
  819. },
  820. getLabelById(labelId) {
  821. return _.findWhere(this.labels, { _id: labelId });
  822. },
  823. labelIndex(labelId) {
  824. return _.pluck(this.labels, '_id').indexOf(labelId);
  825. },
  826. memberIndex(memberId) {
  827. return _.pluck(this.members, 'userId').indexOf(memberId);
  828. },
  829. hasMember(memberId) {
  830. return !!_.findWhere(this.members, { userId: memberId, isActive: true });
  831. },
  832. hasAdmin(memberId) {
  833. return !!_.findWhere(this.members, {
  834. userId: memberId,
  835. isActive: true,
  836. isAdmin: true,
  837. });
  838. },
  839. hasNoComments(memberId) {
  840. return !!_.findWhere(this.members, {
  841. userId: memberId,
  842. isActive: true,
  843. isAdmin: false,
  844. isNoComments: true,
  845. isWorker: false,
  846. });
  847. },
  848. hasCommentOnly(memberId) {
  849. return !!_.findWhere(this.members, {
  850. userId: memberId,
  851. isActive: true,
  852. isAdmin: false,
  853. isCommentOnly: true,
  854. isWorker: false,
  855. });
  856. },
  857. hasWorker(memberId) {
  858. return !!_.findWhere(this.members, {
  859. userId: memberId,
  860. isActive: true,
  861. isAdmin: false,
  862. isCommentOnly: false,
  863. isWorker: true,
  864. });
  865. },
  866. hasAnyAllowsDate() {
  867. const ret = this.allowsReceivedDate || this.allowsStartDate || this.allowsDueDate || this.allowsEndDate;
  868. return ret;
  869. },
  870. hasAnyAllowsUser() {
  871. const ret = this.allowsCreator || this.allowsMembers || this.allowsAssignee || this.allowsRequestedBy || this.allowsAssignedBy;
  872. return ret;
  873. },
  874. absoluteUrl() {
  875. return FlowRouter.url('board', { id: this._id, slug: this.slug });
  876. },
  877. originRelativeUrl() {
  878. return FlowRouter.path('board', { id: this._id, slug: this.slug });
  879. },
  880. colorClass() {
  881. return `board-color-${this.color}`;
  882. },
  883. customFields() {
  884. const ret = ReactiveCache.getCustomFields(
  885. { boardIds: { $in: [this._id] } },
  886. { sort: { name: 1 } },
  887. );
  888. return ret;
  889. },
  890. // XXX currently mutations return no value so we have an issue when using addLabel in import
  891. // XXX waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  892. pushLabel(name, color) {
  893. const _id = Random.id(6);
  894. Boards.direct.update(this._id, { $push: { labels: { _id, name, color } } });
  895. return _id;
  896. },
  897. /** sets the new label order
  898. * @param newLabelOrderOnlyIds new order array of _id, e.g. Array(4) [ "FvtD34", "PAEgDP", "LjRBxH", "YJ8sZz" ]
  899. */
  900. setNewLabelOrder(newLabelOrderOnlyIds) {
  901. if (this.labels.length == newLabelOrderOnlyIds.length) {
  902. if (this.labels.every(_label => newLabelOrderOnlyIds.indexOf(_label._id) >= 0)) {
  903. const newLabels = _.sortBy(this.labels, _label => newLabelOrderOnlyIds.indexOf(_label._id));
  904. if (this.labels.length == newLabels.length) {
  905. Boards.direct.update(this._id, {$set: {labels: newLabels}});
  906. }
  907. }
  908. }
  909. },
  910. searchBoards(term) {
  911. check(term, Match.OneOf(String, null, undefined));
  912. const query = { boardId: this._id };
  913. query.type = 'cardType-linkedBoard';
  914. query.archived = false;
  915. const projection = { limit: 10, sort: { createdAt: -1 } };
  916. if (term) {
  917. const regex = new RegExp(term, 'i');
  918. query.$or = [{ title: regex }, { description: regex }];
  919. }
  920. const ret = ReactiveCache.getCards(query, projection);
  921. return ret;
  922. },
  923. searchSwimlanes(term) {
  924. check(term, Match.OneOf(String, null, undefined));
  925. const query = { boardId: this._id };
  926. if (this.isTemplatesBoard()) {
  927. query.type = 'template-swimlane';
  928. query.archived = false;
  929. } else {
  930. query.type = { $nin: ['template-swimlane'] };
  931. }
  932. const projection = { limit: 10, sort: { createdAt: -1 } };
  933. if (term) {
  934. const regex = new RegExp(term, 'i');
  935. query.$or = [{ title: regex }, { description: regex }];
  936. }
  937. return ReactiveCache.getSwimlanes(query, projection);
  938. },
  939. searchLists(term) {
  940. let ret = null;
  941. if (term) {
  942. check(term, Match.OneOf(String));
  943. term = term.trim();
  944. }
  945. if (term) {
  946. const query = { boardId: this._id };
  947. if (this.isTemplatesBoard()) {
  948. query.type = 'template-list';
  949. query.archived = false;
  950. } else {
  951. query.type = { $nin: ['template-list'] };
  952. }
  953. const projection = { sort: { createdAt: -1 } };
  954. if (term) {
  955. const regex = new RegExp(term, 'i');
  956. query.$or = [{ title: regex }, { description: regex }];
  957. }
  958. ret = ReactiveCache.getLists(query, projection);
  959. }
  960. return ret;
  961. },
  962. searchCards(term, excludeLinked) {
  963. let ret = null;
  964. if (term) {
  965. check(term, Match.OneOf(String));
  966. term = term.trim();
  967. }
  968. if (term) {
  969. const query = { boardId: this._id };
  970. if (excludeLinked) {
  971. query.linkedId = null;
  972. }
  973. if (this.isTemplatesBoard()) {
  974. query.type = 'template-card';
  975. query.archived = false;
  976. } else {
  977. query.type = { $nin: ['template-card'] };
  978. }
  979. const projection = { sort: { createdAt: -1 } };
  980. const regex = new RegExp(term, 'i');
  981. query.$or = [
  982. { title: regex },
  983. { description: regex },
  984. { customFields: { $elemMatch: { value: regex } } },
  985. ];
  986. ret = ReactiveCache.getCards(query, projection);
  987. }
  988. return ret;
  989. },
  990. // A board alwasy has another board where it deposits subtasks of thasks
  991. // that belong to itself.
  992. getDefaultSubtasksBoardId() {
  993. if (
  994. this.subtasksDefaultBoardId === null ||
  995. this.subtasksDefaultBoardId === undefined
  996. ) {
  997. this.subtasksDefaultBoardId = Boards.insert({
  998. title: `^${this.title}^`,
  999. permission: this.permission,
  1000. members: this.members,
  1001. color: this.color,
  1002. description: TAPi18n.__('default-subtasks-board', {
  1003. board: this.title,
  1004. }),
  1005. });
  1006. Swimlanes.insert({
  1007. title: TAPi18n.__('default'),
  1008. boardId: this.subtasksDefaultBoardId,
  1009. });
  1010. Boards.update(this._id, {
  1011. $set: {
  1012. subtasksDefaultBoardId: this.subtasksDefaultBoardId,
  1013. },
  1014. });
  1015. }
  1016. return this.subtasksDefaultBoardId;
  1017. },
  1018. getDefaultSubtasksBoard() {
  1019. return ReactiveCache.getBoard(this.getDefaultSubtasksBoardId());
  1020. },
  1021. //Date Settings option such as received date, start date and so on.
  1022. getDefaultDateSettingsBoardId() {
  1023. if (
  1024. this.dateSettingsDefaultBoardId === null ||
  1025. this.dateSettingsDefaultBoardId === undefined
  1026. ) {
  1027. this.dateSettingsDefaultBoardId = Boards.insert({
  1028. title: `^${this.title}^`,
  1029. permission: this.permission,
  1030. members: this.members,
  1031. color: this.color,
  1032. description: TAPi18n.__('default-dates-board', {
  1033. board: this.title,
  1034. }),
  1035. });
  1036. Swimlanes.insert({
  1037. title: TAPi18n.__('default'),
  1038. boardId: this.dateSettingsDefaultBoardId,
  1039. });
  1040. Boards.update(this._id, {
  1041. $set: {
  1042. dateSettingsDefaultBoardId: this.dateSettingsDefaultBoardId,
  1043. },
  1044. });
  1045. }
  1046. return this.dateSettingsDefaultBoardId;
  1047. },
  1048. getDefaultDateSettingsBoard() {
  1049. return ReactiveCache.getBoard(this.getDefaultDateSettingsBoardId());
  1050. },
  1051. getDefaultSubtasksListId() {
  1052. if (
  1053. this.subtasksDefaultListId === null ||
  1054. this.subtasksDefaultListId === undefined
  1055. ) {
  1056. this.subtasksDefaultListId = Lists.insert({
  1057. title: TAPi18n.__('queue'),
  1058. boardId: this._id,
  1059. });
  1060. this.setSubtasksDefaultListId(this.subtasksDefaultListId);
  1061. }
  1062. return this.subtasksDefaultListId;
  1063. },
  1064. getDefaultSubtasksList() {
  1065. return ReactiveCache.getList(this.getDefaultSubtasksListId());
  1066. },
  1067. getDefaultDateSettingsListId() {
  1068. if (
  1069. this.dateSettingsDefaultListId === null ||
  1070. this.dateSettingsDefaultListId === undefined
  1071. ) {
  1072. this.dateSettingsDefaultListId = Lists.insert({
  1073. title: TAPi18n.__('queue'),
  1074. boardId: this._id,
  1075. });
  1076. this.setDateSettingsDefaultListId(this.dateSettingsDefaultListId);
  1077. }
  1078. return this.dateSettingsDefaultListId;
  1079. },
  1080. getDefaultDateSettingsList() {
  1081. return ReactiveCache.getList(this.getDefaultDateSettingsListId());
  1082. },
  1083. getDefaultSwimline() {
  1084. let result = ReactiveCache.getSwimlane({ boardId: this._id });
  1085. if (result === undefined) {
  1086. Swimlanes.insert({
  1087. title: TAPi18n.__('default'),
  1088. boardId: this._id,
  1089. });
  1090. result = ReactiveCache.getSwimlane({ boardId: this._id });
  1091. }
  1092. return result;
  1093. },
  1094. getNextCardNumber() {
  1095. const boardCards = ReactiveCache.getCard(
  1096. {
  1097. boardId: this._id
  1098. },
  1099. {
  1100. sort: { cardNumber: -1 },
  1101. limit: 1
  1102. }
  1103. , true);
  1104. // If no card is assigned to the board, return 1
  1105. if (!boardCards) {
  1106. return 1;
  1107. }
  1108. const maxCardNr = !!boardCards.cardNumber ? boardCards.cardNumber : 0;
  1109. return maxCardNr + 1;
  1110. },
  1111. cardsDueInBetween(start, end) {
  1112. const ret = ReactiveCache.getCards({
  1113. boardId: this._id,
  1114. dueAt: { $gte: start, $lte: end },
  1115. });
  1116. return ret;
  1117. },
  1118. cardsInInterval(start, end) {
  1119. const ret = ReactiveCache.getCards({
  1120. boardId: this._id,
  1121. $or: [
  1122. {
  1123. startAt: {
  1124. $lte: start,
  1125. },
  1126. endAt: {
  1127. $gte: start,
  1128. },
  1129. },
  1130. {
  1131. startAt: {
  1132. $lte: end,
  1133. },
  1134. endAt: {
  1135. $gte: end,
  1136. },
  1137. },
  1138. {
  1139. startAt: {
  1140. $gte: start,
  1141. },
  1142. endAt: {
  1143. $lte: end,
  1144. },
  1145. },
  1146. ],
  1147. });
  1148. return ret;
  1149. },
  1150. isTemplateBoard() {
  1151. return this.type === 'template-board';
  1152. },
  1153. isTemplatesBoard() {
  1154. return this.type === 'template-container';
  1155. },
  1156. });
  1157. Boards.mutations({
  1158. archive() {
  1159. return { $set: { archived: true, archivedAt: new Date() } };
  1160. },
  1161. restore() {
  1162. return { $set: { archived: false } };
  1163. },
  1164. rename(title) {
  1165. return { $set: { title } };
  1166. },
  1167. setDescription(description) {
  1168. return { $set: { description } };
  1169. },
  1170. setColor(color) {
  1171. return { $set: { color } };
  1172. },
  1173. setBackgroundImageURL(backgroundImageURL) {
  1174. const currentUser = ReactiveCache.getCurrentUser();
  1175. if(currentUser.isBoardAdmin()) {
  1176. return { $set: { backgroundImageURL } };
  1177. } else if (currentUser.isAdmin()) {
  1178. return { $set: { backgroundImageURL } };
  1179. } else {
  1180. return false;
  1181. }
  1182. },
  1183. setVisibility(visibility) {
  1184. return { $set: { permission: visibility } };
  1185. },
  1186. addLabel(name, color) {
  1187. // If label with the same name and color already exists we don't want to
  1188. // create another one because they would be indistinguishable in the UI
  1189. // (they would still have different `_id` but that is not exposed to the
  1190. // user).
  1191. if (!this.getLabel(name, color)) {
  1192. const _id = Random.id(6);
  1193. return { $push: { labels: { _id, name, color } } };
  1194. }
  1195. return {};
  1196. },
  1197. editLabel(labelId, name, color) {
  1198. if (!this.getLabel(name, color)) {
  1199. const labelIndex = this.labelIndex(labelId);
  1200. return {
  1201. $set: {
  1202. [`labels.${labelIndex}.name`]: name,
  1203. [`labels.${labelIndex}.color`]: color,
  1204. },
  1205. };
  1206. }
  1207. return {};
  1208. },
  1209. removeLabel(labelId) {
  1210. return { $pull: { labels: { _id: labelId } } };
  1211. },
  1212. changeOwnership(fromId, toId) {
  1213. const memberIndex = this.memberIndex(fromId);
  1214. return {
  1215. $set: {
  1216. [`members.${memberIndex}.userId`]: toId,
  1217. },
  1218. };
  1219. },
  1220. addMember(memberId) {
  1221. const memberIndex = this.memberIndex(memberId);
  1222. if (memberIndex >= 0) {
  1223. return {
  1224. $set: {
  1225. [`members.${memberIndex}.isActive`]: true,
  1226. },
  1227. };
  1228. }
  1229. return {
  1230. $push: {
  1231. members: {
  1232. userId: memberId,
  1233. isAdmin: false,
  1234. isActive: true,
  1235. isNoComments: false,
  1236. isCommentOnly: false,
  1237. isWorker: false,
  1238. },
  1239. },
  1240. };
  1241. },
  1242. removeMember(memberId) {
  1243. const memberIndex = this.memberIndex(memberId);
  1244. // we do not allow the only one admin to be removed
  1245. const allowRemove =
  1246. !this.members[memberIndex].isAdmin || this.activeAdmins().length > 1;
  1247. if (!allowRemove) {
  1248. return {
  1249. $set: {
  1250. [`members.${memberIndex}.isActive`]: true,
  1251. },
  1252. };
  1253. }
  1254. return {
  1255. $set: {
  1256. [`members.${memberIndex}.isActive`]: false,
  1257. [`members.${memberIndex}.isAdmin`]: false,
  1258. },
  1259. };
  1260. },
  1261. setMemberPermission(
  1262. memberId,
  1263. isAdmin,
  1264. isNoComments,
  1265. isCommentOnly,
  1266. isWorker,
  1267. currentUserId = Meteor.userId(),
  1268. ) {
  1269. const memberIndex = this.memberIndex(memberId);
  1270. // do not allow change permission of self
  1271. if (memberId === currentUserId) {
  1272. isAdmin = this.members[memberIndex].isAdmin;
  1273. }
  1274. return {
  1275. $set: {
  1276. [`members.${memberIndex}.isAdmin`]: isAdmin,
  1277. [`members.${memberIndex}.isNoComments`]: isNoComments,
  1278. [`members.${memberIndex}.isCommentOnly`]: isCommentOnly,
  1279. [`members.${memberIndex}.isWorker`]: isWorker,
  1280. },
  1281. };
  1282. },
  1283. setAllowsSubtasks(allowsSubtasks) {
  1284. return { $set: { allowsSubtasks } };
  1285. },
  1286. setAllowsCreator(allowsCreator) {
  1287. return { $set: { allowsCreator } };
  1288. },
  1289. setAllowsCreatorOnMinicard(allowsCreatorOnMinicard) {
  1290. return { $set: { allowsCreatorOnMinicard } };
  1291. },
  1292. setAllowsMembers(allowsMembers) {
  1293. return { $set: { allowsMembers } };
  1294. },
  1295. setAllowsChecklists(allowsChecklists) {
  1296. return { $set: { allowsChecklists } };
  1297. },
  1298. setAllowsAssignee(allowsAssignee) {
  1299. return { $set: { allowsAssignee } };
  1300. },
  1301. setAllowsAssignedBy(allowsAssignedBy) {
  1302. return { $set: { allowsAssignedBy } };
  1303. },
  1304. setAllowsRequestedBy(allowsRequestedBy) {
  1305. return { $set: { allowsRequestedBy } };
  1306. },
  1307. setAllowsCardSortingByNumber(allowsCardSortingByNumber) {
  1308. return { $set: { allowsCardSortingByNumber } };
  1309. },
  1310. setAllowsShowLists(allowsShowLists) {
  1311. return { $set: { allowsShowLists } };
  1312. },
  1313. setAllowsAttachments(allowsAttachments) {
  1314. return { $set: { allowsAttachments } };
  1315. },
  1316. setAllowsLabels(allowsLabels) {
  1317. return { $set: { allowsLabels } };
  1318. },
  1319. setAllowsComments(allowsComments) {
  1320. return { $set: { allowsComments } };
  1321. },
  1322. setAllowsDescriptionTitle(allowsDescriptionTitle) {
  1323. return { $set: { allowsDescriptionTitle } };
  1324. },
  1325. setAllowsCardNumber(allowsCardNumber) {
  1326. return { $set: { allowsCardNumber } };
  1327. },
  1328. setAllowsDescriptionText(allowsDescriptionText) {
  1329. return { $set: { allowsDescriptionText } };
  1330. },
  1331. setallowsDescriptionTextOnMinicard(allowsDescriptionTextOnMinicard) {
  1332. return { $set: { allowsDescriptionTextOnMinicard } };
  1333. },
  1334. setallowsCoverAttachmentOnMinicard(allowsCoverAttachmentOnMinicard) {
  1335. return { $set: { allowsCoverAttachmentOnMinicard } };
  1336. },
  1337. setallowsBadgeAttachmentOnMinicard(allowsBadgeAttachmentOnMinicard) {
  1338. return { $set: { allowsBadgeAttachmentOnMinicard } };
  1339. },
  1340. setallowsCardSortingByNumberOnMinicard(allowsCardSortingByNumberOnMinicard) {
  1341. return { $set: { allowsCardSortingByNumberOnMinicard } };
  1342. },
  1343. setAllowsActivities(allowsActivities) {
  1344. return { $set: { allowsActivities } };
  1345. },
  1346. setAllowsReceivedDate(allowsReceivedDate) {
  1347. return { $set: { allowsReceivedDate } };
  1348. },
  1349. setAllowsCardCounterList(allowsCardCounterList) {
  1350. return { $set: { allowsCardCounterList } };
  1351. },
  1352. setAllowsBoardMemberList(allowsBoardMemberList) {
  1353. return { $set: { allowsBoardMemberList } };
  1354. },
  1355. setAllowsStartDate(allowsStartDate) {
  1356. return { $set: { allowsStartDate } };
  1357. },
  1358. setAllowsEndDate(allowsEndDate) {
  1359. return { $set: { allowsEndDate } };
  1360. },
  1361. setAllowsDueDate(allowsDueDate) {
  1362. return { $set: { allowsDueDate } };
  1363. },
  1364. setSubtasksDefaultBoardId(subtasksDefaultBoardId) {
  1365. return { $set: { subtasksDefaultBoardId } };
  1366. },
  1367. setSubtasksDefaultListId(subtasksDefaultListId) {
  1368. return { $set: { subtasksDefaultListId } };
  1369. },
  1370. setPresentParentTask(presentParentTask) {
  1371. return { $set: { presentParentTask } };
  1372. },
  1373. move(sortIndex) {
  1374. return { $set: { sort: sortIndex } };
  1375. },
  1376. });
  1377. function boardRemover(userId, doc) {
  1378. [Cards, Lists, Swimlanes, Integrations, Rules, Activities].forEach(
  1379. element => {
  1380. element.remove({ boardId: doc._id });
  1381. },
  1382. );
  1383. }
  1384. Boards.uniqueTitle = title => {
  1385. const m = title.match(
  1386. new RegExp('^(?<title>.*?)\\s*(\\[(?<num>\\d+)]\\s*$|\\s*$)'),
  1387. );
  1388. const base = escapeForRegex(m.groups.title);
  1389. const baseTitle = m.groups.title;
  1390. boards = ReactiveCache.getBoards({ title: new RegExp(`^${base}\\s*(\\[(?<num>\\d+)]\\s*$|\\s*$)`) });
  1391. if (boards.length > 0) {
  1392. let num = 0;
  1393. ReactiveCache.getBoards({ title: new RegExp(`^${base}\\s*\\[\\d+]\\s*$`) }).forEach(
  1394. board => {
  1395. const m = board.title.match(
  1396. new RegExp('^(?<title>.*?)\\s*\\[(?<num>\\d+)]\\s*$'),
  1397. );
  1398. if (m) {
  1399. const n = parseInt(m.groups.num, 10);
  1400. num = num < n ? n : num;
  1401. }
  1402. },
  1403. );
  1404. return `${baseTitle} [${num + 1}]`;
  1405. }
  1406. return title;
  1407. };
  1408. Boards.userSearch = (
  1409. userId,
  1410. selector = {},
  1411. projection = {},
  1412. // includeArchived = false,
  1413. ) => {
  1414. // if (!includeArchived) {
  1415. // selector.archived = false;
  1416. // }
  1417. selector.$or = [{ permission: 'public' }];
  1418. if (userId) {
  1419. selector.$or.push({ members: { $elemMatch: { userId, isActive: true } } });
  1420. }
  1421. const ret = ReactiveCache.getBoards(selector, projection);
  1422. return ret;
  1423. };
  1424. Boards.userBoards = (
  1425. userId,
  1426. archived = false,
  1427. selector = {},
  1428. projection = {},
  1429. ) => {
  1430. const user = ReactiveCache.getUser(userId);
  1431. if (!user) {
  1432. return [];
  1433. }
  1434. if (typeof archived === 'boolean') {
  1435. selector.archived = archived;
  1436. }
  1437. if (!selector.type) {
  1438. selector.type = 'board';
  1439. }
  1440. selector.$or = [
  1441. { permission: 'public' },
  1442. { members: { $elemMatch: { userId, isActive: true } } },
  1443. { orgs: { $elemMatch: { orgId: { $in: user.orgIds() }, isActive: true } } },
  1444. { teams: { $elemMatch: { teamId: { $in: user.teamIds() }, isActive: true } } },
  1445. ];
  1446. return ReactiveCache.getBoards(selector, projection);
  1447. };
  1448. Boards.userBoardIds = (userId, archived = false, selector = {}) => {
  1449. return Boards.userBoards(userId, archived, selector, {
  1450. fields: { _id: 1 },
  1451. }).map(board => {
  1452. return board._id;
  1453. });
  1454. };
  1455. Boards.colorMap = () => {
  1456. const colors = {};
  1457. for (const color of Boards.labelColors()) {
  1458. colors[TAPi18n.__(`color-${color}`)] = color;
  1459. }
  1460. return colors;
  1461. };
  1462. Boards.labelColors = () => {
  1463. return ALLOWED_COLORS;
  1464. };
  1465. if (Meteor.isServer) {
  1466. Boards.allow({
  1467. insert: Meteor.userId,
  1468. update: allowIsBoardAdmin,
  1469. remove: allowIsBoardAdmin,
  1470. fetch: ['members'],
  1471. });
  1472. // All logged in users are allowed to reorder boards by dragging at All Boards page and Public Boards page.
  1473. Boards.allow({
  1474. update(userId, board, fieldNames) {
  1475. return _.contains(fieldNames, 'sort');
  1476. },
  1477. fetch: [],
  1478. });
  1479. // The number of users that have starred this board is managed by trusted code
  1480. // and the user is not allowed to update it
  1481. Boards.deny({
  1482. update(userId, board, fieldNames) {
  1483. return _.contains(fieldNames, 'stars');
  1484. },
  1485. fetch: [],
  1486. });
  1487. // We can't remove a member if it is the last administrator
  1488. Boards.deny({
  1489. update(userId, doc, fieldNames, modifier) {
  1490. if (!_.contains(fieldNames, 'members')) return false;
  1491. // We only care in case of a $pull operation, ie remove a member
  1492. if (!_.isObject(modifier.$pull && modifier.$pull.members)) return false;
  1493. // If there is more than one admin, it's ok to remove anyone
  1494. const nbAdmins = _.where(doc.members, { isActive: true, isAdmin: true })
  1495. .length;
  1496. if (nbAdmins > 1) return false;
  1497. // If all the previous conditions were verified, we can't remove
  1498. // a user if it's an admin
  1499. const removedMemberId = modifier.$pull.members.userId;
  1500. return Boolean(
  1501. _.findWhere(doc.members, {
  1502. userId: removedMemberId,
  1503. isAdmin: true,
  1504. }),
  1505. );
  1506. },
  1507. fetch: ['members'],
  1508. });
  1509. Meteor.methods({
  1510. getBackgroundImageURL(boardId) {
  1511. check(boardId, String);
  1512. return ReactiveCache.getBoard(boardId, {}, { backgroundImageUrl: 1 });
  1513. },
  1514. quitBoard(boardId) {
  1515. check(boardId, String);
  1516. const board = ReactiveCache.getBoard(boardId);
  1517. if (board) {
  1518. const userId = Meteor.userId();
  1519. const index = board.memberIndex(userId);
  1520. if (index >= 0) {
  1521. board.removeMember(userId);
  1522. return true;
  1523. } else throw new Meteor.Error('error-board-notAMember');
  1524. } else throw new Meteor.Error('error-board-doesNotExist');
  1525. },
  1526. acceptInvite(boardId) {
  1527. check(boardId, String);
  1528. const board = ReactiveCache.getBoard(boardId);
  1529. if (!board) {
  1530. throw new Meteor.Error('error-board-doesNotExist');
  1531. }
  1532. Meteor.users.update(Meteor.userId(), {
  1533. $pull: {
  1534. 'profile.invitedBoards': boardId,
  1535. },
  1536. });
  1537. },
  1538. myLabelNames() {
  1539. let names = [];
  1540. Boards.userBoards(Meteor.userId()).forEach(board => {
  1541. // Only return labels when they exist.
  1542. if (board.labels !== undefined) {
  1543. names = names.concat(
  1544. board.labels
  1545. .filter(label => !!label.name)
  1546. .map(label => {
  1547. return label.name;
  1548. }),
  1549. );
  1550. } else {
  1551. return [];
  1552. }
  1553. });
  1554. return _.uniq(names).sort();
  1555. },
  1556. myBoardNames() {
  1557. return _.uniq(
  1558. Boards.userBoards(Meteor.userId()).map(board => {
  1559. return board.title;
  1560. }),
  1561. ).sort();
  1562. },
  1563. });
  1564. Meteor.methods({
  1565. archiveBoard(boardId) {
  1566. check(boardId, String);
  1567. const board = ReactiveCache.getBoard(boardId);
  1568. if (board) {
  1569. const userId = Meteor.userId();
  1570. const index = board.memberIndex(userId);
  1571. if (index >= 0) {
  1572. board.archive();
  1573. return true;
  1574. } else throw new Meteor.Error('error-board-notAMember');
  1575. } else throw new Meteor.Error('error-board-doesNotExist');
  1576. },
  1577. setBoardOrgs(boardOrgsArray, currBoardId){
  1578. check(boardOrgsArray, Array);
  1579. check(currBoardId, String);
  1580. Boards.update(currBoardId, {
  1581. $set: {
  1582. orgs: boardOrgsArray,
  1583. },
  1584. });
  1585. },
  1586. setBoardTeams(boardTeamsArray, membersArray, currBoardId){
  1587. check(boardTeamsArray, Array);
  1588. check(membersArray, Array);
  1589. check(currBoardId, String);
  1590. Boards.update(currBoardId, {
  1591. $set: {
  1592. members: membersArray,
  1593. teams: boardTeamsArray,
  1594. },
  1595. });
  1596. },
  1597. });
  1598. }
  1599. // Insert new board at last position in sort order.
  1600. Boards.before.insert((userId, doc) => {
  1601. const lastBoard = ReactiveCache.getBoard(
  1602. { sort: { $exists: true } },
  1603. { sort: { sort: -1 } },
  1604. );
  1605. if (lastBoard && typeof lastBoard.sort !== 'undefined') {
  1606. doc.sort = lastBoard.sort + 1;
  1607. }
  1608. });
  1609. if (Meteor.isServer) {
  1610. // Let MongoDB ensure that a member is not included twice in the same board
  1611. Meteor.startup(() => {
  1612. Boards._collection.createIndex({ modifiedAt: -1 });
  1613. Boards._collection.createIndex(
  1614. {
  1615. _id: 1,
  1616. 'members.userId': 1,
  1617. },
  1618. { unique: true },
  1619. );
  1620. Boards._collection.createIndex({ 'members.userId': 1 });
  1621. });
  1622. // Genesis: the first activity of the newly created board
  1623. Boards.after.insert((userId, doc) => {
  1624. Activities.insert({
  1625. userId,
  1626. type: 'board',
  1627. activityTypeId: doc._id,
  1628. activityType: 'createBoard',
  1629. boardId: doc._id,
  1630. });
  1631. });
  1632. // If the user remove one label from a board, we cant to remove reference of
  1633. // this label in any card of this board.
  1634. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1635. if (
  1636. !_.contains(fieldNames, 'labels') ||
  1637. !modifier.$pull ||
  1638. !modifier.$pull.labels ||
  1639. !modifier.$pull.labels._id
  1640. ) {
  1641. return;
  1642. }
  1643. const removedLabelId = modifier.$pull.labels._id;
  1644. Cards.update(
  1645. { boardId: doc._id },
  1646. {
  1647. $pull: {
  1648. labelIds: removedLabelId,
  1649. },
  1650. },
  1651. { multi: true },
  1652. );
  1653. });
  1654. const foreachRemovedMember = (doc, modifier, callback) => {
  1655. Object.keys(modifier).forEach(set => {
  1656. if (modifier[set] !== false) {
  1657. return;
  1658. }
  1659. const parts = set.split('.');
  1660. if (
  1661. parts.length === 3 &&
  1662. parts[0] === 'members' &&
  1663. parts[2] === 'isActive'
  1664. ) {
  1665. callback(doc.members[parts[1]].userId);
  1666. }
  1667. });
  1668. };
  1669. // Remove a member from all objects of the board before leaving the board
  1670. Boards.before.update((userId, doc, fieldNames, modifier) => {
  1671. if (!_.contains(fieldNames, 'members')) {
  1672. return;
  1673. }
  1674. if (modifier.$set) {
  1675. const boardId = doc._id;
  1676. foreachRemovedMember(doc, modifier.$set, memberId => {
  1677. Cards.update(
  1678. { boardId },
  1679. {
  1680. $pull: {
  1681. members: memberId,
  1682. watchers: memberId,
  1683. },
  1684. },
  1685. { multi: true },
  1686. );
  1687. Lists.update(
  1688. { boardId },
  1689. {
  1690. $pull: {
  1691. watchers: memberId,
  1692. },
  1693. },
  1694. { multi: true },
  1695. );
  1696. const board = Boards._transform(doc);
  1697. board.setWatcher(memberId, false);
  1698. // Remove board from users starred list
  1699. if (!board.isPublic()) {
  1700. Users.update(memberId, {
  1701. $pull: {
  1702. 'profile.starredBoards': boardId,
  1703. },
  1704. });
  1705. }
  1706. });
  1707. }
  1708. });
  1709. Boards.before.remove((userId, doc) => {
  1710. boardRemover(userId, doc);
  1711. // Add removeBoard activity to keep it
  1712. Activities.insert({
  1713. userId,
  1714. type: 'board',
  1715. activityTypeId: doc._id,
  1716. activityType: 'removeBoard',
  1717. boardId: doc._id,
  1718. });
  1719. });
  1720. // Add a new activity if we add or remove a member to the board
  1721. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1722. if (fieldNames.includes('title')) {
  1723. Activities.insert({
  1724. userId,
  1725. type: 'board',
  1726. activityType: 'changedBoardTitle',
  1727. boardId: doc._id,
  1728. // this preserves the name so that the activity can be useful after the
  1729. // list is deleted
  1730. title: doc.title,
  1731. });
  1732. }
  1733. if (!_.contains(fieldNames, 'members')) {
  1734. return;
  1735. }
  1736. // Say hello to the new member
  1737. if (modifier.$push && modifier.$push.members) {
  1738. const memberId = modifier.$push.members.userId;
  1739. Activities.insert({
  1740. userId,
  1741. memberId,
  1742. type: 'member',
  1743. activityType: 'addBoardMember',
  1744. boardId: doc._id,
  1745. });
  1746. }
  1747. // Say goodbye to the former member
  1748. if (modifier.$set) {
  1749. foreachRemovedMember(doc, modifier.$set, memberId => {
  1750. Activities.insert({
  1751. userId,
  1752. memberId,
  1753. type: 'member',
  1754. activityType: 'removeBoardMember',
  1755. boardId: doc._id,
  1756. });
  1757. });
  1758. }
  1759. });
  1760. }
  1761. //BOARDS REST API
  1762. if (Meteor.isServer) {
  1763. /**
  1764. * @operation get_boards_from_user
  1765. * @summary Get all boards attached to a user
  1766. *
  1767. * @param {string} userId the ID of the user to retrieve the data
  1768. * @return_type [{_id: string,
  1769. title: string}]
  1770. */
  1771. JsonRoutes.add('GET', '/api/users/:userId/boards', function(req, res) {
  1772. try {
  1773. Authentication.checkLoggedIn(req.userId);
  1774. const paramUserId = req.params.userId;
  1775. // A normal user should be able to see their own boards,
  1776. // admins can access boards of any user
  1777. Authentication.checkAdminOrCondition(
  1778. req.userId,
  1779. req.userId === paramUserId,
  1780. );
  1781. const data = ReactiveCache.getBoards(
  1782. {
  1783. archived: false,
  1784. 'members.userId': paramUserId,
  1785. },
  1786. {
  1787. sort: { sort: 1 /* boards default sorting */ },
  1788. },
  1789. ).map(function(board) {
  1790. return {
  1791. _id: board._id,
  1792. title: board.title,
  1793. };
  1794. });
  1795. JsonRoutes.sendResult(res, { code: 200, data });
  1796. } catch (error) {
  1797. JsonRoutes.sendResult(res, {
  1798. code: 200,
  1799. data: error,
  1800. });
  1801. }
  1802. });
  1803. /**
  1804. * @operation get_public_boards
  1805. * @summary Get all public boards
  1806. *
  1807. * @return_type [{_id: string,
  1808. title: string}]
  1809. */
  1810. JsonRoutes.add('GET', '/api/boards', function(req, res) {
  1811. try {
  1812. Authentication.checkUserId(req.userId);
  1813. JsonRoutes.sendResult(res, {
  1814. code: 200,
  1815. data: ReactiveCache.getBoards(
  1816. { permission: 'public' },
  1817. {
  1818. sort: { sort: 1 /* boards default sorting */ },
  1819. },
  1820. ).map(function(doc) {
  1821. return {
  1822. _id: doc._id,
  1823. title: doc.title,
  1824. };
  1825. }),
  1826. });
  1827. } catch (error) {
  1828. JsonRoutes.sendResult(res, {
  1829. code: 200,
  1830. data: error,
  1831. });
  1832. }
  1833. });
  1834. /**
  1835. * @operation get_boards_count
  1836. * @summary Get public and private boards count
  1837. *
  1838. * @return_type {private: integer, public: integer}
  1839. */
  1840. JsonRoutes.add('GET', '/api/boards_count', function(req, res) {
  1841. try {
  1842. Authentication.checkUserId(req.userId);
  1843. JsonRoutes.sendResult(res, {
  1844. code: 200,
  1845. data: {
  1846. private: ReactiveCache.getBoards({ permission: 'private' }).length,
  1847. public: ReactiveCache.getBoards({ permission: 'public' }).length,
  1848. },
  1849. });
  1850. } catch (error) {
  1851. JsonRoutes.sendResult(res, {
  1852. code: 200,
  1853. data: error,
  1854. });
  1855. }
  1856. });
  1857. /**
  1858. * @operation get_board
  1859. * @summary Get the board with that particular ID
  1860. *
  1861. * @param {string} boardId the ID of the board to retrieve the data
  1862. * @return_type Boards
  1863. */
  1864. JsonRoutes.add('GET', '/api/boards/:boardId', function(req, res) {
  1865. try {
  1866. const paramBoardId = req.params.boardId;
  1867. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1868. JsonRoutes.sendResult(res, {
  1869. code: 200,
  1870. data: ReactiveCache.getBoard(paramBoardId),
  1871. });
  1872. } catch (error) {
  1873. JsonRoutes.sendResult(res, {
  1874. code: 200,
  1875. data: error,
  1876. });
  1877. }
  1878. });
  1879. /**
  1880. * @operation new_board
  1881. * @summary Create a board
  1882. *
  1883. * @description This allows to create a board.
  1884. *
  1885. * The color has to be chosen between `belize`, `nephritis`, `pomegranate`,
  1886. * `pumpkin`, `wisteria`, `moderatepink`, `strongcyan`,
  1887. * `limegreen`, `midnight`, `dark`, `relax`, `corteza`:
  1888. *
  1889. * <img src="https://wekan.github.io/board-colors.png" width="40%" alt="Wekan logo" />
  1890. *
  1891. * @param {string} title the new title of the board
  1892. * @param {string} owner "ABCDE12345" <= User ID in Wekan.
  1893. * (Not username or email)
  1894. * @param {boolean} [isAdmin] is the owner an admin of the board (default true)
  1895. * @param {boolean} [isActive] is the board active (default true)
  1896. * @param {boolean} [isNoComments] disable comments (default false)
  1897. * @param {boolean} [isCommentOnly] only enable comments (default false)
  1898. * @param {boolean} [isWorker] only move cards, assign himself to card and comment (default false)
  1899. * @param {string} [permission] "private" board <== Set to "public" if you
  1900. * want public Wekan board
  1901. * @param {string} [color] the color of the board
  1902. *
  1903. * @return_type {_id: string,
  1904. defaultSwimlaneId: string}
  1905. */
  1906. JsonRoutes.add('POST', '/api/boards', function(req, res) {
  1907. try {
  1908. Authentication.checkLoggedIn(req.userId);
  1909. const id = Boards.insert({
  1910. title: req.body.title,
  1911. members: [
  1912. {
  1913. userId: req.body.owner,
  1914. isAdmin: req.body.isAdmin || true,
  1915. isActive: req.body.isActive || true,
  1916. isNoComments: req.body.isNoComments || false,
  1917. isCommentOnly: req.body.isCommentOnly || false,
  1918. isWorker: req.body.isWorker || false,
  1919. },
  1920. ],
  1921. permission: req.body.permission || 'private',
  1922. color: req.body.color || 'belize',
  1923. });
  1924. const swimlaneId = Swimlanes.insert({
  1925. title: TAPi18n.__('default'),
  1926. boardId: id,
  1927. });
  1928. JsonRoutes.sendResult(res, {
  1929. code: 200,
  1930. data: {
  1931. _id: id,
  1932. defaultSwimlaneId: swimlaneId,
  1933. },
  1934. });
  1935. } catch (error) {
  1936. JsonRoutes.sendResult(res, {
  1937. code: 200,
  1938. data: error,
  1939. });
  1940. }
  1941. });
  1942. /**
  1943. * @operation delete_board
  1944. * @summary Delete a board
  1945. *
  1946. * @param {string} boardId the ID of the board
  1947. */
  1948. JsonRoutes.add('DELETE', '/api/boards/:boardId', function(req, res) {
  1949. try {
  1950. Authentication.checkUserId(req.userId);
  1951. const id = req.params.boardId;
  1952. Boards.remove({ _id: id });
  1953. JsonRoutes.sendResult(res, {
  1954. code: 200,
  1955. data: {
  1956. _id: id,
  1957. },
  1958. });
  1959. } catch (error) {
  1960. JsonRoutes.sendResult(res, {
  1961. code: 200,
  1962. data: error,
  1963. });
  1964. }
  1965. });
  1966. /**
  1967. * @operation update_board_title
  1968. * @summary Update the title of a board
  1969. *
  1970. * @param {string} boardId the ID of the board to update
  1971. * @param {string} title the new title for the board
  1972. */
  1973. JsonRoutes.add('PUT', '/api/boards/:boardId/title', function(req, res) {
  1974. try {
  1975. Authentication.checkUserId(req.userId);
  1976. const boardId = req.params.boardId;
  1977. const title = req.body.title;
  1978. Boards.direct.update({ _id: boardId }, { $set: { title } });
  1979. JsonRoutes.sendResult(res, {
  1980. code: 200,
  1981. data: {
  1982. _id: boardId,
  1983. title,
  1984. },
  1985. });
  1986. } catch (error) {
  1987. JsonRoutes.sendResult(res, {
  1988. code: 200,
  1989. data: error,
  1990. });
  1991. }
  1992. });
  1993. /**
  1994. * @operation add_board_label
  1995. * @summary Add a label to a board
  1996. *
  1997. * @description If the board doesn't have the name/color label, this function
  1998. * adds the label to the board.
  1999. *
  2000. * @param {string} boardId the board
  2001. * @param {string} color the color of the new label
  2002. * @param {string} name the name of the new label
  2003. *
  2004. * @return_type string
  2005. */
  2006. JsonRoutes.add('PUT', '/api/boards/:boardId/labels', function(req, res) {
  2007. const id = req.params.boardId;
  2008. Authentication.checkBoardAccess(req.userId, id);
  2009. try {
  2010. if (req.body.hasOwnProperty('label')) {
  2011. const board = ReactiveCache.getBoard(id);
  2012. const color = req.body.label.color;
  2013. const name = req.body.label.name;
  2014. const labelId = Random.id(6);
  2015. if (!board.getLabel(name, color)) {
  2016. Boards.direct.update(
  2017. { _id: id },
  2018. { $push: { labels: { _id: labelId, name, color } } },
  2019. );
  2020. JsonRoutes.sendResult(res, {
  2021. code: 200,
  2022. data: labelId,
  2023. });
  2024. } else {
  2025. JsonRoutes.sendResult(res, {
  2026. code: 200,
  2027. });
  2028. }
  2029. }
  2030. } catch (error) {
  2031. JsonRoutes.sendResult(res, {
  2032. data: error,
  2033. });
  2034. }
  2035. });
  2036. /**
  2037. * @operation copy_board
  2038. * @summary Copy a board to a new one
  2039. *
  2040. * @description If your are board admin or wekan admin, this copies the
  2041. * given board to a new one.
  2042. *
  2043. * @param {string} boardId the board
  2044. * @param {string} title the title of the new board (default to old one)
  2045. *
  2046. * @return_type string
  2047. */
  2048. JsonRoutes.add('POST', '/api/boards/:boardId/copy', function(req, res) {
  2049. const id = req.params.boardId;
  2050. const board = ReactiveCache.getBoard(id);
  2051. const adminAccess = board.members.some(e => e.userId === req.userId && e.isAdmin);
  2052. Authentication.checkAdminOrCondition(req.userId, adminAccess);
  2053. try {
  2054. board['title'] = req.body.title || Boards.uniqueTitle(board.title);
  2055. ret = board.copy();
  2056. JsonRoutes.sendResult(res, {
  2057. code: 200,
  2058. data: ret,
  2059. });
  2060. } catch (error) {
  2061. JsonRoutes.sendResult(res, {
  2062. data: error,
  2063. });
  2064. }
  2065. });
  2066. /**
  2067. * @operation set_board_member_permission
  2068. * @tag Users
  2069. * @summary Change the permission of a member of a board
  2070. *
  2071. * @param {string} boardId the ID of the board that we are changing
  2072. * @param {string} memberId the ID of the user to change permissions
  2073. * @param {boolean} isAdmin admin capability
  2074. * @param {boolean} isNoComments NoComments capability
  2075. * @param {boolean} isCommentOnly CommentsOnly capability
  2076. * @param {boolean} isWorker Worker capability
  2077. */
  2078. JsonRoutes.add('POST', '/api/boards/:boardId/members/:memberId', function(
  2079. req,
  2080. res,
  2081. ) {
  2082. try {
  2083. Authentication.checkUserId(req.userId);
  2084. const boardId = req.params.boardId;
  2085. const memberId = req.params.memberId;
  2086. const { isAdmin, isNoComments, isCommentOnly, isWorker } = req.body;
  2087. const board = ReactiveCache.getBoard(boardId);
  2088. function isTrue(data) {
  2089. try {
  2090. return data.toLowerCase() === 'true';
  2091. } catch (error) {
  2092. return data;
  2093. }
  2094. }
  2095. const query = board.setMemberPermission(
  2096. memberId,
  2097. isTrue(isAdmin),
  2098. isTrue(isNoComments),
  2099. isTrue(isCommentOnly),
  2100. isTrue(isWorker),
  2101. req.userId,
  2102. );
  2103. JsonRoutes.sendResult(res, {
  2104. code: 200,
  2105. data: query,
  2106. });
  2107. } catch (error) {
  2108. JsonRoutes.sendResult(res, {
  2109. code: 200,
  2110. data: error,
  2111. });
  2112. }
  2113. });
  2114. //ATTACHMENTS REST API
  2115. /**
  2116. * @operation get_board_attachments
  2117. * @summary Get the list of attachments of a board
  2118. *
  2119. * @param {string} boardId the board ID
  2120. * @return_type [{attachmentId: string,
  2121. * attachmentName: string,
  2122. * attachmentType: string,
  2123. * url: string,
  2124. * urlDownload: string,
  2125. * boardId: string,
  2126. * swimlaneId: string,
  2127. * listId: string,
  2128. * cardId: string
  2129. * }]
  2130. */
  2131. JsonRoutes.add('GET', '/api/boards/:boardId/attachments', function(req, res) {
  2132. const paramBoardId = req.params.boardId;
  2133. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2134. JsonRoutes.sendResult(res, {
  2135. code: 200,
  2136. data: ReactiveCache
  2137. .getAttachments({'meta.boardId': paramBoardId }, {}, true)
  2138. .each()
  2139. .map(function(attachment) {
  2140. return {
  2141. attachmentId: attachment._id,
  2142. attachmentName: attachment.name,
  2143. attachmentType: attachment.type,
  2144. url: attachment.link(),
  2145. urlDownload: `${attachment.link()}?download=true&token=`,
  2146. boardId: attachment.meta.boardId,
  2147. swimlaneId: attachment.meta.swimlaneId,
  2148. listId: attachment.meta.listId,
  2149. cardId: attachment.meta.cardId
  2150. };
  2151. }),
  2152. });
  2153. });
  2154. }
  2155. export default Boards;