2
0

boards.js 58 KB

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