boards.js 41 KB

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