boards.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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. showActivities: {
  607. type: Boolean,
  608. defaultValue: false,
  609. },
  610. }),
  611. );
  612. Boards.helpers({
  613. copy() {
  614. const oldId = this._id;
  615. const oldWatchers = this.watchers ? this.watchers.slice() : [];
  616. delete this._id;
  617. delete this.slug;
  618. this.title = this.copyTitle();
  619. const _id = Boards.insert(this);
  620. // Temporary remove watchers to disable notifications
  621. Boards.update(_id, {
  622. $set: {
  623. watchers: []
  624. },
  625. });
  626. // Copy all swimlanes in board
  627. ReactiveCache.getSwimlanes({
  628. boardId: oldId,
  629. archived: false,
  630. }).forEach(swimlane => {
  631. swimlane.type = 'swimlane';
  632. swimlane.copy(_id);
  633. });
  634. // copy custom field definitions
  635. const cfMap = {};
  636. ReactiveCache.getCustomFields({ boardIds: oldId }).forEach(cf => {
  637. const id = cf._id;
  638. delete cf._id;
  639. cf.boardIds = [_id];
  640. cfMap[id] = CustomFields.insert(cf);
  641. });
  642. ReactiveCache.getCards({ boardId: _id }).forEach(card => {
  643. Cards.update(card._id, {
  644. $set: {
  645. customFields: card.customFields.map(cf => {
  646. cf._id = cfMap[cf._id];
  647. return cf;
  648. }),
  649. },
  650. });
  651. });
  652. // copy rules, actions, and triggers
  653. const actionsMap = {};
  654. ReactiveCache.getActions({ boardId: oldId }).forEach(action => {
  655. const id = action._id;
  656. delete action._id;
  657. action.boardId = _id;
  658. actionsMap[id] = Actions.insert(action);
  659. });
  660. const triggersMap = {};
  661. ReactiveCache.getTriggers({ boardId: oldId }).forEach(trigger => {
  662. const id = trigger._id;
  663. delete trigger._id;
  664. trigger.boardId = _id;
  665. triggersMap[id] = Triggers.insert(trigger);
  666. });
  667. ReactiveCache.getRules({ boardId: oldId }).forEach(rule => {
  668. delete rule._id;
  669. rule.boardId = _id;
  670. rule.actionId = actionsMap[rule.actionId];
  671. rule.triggerId = triggersMap[rule.triggerId];
  672. Rules.insert(rule);
  673. });
  674. // Re-set Watchers to reenable notification
  675. Boards.update(_id, {
  676. $set: { watchers: oldWatchers }
  677. });
  678. return _id;
  679. },
  680. /**
  681. * Return a unique title based on the current title
  682. *
  683. * @returns {string|null}
  684. */
  685. copyTitle() {
  686. return Boards.uniqueTitle(this.title);
  687. },
  688. /**
  689. * Is supplied user authorized to view this board?
  690. */
  691. isVisibleBy(user) {
  692. if (this.isPublic()) {
  693. // public boards are visible to everyone
  694. return true;
  695. } else {
  696. // otherwise you have to be logged-in and active member
  697. return user && this.isActiveMember(user._id);
  698. }
  699. },
  700. /**
  701. * Is the user one of the active members of the board?
  702. *
  703. * @param userId
  704. * @returns {boolean} the member that matches, or undefined/false
  705. */
  706. isActiveMember(userId) {
  707. if (userId) {
  708. return this.members.find(
  709. member => member.userId === userId && member.isActive,
  710. );
  711. } else {
  712. return false;
  713. }
  714. },
  715. isPublic() {
  716. return this.permission === 'public';
  717. },
  718. cards() {
  719. const ret = ReactiveCache.getCards(
  720. { boardId: this._id, archived: false },
  721. { sort: { title: 1 } },
  722. );
  723. return ret;
  724. },
  725. lists() {
  726. return this.draggableLists();
  727. },
  728. newestLists() {
  729. // sorted lists from newest to the oldest, by its creation date or its cards' last modification date
  730. const value = ReactiveCache.getCurrentUser()._getListSortBy();
  731. const sortKey = { starred: -1, [value[0]]: value[1] }; // [["starred",-1],value];
  732. return ReactiveCache.getLists(
  733. {
  734. boardId: this._id,
  735. archived: false,
  736. },
  737. { sort: sortKey },
  738. );
  739. },
  740. draggableLists() {
  741. return ReactiveCache.getLists({ boardId: this._id }, { sort: { sort: 1 } });
  742. },
  743. /** returns the last list
  744. * @returns Document the last list
  745. */
  746. getLastList() {
  747. const ret = ReactiveCache.getList({ boardId: this._id }, { sort: { sort: 'desc' } });
  748. return ret;
  749. },
  750. nullSortLists() {
  751. return ReactiveCache.getLists({
  752. boardId: this._id,
  753. archived: false,
  754. sort: { $eq: null },
  755. });
  756. },
  757. swimlanes() {
  758. return ReactiveCache.getSwimlanes(
  759. { boardId: this._id, archived: false },
  760. { sort: { sort: 1 } },
  761. );
  762. },
  763. nextSwimlane(swimlane) {
  764. return ReactiveCache.getSwimlane(
  765. {
  766. boardId: this._id,
  767. archived: false,
  768. sort: { $gte: swimlane.sort },
  769. _id: { $ne: swimlane._id },
  770. },
  771. {
  772. sort: { sort: 1 },
  773. },
  774. );
  775. },
  776. nullSortSwimlanes() {
  777. return ReactiveCache.getSwimlanes({
  778. boardId: this._id,
  779. archived: false,
  780. sort: { $eq: null },
  781. });
  782. },
  783. hasOvertimeCards() {
  784. const card = ReactiveCache.getCard({
  785. isOvertime: true,
  786. boardId: this._id,
  787. archived: false,
  788. });
  789. return card !== undefined;
  790. },
  791. hasSpentTimeCards() {
  792. const card = ReactiveCache.getCard({
  793. spentTime: { $gt: 0 },
  794. boardId: this._id,
  795. archived: false,
  796. });
  797. return card !== undefined;
  798. },
  799. activities() {
  800. let linkedBoardId = [this._id];
  801. ReactiveCache.getCards({
  802. "type": "cardType-linkedBoard",
  803. "boardId": this._id}
  804. ).forEach(card => {
  805. linkedBoardId.push(card.linkedId);
  806. });
  807. const ret = ReactiveCache.getActivities({ boardId: { $in: linkedBoardId } }, { sort: { createdAt: -1 } });
  808. return ret;
  809. },
  810. activeMembers(){
  811. const members = _.where(this.members, { isActive: true });
  812. return _.sortBy(members, 'username');
  813. },
  814. activeOrgs() {
  815. return _.where(this.orgs, { isActive: true });
  816. },
  817. // hasNotAnyOrg(){
  818. // return this.orgs === undefined || this.orgs.length <= 0;
  819. // },
  820. activeTeams() {
  821. return _.where(this.teams, { isActive: true });
  822. },
  823. // hasNotAnyTeam(){
  824. // return this.teams === undefined || this.teams.length <= 0;
  825. // },
  826. activeAdmins() {
  827. return _.where(this.members, { isActive: true, isAdmin: true });
  828. },
  829. memberUsers() {
  830. return ReactiveCache.getUsers({ _id: { $in: _.pluck(this.members, 'userId') } });
  831. },
  832. getLabel(name, color) {
  833. return _.findWhere(this.labels, { name, color });
  834. },
  835. getLabelById(labelId) {
  836. return _.findWhere(this.labels, { _id: labelId });
  837. },
  838. labelIndex(labelId) {
  839. return _.pluck(this.labels, '_id').indexOf(labelId);
  840. },
  841. memberIndex(memberId) {
  842. return _.pluck(this.members, 'userId').indexOf(memberId);
  843. },
  844. hasMember(memberId) {
  845. return !!_.findWhere(this.members, { userId: memberId, isActive: true });
  846. },
  847. hasAdmin(memberId) {
  848. return !!_.findWhere(this.members, {
  849. userId: memberId,
  850. isActive: true,
  851. isAdmin: true,
  852. });
  853. },
  854. hasNoComments(memberId) {
  855. return !!_.findWhere(this.members, {
  856. userId: memberId,
  857. isActive: true,
  858. isAdmin: false,
  859. isNoComments: true,
  860. isWorker: false,
  861. });
  862. },
  863. hasCommentOnly(memberId) {
  864. return !!_.findWhere(this.members, {
  865. userId: memberId,
  866. isActive: true,
  867. isAdmin: false,
  868. isCommentOnly: true,
  869. isWorker: false,
  870. });
  871. },
  872. hasWorker(memberId) {
  873. return !!_.findWhere(this.members, {
  874. userId: memberId,
  875. isActive: true,
  876. isAdmin: false,
  877. isCommentOnly: false,
  878. isWorker: true,
  879. });
  880. },
  881. hasAnyAllowsDate() {
  882. const ret = this.allowsReceivedDate || this.allowsStartDate || this.allowsDueDate || this.allowsEndDate;
  883. return ret;
  884. },
  885. hasAnyAllowsUser() {
  886. const ret = this.allowsCreator || this.allowsMembers || this.allowsAssignee || this.allowsRequestedBy || this.allowsAssignedBy;
  887. return ret;
  888. },
  889. absoluteUrl() {
  890. return FlowRouter.url('board', { id: this._id, slug: this.slug });
  891. },
  892. originRelativeUrl() {
  893. return FlowRouter.path('board', { id: this._id, slug: this.slug });
  894. },
  895. colorClass() {
  896. return `board-color-${this.color}`;
  897. },
  898. customFields() {
  899. const ret = ReactiveCache.getCustomFields(
  900. { boardIds: { $in: [this._id] } },
  901. { sort: { name: 1 } },
  902. );
  903. return ret;
  904. },
  905. // XXX currently mutations return no value so we have an issue when using addLabel in import
  906. // XXX waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  907. pushLabel(name, color) {
  908. const _id = Random.id(6);
  909. Boards.direct.update(this._id, { $push: { labels: { _id, name, color } } });
  910. return _id;
  911. },
  912. /** sets the new label order
  913. * @param newLabelOrderOnlyIds new order array of _id, e.g. Array(4) [ "FvtD34", "PAEgDP", "LjRBxH", "YJ8sZz" ]
  914. */
  915. setNewLabelOrder(newLabelOrderOnlyIds) {
  916. if (this.labels.length == newLabelOrderOnlyIds.length) {
  917. if (this.labels.every(_label => newLabelOrderOnlyIds.indexOf(_label._id) >= 0)) {
  918. const newLabels = _.sortBy(this.labels, _label => newLabelOrderOnlyIds.indexOf(_label._id));
  919. if (this.labels.length == newLabels.length) {
  920. Boards.direct.update(this._id, {$set: {labels: newLabels}});
  921. }
  922. }
  923. }
  924. },
  925. searchBoards(term) {
  926. check(term, Match.OneOf(String, null, undefined));
  927. const query = { boardId: this._id };
  928. query.type = 'cardType-linkedBoard';
  929. query.archived = false;
  930. const projection = { limit: 10, sort: { createdAt: -1 } };
  931. if (term) {
  932. const regex = new RegExp(term, 'i');
  933. query.$or = [{ title: regex }, { description: regex }];
  934. }
  935. const ret = ReactiveCache.getCards(query, projection);
  936. return ret;
  937. },
  938. searchSwimlanes(term) {
  939. check(term, Match.OneOf(String, null, undefined));
  940. const query = { boardId: this._id };
  941. if (this.isTemplatesBoard()) {
  942. query.type = 'template-swimlane';
  943. query.archived = false;
  944. } else {
  945. query.type = { $nin: ['template-swimlane'] };
  946. }
  947. const projection = { limit: 10, sort: { createdAt: -1 } };
  948. if (term) {
  949. const regex = new RegExp(term, 'i');
  950. query.$or = [{ title: regex }, { description: regex }];
  951. }
  952. return ReactiveCache.getSwimlanes(query, projection);
  953. },
  954. searchLists(term) {
  955. let ret = null;
  956. if (term) {
  957. check(term, Match.OneOf(String));
  958. term = term.trim();
  959. }
  960. if (term) {
  961. const query = { boardId: this._id };
  962. if (this.isTemplatesBoard()) {
  963. query.type = 'template-list';
  964. query.archived = false;
  965. } else {
  966. query.type = { $nin: ['template-list'] };
  967. }
  968. const projection = { sort: { createdAt: -1 } };
  969. if (term) {
  970. const regex = new RegExp(term, 'i');
  971. query.$or = [{ title: regex }, { description: regex }];
  972. }
  973. ret = ReactiveCache.getLists(query, projection);
  974. }
  975. return ret;
  976. },
  977. searchCards(term, excludeLinked) {
  978. let ret = null;
  979. if (term) {
  980. check(term, Match.OneOf(String));
  981. term = term.trim();
  982. }
  983. if (term) {
  984. const query = { boardId: this._id };
  985. if (excludeLinked) {
  986. query.linkedId = null;
  987. }
  988. if (this.isTemplatesBoard()) {
  989. query.type = 'template-card';
  990. query.archived = false;
  991. } else {
  992. query.type = { $nin: ['template-card'] };
  993. }
  994. const projection = { sort: { createdAt: -1 } };
  995. const regex = new RegExp(term, 'i');
  996. query.$or = [
  997. { title: regex },
  998. { description: regex },
  999. { customFields: { $elemMatch: { value: regex } } },
  1000. ];
  1001. ret = ReactiveCache.getCards(query, projection);
  1002. }
  1003. return ret;
  1004. },
  1005. // A board alwasy has another board where it deposits subtasks of thasks
  1006. // that belong to itself.
  1007. getDefaultSubtasksBoardId() {
  1008. if (
  1009. this.subtasksDefaultBoardId === null ||
  1010. this.subtasksDefaultBoardId === undefined
  1011. ) {
  1012. this.subtasksDefaultBoardId = Boards.insert({
  1013. title: `^${this.title}^`,
  1014. permission: this.permission,
  1015. members: this.members,
  1016. color: this.color,
  1017. description: TAPi18n.__('default-subtasks-board', {
  1018. board: this.title,
  1019. }),
  1020. });
  1021. Swimlanes.insert({
  1022. title: TAPi18n.__('default'),
  1023. boardId: this.subtasksDefaultBoardId,
  1024. });
  1025. Boards.update(this._id, {
  1026. $set: {
  1027. subtasksDefaultBoardId: this.subtasksDefaultBoardId,
  1028. },
  1029. });
  1030. }
  1031. return this.subtasksDefaultBoardId;
  1032. },
  1033. getDefaultSubtasksBoard() {
  1034. return ReactiveCache.getBoard(this.getDefaultSubtasksBoardId());
  1035. },
  1036. //Date Settings option such as received date, start date and so on.
  1037. getDefaultDateSettingsBoardId() {
  1038. if (
  1039. this.dateSettingsDefaultBoardId === null ||
  1040. this.dateSettingsDefaultBoardId === undefined
  1041. ) {
  1042. this.dateSettingsDefaultBoardId = Boards.insert({
  1043. title: `^${this.title}^`,
  1044. permission: this.permission,
  1045. members: this.members,
  1046. color: this.color,
  1047. description: TAPi18n.__('default-dates-board', {
  1048. board: this.title,
  1049. }),
  1050. });
  1051. Swimlanes.insert({
  1052. title: TAPi18n.__('default'),
  1053. boardId: this.dateSettingsDefaultBoardId,
  1054. });
  1055. Boards.update(this._id, {
  1056. $set: {
  1057. dateSettingsDefaultBoardId: this.dateSettingsDefaultBoardId,
  1058. },
  1059. });
  1060. }
  1061. return this.dateSettingsDefaultBoardId;
  1062. },
  1063. getDefaultDateSettingsBoard() {
  1064. return ReactiveCache.getBoard(this.getDefaultDateSettingsBoardId());
  1065. },
  1066. getDefaultSubtasksListId() {
  1067. if (
  1068. this.subtasksDefaultListId === null ||
  1069. this.subtasksDefaultListId === undefined
  1070. ) {
  1071. this.subtasksDefaultListId = Lists.insert({
  1072. title: TAPi18n.__('queue'),
  1073. boardId: this._id,
  1074. });
  1075. this.setSubtasksDefaultListId(this.subtasksDefaultListId);
  1076. }
  1077. return this.subtasksDefaultListId;
  1078. },
  1079. getDefaultSubtasksList() {
  1080. return ReactiveCache.getList(this.getDefaultSubtasksListId());
  1081. },
  1082. getDefaultDateSettingsListId() {
  1083. if (
  1084. this.dateSettingsDefaultListId === null ||
  1085. this.dateSettingsDefaultListId === undefined
  1086. ) {
  1087. this.dateSettingsDefaultListId = Lists.insert({
  1088. title: TAPi18n.__('queue'),
  1089. boardId: this._id,
  1090. });
  1091. this.setDateSettingsDefaultListId(this.dateSettingsDefaultListId);
  1092. }
  1093. return this.dateSettingsDefaultListId;
  1094. },
  1095. getDefaultDateSettingsList() {
  1096. return ReactiveCache.getList(this.getDefaultDateSettingsListId());
  1097. },
  1098. getDefaultSwimline() {
  1099. let result = ReactiveCache.getSwimlane({ boardId: this._id });
  1100. if (result === undefined) {
  1101. Swimlanes.insert({
  1102. title: TAPi18n.__('default'),
  1103. boardId: this._id,
  1104. });
  1105. result = ReactiveCache.getSwimlane({ boardId: this._id });
  1106. }
  1107. return result;
  1108. },
  1109. getNextCardNumber() {
  1110. const boardCards = ReactiveCache.getCard(
  1111. {
  1112. boardId: this._id
  1113. },
  1114. {
  1115. sort: { cardNumber: -1 },
  1116. limit: 1
  1117. }
  1118. , true);
  1119. // If no card is assigned to the board, return 1
  1120. if (!boardCards) {
  1121. return 1;
  1122. }
  1123. const maxCardNr = !!boardCards.cardNumber ? boardCards.cardNumber : 0;
  1124. return maxCardNr + 1;
  1125. },
  1126. cardsDueInBetween(start, end) {
  1127. const ret = ReactiveCache.getCards({
  1128. boardId: this._id,
  1129. dueAt: { $gte: start, $lte: end },
  1130. });
  1131. return ret;
  1132. },
  1133. cardsInInterval(start, end) {
  1134. const ret = ReactiveCache.getCards({
  1135. boardId: this._id,
  1136. $or: [
  1137. {
  1138. startAt: {
  1139. $lte: start,
  1140. },
  1141. endAt: {
  1142. $gte: start,
  1143. },
  1144. },
  1145. {
  1146. startAt: {
  1147. $lte: end,
  1148. },
  1149. endAt: {
  1150. $gte: end,
  1151. },
  1152. },
  1153. {
  1154. startAt: {
  1155. $gte: start,
  1156. },
  1157. endAt: {
  1158. $lte: end,
  1159. },
  1160. },
  1161. ],
  1162. });
  1163. return ret;
  1164. },
  1165. isTemplateBoard() {
  1166. return this.type === 'template-board';
  1167. },
  1168. isTemplatesBoard() {
  1169. return this.type === 'template-container';
  1170. },
  1171. });
  1172. Boards.mutations({
  1173. archive() {
  1174. return { $set: { archived: true, archivedAt: new Date() } };
  1175. },
  1176. restore() {
  1177. return { $set: { archived: false } };
  1178. },
  1179. rename(title) {
  1180. return { $set: { title } };
  1181. },
  1182. setDescription(description) {
  1183. return { $set: { description } };
  1184. },
  1185. setColor(color) {
  1186. return { $set: { color } };
  1187. },
  1188. setBackgroundImageURL(backgroundImageURL) {
  1189. const currentUser = ReactiveCache.getCurrentUser();
  1190. if(currentUser.isBoardAdmin()) {
  1191. return { $set: { backgroundImageURL } };
  1192. } else if (currentUser.isAdmin()) {
  1193. return { $set: { backgroundImageURL } };
  1194. } else {
  1195. return false;
  1196. }
  1197. },
  1198. setVisibility(visibility) {
  1199. return { $set: { permission: visibility } };
  1200. },
  1201. addLabel(name, color) {
  1202. // If label with the same name and color already exists we don't want to
  1203. // create another one because they would be indistinguishable in the UI
  1204. // (they would still have different `_id` but that is not exposed to the
  1205. // user).
  1206. if (!this.getLabel(name, color)) {
  1207. const _id = Random.id(6);
  1208. return { $push: { labels: { _id, name, color } } };
  1209. }
  1210. return {};
  1211. },
  1212. editLabel(labelId, name, color) {
  1213. if (!this.getLabel(name, color)) {
  1214. const labelIndex = this.labelIndex(labelId);
  1215. return {
  1216. $set: {
  1217. [`labels.${labelIndex}.name`]: name,
  1218. [`labels.${labelIndex}.color`]: color,
  1219. },
  1220. };
  1221. }
  1222. return {};
  1223. },
  1224. removeLabel(labelId) {
  1225. return { $pull: { labels: { _id: labelId } } };
  1226. },
  1227. changeOwnership(fromId, toId) {
  1228. const memberIndex = this.memberIndex(fromId);
  1229. return {
  1230. $set: {
  1231. [`members.${memberIndex}.userId`]: toId,
  1232. },
  1233. };
  1234. },
  1235. addMember(memberId) {
  1236. const memberIndex = this.memberIndex(memberId);
  1237. if (memberIndex >= 0) {
  1238. return {
  1239. $set: {
  1240. [`members.${memberIndex}.isActive`]: true,
  1241. },
  1242. };
  1243. }
  1244. return {
  1245. $push: {
  1246. members: {
  1247. userId: memberId,
  1248. isAdmin: false,
  1249. isActive: true,
  1250. isNoComments: false,
  1251. isCommentOnly: false,
  1252. isWorker: false,
  1253. },
  1254. },
  1255. };
  1256. },
  1257. removeMember(memberId) {
  1258. const memberIndex = this.memberIndex(memberId);
  1259. // we do not allow the only one admin to be removed
  1260. const allowRemove =
  1261. !this.members[memberIndex].isAdmin || this.activeAdmins().length > 1;
  1262. if (!allowRemove) {
  1263. return {
  1264. $set: {
  1265. [`members.${memberIndex}.isActive`]: true,
  1266. },
  1267. };
  1268. }
  1269. return {
  1270. $set: {
  1271. [`members.${memberIndex}.isActive`]: false,
  1272. [`members.${memberIndex}.isAdmin`]: false,
  1273. },
  1274. };
  1275. },
  1276. setMemberPermission(
  1277. memberId,
  1278. isAdmin,
  1279. isNoComments,
  1280. isCommentOnly,
  1281. isWorker,
  1282. currentUserId = Meteor.userId(),
  1283. ) {
  1284. const memberIndex = this.memberIndex(memberId);
  1285. // do not allow change permission of self
  1286. if (memberId === currentUserId) {
  1287. isAdmin = this.members[memberIndex].isAdmin;
  1288. }
  1289. return {
  1290. $set: {
  1291. [`members.${memberIndex}.isAdmin`]: isAdmin,
  1292. [`members.${memberIndex}.isNoComments`]: isNoComments,
  1293. [`members.${memberIndex}.isCommentOnly`]: isCommentOnly,
  1294. [`members.${memberIndex}.isWorker`]: isWorker,
  1295. },
  1296. };
  1297. },
  1298. setAllowsSubtasks(allowsSubtasks) {
  1299. return { $set: { allowsSubtasks } };
  1300. },
  1301. setAllowsCreator(allowsCreator) {
  1302. return { $set: { allowsCreator } };
  1303. },
  1304. setAllowsCreatorOnMinicard(allowsCreatorOnMinicard) {
  1305. return { $set: { allowsCreatorOnMinicard } };
  1306. },
  1307. setAllowsMembers(allowsMembers) {
  1308. return { $set: { allowsMembers } };
  1309. },
  1310. setAllowsChecklists(allowsChecklists) {
  1311. return { $set: { allowsChecklists } };
  1312. },
  1313. setAllowsAssignee(allowsAssignee) {
  1314. return { $set: { allowsAssignee } };
  1315. },
  1316. setAllowsAssignedBy(allowsAssignedBy) {
  1317. return { $set: { allowsAssignedBy } };
  1318. },
  1319. setAllowsRequestedBy(allowsRequestedBy) {
  1320. return { $set: { allowsRequestedBy } };
  1321. },
  1322. setAllowsCardSortingByNumber(allowsCardSortingByNumber) {
  1323. return { $set: { allowsCardSortingByNumber } };
  1324. },
  1325. setAllowsShowLists(allowsShowLists) {
  1326. return { $set: { allowsShowLists } };
  1327. },
  1328. setAllowsAttachments(allowsAttachments) {
  1329. return { $set: { allowsAttachments } };
  1330. },
  1331. setAllowsLabels(allowsLabels) {
  1332. return { $set: { allowsLabels } };
  1333. },
  1334. setAllowsComments(allowsComments) {
  1335. return { $set: { allowsComments } };
  1336. },
  1337. setAllowsDescriptionTitle(allowsDescriptionTitle) {
  1338. return { $set: { allowsDescriptionTitle } };
  1339. },
  1340. setAllowsCardNumber(allowsCardNumber) {
  1341. return { $set: { allowsCardNumber } };
  1342. },
  1343. setAllowsDescriptionText(allowsDescriptionText) {
  1344. return { $set: { allowsDescriptionText } };
  1345. },
  1346. setallowsDescriptionTextOnMinicard(allowsDescriptionTextOnMinicard) {
  1347. return { $set: { allowsDescriptionTextOnMinicard } };
  1348. },
  1349. setallowsCoverAttachmentOnMinicard(allowsCoverAttachmentOnMinicard) {
  1350. return { $set: { allowsCoverAttachmentOnMinicard } };
  1351. },
  1352. setallowsBadgeAttachmentOnMinicard(allowsBadgeAttachmentOnMinicard) {
  1353. return { $set: { allowsBadgeAttachmentOnMinicard } };
  1354. },
  1355. setallowsCardSortingByNumberOnMinicard(allowsCardSortingByNumberOnMinicard) {
  1356. return { $set: { allowsCardSortingByNumberOnMinicard } };
  1357. },
  1358. setAllowsActivities(allowsActivities) {
  1359. return { $set: { allowsActivities } };
  1360. },
  1361. setAllowsReceivedDate(allowsReceivedDate) {
  1362. return { $set: { allowsReceivedDate } };
  1363. },
  1364. setAllowsCardCounterList(allowsCardCounterList) {
  1365. return { $set: { allowsCardCounterList } };
  1366. },
  1367. setAllowsBoardMemberList(allowsBoardMemberList) {
  1368. return { $set: { allowsBoardMemberList } };
  1369. },
  1370. setAllowsStartDate(allowsStartDate) {
  1371. return { $set: { allowsStartDate } };
  1372. },
  1373. setAllowsEndDate(allowsEndDate) {
  1374. return { $set: { allowsEndDate } };
  1375. },
  1376. setAllowsDueDate(allowsDueDate) {
  1377. return { $set: { allowsDueDate } };
  1378. },
  1379. setSubtasksDefaultBoardId(subtasksDefaultBoardId) {
  1380. return { $set: { subtasksDefaultBoardId } };
  1381. },
  1382. setSubtasksDefaultListId(subtasksDefaultListId) {
  1383. return { $set: { subtasksDefaultListId } };
  1384. },
  1385. setPresentParentTask(presentParentTask) {
  1386. return { $set: { presentParentTask } };
  1387. },
  1388. move(sortIndex) {
  1389. return { $set: { sort: sortIndex } };
  1390. },
  1391. toggleShowActivities() {
  1392. return { $set: { showActivities: !this.showActivities } };
  1393. },
  1394. });
  1395. function boardRemover(userId, doc) {
  1396. [Cards, Lists, Swimlanes, Integrations, Rules, Activities].forEach(
  1397. element => {
  1398. element.remove({ boardId: doc._id });
  1399. },
  1400. );
  1401. }
  1402. Boards.uniqueTitle = title => {
  1403. const m = title.match(
  1404. new RegExp('^(?<title>.*?)\\s*(\\[(?<num>\\d+)]\\s*$|\\s*$)'),
  1405. );
  1406. const base = escapeForRegex(m.groups.title);
  1407. const baseTitle = m.groups.title;
  1408. boards = ReactiveCache.getBoards({ title: new RegExp(`^${base}\\s*(\\[(?<num>\\d+)]\\s*$|\\s*$)`) });
  1409. if (boards.length > 0) {
  1410. let num = 0;
  1411. ReactiveCache.getBoards({ title: new RegExp(`^${base}\\s*\\[\\d+]\\s*$`) }).forEach(
  1412. board => {
  1413. const m = board.title.match(
  1414. new RegExp('^(?<title>.*?)\\s*\\[(?<num>\\d+)]\\s*$'),
  1415. );
  1416. if (m) {
  1417. const n = parseInt(m.groups.num, 10);
  1418. num = num < n ? n : num;
  1419. }
  1420. },
  1421. );
  1422. return `${baseTitle} [${num + 1}]`;
  1423. }
  1424. return title;
  1425. };
  1426. Boards.userSearch = (
  1427. userId,
  1428. selector = {},
  1429. projection = {},
  1430. // includeArchived = false,
  1431. ) => {
  1432. // if (!includeArchived) {
  1433. // selector.archived = false;
  1434. // }
  1435. selector.$or = [{ permission: 'public' }];
  1436. if (userId) {
  1437. selector.$or.push({ members: { $elemMatch: { userId, isActive: true } } });
  1438. }
  1439. const ret = ReactiveCache.getBoards(selector, projection);
  1440. return ret;
  1441. };
  1442. Boards.userBoards = (
  1443. userId,
  1444. archived = false,
  1445. selector = {},
  1446. projection = {},
  1447. ) => {
  1448. const user = ReactiveCache.getUser(userId);
  1449. if (!user) {
  1450. return [];
  1451. }
  1452. if (typeof archived === 'boolean') {
  1453. selector.archived = archived;
  1454. }
  1455. if (!selector.type) {
  1456. selector.type = 'board';
  1457. }
  1458. selector.$or = [
  1459. { permission: 'public' },
  1460. { members: { $elemMatch: { userId, isActive: true } } },
  1461. { orgs: { $elemMatch: { orgId: { $in: user.orgIds() }, isActive: true } } },
  1462. { teams: { $elemMatch: { teamId: { $in: user.teamIds() }, isActive: true } } },
  1463. ];
  1464. return ReactiveCache.getBoards(selector, projection);
  1465. };
  1466. Boards.userBoardIds = (userId, archived = false, selector = {}) => {
  1467. return Boards.userBoards(userId, archived, selector, {
  1468. fields: { _id: 1 },
  1469. }).map(board => {
  1470. return board._id;
  1471. });
  1472. };
  1473. Boards.colorMap = () => {
  1474. const colors = {};
  1475. for (const color of Boards.labelColors()) {
  1476. colors[TAPi18n.__(`color-${color}`)] = color;
  1477. }
  1478. return colors;
  1479. };
  1480. Boards.labelColors = () => {
  1481. return ALLOWED_COLORS;
  1482. };
  1483. if (Meteor.isServer) {
  1484. Boards.allow({
  1485. insert: Meteor.userId,
  1486. update: allowIsBoardAdmin,
  1487. remove: allowIsBoardAdmin,
  1488. fetch: ['members'],
  1489. });
  1490. // All logged in users are allowed to reorder boards by dragging at All Boards page and Public Boards page.
  1491. Boards.allow({
  1492. update(userId, board, fieldNames) {
  1493. return _.contains(fieldNames, 'sort');
  1494. },
  1495. fetch: [],
  1496. });
  1497. // The number of users that have starred this board is managed by trusted code
  1498. // and the user is not allowed to update it
  1499. Boards.deny({
  1500. update(userId, board, fieldNames) {
  1501. return _.contains(fieldNames, 'stars');
  1502. },
  1503. fetch: [],
  1504. });
  1505. // We can't remove a member if it is the last administrator
  1506. Boards.deny({
  1507. update(userId, doc, fieldNames, modifier) {
  1508. if (!_.contains(fieldNames, 'members')) return false;
  1509. // We only care in case of a $pull operation, ie remove a member
  1510. if (!_.isObject(modifier.$pull && modifier.$pull.members)) return false;
  1511. // If there is more than one admin, it's ok to remove anyone
  1512. const nbAdmins = _.where(doc.members, { isActive: true, isAdmin: true })
  1513. .length;
  1514. if (nbAdmins > 1) return false;
  1515. // If all the previous conditions were verified, we can't remove
  1516. // a user if it's an admin
  1517. const removedMemberId = modifier.$pull.members.userId;
  1518. return Boolean(
  1519. _.findWhere(doc.members, {
  1520. userId: removedMemberId,
  1521. isAdmin: true,
  1522. }),
  1523. );
  1524. },
  1525. fetch: ['members'],
  1526. });
  1527. Meteor.methods({
  1528. getBackgroundImageURL(boardId) {
  1529. check(boardId, String);
  1530. return ReactiveCache.getBoard(boardId, {}, { backgroundImageUrl: 1 });
  1531. },
  1532. quitBoard(boardId) {
  1533. check(boardId, String);
  1534. const board = ReactiveCache.getBoard(boardId);
  1535. if (board) {
  1536. const userId = Meteor.userId();
  1537. const index = board.memberIndex(userId);
  1538. if (index >= 0) {
  1539. board.removeMember(userId);
  1540. return true;
  1541. } else throw new Meteor.Error('error-board-notAMember');
  1542. } else throw new Meteor.Error('error-board-doesNotExist');
  1543. },
  1544. acceptInvite(boardId) {
  1545. check(boardId, String);
  1546. const board = ReactiveCache.getBoard(boardId);
  1547. if (!board) {
  1548. throw new Meteor.Error('error-board-doesNotExist');
  1549. }
  1550. Meteor.users.update(Meteor.userId(), {
  1551. $pull: {
  1552. 'profile.invitedBoards': boardId,
  1553. },
  1554. });
  1555. },
  1556. myLabelNames() {
  1557. let names = [];
  1558. Boards.userBoards(Meteor.userId()).forEach(board => {
  1559. // Only return labels when they exist.
  1560. if (board.labels !== undefined) {
  1561. names = names.concat(
  1562. board.labels
  1563. .filter(label => !!label.name)
  1564. .map(label => {
  1565. return label.name;
  1566. }),
  1567. );
  1568. } else {
  1569. return [];
  1570. }
  1571. });
  1572. return _.uniq(names).sort();
  1573. },
  1574. myBoardNames() {
  1575. return _.uniq(
  1576. Boards.userBoards(Meteor.userId()).map(board => {
  1577. return board.title;
  1578. }),
  1579. ).sort();
  1580. },
  1581. setAllBoardsHideActivities() {
  1582. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  1583. Boards.update(
  1584. {
  1585. showActivities: true
  1586. },
  1587. {
  1588. $set: {
  1589. showActivities: false,
  1590. },
  1591. },
  1592. {
  1593. multi: true,
  1594. },
  1595. );
  1596. return true;
  1597. } else {
  1598. return false;
  1599. }
  1600. },
  1601. });
  1602. Meteor.methods({
  1603. archiveBoard(boardId) {
  1604. check(boardId, String);
  1605. const board = ReactiveCache.getBoard(boardId);
  1606. if (board) {
  1607. const userId = Meteor.userId();
  1608. const index = board.memberIndex(userId);
  1609. if (index >= 0) {
  1610. board.archive();
  1611. return true;
  1612. } else throw new Meteor.Error('error-board-notAMember');
  1613. } else throw new Meteor.Error('error-board-doesNotExist');
  1614. },
  1615. setBoardOrgs(boardOrgsArray, currBoardId){
  1616. check(boardOrgsArray, Array);
  1617. check(currBoardId, String);
  1618. Boards.update(currBoardId, {
  1619. $set: {
  1620. orgs: boardOrgsArray,
  1621. },
  1622. });
  1623. },
  1624. setBoardTeams(boardTeamsArray, membersArray, currBoardId){
  1625. check(boardTeamsArray, Array);
  1626. check(membersArray, Array);
  1627. check(currBoardId, String);
  1628. Boards.update(currBoardId, {
  1629. $set: {
  1630. members: membersArray,
  1631. teams: boardTeamsArray,
  1632. },
  1633. });
  1634. },
  1635. });
  1636. }
  1637. // Insert new board at last position in sort order.
  1638. Boards.before.insert((userId, doc) => {
  1639. const lastBoard = ReactiveCache.getBoard(
  1640. { sort: { $exists: true } },
  1641. { sort: { sort: -1 } },
  1642. );
  1643. if (lastBoard && typeof lastBoard.sort !== 'undefined') {
  1644. doc.sort = lastBoard.sort + 1;
  1645. }
  1646. });
  1647. if (Meteor.isServer) {
  1648. // Let MongoDB ensure that a member is not included twice in the same board
  1649. Meteor.startup(() => {
  1650. Boards._collection.createIndex({ modifiedAt: -1 });
  1651. Boards._collection.createIndex(
  1652. {
  1653. _id: 1,
  1654. 'members.userId': 1,
  1655. },
  1656. { unique: true },
  1657. );
  1658. Boards._collection.createIndex({ 'members.userId': 1 });
  1659. });
  1660. // Genesis: the first activity of the newly created board
  1661. Boards.after.insert((userId, doc) => {
  1662. Activities.insert({
  1663. userId,
  1664. type: 'board',
  1665. activityTypeId: doc._id,
  1666. activityType: 'createBoard',
  1667. boardId: doc._id,
  1668. });
  1669. });
  1670. // If the user remove one label from a board, we cant to remove reference of
  1671. // this label in any card of this board.
  1672. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1673. if (
  1674. !_.contains(fieldNames, 'labels') ||
  1675. !modifier.$pull ||
  1676. !modifier.$pull.labels ||
  1677. !modifier.$pull.labels._id
  1678. ) {
  1679. return;
  1680. }
  1681. const removedLabelId = modifier.$pull.labels._id;
  1682. Cards.update(
  1683. { boardId: doc._id },
  1684. {
  1685. $pull: {
  1686. labelIds: removedLabelId,
  1687. },
  1688. },
  1689. { multi: true },
  1690. );
  1691. });
  1692. const foreachRemovedMember = (doc, modifier, callback) => {
  1693. Object.keys(modifier).forEach(set => {
  1694. if (modifier[set] !== false) {
  1695. return;
  1696. }
  1697. const parts = set.split('.');
  1698. if (
  1699. parts.length === 3 &&
  1700. parts[0] === 'members' &&
  1701. parts[2] === 'isActive'
  1702. ) {
  1703. callback(doc.members[parts[1]].userId);
  1704. }
  1705. });
  1706. };
  1707. // Remove a member from all objects of the board before leaving the board
  1708. Boards.before.update((userId, doc, fieldNames, modifier) => {
  1709. if (!_.contains(fieldNames, 'members')) {
  1710. return;
  1711. }
  1712. if (modifier.$set) {
  1713. const boardId = doc._id;
  1714. foreachRemovedMember(doc, modifier.$set, memberId => {
  1715. Cards.update(
  1716. { boardId },
  1717. {
  1718. $pull: {
  1719. members: memberId,
  1720. watchers: memberId,
  1721. },
  1722. },
  1723. { multi: true },
  1724. );
  1725. Lists.update(
  1726. { boardId },
  1727. {
  1728. $pull: {
  1729. watchers: memberId,
  1730. },
  1731. },
  1732. { multi: true },
  1733. );
  1734. const board = Boards._transform(doc);
  1735. board.setWatcher(memberId, false);
  1736. // Remove board from users starred list
  1737. if (!board.isPublic()) {
  1738. Users.update(memberId, {
  1739. $pull: {
  1740. 'profile.starredBoards': boardId,
  1741. },
  1742. });
  1743. }
  1744. });
  1745. }
  1746. });
  1747. Boards.before.remove((userId, doc) => {
  1748. boardRemover(userId, doc);
  1749. // Add removeBoard activity to keep it
  1750. Activities.insert({
  1751. userId,
  1752. type: 'board',
  1753. activityTypeId: doc._id,
  1754. activityType: 'removeBoard',
  1755. boardId: doc._id,
  1756. });
  1757. });
  1758. // Add a new activity if we add or remove a member to the board
  1759. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1760. if (fieldNames.includes('title')) {
  1761. Activities.insert({
  1762. userId,
  1763. type: 'board',
  1764. activityType: 'changedBoardTitle',
  1765. boardId: doc._id,
  1766. // this preserves the name so that the activity can be useful after the
  1767. // list is deleted
  1768. title: doc.title,
  1769. });
  1770. }
  1771. if (!_.contains(fieldNames, 'members')) {
  1772. return;
  1773. }
  1774. // Say hello to the new member
  1775. if (modifier.$push && modifier.$push.members) {
  1776. const memberId = modifier.$push.members.userId;
  1777. Activities.insert({
  1778. userId,
  1779. memberId,
  1780. type: 'member',
  1781. activityType: 'addBoardMember',
  1782. boardId: doc._id,
  1783. });
  1784. }
  1785. // Say goodbye to the former member
  1786. if (modifier.$set) {
  1787. foreachRemovedMember(doc, modifier.$set, memberId => {
  1788. Activities.insert({
  1789. userId,
  1790. memberId,
  1791. type: 'member',
  1792. activityType: 'removeBoardMember',
  1793. boardId: doc._id,
  1794. });
  1795. });
  1796. }
  1797. });
  1798. }
  1799. //BOARDS REST API
  1800. if (Meteor.isServer) {
  1801. /**
  1802. * @operation get_boards_from_user
  1803. * @summary Get all boards attached to a user
  1804. *
  1805. * @param {string} userId the ID of the user to retrieve the data
  1806. * @return_type [{_id: string,
  1807. title: string}]
  1808. */
  1809. JsonRoutes.add('GET', '/api/users/:userId/boards', function(req, res) {
  1810. try {
  1811. Authentication.checkLoggedIn(req.userId);
  1812. const paramUserId = req.params.userId;
  1813. // A normal user should be able to see their own boards,
  1814. // admins can access boards of any user
  1815. Authentication.checkAdminOrCondition(
  1816. req.userId,
  1817. req.userId === paramUserId,
  1818. );
  1819. const data = ReactiveCache.getBoards(
  1820. {
  1821. archived: false,
  1822. 'members.userId': paramUserId,
  1823. },
  1824. {
  1825. sort: { sort: 1 /* boards default sorting */ },
  1826. },
  1827. ).map(function(board) {
  1828. return {
  1829. _id: board._id,
  1830. title: board.title,
  1831. };
  1832. });
  1833. JsonRoutes.sendResult(res, { code: 200, data });
  1834. } catch (error) {
  1835. JsonRoutes.sendResult(res, {
  1836. code: 200,
  1837. data: error,
  1838. });
  1839. }
  1840. });
  1841. /**
  1842. * @operation get_public_boards
  1843. * @summary Get all public boards
  1844. *
  1845. * @return_type [{_id: string,
  1846. title: string}]
  1847. */
  1848. JsonRoutes.add('GET', '/api/boards', function(req, res) {
  1849. try {
  1850. Authentication.checkUserId(req.userId);
  1851. JsonRoutes.sendResult(res, {
  1852. code: 200,
  1853. data: ReactiveCache.getBoards(
  1854. { permission: 'public' },
  1855. {
  1856. sort: { sort: 1 /* boards default sorting */ },
  1857. },
  1858. ).map(function(doc) {
  1859. return {
  1860. _id: doc._id,
  1861. title: doc.title,
  1862. };
  1863. }),
  1864. });
  1865. } catch (error) {
  1866. JsonRoutes.sendResult(res, {
  1867. code: 200,
  1868. data: error,
  1869. });
  1870. }
  1871. });
  1872. /**
  1873. * @operation get_boards_count
  1874. * @summary Get public and private boards count
  1875. *
  1876. * @return_type {private: integer, public: integer}
  1877. */
  1878. JsonRoutes.add('GET', '/api/boards_count', function(req, res) {
  1879. try {
  1880. Authentication.checkUserId(req.userId);
  1881. JsonRoutes.sendResult(res, {
  1882. code: 200,
  1883. data: {
  1884. private: ReactiveCache.getBoards({ permission: 'private' }).length,
  1885. public: ReactiveCache.getBoards({ permission: 'public' }).length,
  1886. },
  1887. });
  1888. } catch (error) {
  1889. JsonRoutes.sendResult(res, {
  1890. code: 200,
  1891. data: error,
  1892. });
  1893. }
  1894. });
  1895. /**
  1896. * @operation get_board
  1897. * @summary Get the board with that particular ID
  1898. *
  1899. * @param {string} boardId the ID of the board to retrieve the data
  1900. * @return_type Boards
  1901. */
  1902. JsonRoutes.add('GET', '/api/boards/:boardId', function(req, res) {
  1903. try {
  1904. const paramBoardId = req.params.boardId;
  1905. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1906. JsonRoutes.sendResult(res, {
  1907. code: 200,
  1908. data: ReactiveCache.getBoard(paramBoardId),
  1909. });
  1910. } catch (error) {
  1911. JsonRoutes.sendResult(res, {
  1912. code: 200,
  1913. data: error,
  1914. });
  1915. }
  1916. });
  1917. /**
  1918. * @operation new_board
  1919. * @summary Create a board
  1920. *
  1921. * @description This allows to create a board.
  1922. *
  1923. * The color has to be chosen between `belize`, `nephritis`, `pomegranate`,
  1924. * `pumpkin`, `wisteria`, `moderatepink`, `strongcyan`,
  1925. * `limegreen`, `midnight`, `dark`, `relax`, `corteza`:
  1926. *
  1927. * <img src="https://wekan.github.io/board-colors.png" width="40%" alt="Wekan logo" />
  1928. *
  1929. * @param {string} title the new title of the board
  1930. * @param {string} owner "ABCDE12345" <= User ID in Wekan.
  1931. * (Not username or email)
  1932. * @param {boolean} [isAdmin] is the owner an admin of the board (default true)
  1933. * @param {boolean} [isActive] is the board active (default true)
  1934. * @param {boolean} [isNoComments] disable comments (default false)
  1935. * @param {boolean} [isCommentOnly] only enable comments (default false)
  1936. * @param {boolean} [isWorker] only move cards, assign himself to card and comment (default false)
  1937. * @param {string} [permission] "private" board <== Set to "public" if you
  1938. * want public Wekan board
  1939. * @param {string} [color] the color of the board
  1940. *
  1941. * @return_type {_id: string,
  1942. defaultSwimlaneId: string}
  1943. */
  1944. JsonRoutes.add('POST', '/api/boards', function(req, res) {
  1945. try {
  1946. Authentication.checkLoggedIn(req.userId);
  1947. const id = Boards.insert({
  1948. title: req.body.title,
  1949. members: [
  1950. {
  1951. userId: req.body.owner,
  1952. isAdmin: req.body.isAdmin || true,
  1953. isActive: req.body.isActive || true,
  1954. isNoComments: req.body.isNoComments || false,
  1955. isCommentOnly: req.body.isCommentOnly || false,
  1956. isWorker: req.body.isWorker || false,
  1957. },
  1958. ],
  1959. permission: req.body.permission || 'private',
  1960. color: req.body.color || 'belize',
  1961. });
  1962. const swimlaneId = Swimlanes.insert({
  1963. title: TAPi18n.__('default'),
  1964. boardId: id,
  1965. });
  1966. JsonRoutes.sendResult(res, {
  1967. code: 200,
  1968. data: {
  1969. _id: id,
  1970. defaultSwimlaneId: swimlaneId,
  1971. },
  1972. });
  1973. } catch (error) {
  1974. JsonRoutes.sendResult(res, {
  1975. code: 200,
  1976. data: error,
  1977. });
  1978. }
  1979. });
  1980. /**
  1981. * @operation delete_board
  1982. * @summary Delete a board
  1983. *
  1984. * @param {string} boardId the ID of the board
  1985. */
  1986. JsonRoutes.add('DELETE', '/api/boards/:boardId', function(req, res) {
  1987. try {
  1988. Authentication.checkUserId(req.userId);
  1989. const id = req.params.boardId;
  1990. Boards.remove({ _id: id });
  1991. JsonRoutes.sendResult(res, {
  1992. code: 200,
  1993. data: {
  1994. _id: id,
  1995. },
  1996. });
  1997. } catch (error) {
  1998. JsonRoutes.sendResult(res, {
  1999. code: 200,
  2000. data: error,
  2001. });
  2002. }
  2003. });
  2004. /**
  2005. * @operation update_board_title
  2006. * @summary Update the title of a board
  2007. *
  2008. * @param {string} boardId the ID of the board to update
  2009. * @param {string} title the new title for the board
  2010. */
  2011. JsonRoutes.add('PUT', '/api/boards/:boardId/title', function(req, res) {
  2012. try {
  2013. Authentication.checkUserId(req.userId);
  2014. const boardId = req.params.boardId;
  2015. const title = req.body.title;
  2016. Boards.direct.update({ _id: boardId }, { $set: { title } });
  2017. JsonRoutes.sendResult(res, {
  2018. code: 200,
  2019. data: {
  2020. _id: boardId,
  2021. title,
  2022. },
  2023. });
  2024. } catch (error) {
  2025. JsonRoutes.sendResult(res, {
  2026. code: 200,
  2027. data: error,
  2028. });
  2029. }
  2030. });
  2031. /**
  2032. * @operation add_board_label
  2033. * @summary Add a label to a board
  2034. *
  2035. * @description If the board doesn't have the name/color label, this function
  2036. * adds the label to the board.
  2037. *
  2038. * @param {string} boardId the board
  2039. * @param {string} color the color of the new label
  2040. * @param {string} name the name of the new label
  2041. *
  2042. * @return_type string
  2043. */
  2044. JsonRoutes.add('PUT', '/api/boards/:boardId/labels', function(req, res) {
  2045. const id = req.params.boardId;
  2046. Authentication.checkBoardAccess(req.userId, id);
  2047. try {
  2048. if (req.body.hasOwnProperty('label')) {
  2049. const board = ReactiveCache.getBoard(id);
  2050. const color = req.body.label.color;
  2051. const name = req.body.label.name;
  2052. const labelId = Random.id(6);
  2053. if (!board.getLabel(name, color)) {
  2054. Boards.direct.update(
  2055. { _id: id },
  2056. { $push: { labels: { _id: labelId, name, color } } },
  2057. );
  2058. JsonRoutes.sendResult(res, {
  2059. code: 200,
  2060. data: labelId,
  2061. });
  2062. } else {
  2063. JsonRoutes.sendResult(res, {
  2064. code: 200,
  2065. });
  2066. }
  2067. }
  2068. } catch (error) {
  2069. JsonRoutes.sendResult(res, {
  2070. data: error,
  2071. });
  2072. }
  2073. });
  2074. /**
  2075. * @operation copy_board
  2076. * @summary Copy a board to a new one
  2077. *
  2078. * @description If your are board admin or wekan admin, this copies the
  2079. * given board to a new one.
  2080. *
  2081. * @param {string} boardId the board
  2082. * @param {string} title the title of the new board (default to old one)
  2083. *
  2084. * @return_type string
  2085. */
  2086. JsonRoutes.add('POST', '/api/boards/:boardId/copy', function(req, res) {
  2087. const id = req.params.boardId;
  2088. const board = ReactiveCache.getBoard(id);
  2089. const adminAccess = board.members.some(e => e.userId === req.userId && e.isAdmin);
  2090. Authentication.checkAdminOrCondition(req.userId, adminAccess);
  2091. try {
  2092. board['title'] = req.body.title || Boards.uniqueTitle(board.title);
  2093. ret = board.copy();
  2094. JsonRoutes.sendResult(res, {
  2095. code: 200,
  2096. data: ret,
  2097. });
  2098. } catch (error) {
  2099. JsonRoutes.sendResult(res, {
  2100. data: error,
  2101. });
  2102. }
  2103. });
  2104. /**
  2105. * @operation set_board_member_permission
  2106. * @tag Users
  2107. * @summary Change the permission of a member of a board
  2108. *
  2109. * @param {string} boardId the ID of the board that we are changing
  2110. * @param {string} memberId the ID of the user to change permissions
  2111. * @param {boolean} isAdmin admin capability
  2112. * @param {boolean} isNoComments NoComments capability
  2113. * @param {boolean} isCommentOnly CommentsOnly capability
  2114. * @param {boolean} isWorker Worker capability
  2115. */
  2116. JsonRoutes.add('POST', '/api/boards/:boardId/members/:memberId', function(
  2117. req,
  2118. res,
  2119. ) {
  2120. try {
  2121. Authentication.checkUserId(req.userId);
  2122. const boardId = req.params.boardId;
  2123. const memberId = req.params.memberId;
  2124. const { isAdmin, isNoComments, isCommentOnly, isWorker } = req.body;
  2125. const board = ReactiveCache.getBoard(boardId);
  2126. function isTrue(data) {
  2127. try {
  2128. return data.toLowerCase() === 'true';
  2129. } catch (error) {
  2130. return data;
  2131. }
  2132. }
  2133. const query = board.setMemberPermission(
  2134. memberId,
  2135. isTrue(isAdmin),
  2136. isTrue(isNoComments),
  2137. isTrue(isCommentOnly),
  2138. isTrue(isWorker),
  2139. req.userId,
  2140. );
  2141. JsonRoutes.sendResult(res, {
  2142. code: 200,
  2143. data: query,
  2144. });
  2145. } catch (error) {
  2146. JsonRoutes.sendResult(res, {
  2147. code: 200,
  2148. data: error,
  2149. });
  2150. }
  2151. });
  2152. //ATTACHMENTS REST API
  2153. /**
  2154. * @operation get_board_attachments
  2155. * @summary Get the list of attachments of a board
  2156. *
  2157. * @param {string} boardId the board ID
  2158. * @return_type [{attachmentId: string,
  2159. * attachmentName: string,
  2160. * attachmentType: string,
  2161. * url: string,
  2162. * urlDownload: string,
  2163. * boardId: string,
  2164. * swimlaneId: string,
  2165. * listId: string,
  2166. * cardId: string
  2167. * }]
  2168. */
  2169. JsonRoutes.add('GET', '/api/boards/:boardId/attachments', function(req, res) {
  2170. const paramBoardId = req.params.boardId;
  2171. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2172. JsonRoutes.sendResult(res, {
  2173. code: 200,
  2174. data: ReactiveCache
  2175. .getAttachments({'meta.boardId': paramBoardId }, {}, true)
  2176. .each()
  2177. .map(function(attachment) {
  2178. return {
  2179. attachmentId: attachment._id,
  2180. attachmentName: attachment.name,
  2181. attachmentType: attachment.type,
  2182. url: attachment.link(),
  2183. urlDownload: `${attachment.link()}?download=true&token=`,
  2184. boardId: attachment.meta.boardId,
  2185. swimlaneId: attachment.meta.swimlaneId,
  2186. listId: attachment.meta.listId,
  2187. cardId: attachment.meta.cardId
  2188. };
  2189. }),
  2190. });
  2191. });
  2192. }
  2193. export default Boards;