boards.js 40 KB

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