boards.js 41 KB

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