boards.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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 = [
  739. { title: regex },
  740. { description: regex },
  741. { customFields: { $elemMatch: { value: regex } } },
  742. ];
  743. }
  744. return Cards.find(query, projection);
  745. },
  746. // A board alwasy has another board where it deposits subtasks of thasks
  747. // that belong to itself.
  748. getDefaultSubtasksBoardId() {
  749. if (
  750. this.subtasksDefaultBoardId === null ||
  751. this.subtasksDefaultBoardId === undefined
  752. ) {
  753. this.subtasksDefaultBoardId = Boards.insert({
  754. title: `^${this.title}^`,
  755. permission: this.permission,
  756. members: this.members,
  757. color: this.color,
  758. description: TAPi18n.__('default-subtasks-board', {
  759. board: this.title,
  760. }),
  761. });
  762. Swimlanes.insert({
  763. title: TAPi18n.__('default'),
  764. boardId: this.subtasksDefaultBoardId,
  765. });
  766. Boards.update(this._id, {
  767. $set: {
  768. subtasksDefaultBoardId: this.subtasksDefaultBoardId,
  769. },
  770. });
  771. }
  772. return this.subtasksDefaultBoardId;
  773. },
  774. getDefaultSubtasksBoard() {
  775. return Boards.findOne(this.getDefaultSubtasksBoardId());
  776. },
  777. //Date Settings option such as received date, start date and so on.
  778. getDefaultDateSettingsBoardId() {
  779. if (
  780. this.dateSettingsDefaultBoardId === null ||
  781. this.dateSettingsDefaultBoardId === undefined
  782. ) {
  783. this.dateSettingsDefaultBoardId = Boards.insert({
  784. title: `^${this.title}^`,
  785. permission: this.permission,
  786. members: this.members,
  787. color: this.color,
  788. description: TAPi18n.__('default-dates-board', {
  789. board: this.title,
  790. }),
  791. });
  792. Swimlanes.insert({
  793. title: TAPi18n.__('default'),
  794. boardId: this.dateSettingsDefaultBoardId,
  795. });
  796. Boards.update(this._id, {
  797. $set: {
  798. dateSettingsDefaultBoardId: this.dateSettingsDefaultBoardId,
  799. },
  800. });
  801. }
  802. return this.dateSettingsDefaultBoardId;
  803. },
  804. getDefaultDateSettingsBoard() {
  805. return Boards.findOne(this.getDefaultDateSettingsBoardId());
  806. },
  807. getDefaultSubtasksListId() {
  808. if (
  809. this.subtasksDefaultListId === null ||
  810. this.subtasksDefaultListId === undefined
  811. ) {
  812. this.subtasksDefaultListId = Lists.insert({
  813. title: TAPi18n.__('queue'),
  814. boardId: this._id,
  815. });
  816. this.setSubtasksDefaultListId(this.subtasksDefaultListId);
  817. }
  818. return this.subtasksDefaultListId;
  819. },
  820. getDefaultSubtasksList() {
  821. return Lists.findOne(this.getDefaultSubtasksListId());
  822. },
  823. getDefaultDateSettingsListId() {
  824. if (
  825. this.dateSettingsDefaultListId === null ||
  826. this.dateSettingsDefaultListId === undefined
  827. ) {
  828. this.dateSettingsDefaultListId = Lists.insert({
  829. title: TAPi18n.__('queue'),
  830. boardId: this._id,
  831. });
  832. this.setDateSettingsDefaultListId(this.dateSettingsDefaultListId);
  833. }
  834. return this.dateSettingsDefaultListId;
  835. },
  836. getDefaultDateSettingsList() {
  837. return Lists.findOne(this.getDefaultDateSettingsListId());
  838. },
  839. getDefaultSwimline() {
  840. let result = Swimlanes.findOne({ boardId: this._id });
  841. if (result === undefined) {
  842. Swimlanes.insert({
  843. title: TAPi18n.__('default'),
  844. boardId: this._id,
  845. });
  846. result = Swimlanes.findOne({ boardId: this._id });
  847. }
  848. return result;
  849. },
  850. cardsDueInBetween(start, end) {
  851. return Cards.find({
  852. boardId: this._id,
  853. dueAt: { $gte: start, $lte: end },
  854. });
  855. },
  856. cardsInInterval(start, end) {
  857. return Cards.find({
  858. boardId: this._id,
  859. $or: [
  860. {
  861. startAt: {
  862. $lte: start,
  863. },
  864. endAt: {
  865. $gte: start,
  866. },
  867. },
  868. {
  869. startAt: {
  870. $lte: end,
  871. },
  872. endAt: {
  873. $gte: end,
  874. },
  875. },
  876. {
  877. startAt: {
  878. $gte: start,
  879. },
  880. endAt: {
  881. $lte: end,
  882. },
  883. },
  884. ],
  885. });
  886. },
  887. isTemplateBoard() {
  888. return this.type === 'template-board';
  889. },
  890. isTemplatesBoard() {
  891. return this.type === 'template-container';
  892. },
  893. });
  894. Boards.mutations({
  895. archive() {
  896. return { $set: { archived: true } };
  897. },
  898. restore() {
  899. return { $set: { archived: false } };
  900. },
  901. rename(title) {
  902. return { $set: { title } };
  903. },
  904. setDescription(description) {
  905. return { $set: { description } };
  906. },
  907. setColor(color) {
  908. return { $set: { color } };
  909. },
  910. setVisibility(visibility) {
  911. return { $set: { permission: visibility } };
  912. },
  913. addLabel(name, color) {
  914. // If label with the same name and color already exists we don't want to
  915. // create another one because they would be indistinguishable in the UI
  916. // (they would still have different `_id` but that is not exposed to the
  917. // user).
  918. if (!this.getLabel(name, color)) {
  919. const _id = Random.id(6);
  920. return { $push: { labels: { _id, name, color } } };
  921. }
  922. return {};
  923. },
  924. editLabel(labelId, name, color) {
  925. if (!this.getLabel(name, color)) {
  926. const labelIndex = this.labelIndex(labelId);
  927. return {
  928. $set: {
  929. [`labels.${labelIndex}.name`]: name,
  930. [`labels.${labelIndex}.color`]: color,
  931. },
  932. };
  933. }
  934. return {};
  935. },
  936. removeLabel(labelId) {
  937. return { $pull: { labels: { _id: labelId } } };
  938. },
  939. changeOwnership(fromId, toId) {
  940. const memberIndex = this.memberIndex(fromId);
  941. return {
  942. $set: {
  943. [`members.${memberIndex}.userId`]: toId,
  944. },
  945. };
  946. },
  947. addMember(memberId) {
  948. const memberIndex = this.memberIndex(memberId);
  949. if (memberIndex >= 0) {
  950. return {
  951. $set: {
  952. [`members.${memberIndex}.isActive`]: true,
  953. },
  954. };
  955. }
  956. return {
  957. $push: {
  958. members: {
  959. userId: memberId,
  960. isAdmin: false,
  961. isActive: true,
  962. isNoComments: false,
  963. isCommentOnly: false,
  964. isWorker: false,
  965. },
  966. },
  967. };
  968. },
  969. removeMember(memberId) {
  970. const memberIndex = this.memberIndex(memberId);
  971. // we do not allow the only one admin to be removed
  972. const allowRemove =
  973. !this.members[memberIndex].isAdmin || this.activeAdmins().length > 1;
  974. if (!allowRemove) {
  975. return {
  976. $set: {
  977. [`members.${memberIndex}.isActive`]: true,
  978. },
  979. };
  980. }
  981. return {
  982. $set: {
  983. [`members.${memberIndex}.isActive`]: false,
  984. [`members.${memberIndex}.isAdmin`]: false,
  985. },
  986. };
  987. },
  988. setMemberPermission(
  989. memberId,
  990. isAdmin,
  991. isNoComments,
  992. isCommentOnly,
  993. isWorker,
  994. currentUserId = Meteor.userId(),
  995. ) {
  996. const memberIndex = this.memberIndex(memberId);
  997. // do not allow change permission of self
  998. if (memberId === currentUserId) {
  999. isAdmin = this.members[memberIndex].isAdmin;
  1000. }
  1001. return {
  1002. $set: {
  1003. [`members.${memberIndex}.isAdmin`]: isAdmin,
  1004. [`members.${memberIndex}.isNoComments`]: isNoComments,
  1005. [`members.${memberIndex}.isCommentOnly`]: isCommentOnly,
  1006. [`members.${memberIndex}.isWorker`]: isWorker,
  1007. },
  1008. };
  1009. },
  1010. setAllowsSubtasks(allowsSubtasks) {
  1011. return { $set: { allowsSubtasks } };
  1012. },
  1013. setAllowsMembers(allowsMembers) {
  1014. return { $set: { allowsMembers } };
  1015. },
  1016. setAllowsChecklists(allowsChecklists) {
  1017. return { $set: { allowsChecklists } };
  1018. },
  1019. setAllowsAssignee(allowsAssignee) {
  1020. return { $set: { allowsAssignee } };
  1021. },
  1022. setAllowsAssignedBy(allowsAssignedBy) {
  1023. return { $set: { allowsAssignedBy } };
  1024. },
  1025. setAllowsRequestedBy(allowsRequestedBy) {
  1026. return { $set: { allowsRequestedBy } };
  1027. },
  1028. setAllowsAttachments(allowsAttachments) {
  1029. return { $set: { allowsAttachments } };
  1030. },
  1031. setAllowsLabels(allowsLabels) {
  1032. return { $set: { allowsLabels } };
  1033. },
  1034. setAllowsComments(allowsComments) {
  1035. return { $set: { allowsComments } };
  1036. },
  1037. setAllowsDescriptionTitle(allowsDescriptionTitle) {
  1038. return { $set: { allowsDescriptionTitle } };
  1039. },
  1040. setAllowsDescriptionText(allowsDescriptionText) {
  1041. return { $set: { allowsDescriptionText } };
  1042. },
  1043. setAllowsActivities(allowsActivities) {
  1044. return { $set: { allowsActivities } };
  1045. },
  1046. setAllowsReceivedDate(allowsReceivedDate) {
  1047. return { $set: { allowsReceivedDate } };
  1048. },
  1049. setAllowsStartDate(allowsStartDate) {
  1050. return { $set: { allowsStartDate } };
  1051. },
  1052. setAllowsEndDate(allowsEndDate) {
  1053. return { $set: { allowsEndDate } };
  1054. },
  1055. setAllowsDueDate(allowsDueDate) {
  1056. return { $set: { allowsDueDate } };
  1057. },
  1058. setSubtasksDefaultBoardId(subtasksDefaultBoardId) {
  1059. return { $set: { subtasksDefaultBoardId } };
  1060. },
  1061. setSubtasksDefaultListId(subtasksDefaultListId) {
  1062. return { $set: { subtasksDefaultListId } };
  1063. },
  1064. setPresentParentTask(presentParentTask) {
  1065. return { $set: { presentParentTask } };
  1066. },
  1067. });
  1068. function boardRemover(userId, doc) {
  1069. [Cards, Lists, Swimlanes, Integrations, Rules, Activities].forEach(
  1070. element => {
  1071. element.remove({ boardId: doc._id });
  1072. },
  1073. );
  1074. }
  1075. if (Meteor.isServer) {
  1076. Boards.allow({
  1077. insert: Meteor.userId,
  1078. update: allowIsBoardAdmin,
  1079. remove: allowIsBoardAdmin,
  1080. fetch: ['members'],
  1081. });
  1082. // The number of users that have starred this board is managed by trusted code
  1083. // and the user is not allowed to update it
  1084. Boards.deny({
  1085. update(userId, board, fieldNames) {
  1086. return _.contains(fieldNames, 'stars');
  1087. },
  1088. fetch: [],
  1089. });
  1090. // We can't remove a member if it is the last administrator
  1091. Boards.deny({
  1092. update(userId, doc, fieldNames, modifier) {
  1093. if (!_.contains(fieldNames, 'members')) return false;
  1094. // We only care in case of a $pull operation, ie remove a member
  1095. if (!_.isObject(modifier.$pull && modifier.$pull.members)) return false;
  1096. // If there is more than one admin, it's ok to remove anyone
  1097. const nbAdmins = _.where(doc.members, { isActive: true, isAdmin: true })
  1098. .length;
  1099. if (nbAdmins > 1) return false;
  1100. // If all the previous conditions were verified, we can't remove
  1101. // a user if it's an admin
  1102. const removedMemberId = modifier.$pull.members.userId;
  1103. return Boolean(
  1104. _.findWhere(doc.members, {
  1105. userId: removedMemberId,
  1106. isAdmin: true,
  1107. }),
  1108. );
  1109. },
  1110. fetch: ['members'],
  1111. });
  1112. Meteor.methods({
  1113. quitBoard(boardId) {
  1114. check(boardId, String);
  1115. const board = Boards.findOne(boardId);
  1116. if (board) {
  1117. const userId = Meteor.userId();
  1118. const index = board.memberIndex(userId);
  1119. if (index >= 0) {
  1120. board.removeMember(userId);
  1121. return true;
  1122. } else throw new Meteor.Error('error-board-notAMember');
  1123. } else throw new Meteor.Error('error-board-doesNotExist');
  1124. },
  1125. acceptInvite(boardId) {
  1126. check(boardId, String);
  1127. const board = Boards.findOne(boardId);
  1128. if (!board) {
  1129. throw new Meteor.Error('error-board-doesNotExist');
  1130. }
  1131. Meteor.users.update(Meteor.userId(), {
  1132. $pull: {
  1133. 'profile.invitedBoards': boardId,
  1134. },
  1135. });
  1136. },
  1137. });
  1138. Meteor.methods({
  1139. archiveBoard(boardId) {
  1140. check(boardId, String);
  1141. const board = Boards.findOne(boardId);
  1142. if (board) {
  1143. const userId = Meteor.userId();
  1144. const index = board.memberIndex(userId);
  1145. if (index >= 0) {
  1146. board.archive();
  1147. return true;
  1148. } else throw new Meteor.Error('error-board-notAMember');
  1149. } else throw new Meteor.Error('error-board-doesNotExist');
  1150. },
  1151. });
  1152. }
  1153. if (Meteor.isServer) {
  1154. // Let MongoDB ensure that a member is not included twice in the same board
  1155. Meteor.startup(() => {
  1156. Boards._collection._ensureIndex({ modifiedAt: -1 });
  1157. Boards._collection._ensureIndex(
  1158. {
  1159. _id: 1,
  1160. 'members.userId': 1,
  1161. },
  1162. { unique: true },
  1163. );
  1164. Boards._collection._ensureIndex({ 'members.userId': 1 });
  1165. });
  1166. // Genesis: the first activity of the newly created board
  1167. Boards.after.insert((userId, doc) => {
  1168. Activities.insert({
  1169. userId,
  1170. type: 'board',
  1171. activityTypeId: doc._id,
  1172. activityType: 'createBoard',
  1173. boardId: doc._id,
  1174. });
  1175. });
  1176. // If the user remove one label from a board, we cant to remove reference of
  1177. // this label in any card of this board.
  1178. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1179. if (
  1180. !_.contains(fieldNames, 'labels') ||
  1181. !modifier.$pull ||
  1182. !modifier.$pull.labels ||
  1183. !modifier.$pull.labels._id
  1184. ) {
  1185. return;
  1186. }
  1187. const removedLabelId = modifier.$pull.labels._id;
  1188. Cards.update(
  1189. { boardId: doc._id },
  1190. {
  1191. $pull: {
  1192. labelIds: removedLabelId,
  1193. },
  1194. },
  1195. { multi: true },
  1196. );
  1197. });
  1198. const foreachRemovedMember = (doc, modifier, callback) => {
  1199. Object.keys(modifier).forEach(set => {
  1200. if (modifier[set] !== false) {
  1201. return;
  1202. }
  1203. const parts = set.split('.');
  1204. if (
  1205. parts.length === 3 &&
  1206. parts[0] === 'members' &&
  1207. parts[2] === 'isActive'
  1208. ) {
  1209. callback(doc.members[parts[1]].userId);
  1210. }
  1211. });
  1212. };
  1213. // Remove a member from all objects of the board before leaving the board
  1214. Boards.before.update((userId, doc, fieldNames, modifier) => {
  1215. if (!_.contains(fieldNames, 'members')) {
  1216. return;
  1217. }
  1218. if (modifier.$set) {
  1219. const boardId = doc._id;
  1220. foreachRemovedMember(doc, modifier.$set, memberId => {
  1221. Cards.update(
  1222. { boardId },
  1223. {
  1224. $pull: {
  1225. members: memberId,
  1226. watchers: memberId,
  1227. },
  1228. },
  1229. { multi: true },
  1230. );
  1231. Lists.update(
  1232. { boardId },
  1233. {
  1234. $pull: {
  1235. watchers: memberId,
  1236. },
  1237. },
  1238. { multi: true },
  1239. );
  1240. const board = Boards._transform(doc);
  1241. board.setWatcher(memberId, false);
  1242. // Remove board from users starred list
  1243. if (!board.isPublic()) {
  1244. Users.update(memberId, {
  1245. $pull: {
  1246. 'profile.starredBoards': boardId,
  1247. },
  1248. });
  1249. }
  1250. });
  1251. }
  1252. });
  1253. Boards.before.remove((userId, doc) => {
  1254. boardRemover(userId, doc);
  1255. // Add removeBoard activity to keep it
  1256. Activities.insert({
  1257. userId,
  1258. type: 'board',
  1259. activityTypeId: doc._id,
  1260. activityType: 'removeBoard',
  1261. boardId: doc._id,
  1262. });
  1263. });
  1264. // Add a new activity if we add or remove a member to the board
  1265. Boards.after.update((userId, doc, fieldNames, modifier) => {
  1266. if (!_.contains(fieldNames, 'members')) {
  1267. return;
  1268. }
  1269. // Say hello to the new member
  1270. if (modifier.$push && modifier.$push.members) {
  1271. const memberId = modifier.$push.members.userId;
  1272. Activities.insert({
  1273. userId,
  1274. memberId,
  1275. type: 'member',
  1276. activityType: 'addBoardMember',
  1277. boardId: doc._id,
  1278. });
  1279. }
  1280. // Say goodbye to the former member
  1281. if (modifier.$set) {
  1282. foreachRemovedMember(doc, modifier.$set, memberId => {
  1283. Activities.insert({
  1284. userId,
  1285. memberId,
  1286. type: 'member',
  1287. activityType: 'removeBoardMember',
  1288. boardId: doc._id,
  1289. });
  1290. });
  1291. }
  1292. });
  1293. }
  1294. //BOARDS REST API
  1295. if (Meteor.isServer) {
  1296. /**
  1297. * @operation get_boards_from_user
  1298. * @summary Get all boards attached to a user
  1299. *
  1300. * @param {string} userId the ID of the user to retrieve the data
  1301. * @return_type [{_id: string,
  1302. title: string}]
  1303. */
  1304. JsonRoutes.add('GET', '/api/users/:userId/boards', function(req, res) {
  1305. try {
  1306. Authentication.checkLoggedIn(req.userId);
  1307. const paramUserId = req.params.userId;
  1308. // A normal user should be able to see their own boards,
  1309. // admins can access boards of any user
  1310. Authentication.checkAdminOrCondition(
  1311. req.userId,
  1312. req.userId === paramUserId,
  1313. );
  1314. const data = Boards.find(
  1315. {
  1316. archived: false,
  1317. 'members.userId': paramUserId,
  1318. },
  1319. {
  1320. sort: ['title'],
  1321. },
  1322. ).map(function(board) {
  1323. return {
  1324. _id: board._id,
  1325. title: board.title,
  1326. };
  1327. });
  1328. JsonRoutes.sendResult(res, { code: 200, data });
  1329. } catch (error) {
  1330. JsonRoutes.sendResult(res, {
  1331. code: 200,
  1332. data: error,
  1333. });
  1334. }
  1335. });
  1336. /**
  1337. * @operation get_public_boards
  1338. * @summary Get all public boards
  1339. *
  1340. * @return_type [{_id: string,
  1341. title: string}]
  1342. */
  1343. JsonRoutes.add('GET', '/api/boards', function(req, res) {
  1344. try {
  1345. Authentication.checkUserId(req.userId);
  1346. JsonRoutes.sendResult(res, {
  1347. code: 200,
  1348. data: Boards.find({ permission: 'public' }).map(function(doc) {
  1349. return {
  1350. _id: doc._id,
  1351. title: doc.title,
  1352. };
  1353. }),
  1354. });
  1355. } catch (error) {
  1356. JsonRoutes.sendResult(res, {
  1357. code: 200,
  1358. data: error,
  1359. });
  1360. }
  1361. });
  1362. /**
  1363. * @operation get_board
  1364. * @summary Get the board with that particular ID
  1365. *
  1366. * @param {string} boardId the ID of the board to retrieve the data
  1367. * @return_type Boards
  1368. */
  1369. JsonRoutes.add('GET', '/api/boards/:boardId', function(req, res) {
  1370. try {
  1371. const id = req.params.boardId;
  1372. Authentication.checkBoardAccess(req.userId, id);
  1373. JsonRoutes.sendResult(res, {
  1374. code: 200,
  1375. data: Boards.findOne({ _id: id }),
  1376. });
  1377. } catch (error) {
  1378. JsonRoutes.sendResult(res, {
  1379. code: 200,
  1380. data: error,
  1381. });
  1382. }
  1383. });
  1384. /**
  1385. * @operation new_board
  1386. * @summary Create a board
  1387. *
  1388. * @description This allows to create a board.
  1389. *
  1390. * The color has to be chosen between `belize`, `nephritis`, `pomegranate`,
  1391. * `pumpkin`, `wisteria`, `moderatepink`, `strongcyan`,
  1392. * `limegreen`, `midnight`, `dark`, `relax`, `corteza`:
  1393. *
  1394. * <img src="https://wekan.github.io/board-colors.png" width="40%" alt="Wekan logo" />
  1395. *
  1396. * @param {string} title the new title of the board
  1397. * @param {string} owner "ABCDE12345" <= User ID in Wekan.
  1398. * (Not username or email)
  1399. * @param {boolean} [isAdmin] is the owner an admin of the board (default true)
  1400. * @param {boolean} [isActive] is the board active (default true)
  1401. * @param {boolean} [isNoComments] disable comments (default false)
  1402. * @param {boolean} [isCommentOnly] only enable comments (default false)
  1403. * @param {boolean} [isWorker] only move cards, assign himself to card and comment (default false)
  1404. * @param {string} [permission] "private" board <== Set to "public" if you
  1405. * want public Wekan board
  1406. * @param {string} [color] the color of the board
  1407. *
  1408. * @return_type {_id: string,
  1409. defaultSwimlaneId: string}
  1410. */
  1411. JsonRoutes.add('POST', '/api/boards', function(req, res) {
  1412. try {
  1413. Authentication.checkUserId(req.userId);
  1414. const id = Boards.insert({
  1415. title: req.body.title,
  1416. members: [
  1417. {
  1418. userId: req.body.owner,
  1419. isAdmin: req.body.isAdmin || true,
  1420. isActive: req.body.isActive || true,
  1421. isNoComments: req.body.isNoComments || false,
  1422. isCommentOnly: req.body.isCommentOnly || false,
  1423. isWorker: req.body.isWorker || false,
  1424. },
  1425. ],
  1426. permission: req.body.permission || 'private',
  1427. color: req.body.color || 'belize',
  1428. });
  1429. const swimlaneId = Swimlanes.insert({
  1430. title: TAPi18n.__('default'),
  1431. boardId: id,
  1432. });
  1433. JsonRoutes.sendResult(res, {
  1434. code: 200,
  1435. data: {
  1436. _id: id,
  1437. defaultSwimlaneId: swimlaneId,
  1438. },
  1439. });
  1440. } catch (error) {
  1441. JsonRoutes.sendResult(res, {
  1442. code: 200,
  1443. data: error,
  1444. });
  1445. }
  1446. });
  1447. /**
  1448. * @operation delete_board
  1449. * @summary Delete a board
  1450. *
  1451. * @param {string} boardId the ID of the board
  1452. */
  1453. JsonRoutes.add('DELETE', '/api/boards/:boardId', function(req, res) {
  1454. try {
  1455. Authentication.checkUserId(req.userId);
  1456. const id = req.params.boardId;
  1457. Boards.remove({ _id: id });
  1458. JsonRoutes.sendResult(res, {
  1459. code: 200,
  1460. data: {
  1461. _id: id,
  1462. },
  1463. });
  1464. } catch (error) {
  1465. JsonRoutes.sendResult(res, {
  1466. code: 200,
  1467. data: error,
  1468. });
  1469. }
  1470. });
  1471. /**
  1472. * @operation add_board_label
  1473. * @summary Add a label to a board
  1474. *
  1475. * @description If the board doesn't have the name/color label, this function
  1476. * adds the label to the board.
  1477. *
  1478. * @param {string} boardId the board
  1479. * @param {string} color the color of the new label
  1480. * @param {string} name the name of the new label
  1481. *
  1482. * @return_type string
  1483. */
  1484. JsonRoutes.add('PUT', '/api/boards/:boardId/labels', function(req, res) {
  1485. Authentication.checkUserId(req.userId);
  1486. const id = req.params.boardId;
  1487. try {
  1488. if (req.body.hasOwnProperty('label')) {
  1489. const board = Boards.findOne({ _id: id });
  1490. const color = req.body.label.color;
  1491. const name = req.body.label.name;
  1492. const labelId = Random.id(6);
  1493. if (!board.getLabel(name, color)) {
  1494. Boards.direct.update(
  1495. { _id: id },
  1496. { $push: { labels: { _id: labelId, name, color } } },
  1497. );
  1498. JsonRoutes.sendResult(res, {
  1499. code: 200,
  1500. data: labelId,
  1501. });
  1502. } else {
  1503. JsonRoutes.sendResult(res, {
  1504. code: 200,
  1505. });
  1506. }
  1507. }
  1508. } catch (error) {
  1509. JsonRoutes.sendResult(res, {
  1510. data: error,
  1511. });
  1512. }
  1513. });
  1514. /**
  1515. * @operation set_board_member_permission
  1516. * @tag Users
  1517. * @summary Change the permission of a member of a board
  1518. *
  1519. * @param {string} boardId the ID of the board that we are changing
  1520. * @param {string} memberId the ID of the user to change permissions
  1521. * @param {boolean} isAdmin admin capability
  1522. * @param {boolean} isNoComments NoComments capability
  1523. * @param {boolean} isCommentOnly CommentsOnly capability
  1524. * @param {boolean} isWorker Worker capability
  1525. */
  1526. JsonRoutes.add('POST', '/api/boards/:boardId/members/:memberId', function(
  1527. req,
  1528. res,
  1529. ) {
  1530. try {
  1531. const boardId = req.params.boardId;
  1532. const memberId = req.params.memberId;
  1533. const { isAdmin, isNoComments, isCommentOnly, isWorker } = req.body;
  1534. Authentication.checkBoardAccess(req.userId, boardId);
  1535. const board = Boards.findOne({ _id: boardId });
  1536. function isTrue(data) {
  1537. try {
  1538. return data.toLowerCase() === 'true';
  1539. } catch (error) {
  1540. return data;
  1541. }
  1542. }
  1543. const query = board.setMemberPermission(
  1544. memberId,
  1545. isTrue(isAdmin),
  1546. isTrue(isNoComments),
  1547. isTrue(isCommentOnly),
  1548. isTrue(isWorker),
  1549. req.userId,
  1550. );
  1551. JsonRoutes.sendResult(res, {
  1552. code: 200,
  1553. data: query,
  1554. });
  1555. } catch (error) {
  1556. JsonRoutes.sendResult(res, {
  1557. code: 200,
  1558. data: error,
  1559. });
  1560. }
  1561. });
  1562. }
  1563. export default Boards;