boards.js 59 KB

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