boards.js 41 KB

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