boards.js 58 KB

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