boards.js 59 KB

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