boards.js 41 KB

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