2
0

boards.js 44 KB

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