boards.js 59 KB

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