boards.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. Boards = new Mongo.Collection('boards');
  2. /**
  3. * This is a Board.
  4. */
  5. Boards.attachSchema(
  6. new SimpleSchema({
  7. title: {
  8. /**
  9. * The title of the board
  10. */
  11. type: String,
  12. },
  13. slug: {
  14. /**
  15. * The title slugified.
  16. */
  17. type: String,
  18. // eslint-disable-next-line consistent-return
  19. autoValue() {
  20. // In some cases (Chinese and Japanese for instance) the `getSlug` function
  21. // return an empty string. This is causes bugs in our application so we set
  22. // a default slug in this case.
  23. // Improvment would be to change client URL after slug is changed
  24. const title = this.field('title');
  25. if (title.isSet && !this.isSet) {
  26. let slug = 'board';
  27. slug = getSlug(title.value) || slug;
  28. return slug;
  29. }
  30. },
  31. },
  32. archived: {
  33. /**
  34. * Is the board archived?
  35. */
  36. type: Boolean,
  37. // eslint-disable-next-line consistent-return
  38. autoValue() {
  39. if (this.isInsert && !this.isSet) {
  40. return false;
  41. }
  42. },
  43. },
  44. createdAt: {
  45. /**
  46. * Creation time of the board
  47. */
  48. type: Date,
  49. // eslint-disable-next-line consistent-return
  50. autoValue() {
  51. if (this.isInsert) {
  52. return new Date();
  53. } else if (this.isUpsert) {
  54. return { $setOnInsert: new Date() };
  55. } else {
  56. this.unset();
  57. }
  58. },
  59. },
  60. // XXX Inconsistent field naming
  61. modifiedAt: {
  62. /**
  63. * Last modification time of the board
  64. */
  65. type: Date,
  66. optional: true,
  67. // eslint-disable-next-line consistent-return
  68. autoValue() {
  69. if (this.isInsert || this.isUpsert || this.isUpdate) {
  70. return new Date();
  71. } else {
  72. this.unset();
  73. }
  74. },
  75. },
  76. // De-normalized number of users that have starred this board
  77. stars: {
  78. /**
  79. * How many stars the board has
  80. */
  81. type: Number,
  82. // eslint-disable-next-line consistent-return
  83. autoValue() {
  84. if (this.isInsert) {
  85. return 0;
  86. }
  87. },
  88. },
  89. // De-normalized label system
  90. labels: {
  91. /**
  92. * List of labels attached to a board
  93. */
  94. type: [Object],
  95. // eslint-disable-next-line consistent-return
  96. autoValue() {
  97. if (this.isInsert && !this.isSet) {
  98. const colors = Boards.simpleSchema()._schema['labels.$.color']
  99. .allowedValues;
  100. const defaultLabelsColors = _.clone(colors).splice(0, 6);
  101. return defaultLabelsColors.map(color => ({
  102. color,
  103. _id: Random.id(6),
  104. name: '',
  105. }));
  106. }
  107. },
  108. },
  109. 'labels.$._id': {
  110. /**
  111. * Unique id of a label
  112. */
  113. // We don't specify that this field must be unique in the board because that
  114. // will cause performance penalties and is not necessary since this field is
  115. // always set on the server.
  116. // XXX Actually if we create a new label, the `_id` is set on the client
  117. // without being overwritten by the server, could it be a problem?
  118. type: String,
  119. },
  120. 'labels.$.name': {
  121. /**
  122. * Name of a label
  123. */
  124. type: String,
  125. optional: true,
  126. },
  127. 'labels.$.color': {
  128. /**
  129. * color of a label.
  130. *
  131. * Can be amongst `green`, `yellow`, `orange`, `red`, `purple`,
  132. * `blue`, `sky`, `lime`, `pink`, `black`,
  133. * `silver`, `peachpuff`, `crimson`, `plum`, `darkgreen`,
  134. * `slateblue`, `magenta`, `gold`, `navy`, `gray`,
  135. * `saddlebrown`, `paleturquoise`, `mistyrose`, `indigo`
  136. */
  137. type: String,
  138. allowedValues: [
  139. 'green',
  140. 'yellow',
  141. 'orange',
  142. 'red',
  143. 'purple',
  144. 'blue',
  145. 'sky',
  146. 'lime',
  147. 'pink',
  148. 'black',
  149. 'silver',
  150. 'peachpuff',
  151. 'crimson',
  152. 'plum',
  153. 'darkgreen',
  154. 'slateblue',
  155. 'magenta',
  156. 'gold',
  157. 'navy',
  158. 'gray',
  159. 'saddlebrown',
  160. 'paleturquoise',
  161. 'mistyrose',
  162. 'indigo',
  163. ],
  164. },
  165. // XXX We might want to maintain more informations under the member sub-
  166. // documents like de-normalized meta-data (the date the member joined the
  167. // board, the number of contributions, etc.).
  168. members: {
  169. /**
  170. * List of members of a board
  171. */
  172. type: [Object],
  173. // eslint-disable-next-line consistent-return
  174. autoValue() {
  175. if (this.isInsert && !this.isSet) {
  176. return [
  177. {
  178. userId: this.userId,
  179. isAdmin: true,
  180. isActive: true,
  181. isNoComments: false,
  182. isCommentOnly: false,
  183. isWorker: false,
  184. },
  185. ];
  186. }
  187. },
  188. },
  189. 'members.$.userId': {
  190. /**
  191. * The uniq ID of the member
  192. */
  193. type: String,
  194. },
  195. 'members.$.isAdmin': {
  196. /**
  197. * Is the member an admin of the board?
  198. */
  199. type: Boolean,
  200. },
  201. 'members.$.isActive': {
  202. /**
  203. * Is the member active?
  204. */
  205. type: Boolean,
  206. },
  207. 'members.$.isNoComments': {
  208. /**
  209. * Is the member not allowed to make comments
  210. */
  211. type: Boolean,
  212. optional: true,
  213. },
  214. 'members.$.isCommentOnly': {
  215. /**
  216. * Is the member only allowed to comment on the board
  217. */
  218. type: Boolean,
  219. optional: true,
  220. },
  221. 'members.$.isWorker': {
  222. /**
  223. * Is the member only allowed to move card, assign himself to card and comment
  224. */
  225. type: Boolean,
  226. optional: true,
  227. },
  228. permission: {
  229. /**
  230. * visibility of the board
  231. */
  232. type: String,
  233. allowedValues: ['public', 'private'],
  234. },
  235. color: {
  236. /**
  237. * The color of the board.
  238. */
  239. type: String,
  240. allowedValues: [
  241. 'belize',
  242. 'nephritis',
  243. 'pomegranate',
  244. 'pumpkin',
  245. 'wisteria',
  246. 'moderatepink',
  247. 'strongcyan',
  248. 'limegreen',
  249. 'midnight',
  250. 'dark',
  251. 'relax',
  252. 'corteza',
  253. 'clearblue',
  254. 'natural',
  255. 'modern',
  256. 'moderndark',
  257. ],
  258. // eslint-disable-next-line consistent-return
  259. autoValue() {
  260. if (this.isInsert && !this.isSet) {
  261. return Boards.simpleSchema()._schema.color.allowedValues[0];
  262. }
  263. },
  264. },
  265. description: {
  266. /**
  267. * The description of the board
  268. */
  269. type: String,
  270. optional: true,
  271. },
  272. subtasksDefaultBoardId: {
  273. /**
  274. * The default board ID assigned to subtasks.
  275. */
  276. type: String,
  277. optional: true,
  278. defaultValue: null,
  279. },
  280. subtasksDefaultListId: {
  281. /**
  282. * The default List ID assigned to subtasks.
  283. */
  284. type: String,
  285. optional: true,
  286. defaultValue: null,
  287. },
  288. dateSettingsDefaultBoardId: {
  289. type: String,
  290. optional: true,
  291. defaultValue: null,
  292. },
  293. dateSettingsDefaultListId: {
  294. type: String,
  295. optional: true,
  296. defaultValue: null,
  297. },
  298. allowsSubtasks: {
  299. /**
  300. * Does the board allows subtasks?
  301. */
  302. type: Boolean,
  303. defaultValue: true,
  304. },
  305. allowsAttachments: {
  306. /**
  307. * Does the board allows attachments?
  308. */
  309. type: Boolean,
  310. defaultValue: true,
  311. },
  312. allowsChecklists: {
  313. /**
  314. * Does the board allows checklists?
  315. */
  316. type: Boolean,
  317. defaultValue: true,
  318. },
  319. allowsComments: {
  320. /**
  321. * Does the board allows comments?
  322. */
  323. type: Boolean,
  324. defaultValue: true,
  325. },
  326. allowsDescriptionTitle: {
  327. /**
  328. * Does the board allows description title?
  329. */
  330. type: Boolean,
  331. defaultValue: true,
  332. },
  333. allowsDescriptionText: {
  334. /**
  335. * Does the board allows description text?
  336. */
  337. type: Boolean,
  338. defaultValue: true,
  339. },
  340. allowsActivities: {
  341. /**
  342. * Does the board allows comments?
  343. */
  344. type: Boolean,
  345. defaultValue: true,
  346. },
  347. allowsLabels: {
  348. /**
  349. * Does the board allows labels?
  350. */
  351. type: Boolean,
  352. defaultValue: true,
  353. },
  354. allowsAssignee: {
  355. /**
  356. * Does the board allows assignee?
  357. */
  358. type: Boolean,
  359. defaultValue: true,
  360. },
  361. allowsMembers: {
  362. /**
  363. * Does the board allows members?
  364. */
  365. type: Boolean,
  366. defaultValue: true,
  367. },
  368. allowsRequestedBy: {
  369. /**
  370. * Does the board allows requested by?
  371. */
  372. type: Boolean,
  373. defaultValue: true,
  374. },
  375. allowsAssignedBy: {
  376. /**
  377. * Does the board allows requested by?
  378. */
  379. type: Boolean,
  380. defaultValue: true,
  381. },
  382. allowsReceivedDate: {
  383. /**
  384. * Does the board allows received date?
  385. */
  386. type: Boolean,
  387. defaultValue: true,
  388. },
  389. allowsStartDate: {
  390. /**
  391. * Does the board allows start date?
  392. */
  393. type: Boolean,
  394. defaultValue: true,
  395. },
  396. allowsEndDate: {
  397. /**
  398. * Does the board allows end date?
  399. */
  400. type: Boolean,
  401. defaultValue: true,
  402. },
  403. allowsDueDate: {
  404. /**
  405. * Does the board allows due date?
  406. */
  407. type: Boolean,
  408. defaultValue: true,
  409. },
  410. presentParentTask: {
  411. /**
  412. * Controls how to present the parent task:
  413. *
  414. * - `prefix-with-full-path`: add a prefix with the full path
  415. * - `prefix-with-parent`: add a prefisx with the parent name
  416. * - `subtext-with-full-path`: add a subtext with the full path
  417. * - `subtext-with-parent`: add a subtext with the parent name
  418. * - `no-parent`: does not show the parent at all
  419. */
  420. type: String,
  421. allowedValues: [
  422. 'prefix-with-full-path',
  423. 'prefix-with-parent',
  424. 'subtext-with-full-path',
  425. 'subtext-with-parent',
  426. 'no-parent',
  427. ],
  428. optional: true,
  429. defaultValue: 'no-parent',
  430. },
  431. startAt: {
  432. /**
  433. * Starting date of the board.
  434. */
  435. type: Date,
  436. optional: true,
  437. },
  438. dueAt: {
  439. /**
  440. * Due date of the board.
  441. */
  442. type: Date,
  443. optional: true,
  444. },
  445. endAt: {
  446. /**
  447. * End date of the board.
  448. */
  449. type: Date,
  450. optional: true,
  451. },
  452. spentTime: {
  453. /**
  454. * Time spent in the board.
  455. */
  456. type: Number,
  457. decimal: true,
  458. optional: true,
  459. },
  460. isOvertime: {
  461. /**
  462. * Is the board overtimed?
  463. */
  464. type: Boolean,
  465. defaultValue: false,
  466. optional: true,
  467. },
  468. type: {
  469. /**
  470. * The type of board
  471. */
  472. type: String,
  473. defaultValue: 'board',
  474. },
  475. sort: {
  476. /**
  477. * Sort value
  478. */
  479. type: Number,
  480. decimal: true,
  481. defaultValue: -1,
  482. },
  483. }),
  484. );
  485. Boards.helpers({
  486. copy() {
  487. const oldId = this._id;
  488. delete this._id;
  489. const _id = Boards.insert(this);
  490. // Copy all swimlanes in board
  491. Swimlanes.find({
  492. boardId: oldId,
  493. archived: false,
  494. }).forEach(swimlane => {
  495. swimlane.type = 'swimlane';
  496. swimlane.copy(_id);
  497. });
  498. },
  499. /**
  500. * Is supplied user authorized to view this board?
  501. */
  502. isVisibleBy(user) {
  503. if (this.isPublic()) {
  504. // public boards are visible to everyone
  505. return true;
  506. } else {
  507. // otherwise you have to be logged-in and active member
  508. return user && this.isActiveMember(user._id);
  509. }
  510. },
  511. /**
  512. * Is the user one of the active members of the board?
  513. *
  514. * @param userId
  515. * @returns {boolean} the member that matches, or undefined/false
  516. */
  517. isActiveMember(userId) {
  518. if (userId) {
  519. return this.members.find(
  520. member => member.userId === userId && member.isActive,
  521. );
  522. } else {
  523. return false;
  524. }
  525. },
  526. isPublic() {
  527. return this.permission === 'public';
  528. },
  529. cards() {
  530. return Cards.find(
  531. { boardId: this._id, archived: false },
  532. { sort: { title: 1 } },
  533. );
  534. },
  535. lists() {
  536. //currentUser = Meteor.user();
  537. //if (currentUser) {
  538. // enabled = Meteor.user().hasSortBy();
  539. //}
  540. //return enabled ? this.newestLists() : this.draggableLists();
  541. return this.draggableLists();
  542. },
  543. newestLists() {
  544. // sorted lists from newest to the oldest, by its creation date or its cards' last modification date
  545. const value = Meteor.user()._getListSortBy();
  546. const sortKey = { starred: -1, [value[0]]: value[1] }; // [["starred",-1],value];
  547. return Lists.find(
  548. {
  549. boardId: this._id,
  550. archived: false,
  551. },
  552. { sort: sortKey },
  553. );
  554. },
  555. draggableLists() {
  556. return Lists.find({ boardId: this._id }, { sort: { sort: 1 } });
  557. },
  558. nullSortLists() {
  559. return Lists.find({
  560. boardId: this._id,
  561. archived: false,
  562. sort: { $eq: null },
  563. });
  564. },
  565. swimlanes() {
  566. return Swimlanes.find(
  567. { boardId: this._id, archived: false },
  568. { sort: { sort: 1 } },
  569. );
  570. },
  571. nextSwimlane(swimlane) {
  572. return Swimlanes.findOne(
  573. {
  574. boardId: this._id,
  575. archived: false,
  576. sort: { $gte: swimlane.sort },
  577. _id: { $ne: swimlane._id },
  578. },
  579. {
  580. sort: { sort: 1 },
  581. },
  582. );
  583. },
  584. nullSortSwimlanes() {
  585. return Swimlanes.find({
  586. boardId: this._id,
  587. archived: false,
  588. sort: { $eq: null },
  589. });
  590. },
  591. hasOvertimeCards() {
  592. const card = Cards.findOne({
  593. isOvertime: true,
  594. boardId: this._id,
  595. archived: false,
  596. });
  597. return card !== undefined;
  598. },
  599. hasSpentTimeCards() {
  600. const card = Cards.findOne({
  601. spentTime: { $gt: 0 },
  602. boardId: this._id,
  603. archived: false,
  604. });
  605. return card !== undefined;
  606. },
  607. activities() {
  608. return Activities.find({ boardId: this._id }, { sort: { createdAt: -1 } });
  609. },
  610. activeMembers() {
  611. return _.where(this.members, { isActive: true });
  612. },
  613. activeAdmins() {
  614. return _.where(this.members, { isActive: true, isAdmin: true });
  615. },
  616. memberUsers() {
  617. return Users.find({ _id: { $in: _.pluck(this.members, 'userId') } });
  618. },
  619. getLabel(name, color) {
  620. return _.findWhere(this.labels, { name, color });
  621. },
  622. getLabelById(labelId) {
  623. return _.findWhere(this.labels, { _id: labelId });
  624. },
  625. labelIndex(labelId) {
  626. return _.pluck(this.labels, '_id').indexOf(labelId);
  627. },
  628. memberIndex(memberId) {
  629. return _.pluck(this.members, 'userId').indexOf(memberId);
  630. },
  631. hasMember(memberId) {
  632. return !!_.findWhere(this.members, { userId: memberId, isActive: true });
  633. },
  634. hasAdmin(memberId) {
  635. return !!_.findWhere(this.members, {
  636. userId: memberId,
  637. isActive: true,
  638. isAdmin: true,
  639. });
  640. },
  641. hasNoComments(memberId) {
  642. return !!_.findWhere(this.members, {
  643. userId: memberId,
  644. isActive: true,
  645. isAdmin: false,
  646. isNoComments: true,
  647. isWorker: false,
  648. });
  649. },
  650. hasCommentOnly(memberId) {
  651. return !!_.findWhere(this.members, {
  652. userId: memberId,
  653. isActive: true,
  654. isAdmin: false,
  655. isCommentOnly: true,
  656. isWorker: false,
  657. });
  658. },
  659. hasWorker(memberId) {
  660. return !!_.findWhere(this.members, {
  661. userId: memberId,
  662. isActive: true,
  663. isAdmin: false,
  664. isCommentOnly: false,
  665. isWorker: true,
  666. });
  667. },
  668. absoluteUrl() {
  669. return FlowRouter.url('board', { id: this._id, slug: this.slug });
  670. },
  671. colorClass() {
  672. return `board-color-${this.color}`;
  673. },
  674. customFields() {
  675. return CustomFields.find(
  676. { boardIds: { $in: [this._id] } },
  677. { sort: { name: 1 } },
  678. );
  679. },
  680. // XXX currently mutations return no value so we have an issue when using addLabel in import
  681. // XXX waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  682. pushLabel(name, color) {
  683. const _id = Random.id(6);
  684. Boards.direct.update(this._id, { $push: { labels: { _id, name, color } } });
  685. return _id;
  686. },
  687. searchBoards(term) {
  688. check(term, Match.OneOf(String, null, undefined));
  689. const query = { boardId: this._id };
  690. query.type = 'cardType-linkedBoard';
  691. query.archived = false;
  692. const projection = { limit: 10, sort: { createdAt: -1 } };
  693. if (term) {
  694. const regex = new RegExp(term, 'i');
  695. query.$or = [{ title: regex }, { description: regex }];
  696. }
  697. return Cards.find(query, projection);
  698. },
  699. searchSwimlanes(term) {
  700. check(term, Match.OneOf(String, null, undefined));
  701. const query = { boardId: this._id };
  702. if (this.isTemplatesBoard()) {
  703. query.type = 'template-swimlane';
  704. query.archived = false;
  705. } else {
  706. query.type = { $nin: ['template-swimlane'] };
  707. }
  708. const projection = { limit: 10, sort: { createdAt: -1 } };
  709. if (term) {
  710. const regex = new RegExp(term, 'i');
  711. query.$or = [{ title: regex }, { description: regex }];
  712. }
  713. return Swimlanes.find(query, projection);
  714. },
  715. searchLists(term) {
  716. check(term, Match.OneOf(String, null, undefined));
  717. const query = { boardId: this._id };
  718. if (this.isTemplatesBoard()) {
  719. query.type = 'template-list';
  720. query.archived = false;
  721. } else {
  722. query.type = { $nin: ['template-list'] };
  723. }
  724. const projection = { limit: 10, sort: { createdAt: -1 } };
  725. if (term) {
  726. const regex = new RegExp(term, 'i');
  727. query.$or = [{ title: regex }, { description: regex }];
  728. }
  729. return Lists.find(query, projection);
  730. },
  731. searchCards(term, excludeLinked) {
  732. check(term, Match.OneOf(String, null, undefined));
  733. const query = { boardId: this._id };
  734. if (excludeLinked) {
  735. query.linkedId = null;
  736. }
  737. if (this.isTemplatesBoard()) {
  738. query.type = 'template-card';
  739. query.archived = false;
  740. } else {
  741. query.type = { $nin: ['template-card'] };
  742. }
  743. const projection = { limit: 10, sort: { createdAt: -1 } };
  744. if (term) {
  745. const regex = new RegExp(term, 'i');
  746. query.$or = [
  747. { title: regex },
  748. { description: regex },
  749. { customFields: { $elemMatch: { value: regex } } },
  750. ];
  751. }
  752. return Cards.find(query, projection);
  753. },
  754. // A board alwasy has another board where it deposits subtasks of thasks
  755. // that belong to itself.
  756. getDefaultSubtasksBoardId() {
  757. if (
  758. this.subtasksDefaultBoardId === null ||
  759. this.subtasksDefaultBoardId === undefined
  760. ) {
  761. this.subtasksDefaultBoardId = Boards.insert({
  762. title: `^${this.title}^`,
  763. permission: this.permission,
  764. members: this.members,
  765. color: this.color,
  766. description: TAPi18n.__('default-subtasks-board', {
  767. board: this.title,
  768. }),
  769. });
  770. Swimlanes.insert({
  771. title: TAPi18n.__('default'),
  772. boardId: this.subtasksDefaultBoardId,
  773. });
  774. Boards.update(this._id, {
  775. $set: {
  776. subtasksDefaultBoardId: this.subtasksDefaultBoardId,
  777. },
  778. });
  779. }
  780. return this.subtasksDefaultBoardId;
  781. },
  782. getDefaultSubtasksBoard() {
  783. return Boards.findOne(this.getDefaultSubtasksBoardId());
  784. },
  785. //Date Settings option such as received date, start date and so on.
  786. getDefaultDateSettingsBoardId() {
  787. if (
  788. this.dateSettingsDefaultBoardId === null ||
  789. this.dateSettingsDefaultBoardId === undefined
  790. ) {
  791. this.dateSettingsDefaultBoardId = Boards.insert({
  792. title: `^${this.title}^`,
  793. permission: this.permission,
  794. members: this.members,
  795. color: this.color,
  796. description: TAPi18n.__('default-dates-board', {
  797. board: this.title,
  798. }),
  799. });
  800. Swimlanes.insert({
  801. title: TAPi18n.__('default'),
  802. boardId: this.dateSettingsDefaultBoardId,
  803. });
  804. Boards.update(this._id, {
  805. $set: {
  806. dateSettingsDefaultBoardId: this.dateSettingsDefaultBoardId,
  807. },
  808. });
  809. }
  810. return this.dateSettingsDefaultBoardId;
  811. },
  812. getDefaultDateSettingsBoard() {
  813. return Boards.findOne(this.getDefaultDateSettingsBoardId());
  814. },
  815. getDefaultSubtasksListId() {
  816. if (
  817. this.subtasksDefaultListId === null ||
  818. this.subtasksDefaultListId === undefined
  819. ) {
  820. this.subtasksDefaultListId = Lists.insert({
  821. title: TAPi18n.__('queue'),
  822. boardId: this._id,
  823. });
  824. this.setSubtasksDefaultListId(this.subtasksDefaultListId);
  825. }
  826. return this.subtasksDefaultListId;
  827. },
  828. getDefaultSubtasksList() {
  829. return Lists.findOne(this.getDefaultSubtasksListId());
  830. },
  831. getDefaultDateSettingsListId() {
  832. if (
  833. this.dateSettingsDefaultListId === null ||
  834. this.dateSettingsDefaultListId === undefined
  835. ) {
  836. this.dateSettingsDefaultListId = Lists.insert({
  837. title: TAPi18n.__('queue'),
  838. boardId: this._id,
  839. });
  840. this.setDateSettingsDefaultListId(this.dateSettingsDefaultListId);
  841. }
  842. return this.dateSettingsDefaultListId;
  843. },
  844. getDefaultDateSettingsList() {
  845. return Lists.findOne(this.getDefaultDateSettingsListId());
  846. },
  847. getDefaultSwimline() {
  848. let result = Swimlanes.findOne({ boardId: this._id });
  849. if (result === undefined) {
  850. Swimlanes.insert({
  851. title: TAPi18n.__('default'),
  852. boardId: this._id,
  853. });
  854. result = Swimlanes.findOne({ boardId: this._id });
  855. }
  856. return result;
  857. },
  858. cardsDueInBetween(start, end) {
  859. return Cards.find({
  860. boardId: this._id,
  861. dueAt: { $gte: start, $lte: end },
  862. });
  863. },
  864. cardsInInterval(start, end) {
  865. return Cards.find({
  866. boardId: this._id,
  867. $or: [
  868. {
  869. startAt: {
  870. $lte: start,
  871. },
  872. endAt: {
  873. $gte: start,
  874. },
  875. },
  876. {
  877. startAt: {
  878. $lte: end,
  879. },
  880. endAt: {
  881. $gte: end,
  882. },
  883. },
  884. {
  885. startAt: {
  886. $gte: start,
  887. },
  888. endAt: {
  889. $lte: end,
  890. },
  891. },
  892. ],
  893. });
  894. },
  895. isTemplateBoard() {
  896. return this.type === 'template-board';
  897. },
  898. isTemplatesBoard() {
  899. return this.type === 'template-container';
  900. },
  901. });
  902. Boards.mutations({
  903. archive() {
  904. return { $set: { archived: true } };
  905. },
  906. restore() {
  907. return { $set: { archived: false } };
  908. },
  909. rename(title) {
  910. return { $set: { title } };
  911. },
  912. setDescription(description) {
  913. return { $set: { description } };
  914. },
  915. setColor(color) {
  916. return { $set: { color } };
  917. },
  918. setVisibility(visibility) {
  919. return { $set: { permission: visibility } };
  920. },
  921. addLabel(name, color) {
  922. // If label with the same name and color already exists we don't want to
  923. // create another one because they would be indistinguishable in the UI
  924. // (they would still have different `_id` but that is not exposed to the
  925. // user).
  926. if (!this.getLabel(name, color)) {
  927. const _id = Random.id(6);
  928. return { $push: { labels: { _id, name, color } } };
  929. }
  930. return {};
  931. },
  932. editLabel(labelId, name, color) {
  933. if (!this.getLabel(name, color)) {
  934. const labelIndex = this.labelIndex(labelId);
  935. return {
  936. $set: {
  937. [`labels.${labelIndex}.name`]: name,
  938. [`labels.${labelIndex}.color`]: color,
  939. },
  940. };
  941. }
  942. return {};
  943. },
  944. removeLabel(labelId) {
  945. return { $pull: { labels: { _id: labelId } } };
  946. },
  947. changeOwnership(fromId, toId) {
  948. const memberIndex = this.memberIndex(fromId);
  949. return {
  950. $set: {
  951. [`members.${memberIndex}.userId`]: toId,
  952. },
  953. };
  954. },
  955. addMember(memberId) {
  956. const memberIndex = this.memberIndex(memberId);
  957. if (memberIndex >= 0) {
  958. return {
  959. $set: {
  960. [`members.${memberIndex}.isActive`]: true,
  961. },
  962. };
  963. }
  964. return {
  965. $push: {
  966. members: {
  967. userId: memberId,
  968. isAdmin: false,
  969. isActive: true,
  970. isNoComments: false,
  971. isCommentOnly: false,
  972. isWorker: false,
  973. },
  974. },
  975. };
  976. },
  977. removeMember(memberId) {
  978. const memberIndex = this.memberIndex(memberId);
  979. // we do not allow the only one admin to be removed
  980. const allowRemove =
  981. !this.members[memberIndex].isAdmin || this.activeAdmins().length > 1;
  982. if (!allowRemove) {
  983. return {
  984. $set: {
  985. [`members.${memberIndex}.isActive`]: true,
  986. },
  987. };
  988. }
  989. return {
  990. $set: {
  991. [`members.${memberIndex}.isActive`]: false,
  992. [`members.${memberIndex}.isAdmin`]: false,
  993. },
  994. };
  995. },
  996. setMemberPermission(
  997. memberId,
  998. isAdmin,
  999. isNoComments,
  1000. isCommentOnly,
  1001. isWorker,
  1002. currentUserId = Meteor.userId(),
  1003. ) {
  1004. const memberIndex = this.memberIndex(memberId);
  1005. // do not allow change permission of self
  1006. if (memberId === currentUserId) {
  1007. isAdmin = this.members[memberIndex].isAdmin;
  1008. }
  1009. return {
  1010. $set: {
  1011. [`members.${memberIndex}.isAdmin`]: isAdmin,
  1012. [`members.${memberIndex}.isNoComments`]: isNoComments,
  1013. [`members.${memberIndex}.isCommentOnly`]: isCommentOnly,
  1014. [`members.${memberIndex}.isWorker`]: isWorker,
  1015. },
  1016. };
  1017. },
  1018. setAllowsSubtasks(allowsSubtasks) {
  1019. return { $set: { allowsSubtasks } };
  1020. },
  1021. setAllowsMembers(allowsMembers) {
  1022. return { $set: { allowsMembers } };
  1023. },
  1024. setAllowsChecklists(allowsChecklists) {
  1025. return { $set: { allowsChecklists } };
  1026. },
  1027. setAllowsAssignee(allowsAssignee) {
  1028. return { $set: { allowsAssignee } };
  1029. },
  1030. setAllowsAssignedBy(allowsAssignedBy) {
  1031. return { $set: { allowsAssignedBy } };
  1032. },
  1033. setAllowsRequestedBy(allowsRequestedBy) {
  1034. return { $set: { allowsRequestedBy } };
  1035. },
  1036. setAllowsAttachments(allowsAttachments) {
  1037. return { $set: { allowsAttachments } };
  1038. },
  1039. setAllowsLabels(allowsLabels) {
  1040. return { $set: { allowsLabels } };
  1041. },
  1042. setAllowsComments(allowsComments) {
  1043. return { $set: { allowsComments } };
  1044. },
  1045. setAllowsDescriptionTitle(allowsDescriptionTitle) {
  1046. return { $set: { allowsDescriptionTitle } };
  1047. },
  1048. setAllowsDescriptionText(allowsDescriptionText) {
  1049. return { $set: { allowsDescriptionText } };
  1050. },
  1051. setAllowsActivities(allowsActivities) {
  1052. return { $set: { allowsActivities } };
  1053. },
  1054. setAllowsReceivedDate(allowsReceivedDate) {
  1055. return { $set: { allowsReceivedDate } };
  1056. },
  1057. setAllowsStartDate(allowsStartDate) {
  1058. return { $set: { allowsStartDate } };
  1059. },
  1060. setAllowsEndDate(allowsEndDate) {
  1061. return { $set: { allowsEndDate } };
  1062. },
  1063. setAllowsDueDate(allowsDueDate) {
  1064. return { $set: { allowsDueDate } };
  1065. },
  1066. setSubtasksDefaultBoardId(subtasksDefaultBoardId) {
  1067. return { $set: { subtasksDefaultBoardId } };
  1068. },
  1069. setSubtasksDefaultListId(subtasksDefaultListId) {
  1070. return { $set: { subtasksDefaultListId } };
  1071. },
  1072. setPresentParentTask(presentParentTask) {
  1073. return { $set: { presentParentTask } };
  1074. },
  1075. move(sortIndex) {
  1076. return { $set: { sort: sortIndex } };
  1077. },
  1078. });
  1079. function boardRemover(userId, doc) {
  1080. [Cards, Lists, Swimlanes, Integrations, Rules, Activities].forEach(
  1081. element => {
  1082. element.remove({ boardId: doc._id });
  1083. },
  1084. );
  1085. }
  1086. if (Meteor.isServer) {
  1087. Boards.allow({
  1088. insert: Meteor.userId,
  1089. update: allowIsBoardAdmin,
  1090. remove: allowIsBoardAdmin,
  1091. fetch: ['members'],
  1092. });
  1093. // All logged in users are allowed to reorder boards by dragging at All Boards page and Public Boards page.
  1094. Boards.allow({
  1095. update(userId, board, fieldNames) {
  1096. return _.contains(fieldNames, 'sort');
  1097. },
  1098. fetch: [],
  1099. });
  1100. // The number of users that have starred this board is managed by trusted code
  1101. // and the user is not allowed to update it
  1102. Boards.deny({
  1103. update(userId, board, fieldNames) {
  1104. return _.contains(fieldNames, 'stars');
  1105. },
  1106. fetch: [],
  1107. });
  1108. // We can't remove a member if it is the last administrator
  1109. Boards.deny({
  1110. update(userId, doc, fieldNames, modifier) {
  1111. if (!_.contains(fieldNames, 'members')) return false;
  1112. // We only care in case of a $pull operation, ie remove a member
  1113. if (!_.isObject(modifier.$pull && modifier.$pull.members)) return false;
  1114. // If there is more than one admin, it's ok to remove anyone
  1115. const nbAdmins = _.where(doc.members, { isActive: true, isAdmin: true })
  1116. .length;
  1117. if (nbAdmins > 1) return false;
  1118. // If all the previous conditions were verified, we can't remove
  1119. // a user if it's an admin
  1120. const removedMemberId = modifier.$pull.members.userId;
  1121. return Boolean(
  1122. _.findWhere(doc.members, {
  1123. userId: removedMemberId,
  1124. isAdmin: true,
  1125. }),
  1126. );
  1127. },
  1128. fetch: ['members'],
  1129. });
  1130. Meteor.methods({
  1131. quitBoard(boardId) {
  1132. check(boardId, String);
  1133. const board = Boards.findOne(boardId);
  1134. if (board) {
  1135. const userId = Meteor.userId();
  1136. const index = board.memberIndex(userId);
  1137. if (index >= 0) {
  1138. board.removeMember(userId);
  1139. return true;
  1140. } else throw new Meteor.Error('error-board-notAMember');
  1141. } else throw new Meteor.Error('error-board-doesNotExist');
  1142. },
  1143. acceptInvite(boardId) {
  1144. check(boardId, String);
  1145. const board = Boards.findOne(boardId);
  1146. if (!board) {
  1147. throw new Meteor.Error('error-board-doesNotExist');
  1148. }
  1149. Meteor.users.update(Meteor.userId(), {
  1150. $pull: {
  1151. 'profile.invitedBoards': boardId,
  1152. },
  1153. });
  1154. },
  1155. });
  1156. Meteor.methods({
  1157. archiveBoard(boardId) {
  1158. check(boardId, String);
  1159. const board = Boards.findOne(boardId);
  1160. if (board) {
  1161. const userId = Meteor.userId();
  1162. const index = board.memberIndex(userId);
  1163. if (index >= 0) {
  1164. board.archive();
  1165. return true;
  1166. } else throw new Meteor.Error('error-board-notAMember');
  1167. } else throw new Meteor.Error('error-board-doesNotExist');
  1168. },
  1169. });
  1170. }
  1171. // Insert new board at last position in sort order.
  1172. Boards.before.insert((userId, doc) => {
  1173. const lastBoard = Boards.findOne(
  1174. { sort: { $exists: true } },
  1175. { sort: { sort: -1 } },
  1176. );
  1177. if (lastBoard && typeof lastBoard.sort !== 'undefined') {
  1178. doc.sort = lastBoard.sort + 1;
  1179. }
  1180. });
  1181. if (Meteor.isServer) {
  1182. // Let MongoDB ensure that a member is not included twice in the same board
  1183. Meteor.startup(() => {
  1184. Boards._collection._ensureIndex({ modifiedAt: -1 });
  1185. Boards._collection._ensureIndex(
  1186. {
  1187. _id: 1,
  1188. 'members.userId': 1,
  1189. },
  1190. { unique: true },
  1191. );
  1192. Boards._collection._ensureIndex({ 'members.userId': 1 });
  1193. });
  1194. // Genesis: the first activity of the newly created board
  1195. Boards.after.insert((userId, doc) => {
  1196. Activities.insert({
  1197. userId,
  1198. type: 'board',
  1199. activityTypeId: doc._id,
  1200. activityType: 'createBoard',
  1201. boardId: doc._id,
  1202. });
  1203. });
  1204. // If the user remove one label from a board, we cant to remove reference of
  1205. // this label in any card of this board.
  1206. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1207. if (
  1208. !_.contains(fieldNames, 'labels') ||
  1209. !modifier.$pull ||
  1210. !modifier.$pull.labels ||
  1211. !modifier.$pull.labels._id
  1212. ) {
  1213. return;
  1214. }
  1215. const removedLabelId = modifier.$pull.labels._id;
  1216. Cards.update(
  1217. { boardId: doc._id },
  1218. {
  1219. $pull: {
  1220. labelIds: removedLabelId,
  1221. },
  1222. },
  1223. { multi: true },
  1224. );
  1225. });
  1226. const foreachRemovedMember = (doc, modifier, callback) => {
  1227. Object.keys(modifier).forEach(set => {
  1228. if (modifier[set] !== false) {
  1229. return;
  1230. }
  1231. const parts = set.split('.');
  1232. if (
  1233. parts.length === 3 &&
  1234. parts[0] === 'members' &&
  1235. parts[2] === 'isActive'
  1236. ) {
  1237. callback(doc.members[parts[1]].userId);
  1238. }
  1239. });
  1240. };
  1241. // Remove a member from all objects of the board before leaving the board
  1242. Boards.before.update((userId, doc, fieldNames, modifier) => {
  1243. if (!_.contains(fieldNames, 'members')) {
  1244. return;
  1245. }
  1246. if (modifier.$set) {
  1247. const boardId = doc._id;
  1248. foreachRemovedMember(doc, modifier.$set, memberId => {
  1249. Cards.update(
  1250. { boardId },
  1251. {
  1252. $pull: {
  1253. members: memberId,
  1254. watchers: memberId,
  1255. },
  1256. },
  1257. { multi: true },
  1258. );
  1259. Lists.update(
  1260. { boardId },
  1261. {
  1262. $pull: {
  1263. watchers: memberId,
  1264. },
  1265. },
  1266. { multi: true },
  1267. );
  1268. const board = Boards._transform(doc);
  1269. board.setWatcher(memberId, false);
  1270. // Remove board from users starred list
  1271. if (!board.isPublic()) {
  1272. Users.update(memberId, {
  1273. $pull: {
  1274. 'profile.starredBoards': boardId,
  1275. },
  1276. });
  1277. }
  1278. });
  1279. }
  1280. });
  1281. Boards.before.remove((userId, doc) => {
  1282. boardRemover(userId, doc);
  1283. // Add removeBoard activity to keep it
  1284. Activities.insert({
  1285. userId,
  1286. type: 'board',
  1287. activityTypeId: doc._id,
  1288. activityType: 'removeBoard',
  1289. boardId: doc._id,
  1290. });
  1291. });
  1292. // Add a new activity if we add or remove a member to the board
  1293. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1294. if (!_.contains(fieldNames, 'members')) {
  1295. return;
  1296. }
  1297. // Say hello to the new member
  1298. if (modifier.$push && modifier.$push.members) {
  1299. const memberId = modifier.$push.members.userId;
  1300. Activities.insert({
  1301. userId,
  1302. memberId,
  1303. type: 'member',
  1304. activityType: 'addBoardMember',
  1305. boardId: doc._id,
  1306. });
  1307. }
  1308. // Say goodbye to the former member
  1309. if (modifier.$set) {
  1310. foreachRemovedMember(doc, modifier.$set, memberId => {
  1311. Activities.insert({
  1312. userId,
  1313. memberId,
  1314. type: 'member',
  1315. activityType: 'removeBoardMember',
  1316. boardId: doc._id,
  1317. });
  1318. });
  1319. }
  1320. });
  1321. }
  1322. //BOARDS REST API
  1323. if (Meteor.isServer) {
  1324. /**
  1325. * @operation get_boards_from_user
  1326. * @summary Get all boards attached to a user
  1327. *
  1328. * @param {string} userId the ID of the user to retrieve the data
  1329. * @return_type [{_id: string,
  1330. title: string}]
  1331. */
  1332. JsonRoutes.add('GET', '/api/users/:userId/boards', function(req, res) {
  1333. try {
  1334. Authentication.checkLoggedIn(req.userId);
  1335. const paramUserId = req.params.userId;
  1336. // A normal user should be able to see their own boards,
  1337. // admins can access boards of any user
  1338. Authentication.checkAdminOrCondition(
  1339. req.userId,
  1340. req.userId === paramUserId,
  1341. );
  1342. const data = Boards.find(
  1343. {
  1344. archived: false,
  1345. 'members.userId': paramUserId,
  1346. },
  1347. {
  1348. sort: { sort: 1 /* boards default sorting */ },
  1349. },
  1350. ).map(function(board) {
  1351. return {
  1352. _id: board._id,
  1353. title: board.title,
  1354. };
  1355. });
  1356. JsonRoutes.sendResult(res, { code: 200, data });
  1357. } catch (error) {
  1358. JsonRoutes.sendResult(res, {
  1359. code: 200,
  1360. data: error,
  1361. });
  1362. }
  1363. });
  1364. /**
  1365. * @operation get_public_boards
  1366. * @summary Get all public boards
  1367. *
  1368. * @return_type [{_id: string,
  1369. title: string}]
  1370. */
  1371. JsonRoutes.add('GET', '/api/boards', function(req, res) {
  1372. try {
  1373. Authentication.checkUserId(req.userId);
  1374. JsonRoutes.sendResult(res, {
  1375. code: 200,
  1376. data: Boards.find(
  1377. { permission: 'public' },
  1378. {
  1379. sort: { sort: 1 /* boards default sorting */ },
  1380. },
  1381. ).map(function(doc) {
  1382. return {
  1383. _id: doc._id,
  1384. title: doc.title,
  1385. };
  1386. }),
  1387. });
  1388. } catch (error) {
  1389. JsonRoutes.sendResult(res, {
  1390. code: 200,
  1391. data: error,
  1392. });
  1393. }
  1394. });
  1395. /**
  1396. * @operation get_board
  1397. * @summary Get the board with that particular ID
  1398. *
  1399. * @param {string} boardId the ID of the board to retrieve the data
  1400. * @return_type Boards
  1401. */
  1402. JsonRoutes.add('GET', '/api/boards/:boardId', function(req, res) {
  1403. try {
  1404. const id = req.params.boardId;
  1405. Authentication.checkBoardAccess(req.userId, id);
  1406. JsonRoutes.sendResult(res, {
  1407. code: 200,
  1408. data: Boards.findOne({ _id: id }),
  1409. });
  1410. } catch (error) {
  1411. JsonRoutes.sendResult(res, {
  1412. code: 200,
  1413. data: error,
  1414. });
  1415. }
  1416. });
  1417. /**
  1418. * @operation new_board
  1419. * @summary Create a board
  1420. *
  1421. * @description This allows to create a board.
  1422. *
  1423. * The color has to be chosen between `belize`, `nephritis`, `pomegranate`,
  1424. * `pumpkin`, `wisteria`, `moderatepink`, `strongcyan`,
  1425. * `limegreen`, `midnight`, `dark`, `relax`, `corteza`:
  1426. *
  1427. * <img src="https://wekan.github.io/board-colors.png" width="40%" alt="Wekan logo" />
  1428. *
  1429. * @param {string} title the new title of the board
  1430. * @param {string} owner "ABCDE12345" <= User ID in Wekan.
  1431. * (Not username or email)
  1432. * @param {boolean} [isAdmin] is the owner an admin of the board (default true)
  1433. * @param {boolean} [isActive] is the board active (default true)
  1434. * @param {boolean} [isNoComments] disable comments (default false)
  1435. * @param {boolean} [isCommentOnly] only enable comments (default false)
  1436. * @param {boolean} [isWorker] only move cards, assign himself to card and comment (default false)
  1437. * @param {string} [permission] "private" board <== Set to "public" if you
  1438. * want public Wekan board
  1439. * @param {string} [color] the color of the board
  1440. *
  1441. * @return_type {_id: string,
  1442. defaultSwimlaneId: string}
  1443. */
  1444. JsonRoutes.add('POST', '/api/boards', function(req, res) {
  1445. try {
  1446. Authentication.checkUserId(req.userId);
  1447. const id = Boards.insert({
  1448. title: req.body.title,
  1449. members: [
  1450. {
  1451. userId: req.body.owner,
  1452. isAdmin: req.body.isAdmin || true,
  1453. isActive: req.body.isActive || true,
  1454. isNoComments: req.body.isNoComments || false,
  1455. isCommentOnly: req.body.isCommentOnly || false,
  1456. isWorker: req.body.isWorker || false,
  1457. },
  1458. ],
  1459. permission: req.body.permission || 'private',
  1460. color: req.body.color || 'belize',
  1461. });
  1462. const swimlaneId = Swimlanes.insert({
  1463. title: TAPi18n.__('default'),
  1464. boardId: id,
  1465. });
  1466. JsonRoutes.sendResult(res, {
  1467. code: 200,
  1468. data: {
  1469. _id: id,
  1470. defaultSwimlaneId: swimlaneId,
  1471. },
  1472. });
  1473. } catch (error) {
  1474. JsonRoutes.sendResult(res, {
  1475. code: 200,
  1476. data: error,
  1477. });
  1478. }
  1479. });
  1480. /**
  1481. * @operation delete_board
  1482. * @summary Delete a board
  1483. *
  1484. * @param {string} boardId the ID of the board
  1485. */
  1486. JsonRoutes.add('DELETE', '/api/boards/:boardId', function(req, res) {
  1487. try {
  1488. Authentication.checkUserId(req.userId);
  1489. const id = req.params.boardId;
  1490. Boards.remove({ _id: id });
  1491. JsonRoutes.sendResult(res, {
  1492. code: 200,
  1493. data: {
  1494. _id: id,
  1495. },
  1496. });
  1497. } catch (error) {
  1498. JsonRoutes.sendResult(res, {
  1499. code: 200,
  1500. data: error,
  1501. });
  1502. }
  1503. });
  1504. /**
  1505. * @operation add_board_label
  1506. * @summary Add a label to a board
  1507. *
  1508. * @description If the board doesn't have the name/color label, this function
  1509. * adds the label to the board.
  1510. *
  1511. * @param {string} boardId the board
  1512. * @param {string} color the color of the new label
  1513. * @param {string} name the name of the new label
  1514. *
  1515. * @return_type string
  1516. */
  1517. JsonRoutes.add('PUT', '/api/boards/:boardId/labels', function(req, res) {
  1518. Authentication.checkUserId(req.userId);
  1519. const id = req.params.boardId;
  1520. try {
  1521. if (req.body.hasOwnProperty('label')) {
  1522. const board = Boards.findOne({ _id: id });
  1523. const color = req.body.label.color;
  1524. const name = req.body.label.name;
  1525. const labelId = Random.id(6);
  1526. if (!board.getLabel(name, color)) {
  1527. Boards.direct.update(
  1528. { _id: id },
  1529. { $push: { labels: { _id: labelId, name, color } } },
  1530. );
  1531. JsonRoutes.sendResult(res, {
  1532. code: 200,
  1533. data: labelId,
  1534. });
  1535. } else {
  1536. JsonRoutes.sendResult(res, {
  1537. code: 200,
  1538. });
  1539. }
  1540. }
  1541. } catch (error) {
  1542. JsonRoutes.sendResult(res, {
  1543. data: error,
  1544. });
  1545. }
  1546. });
  1547. /**
  1548. * @operation set_board_member_permission
  1549. * @tag Users
  1550. * @summary Change the permission of a member of a board
  1551. *
  1552. * @param {string} boardId the ID of the board that we are changing
  1553. * @param {string} memberId the ID of the user to change permissions
  1554. * @param {boolean} isAdmin admin capability
  1555. * @param {boolean} isNoComments NoComments capability
  1556. * @param {boolean} isCommentOnly CommentsOnly capability
  1557. * @param {boolean} isWorker Worker capability
  1558. */
  1559. JsonRoutes.add('POST', '/api/boards/:boardId/members/:memberId', function(
  1560. req,
  1561. res,
  1562. ) {
  1563. try {
  1564. const boardId = req.params.boardId;
  1565. const memberId = req.params.memberId;
  1566. const { isAdmin, isNoComments, isCommentOnly, isWorker } = req.body;
  1567. Authentication.checkBoardAccess(req.userId, boardId);
  1568. const board = Boards.findOne({ _id: boardId });
  1569. function isTrue(data) {
  1570. try {
  1571. return data.toLowerCase() === 'true';
  1572. } catch (error) {
  1573. return data;
  1574. }
  1575. }
  1576. const query = board.setMemberPermission(
  1577. memberId,
  1578. isTrue(isAdmin),
  1579. isTrue(isNoComments),
  1580. isTrue(isCommentOnly),
  1581. isTrue(isWorker),
  1582. req.userId,
  1583. );
  1584. JsonRoutes.sendResult(res, {
  1585. code: 200,
  1586. data: query,
  1587. });
  1588. } catch (error) {
  1589. JsonRoutes.sendResult(res, {
  1590. code: 200,
  1591. data: error,
  1592. });
  1593. }
  1594. });
  1595. //ATTACHMENTS REST API
  1596. /**
  1597. * @operation get_board_attachments
  1598. * @summary Get the list of attachments of a board
  1599. *
  1600. * @param {string} boardId the board ID
  1601. * @return_type [{attachmentId: string,
  1602. * attachmentName: string,
  1603. * attachmentType: string,
  1604. * cardId: string,
  1605. * listId: string,
  1606. * swimlaneId: string}]
  1607. */
  1608. JsonRoutes.add('GET', '/api/boards/:boardId/attachments', function(req, res) {
  1609. const paramBoardId = req.params.boardId;
  1610. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1611. JsonRoutes.sendResult(res, {
  1612. code: 200,
  1613. data: Attachments.files
  1614. .find({ boardId: paramBoardId }, { fields: { boardId: 0 } })
  1615. .map(function(doc) {
  1616. return {
  1617. attachmentId: doc._id,
  1618. attachmentName: doc.original.name,
  1619. attachmentType: doc.original.type,
  1620. url: FlowRouter.url(doc.url()),
  1621. urlDownload: `${FlowRouter.url(doc.url())}?&download=true&token=`,
  1622. cardId: doc.cardId,
  1623. listId: doc.listId,
  1624. swimlaneId: doc.swimlaneId,
  1625. };
  1626. }),
  1627. });
  1628. });
  1629. }
  1630. export default Boards;