cards.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  1. Cards = new Mongo.Collection('cards');
  2. // XXX To improve pub/sub performances a card document should include a
  3. // de-normalized number of comments so we don't have to publish the whole list
  4. // of comments just to display the number of them in the board view.
  5. Cards.attachSchema(new SimpleSchema({
  6. title: {
  7. /**
  8. * the title of the card
  9. */
  10. type: String,
  11. optional: true,
  12. defaultValue: '',
  13. },
  14. archived: {
  15. /**
  16. * is the card archived
  17. */
  18. type: Boolean,
  19. autoValue() { // eslint-disable-line consistent-return
  20. if (this.isInsert && !this.isSet) {
  21. return false;
  22. }
  23. },
  24. },
  25. parentId: {
  26. /**
  27. * ID of the parent card
  28. */
  29. type: String,
  30. optional: true,
  31. defaultValue: '',
  32. },
  33. listId: {
  34. /**
  35. * List ID where the card is
  36. */
  37. type: String,
  38. optional: true,
  39. defaultValue: '',
  40. },
  41. swimlaneId: {
  42. /**
  43. * Swimlane ID where the card is
  44. */
  45. type: String,
  46. },
  47. // The system could work without this `boardId` information (we could deduce
  48. // the board identifier from the card), but it would make the system more
  49. // difficult to manage and less efficient.
  50. boardId: {
  51. /**
  52. * Board ID of the card
  53. */
  54. type: String,
  55. optional: true,
  56. defaultValue: '',
  57. },
  58. coverId: {
  59. /**
  60. * Cover ID of the card
  61. */
  62. type: String,
  63. optional: true,
  64. defaultValue: '',
  65. },
  66. color: {
  67. type: String,
  68. optional: true,
  69. allowedValues: [
  70. 'white', 'green', 'yellow', 'orange', 'red', 'purple',
  71. 'blue', 'sky', 'lime', 'pink', 'black',
  72. 'silver', 'peachpuff', 'crimson', 'plum', 'darkgreen',
  73. 'slateblue', 'magenta', 'gold', 'navy', 'gray',
  74. 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo',
  75. ],
  76. },
  77. createdAt: {
  78. /**
  79. * creation date
  80. */
  81. type: Date,
  82. autoValue() { // eslint-disable-line consistent-return
  83. if (this.isInsert) {
  84. return new Date();
  85. } else {
  86. this.unset();
  87. }
  88. },
  89. },
  90. customFields: {
  91. /**
  92. * list of custom fields
  93. */
  94. type: [Object],
  95. optional: true,
  96. defaultValue: [],
  97. },
  98. 'customFields.$': {
  99. type: new SimpleSchema({
  100. _id: {
  101. /**
  102. * the ID of the related custom field
  103. */
  104. type: String,
  105. optional: true,
  106. defaultValue: '',
  107. },
  108. value: {
  109. /**
  110. * value attached to the custom field
  111. */
  112. type: Match.OneOf(String, Number, Boolean, Date),
  113. optional: true,
  114. defaultValue: '',
  115. },
  116. }),
  117. },
  118. dateLastActivity: {
  119. /**
  120. * Date of last activity
  121. */
  122. type: Date,
  123. autoValue() {
  124. return new Date();
  125. },
  126. },
  127. description: {
  128. /**
  129. * description of the card
  130. */
  131. type: String,
  132. optional: true,
  133. defaultValue: '',
  134. },
  135. requestedBy: {
  136. /**
  137. * who requested the card (ID of the user)
  138. */
  139. type: String,
  140. optional: true,
  141. defaultValue: '',
  142. },
  143. assignedBy: {
  144. /**
  145. * who assigned the card (ID of the user)
  146. */
  147. type: String,
  148. optional: true,
  149. defaultValue: '',
  150. },
  151. labelIds: {
  152. /**
  153. * list of labels ID the card has
  154. */
  155. type: [String],
  156. optional: true,
  157. defaultValue: [],
  158. },
  159. members: {
  160. /**
  161. * list of members (user IDs)
  162. */
  163. type: [String],
  164. optional: true,
  165. defaultValue: [],
  166. },
  167. receivedAt: {
  168. /**
  169. * Date the card was received
  170. */
  171. type: Date,
  172. optional: true,
  173. },
  174. startAt: {
  175. /**
  176. * Date the card was started to be worked on
  177. */
  178. type: Date,
  179. optional: true,
  180. },
  181. dueAt: {
  182. /**
  183. * Date the card is due
  184. */
  185. type: Date,
  186. optional: true,
  187. },
  188. endAt: {
  189. /**
  190. * Date the card ended
  191. */
  192. type: Date,
  193. optional: true,
  194. },
  195. spentTime: {
  196. /**
  197. * How much time has been spent on this
  198. */
  199. type: Number,
  200. decimal: true,
  201. optional: true,
  202. defaultValue: 0,
  203. },
  204. isOvertime: {
  205. /**
  206. * is the card over time?
  207. */
  208. type: Boolean,
  209. defaultValue: false,
  210. optional: true,
  211. },
  212. // XXX Should probably be called `authorId`. Is it even needed since we have
  213. // the `members` field?
  214. userId: {
  215. /**
  216. * user ID of the author of the card
  217. */
  218. type: String,
  219. autoValue() { // eslint-disable-line consistent-return
  220. if (this.isInsert && !this.isSet) {
  221. return this.userId;
  222. }
  223. },
  224. },
  225. sort: {
  226. /**
  227. * Sort value
  228. */
  229. type: Number,
  230. decimal: true,
  231. defaultValue: '',
  232. },
  233. subtaskSort: {
  234. /**
  235. * subtask sort value
  236. */
  237. type: Number,
  238. decimal: true,
  239. defaultValue: -1,
  240. optional: true,
  241. },
  242. type: {
  243. /**
  244. * type of the card
  245. */
  246. type: String,
  247. defaultValue: 'cardType-card',
  248. },
  249. linkedId: {
  250. /**
  251. * ID of the linked card
  252. */
  253. type: String,
  254. optional: true,
  255. defaultValue: '',
  256. },
  257. }));
  258. Cards.allow({
  259. insert(userId, doc) {
  260. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  261. },
  262. update(userId, doc) {
  263. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  264. },
  265. remove(userId, doc) {
  266. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  267. },
  268. fetch: ['boardId'],
  269. });
  270. Cards.helpers({
  271. copy(boardId, swimlaneId, listId) {
  272. const oldBoard = Boards.findOne(this.boardId);
  273. const oldBoardLabels = oldBoard.labels;
  274. // Get old label names
  275. const oldCardLabels = _.pluck(_.filter(oldBoardLabels, (label) => {
  276. return _.contains(this.labelIds, label._id);
  277. }), 'name');
  278. const newBoard = Boards.findOne(boardId);
  279. const newBoardLabels = newBoard.labels;
  280. const newCardLabels = _.pluck(_.filter(newBoardLabels, (label) => {
  281. return _.contains(oldCardLabels, label.name);
  282. }), '_id');
  283. const oldId = this._id;
  284. const oldCard = Cards.findOne(oldId);
  285. // Copy Custom Fields
  286. if (oldBoard._id !== boardId) {
  287. CustomFields.find({
  288. _id: {$in: oldCard.customFields.map((cf) => { return cf._id; })},
  289. }).forEach((cf) => {
  290. if (!_.contains(cf.boardIds, boardId))
  291. cf.addBoard(boardId);
  292. });
  293. }
  294. delete this._id;
  295. delete this.labelIds;
  296. this.labelIds = newCardLabels;
  297. this.boardId = boardId;
  298. this.swimlaneId = swimlaneId;
  299. this.listId = listId;
  300. const _id = Cards.insert(this);
  301. // Copy attachments
  302. oldCard.attachments().forEach((att) => {
  303. att.cardId = _id;
  304. delete att._id;
  305. return Attachments.insert(att);
  306. });
  307. // copy checklists
  308. Checklists.find({cardId: oldId}).forEach((ch) => {
  309. ch.copy(_id);
  310. });
  311. // copy subtasks
  312. Cards.find({parentId: oldId}).forEach((subtask) => {
  313. subtask.parentId = _id;
  314. subtask._id = null;
  315. Cards.insert(subtask);
  316. });
  317. // copy card comments
  318. CardComments.find({cardId: oldId}).forEach((cmt) => {
  319. cmt.copy(_id);
  320. });
  321. return _id;
  322. },
  323. list() {
  324. return Lists.findOne(this.listId);
  325. },
  326. board() {
  327. return Boards.findOne(this.boardId);
  328. },
  329. labels() {
  330. const boardLabels = this.board().labels;
  331. const cardLabels = _.filter(boardLabels, (label) => {
  332. return _.contains(this.labelIds, label._id);
  333. });
  334. return cardLabels;
  335. },
  336. hasLabel(labelId) {
  337. return _.contains(this.labelIds, labelId);
  338. },
  339. user() {
  340. return Users.findOne(this.userId);
  341. },
  342. isAssigned(memberId) {
  343. return _.contains(this.getMembers(), memberId);
  344. },
  345. activities() {
  346. if (this.isLinkedCard()) {
  347. return Activities.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  348. } else if (this.isLinkedBoard()) {
  349. return Activities.find({boardId: this.linkedId}, {sort: {createdAt: -1}});
  350. } else {
  351. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  352. }
  353. },
  354. comments() {
  355. if (this.isLinkedCard()) {
  356. return CardComments.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  357. } else {
  358. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  359. }
  360. },
  361. attachments() {
  362. if (this.isLinkedCard()) {
  363. return Attachments.find({cardId: this.linkedId}, {sort: {uploadedAt: -1}});
  364. } else {
  365. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  366. }
  367. },
  368. cover() {
  369. const cover = Attachments.findOne(this.coverId);
  370. // if we return a cover before it is fully stored, we will get errors when we try to display it
  371. // todo XXX we could return a default "upload pending" image in the meantime?
  372. return cover && cover.url() && cover;
  373. },
  374. checklists() {
  375. if (this.isLinkedCard()) {
  376. return Checklists.find({cardId: this.linkedId}, {sort: { sort: 1 } });
  377. } else {
  378. return Checklists.find({cardId: this._id}, {sort: { sort: 1 } });
  379. }
  380. },
  381. checklistItemCount() {
  382. const checklists = this.checklists().fetch();
  383. return checklists.map((checklist) => {
  384. return checklist.itemCount();
  385. }).reduce((prev, next) => {
  386. return prev + next;
  387. }, 0);
  388. },
  389. checklistFinishedCount() {
  390. const checklists = this.checklists().fetch();
  391. return checklists.map((checklist) => {
  392. return checklist.finishedCount();
  393. }).reduce((prev, next) => {
  394. return prev + next;
  395. }, 0);
  396. },
  397. checklistFinished() {
  398. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  399. },
  400. hasChecklist() {
  401. return this.checklistItemCount() !== 0;
  402. },
  403. subtasks() {
  404. return Cards.find({
  405. parentId: this._id,
  406. archived: false,
  407. }, {
  408. sort: {
  409. sort: 1,
  410. },
  411. });
  412. },
  413. allSubtasks() {
  414. return Cards.find({
  415. parentId: this._id,
  416. archived: false,
  417. }, {
  418. sort: {
  419. sort: 1,
  420. },
  421. });
  422. },
  423. subtasksCount() {
  424. return Cards.find({
  425. parentId: this._id,
  426. archived: false,
  427. }).count();
  428. },
  429. subtasksFinishedCount() {
  430. return Cards.find({
  431. parentId: this._id,
  432. archived: true,
  433. }).count();
  434. },
  435. subtasksFinished() {
  436. const finishCount = this.subtasksFinishedCount();
  437. return finishCount > 0 && this.subtasksCount() === finishCount;
  438. },
  439. allowsSubtasks() {
  440. return this.subtasksCount() !== 0;
  441. },
  442. customFieldIndex(customFieldId) {
  443. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  444. },
  445. // customFields with definitions
  446. customFieldsWD() {
  447. // get all definitions
  448. const definitions = CustomFields.find({
  449. boardIds: {$in: [this.boardId]},
  450. }).fetch();
  451. // match right definition to each field
  452. if (!this.customFields) return [];
  453. return this.customFields.map((customField) => {
  454. const definition = definitions.find((definition) => {
  455. return definition._id === customField._id;
  456. });
  457. if (!definition) {
  458. return {};
  459. }
  460. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  461. let trueValue = customField.value;
  462. if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0) {
  463. for (let i = 0; i < definition.settings.dropdownItems.length; i++) {
  464. if (definition.settings.dropdownItems[i]._id === customField.value) {
  465. trueValue = definition.settings.dropdownItems[i].name;
  466. }
  467. }
  468. }
  469. return {
  470. _id: customField._id,
  471. value: customField.value,
  472. trueValue,
  473. definition,
  474. };
  475. });
  476. },
  477. colorClass() {
  478. if (this.color)
  479. return this.color;
  480. return '';
  481. },
  482. absoluteUrl() {
  483. const board = this.board();
  484. return FlowRouter.url('card', {
  485. boardId: board._id,
  486. slug: board.slug,
  487. cardId: this._id,
  488. });
  489. },
  490. canBeRestored() {
  491. const list = Lists.findOne({
  492. _id: this.listId,
  493. });
  494. if (!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()) {
  495. return false;
  496. }
  497. return true;
  498. },
  499. parentCard() {
  500. if (this.parentId === '') {
  501. return null;
  502. }
  503. return Cards.findOne(this.parentId);
  504. },
  505. parentCardName() {
  506. let result = '';
  507. if (this.parentId !== '') {
  508. const card = Cards.findOne(this.parentId);
  509. if (card) {
  510. result = card.title;
  511. }
  512. }
  513. return result;
  514. },
  515. parentListId() {
  516. const result = [];
  517. let crtParentId = this.parentId;
  518. while (crtParentId !== '') {
  519. const crt = Cards.findOne(crtParentId);
  520. if ((crt === null) || (crt === undefined)) {
  521. // maybe it has been deleted
  522. break;
  523. }
  524. if (crtParentId in result) {
  525. // circular reference
  526. break;
  527. }
  528. result.unshift(crtParentId);
  529. crtParentId = crt.parentId;
  530. }
  531. return result;
  532. },
  533. parentList() {
  534. const resultId = [];
  535. const result = [];
  536. let crtParentId = this.parentId;
  537. while (crtParentId !== '') {
  538. const crt = Cards.findOne(crtParentId);
  539. if ((crt === null) || (crt === undefined)) {
  540. // maybe it has been deleted
  541. break;
  542. }
  543. if (crtParentId in resultId) {
  544. // circular reference
  545. break;
  546. }
  547. resultId.unshift(crtParentId);
  548. result.unshift(crt);
  549. crtParentId = crt.parentId;
  550. }
  551. return result;
  552. },
  553. parentString(sep) {
  554. return this.parentList().map(function(elem) {
  555. return elem.title;
  556. }).join(sep);
  557. },
  558. isTopLevel() {
  559. return this.parentId === '';
  560. },
  561. isLinkedCard() {
  562. return this.type === 'cardType-linkedCard';
  563. },
  564. isLinkedBoard() {
  565. return this.type === 'cardType-linkedBoard';
  566. },
  567. isLinked() {
  568. return this.isLinkedCard() || this.isLinkedBoard();
  569. },
  570. setDescription(description) {
  571. if (this.isLinkedCard()) {
  572. return Cards.update({_id: this.linkedId}, {$set: {description}});
  573. } else if (this.isLinkedBoard()) {
  574. return Boards.update({_id: this.linkedId}, {$set: {description}});
  575. } else {
  576. return Cards.update(
  577. {_id: this._id},
  578. {$set: {description}}
  579. );
  580. }
  581. },
  582. getDescription() {
  583. if (this.isLinkedCard()) {
  584. const card = Cards.findOne({_id: this.linkedId});
  585. if (card && card.description)
  586. return card.description;
  587. else
  588. return null;
  589. } else if (this.isLinkedBoard()) {
  590. const board = Boards.findOne({_id: this.linkedId});
  591. if (board && board.description)
  592. return board.description;
  593. else
  594. return null;
  595. } else if (this.description) {
  596. return this.description;
  597. } else {
  598. return null;
  599. }
  600. },
  601. getMembers() {
  602. if (this.isLinkedCard()) {
  603. const card = Cards.findOne({_id: this.linkedId});
  604. return card.members;
  605. } else if (this.isLinkedBoard()) {
  606. const board = Boards.findOne({_id: this.linkedId});
  607. return board.activeMembers().map((member) => {
  608. return member.userId;
  609. });
  610. } else {
  611. return this.members;
  612. }
  613. },
  614. assignMember(memberId) {
  615. if (this.isLinkedCard()) {
  616. return Cards.update(
  617. { _id: this.linkedId },
  618. { $addToSet: { members: memberId }}
  619. );
  620. } else if (this.isLinkedBoard()) {
  621. const board = Boards.findOne({_id: this.linkedId});
  622. return board.addMember(memberId);
  623. } else {
  624. return Cards.update(
  625. { _id: this._id },
  626. { $addToSet: { members: memberId}}
  627. );
  628. }
  629. },
  630. unassignMember(memberId) {
  631. if (this.isLinkedCard()) {
  632. return Cards.update(
  633. { _id: this.linkedId },
  634. { $pull: { members: memberId }}
  635. );
  636. } else if (this.isLinkedBoard()) {
  637. const board = Boards.findOne({_id: this.linkedId});
  638. return board.removeMember(memberId);
  639. } else {
  640. return Cards.update(
  641. { _id: this._id },
  642. { $pull: { members: memberId}}
  643. );
  644. }
  645. },
  646. toggleMember(memberId) {
  647. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  648. return this.unassignMember(memberId);
  649. } else {
  650. return this.assignMember(memberId);
  651. }
  652. },
  653. getReceived() {
  654. if (this.isLinkedCard()) {
  655. const card = Cards.findOne({_id: this.linkedId});
  656. return card.receivedAt;
  657. } else {
  658. return this.receivedAt;
  659. }
  660. },
  661. setReceived(receivedAt) {
  662. if (this.isLinkedCard()) {
  663. return Cards.update(
  664. {_id: this.linkedId},
  665. {$set: {receivedAt}}
  666. );
  667. } else {
  668. return Cards.update(
  669. {_id: this._id},
  670. {$set: {receivedAt}}
  671. );
  672. }
  673. },
  674. getStart() {
  675. if (this.isLinkedCard()) {
  676. const card = Cards.findOne({_id: this.linkedId});
  677. return card.startAt;
  678. } else if (this.isLinkedBoard()) {
  679. const board = Boards.findOne({_id: this.linkedId});
  680. return board.startAt;
  681. } else {
  682. return this.startAt;
  683. }
  684. },
  685. setStart(startAt) {
  686. if (this.isLinkedCard()) {
  687. return Cards.update(
  688. { _id: this.linkedId },
  689. {$set: {startAt}}
  690. );
  691. } else if (this.isLinkedBoard()) {
  692. return Boards.update(
  693. {_id: this.linkedId},
  694. {$set: {startAt}}
  695. );
  696. } else {
  697. return Cards.update(
  698. {_id: this._id},
  699. {$set: {startAt}}
  700. );
  701. }
  702. },
  703. getDue() {
  704. if (this.isLinkedCard()) {
  705. const card = Cards.findOne({_id: this.linkedId});
  706. return card.dueAt;
  707. } else if (this.isLinkedBoard()) {
  708. const board = Boards.findOne({_id: this.linkedId});
  709. return board.dueAt;
  710. } else {
  711. return this.dueAt;
  712. }
  713. },
  714. setDue(dueAt) {
  715. if (this.isLinkedCard()) {
  716. return Cards.update(
  717. { _id: this.linkedId },
  718. {$set: {dueAt}}
  719. );
  720. } else if (this.isLinkedBoard()) {
  721. return Boards.update(
  722. {_id: this.linkedId},
  723. {$set: {dueAt}}
  724. );
  725. } else {
  726. return Cards.update(
  727. {_id: this._id},
  728. {$set: {dueAt}}
  729. );
  730. }
  731. },
  732. getEnd() {
  733. if (this.isLinkedCard()) {
  734. const card = Cards.findOne({_id: this.linkedId});
  735. return card.endAt;
  736. } else if (this.isLinkedBoard()) {
  737. const board = Boards.findOne({_id: this.linkedId});
  738. return board.endAt;
  739. } else {
  740. return this.endAt;
  741. }
  742. },
  743. setEnd(endAt) {
  744. if (this.isLinkedCard()) {
  745. return Cards.update(
  746. { _id: this.linkedId },
  747. {$set: {endAt}}
  748. );
  749. } else if (this.isLinkedBoard()) {
  750. return Boards.update(
  751. {_id: this.linkedId},
  752. {$set: {endAt}}
  753. );
  754. } else {
  755. return Cards.update(
  756. {_id: this._id},
  757. {$set: {endAt}}
  758. );
  759. }
  760. },
  761. getIsOvertime() {
  762. if (this.isLinkedCard()) {
  763. const card = Cards.findOne({ _id: this.linkedId });
  764. return card.isOvertime;
  765. } else if (this.isLinkedBoard()) {
  766. const board = Boards.findOne({ _id: this.linkedId});
  767. return board.isOvertime;
  768. } else {
  769. return this.isOvertime;
  770. }
  771. },
  772. setIsOvertime(isOvertime) {
  773. if (this.isLinkedCard()) {
  774. return Cards.update(
  775. { _id: this.linkedId },
  776. {$set: {isOvertime}}
  777. );
  778. } else if (this.isLinkedBoard()) {
  779. return Boards.update(
  780. {_id: this.linkedId},
  781. {$set: {isOvertime}}
  782. );
  783. } else {
  784. return Cards.update(
  785. {_id: this._id},
  786. {$set: {isOvertime}}
  787. );
  788. }
  789. },
  790. getSpentTime() {
  791. if (this.isLinkedCard()) {
  792. const card = Cards.findOne({ _id: this.linkedId });
  793. return card.spentTime;
  794. } else if (this.isLinkedBoard()) {
  795. const board = Boards.findOne({ _id: this.linkedId});
  796. return board.spentTime;
  797. } else {
  798. return this.spentTime;
  799. }
  800. },
  801. setSpentTime(spentTime) {
  802. if (this.isLinkedCard()) {
  803. return Cards.update(
  804. { _id: this.linkedId },
  805. {$set: {spentTime}}
  806. );
  807. } else if (this.isLinkedBoard()) {
  808. return Boards.update(
  809. {_id: this.linkedId},
  810. {$set: {spentTime}}
  811. );
  812. } else {
  813. return Cards.update(
  814. {_id: this._id},
  815. {$set: {spentTime}}
  816. );
  817. }
  818. },
  819. getId() {
  820. if (this.isLinked()) {
  821. return this.linkedId;
  822. } else {
  823. return this._id;
  824. }
  825. },
  826. getTitle() {
  827. if (this.isLinkedCard()) {
  828. const card = Cards.findOne({ _id: this.linkedId });
  829. return card.title;
  830. } else if (this.isLinkedBoard()) {
  831. const board = Boards.findOne({ _id: this.linkedId});
  832. return board.title;
  833. } else {
  834. return this.title;
  835. }
  836. },
  837. getBoardTitle() {
  838. if (this.isLinkedCard()) {
  839. const card = Cards.findOne({ _id: this.linkedId });
  840. const board = Boards.findOne({ _id: card.boardId });
  841. return board.title;
  842. } else if (this.isLinkedBoard()) {
  843. const board = Boards.findOne({ _id: this.linkedId});
  844. return board.title;
  845. } else {
  846. const board = Boards.findOne({ _id: this.boardId });
  847. return board.title;
  848. }
  849. },
  850. setTitle(title) {
  851. if (this.isLinkedCard()) {
  852. return Cards.update(
  853. { _id: this.linkedId },
  854. {$set: {title}}
  855. );
  856. } else if (this.isLinkedBoard()) {
  857. return Boards.update(
  858. {_id: this.linkedId},
  859. {$set: {title}}
  860. );
  861. } else {
  862. return Cards.update(
  863. {_id: this._id},
  864. {$set: {title}}
  865. );
  866. }
  867. },
  868. getArchived() {
  869. if (this.isLinkedCard()) {
  870. const card = Cards.findOne({ _id: this.linkedId });
  871. return card.archived;
  872. } else if (this.isLinkedBoard()) {
  873. const board = Boards.findOne({ _id: this.linkedId});
  874. return board.archived;
  875. } else {
  876. return this.archived;
  877. }
  878. },
  879. setRequestedBy(requestedBy) {
  880. if (this.isLinkedCard()) {
  881. return Cards.update(
  882. { _id: this.linkedId },
  883. {$set: {requestedBy}}
  884. );
  885. } else {
  886. return Cards.update(
  887. {_id: this._id},
  888. {$set: {requestedBy}}
  889. );
  890. }
  891. },
  892. getRequestedBy() {
  893. if (this.isLinkedCard()) {
  894. const card = Cards.findOne({ _id: this.linkedId });
  895. return card.requestedBy;
  896. } else {
  897. return this.requestedBy;
  898. }
  899. },
  900. setAssignedBy(assignedBy) {
  901. if (this.isLinkedCard()) {
  902. return Cards.update(
  903. { _id: this.linkedId },
  904. {$set: {assignedBy}}
  905. );
  906. } else {
  907. return Cards.update(
  908. {_id: this._id},
  909. {$set: {assignedBy}}
  910. );
  911. }
  912. },
  913. getAssignedBy() {
  914. if (this.isLinkedCard()) {
  915. const card = Cards.findOne({ _id: this.linkedId });
  916. return card.assignedBy;
  917. } else {
  918. return this.assignedBy;
  919. }
  920. },
  921. isTemplateCard() {
  922. return this.type === 'template-card';
  923. },
  924. });
  925. Cards.mutations({
  926. applyToChildren(funct) {
  927. Cards.find({
  928. parentId: this._id,
  929. }).forEach((card) => {
  930. funct(card);
  931. });
  932. },
  933. archive() {
  934. this.applyToChildren((card) => {
  935. return card.archive();
  936. });
  937. return {
  938. $set: {
  939. archived: true,
  940. },
  941. };
  942. },
  943. restore() {
  944. this.applyToChildren((card) => {
  945. return card.restore();
  946. });
  947. return {
  948. $set: {
  949. archived: false,
  950. },
  951. };
  952. },
  953. move(boardId, swimlaneId, listId, sort) {
  954. // Copy Custom Fields
  955. if (this.boardId !== boardId) {
  956. CustomFields.find({
  957. _id: {$in: this.customFields.map((cf) => { return cf._id; })},
  958. }).forEach((cf) => {
  959. if (!_.contains(cf.boardIds, boardId))
  960. cf.addBoard(boardId);
  961. });
  962. }
  963. // Get label names
  964. const oldBoard = Boards.findOne(this.boardId);
  965. const oldBoardLabels = oldBoard.labels;
  966. const oldCardLabels = _.pluck(_.filter(oldBoardLabels, (label) => {
  967. return _.contains(this.labelIds, label._id);
  968. }), 'name');
  969. const newBoard = Boards.findOne(boardId);
  970. const newBoardLabels = newBoard.labels;
  971. const newCardLabelIds = _.pluck(_.filter(newBoardLabels, (label) => {
  972. return label.name && _.contains(oldCardLabels, label.name);
  973. }), '_id');
  974. const mutatedFields = {
  975. boardId,
  976. swimlaneId,
  977. listId,
  978. sort,
  979. labelIds: newCardLabelIds,
  980. };
  981. Cards.update(this._id, {
  982. $set: mutatedFields,
  983. });
  984. },
  985. addLabel(labelId) {
  986. return {
  987. $addToSet: {
  988. labelIds: labelId,
  989. },
  990. };
  991. },
  992. removeLabel(labelId) {
  993. return {
  994. $pull: {
  995. labelIds: labelId,
  996. },
  997. };
  998. },
  999. toggleLabel(labelId) {
  1000. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1001. return this.removeLabel(labelId);
  1002. } else {
  1003. return this.addLabel(labelId);
  1004. }
  1005. },
  1006. setColor(newColor) {
  1007. if (newColor === 'white') {
  1008. newColor = null;
  1009. }
  1010. return {
  1011. $set: {
  1012. color: newColor,
  1013. },
  1014. };
  1015. },
  1016. assignMember(memberId) {
  1017. return {
  1018. $addToSet: {
  1019. members: memberId,
  1020. },
  1021. };
  1022. },
  1023. unassignMember(memberId) {
  1024. return {
  1025. $pull: {
  1026. members: memberId,
  1027. },
  1028. };
  1029. },
  1030. toggleMember(memberId) {
  1031. if (this.members && this.members.indexOf(memberId) > -1) {
  1032. return this.unassignMember(memberId);
  1033. } else {
  1034. return this.assignMember(memberId);
  1035. }
  1036. },
  1037. assignCustomField(customFieldId) {
  1038. return {
  1039. $addToSet: {
  1040. customFields: {
  1041. _id: customFieldId,
  1042. value: null,
  1043. },
  1044. },
  1045. };
  1046. },
  1047. unassignCustomField(customFieldId) {
  1048. return {
  1049. $pull: {
  1050. customFields: {
  1051. _id: customFieldId,
  1052. },
  1053. },
  1054. };
  1055. },
  1056. toggleCustomField(customFieldId) {
  1057. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1058. return this.unassignCustomField(customFieldId);
  1059. } else {
  1060. return this.assignCustomField(customFieldId);
  1061. }
  1062. },
  1063. setCustomField(customFieldId, value) {
  1064. // todo
  1065. const index = this.customFieldIndex(customFieldId);
  1066. if (index > -1) {
  1067. const update = {
  1068. $set: {},
  1069. };
  1070. update.$set[`customFields.${index}.value`] = value;
  1071. return update;
  1072. }
  1073. // TODO
  1074. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1075. return null;
  1076. },
  1077. setCover(coverId) {
  1078. return {
  1079. $set: {
  1080. coverId,
  1081. },
  1082. };
  1083. },
  1084. unsetCover() {
  1085. return {
  1086. $unset: {
  1087. coverId: '',
  1088. },
  1089. };
  1090. },
  1091. setReceived(receivedAt) {
  1092. return {
  1093. $set: {
  1094. receivedAt,
  1095. },
  1096. };
  1097. },
  1098. unsetReceived() {
  1099. return {
  1100. $unset: {
  1101. receivedAt: '',
  1102. },
  1103. };
  1104. },
  1105. setStart(startAt) {
  1106. return {
  1107. $set: {
  1108. startAt,
  1109. },
  1110. };
  1111. },
  1112. unsetStart() {
  1113. return {
  1114. $unset: {
  1115. startAt: '',
  1116. },
  1117. };
  1118. },
  1119. setDue(dueAt) {
  1120. return {
  1121. $set: {
  1122. dueAt,
  1123. },
  1124. };
  1125. },
  1126. unsetDue() {
  1127. return {
  1128. $unset: {
  1129. dueAt: '',
  1130. },
  1131. };
  1132. },
  1133. setEnd(endAt) {
  1134. return {
  1135. $set: {
  1136. endAt,
  1137. },
  1138. };
  1139. },
  1140. unsetEnd() {
  1141. return {
  1142. $unset: {
  1143. endAt: '',
  1144. },
  1145. };
  1146. },
  1147. setOvertime(isOvertime) {
  1148. return {
  1149. $set: {
  1150. isOvertime,
  1151. },
  1152. };
  1153. },
  1154. setSpentTime(spentTime) {
  1155. return {
  1156. $set: {
  1157. spentTime,
  1158. },
  1159. };
  1160. },
  1161. unsetSpentTime() {
  1162. return {
  1163. $unset: {
  1164. spentTime: '',
  1165. isOvertime: false,
  1166. },
  1167. };
  1168. },
  1169. setParentId(parentId) {
  1170. return {
  1171. $set: {
  1172. parentId,
  1173. },
  1174. };
  1175. },
  1176. });
  1177. //FUNCTIONS FOR creation of Activities
  1178. function updateActivities(doc, fieldNames, modifier) {
  1179. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  1180. Activities.find({
  1181. activityType: 'addedLabel',
  1182. cardId: doc._id,
  1183. }).forEach((a) => {
  1184. const lidx = doc.labelIds.indexOf(a.labelId);
  1185. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  1186. Activities.update(a._id, {
  1187. $set: {
  1188. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  1189. boardId: modifier.$set.boardId,
  1190. },
  1191. });
  1192. } else {
  1193. Activities.remove(a._id);
  1194. }
  1195. });
  1196. } else if (_.contains(fieldNames, 'boardId')) {
  1197. Activities.remove({
  1198. activityType: 'addedLabel',
  1199. cardId: doc._id,
  1200. });
  1201. }
  1202. }
  1203. function cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId) {
  1204. if (_.contains(fieldNames, 'boardId') && (doc.boardId !== oldBoardId)) {
  1205. Activities.insert({
  1206. userId,
  1207. activityType: 'moveCardBoard',
  1208. boardName: Boards.findOne(doc.boardId).title,
  1209. boardId: doc.boardId,
  1210. oldBoardId,
  1211. oldBoardName: Boards.findOne(oldBoardId).title,
  1212. cardId: doc._id,
  1213. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1214. swimlaneId: doc.swimlaneId,
  1215. oldSwimlaneId,
  1216. });
  1217. } else if ((_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1218. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)){
  1219. Activities.insert({
  1220. userId,
  1221. oldListId,
  1222. activityType: 'moveCard',
  1223. listName: Lists.findOne(doc.listId).title,
  1224. listId: doc.listId,
  1225. boardId: doc.boardId,
  1226. cardId: doc._id,
  1227. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1228. swimlaneId: doc.swimlaneId,
  1229. oldSwimlaneId,
  1230. });
  1231. }
  1232. }
  1233. function cardState(userId, doc, fieldNames) {
  1234. if (_.contains(fieldNames, 'archived')) {
  1235. if (doc.archived) {
  1236. Activities.insert({
  1237. userId,
  1238. activityType: 'archivedCard',
  1239. listName: Lists.findOne(doc.listId).title,
  1240. boardId: doc.boardId,
  1241. listId: doc.listId,
  1242. cardId: doc._id,
  1243. });
  1244. } else {
  1245. Activities.insert({
  1246. userId,
  1247. activityType: 'restoredCard',
  1248. boardId: doc.boardId,
  1249. listName: Lists.findOne(doc.listId).title,
  1250. listId: doc.listId,
  1251. cardId: doc._id,
  1252. });
  1253. }
  1254. }
  1255. }
  1256. function cardMembers(userId, doc, fieldNames, modifier) {
  1257. if (!_.contains(fieldNames, 'members'))
  1258. return;
  1259. let memberId;
  1260. // Say hello to the new member
  1261. if (modifier.$addToSet && modifier.$addToSet.members) {
  1262. memberId = modifier.$addToSet.members;
  1263. const username = Users.findOne(memberId).username;
  1264. if (!_.contains(doc.members, memberId)) {
  1265. Activities.insert({
  1266. userId,
  1267. username,
  1268. activityType: 'joinMember',
  1269. boardId: doc.boardId,
  1270. cardId: doc._id,
  1271. });
  1272. }
  1273. }
  1274. // Say goodbye to the former member
  1275. if (modifier.$pull && modifier.$pull.members) {
  1276. memberId = modifier.$pull.members;
  1277. const username = Users.findOne(memberId).username;
  1278. // Check that the former member is member of the card
  1279. if (_.contains(doc.members, memberId)) {
  1280. Activities.insert({
  1281. userId,
  1282. username,
  1283. activityType: 'unjoinMember',
  1284. boardId: doc.boardId,
  1285. cardId: doc._id,
  1286. });
  1287. }
  1288. }
  1289. }
  1290. function cardLabels(userId, doc, fieldNames, modifier) {
  1291. if (!_.contains(fieldNames, 'labelIds'))
  1292. return;
  1293. let labelId;
  1294. // Say hello to the new label
  1295. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1296. labelId = modifier.$addToSet.labelIds;
  1297. if (!_.contains(doc.labelIds, labelId)) {
  1298. const act = {
  1299. userId,
  1300. labelId,
  1301. activityType: 'addedLabel',
  1302. boardId: doc.boardId,
  1303. cardId: doc._id,
  1304. };
  1305. Activities.insert(act);
  1306. }
  1307. }
  1308. // Say goodbye to the label
  1309. if (modifier.$pull && modifier.$pull.labelIds) {
  1310. labelId = modifier.$pull.labelIds;
  1311. // Check that the former member is member of the card
  1312. if (_.contains(doc.labelIds, labelId)) {
  1313. Activities.insert({
  1314. userId,
  1315. labelId,
  1316. activityType: 'removedLabel',
  1317. boardId: doc.boardId,
  1318. cardId: doc._id,
  1319. });
  1320. }
  1321. }
  1322. }
  1323. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1324. if (!_.contains(fieldNames, 'customFields'))
  1325. return;
  1326. // Say hello to the new customField value
  1327. if (modifier.$set) {
  1328. _.each(modifier.$set, (value, key) => {
  1329. if (key.startsWith('customFields')) {
  1330. const dotNotation = key.split('.');
  1331. // only individual changes are registered
  1332. if (dotNotation.length > 1) {
  1333. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1334. const act = {
  1335. userId,
  1336. customFieldId,
  1337. value,
  1338. activityType: 'setCustomField',
  1339. boardId: doc.boardId,
  1340. cardId: doc._id,
  1341. };
  1342. Activities.insert(act);
  1343. }
  1344. }
  1345. });
  1346. }
  1347. // Say goodbye to the former customField value
  1348. if (modifier.$unset) {
  1349. _.each(modifier.$unset, (value, key) => {
  1350. if (key.startsWith('customFields')) {
  1351. const dotNotation = key.split('.');
  1352. // only individual changes are registered
  1353. if (dotNotation.length > 1) {
  1354. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1355. const act = {
  1356. userId,
  1357. customFieldId,
  1358. activityType: 'unsetCustomField',
  1359. boardId: doc.boardId,
  1360. cardId: doc._id,
  1361. };
  1362. Activities.insert(act);
  1363. }
  1364. }
  1365. });
  1366. }
  1367. }
  1368. function cardCreation(userId, doc) {
  1369. Activities.insert({
  1370. userId,
  1371. activityType: 'createCard',
  1372. boardId: doc.boardId,
  1373. listName: Lists.findOne(doc.listId).title,
  1374. listId: doc.listId,
  1375. cardId: doc._id,
  1376. cardTitle:doc.title,
  1377. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1378. swimlaneId: doc.swimlaneId,
  1379. });
  1380. }
  1381. function cardRemover(userId, doc) {
  1382. Activities.remove({
  1383. cardId: doc._id,
  1384. });
  1385. Checklists.remove({
  1386. cardId: doc._id,
  1387. });
  1388. Cards.remove({
  1389. parentId: doc._id,
  1390. });
  1391. CardComments.remove({
  1392. cardId: doc._id,
  1393. });
  1394. Attachments.remove({
  1395. cardId: doc._id,
  1396. });
  1397. }
  1398. if (Meteor.isServer) {
  1399. // Cards are often fetched within a board, so we create an index to make these
  1400. // queries more efficient.
  1401. Meteor.startup(() => {
  1402. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  1403. // https://github.com/wekan/wekan/issues/1863
  1404. // Swimlane added a new field in the cards collection of mongodb named parentId.
  1405. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  1406. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  1407. // To correct it, add Index to parentId:
  1408. Cards._collection._ensureIndex({parentId: 1});
  1409. });
  1410. Cards.after.insert((userId, doc) => {
  1411. cardCreation(userId, doc);
  1412. });
  1413. // New activity for card (un)archivage
  1414. Cards.after.update((userId, doc, fieldNames) => {
  1415. cardState(userId, doc, fieldNames);
  1416. });
  1417. //New activity for card moves
  1418. Cards.after.update(function(userId, doc, fieldNames) {
  1419. const oldListId = this.previous.listId;
  1420. const oldSwimlaneId = this.previous.swimlaneId;
  1421. const oldBoardId = this.previous.boardId;
  1422. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  1423. });
  1424. // Add a new activity if we add or remove a member to the card
  1425. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1426. cardMembers(userId, doc, fieldNames, modifier);
  1427. updateActivities(doc, fieldNames, modifier);
  1428. });
  1429. // Add a new activity if we add or remove a label to the card
  1430. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1431. cardLabels(userId, doc, fieldNames, modifier);
  1432. });
  1433. // Add a new activity if we edit a custom field
  1434. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1435. cardCustomFields(userId, doc, fieldNames, modifier);
  1436. });
  1437. // Remove all activities associated with a card if we remove the card
  1438. // Remove also card_comments / checklists / attachments
  1439. Cards.after.remove((userId, doc) => {
  1440. cardRemover(userId, doc);
  1441. });
  1442. }
  1443. //SWIMLANES REST API
  1444. if (Meteor.isServer) {
  1445. /**
  1446. * @operation get_swimlane_cards
  1447. * @summary get all cards attached to a swimlane
  1448. *
  1449. * @param {string} boardId the board ID
  1450. * @param {string} swimlaneId the swimlane ID
  1451. * @return_type [{_id: string,
  1452. * title: string,
  1453. * description: string,
  1454. * listId: string}]
  1455. */
  1456. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId/cards', function(req, res) {
  1457. const paramBoardId = req.params.boardId;
  1458. const paramSwimlaneId = req.params.swimlaneId;
  1459. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1460. JsonRoutes.sendResult(res, {
  1461. code: 200,
  1462. data: Cards.find({
  1463. boardId: paramBoardId,
  1464. swimlaneId: paramSwimlaneId,
  1465. archived: false,
  1466. }).map(function(doc) {
  1467. return {
  1468. _id: doc._id,
  1469. title: doc.title,
  1470. description: doc.description,
  1471. listId: doc.listId,
  1472. };
  1473. }),
  1474. });
  1475. });
  1476. }
  1477. //LISTS REST API
  1478. if (Meteor.isServer) {
  1479. /**
  1480. * @operation get_all_cards
  1481. * @summary Get all Cards attached to a List
  1482. *
  1483. * @param {string} boardId the board ID
  1484. * @param {string} listId the list ID
  1485. * @return_type [{_id: string,
  1486. * title: string,
  1487. * description: string}]
  1488. */
  1489. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1490. const paramBoardId = req.params.boardId;
  1491. const paramListId = req.params.listId;
  1492. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1493. JsonRoutes.sendResult(res, {
  1494. code: 200,
  1495. data: Cards.find({
  1496. boardId: paramBoardId,
  1497. listId: paramListId,
  1498. archived: false,
  1499. }).map(function(doc) {
  1500. return {
  1501. _id: doc._id,
  1502. title: doc.title,
  1503. description: doc.description,
  1504. };
  1505. }),
  1506. });
  1507. });
  1508. /**
  1509. * @operation get_card
  1510. * @summary Get a Card
  1511. *
  1512. * @param {string} boardId the board ID
  1513. * @param {string} listId the list ID of the card
  1514. * @param {string} cardId the card ID
  1515. * @return_type Cards
  1516. */
  1517. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1518. const paramBoardId = req.params.boardId;
  1519. const paramListId = req.params.listId;
  1520. const paramCardId = req.params.cardId;
  1521. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1522. JsonRoutes.sendResult(res, {
  1523. code: 200,
  1524. data: Cards.findOne({
  1525. _id: paramCardId,
  1526. listId: paramListId,
  1527. boardId: paramBoardId,
  1528. archived: false,
  1529. }),
  1530. });
  1531. });
  1532. /**
  1533. * @operation new_card
  1534. * @summary Create a new Card
  1535. *
  1536. * @param {string} boardId the board ID of the new card
  1537. * @param {string} listId the list ID of the new card
  1538. * @param {string} authorID the user ID of the person owning the card
  1539. * @param {string} title the title of the new card
  1540. * @param {string} description the description of the new card
  1541. * @param {string} swimlaneId the swimlane ID of the new card
  1542. * @param {string} [members] the member IDs list of the new card
  1543. * @return_type {_id: string}
  1544. */
  1545. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1546. Authentication.checkUserId(req.userId);
  1547. const paramBoardId = req.params.boardId;
  1548. const paramListId = req.params.listId;
  1549. const currentCards = Cards.find({
  1550. listId: paramListId,
  1551. archived: false,
  1552. }, { sort: ['sort'] });
  1553. const check = Users.findOne({
  1554. _id: req.body.authorId,
  1555. });
  1556. const members = req.body.members || [req.body.authorId];
  1557. if (typeof check !== 'undefined') {
  1558. const id = Cards.direct.insert({
  1559. title: req.body.title,
  1560. boardId: paramBoardId,
  1561. listId: paramListId,
  1562. description: req.body.description,
  1563. userId: req.body.authorId,
  1564. swimlaneId: req.body.swimlaneId,
  1565. sort: currentCards.count(),
  1566. members,
  1567. });
  1568. JsonRoutes.sendResult(res, {
  1569. code: 200,
  1570. data: {
  1571. _id: id,
  1572. },
  1573. });
  1574. const card = Cards.findOne({
  1575. _id: id,
  1576. });
  1577. cardCreation(req.body.authorId, card);
  1578. } else {
  1579. JsonRoutes.sendResult(res, {
  1580. code: 401,
  1581. });
  1582. }
  1583. });
  1584. /*
  1585. * Note for the JSDoc:
  1586. * 'list' will be interpreted as the path parameter
  1587. * 'listID' will be interpreted as the body parameter
  1588. */
  1589. /**
  1590. * @operation edit_card
  1591. * @summary Edit Fields in a Card
  1592. *
  1593. * @description Edit a card
  1594. *
  1595. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  1596. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  1597. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  1598. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  1599. * `indigo`:
  1600. *
  1601. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  1602. *
  1603. * Note: setting the color to white has the same effect than removing it.
  1604. *
  1605. * @param {string} boardId the board ID of the card
  1606. * @param {string} list the list ID of the card
  1607. * @param {string} cardId the ID of the card
  1608. * @param {string} [title] the new title of the card
  1609. * @param {string} [listId] the new list ID of the card (move operation)
  1610. * @param {string} [description] the new description of the card
  1611. * @param {string} [authorId] change the owner of the card
  1612. * @param {string} [labelIds] the new list of label IDs attached to the card
  1613. * @param {string} [swimlaneId] the new swimlane ID of the card
  1614. * @param {string} [members] the new list of member IDs attached to the card
  1615. * @param {string} [requestedBy] the new requestedBy field of the card
  1616. * @param {string} [assignedBy] the new assignedBy field of the card
  1617. * @param {string} [receivedAt] the new receivedAt field of the card
  1618. * @param {string} [assignBy] the new assignBy field of the card
  1619. * @param {string} [startAt] the new startAt field of the card
  1620. * @param {string} [dueAt] the new dueAt field of the card
  1621. * @param {string} [endAt] the new endAt field of the card
  1622. * @param {string} [spentTime] the new spentTime field of the card
  1623. * @param {boolean} [isOverTime] the new isOverTime field of the card
  1624. * @param {string} [customFields] the new customFields value of the card
  1625. * @param {string} [color] the new color of the card
  1626. * @return_type {_id: string}
  1627. */
  1628. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1629. Authentication.checkUserId(req.userId);
  1630. const paramBoardId = req.params.boardId;
  1631. const paramCardId = req.params.cardId;
  1632. const paramListId = req.params.listId;
  1633. if (req.body.hasOwnProperty('title')) {
  1634. const newTitle = req.body.title;
  1635. Cards.direct.update({
  1636. _id: paramCardId,
  1637. listId: paramListId,
  1638. boardId: paramBoardId,
  1639. archived: false,
  1640. }, {
  1641. $set: {
  1642. title: newTitle,
  1643. },
  1644. });
  1645. }
  1646. if (req.body.hasOwnProperty('listId')) {
  1647. const newParamListId = req.body.listId;
  1648. Cards.direct.update({
  1649. _id: paramCardId,
  1650. listId: paramListId,
  1651. boardId: paramBoardId,
  1652. archived: false,
  1653. }, {
  1654. $set: {
  1655. listId: newParamListId,
  1656. },
  1657. });
  1658. const card = Cards.findOne({
  1659. _id: paramCardId,
  1660. });
  1661. cardMove(req.body.authorId, card, {
  1662. fieldName: 'listId',
  1663. }, paramListId);
  1664. }
  1665. if (req.body.hasOwnProperty('description')) {
  1666. const newDescription = req.body.description;
  1667. Cards.direct.update({
  1668. _id: paramCardId,
  1669. listId: paramListId,
  1670. boardId: paramBoardId,
  1671. archived: false,
  1672. }, {
  1673. $set: {
  1674. description: newDescription,
  1675. },
  1676. });
  1677. }
  1678. if (req.body.hasOwnProperty('color')) {
  1679. const newColor = req.body.color;
  1680. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1681. {$set: {color: newColor}});
  1682. }
  1683. if (req.body.hasOwnProperty('labelIds')) {
  1684. let newlabelIds = req.body.labelIds;
  1685. if (_.isString(newlabelIds)) {
  1686. if (newlabelIds === '') {
  1687. newlabelIds = null;
  1688. }
  1689. else {
  1690. newlabelIds = [newlabelIds];
  1691. }
  1692. }
  1693. Cards.direct.update({
  1694. _id: paramCardId,
  1695. listId: paramListId,
  1696. boardId: paramBoardId,
  1697. archived: false,
  1698. }, {
  1699. $set: {
  1700. labelIds: newlabelIds,
  1701. },
  1702. });
  1703. }
  1704. if (req.body.hasOwnProperty('requestedBy')) {
  1705. const newrequestedBy = req.body.requestedBy;
  1706. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1707. {$set: {requestedBy: newrequestedBy}});
  1708. }
  1709. if (req.body.hasOwnProperty('assignedBy')) {
  1710. const newassignedBy = req.body.assignedBy;
  1711. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1712. {$set: {assignedBy: newassignedBy}});
  1713. }
  1714. if (req.body.hasOwnProperty('receivedAt')) {
  1715. const newreceivedAt = req.body.receivedAt;
  1716. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1717. {$set: {receivedAt: newreceivedAt}});
  1718. }
  1719. if (req.body.hasOwnProperty('startAt')) {
  1720. const newstartAt = req.body.startAt;
  1721. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1722. {$set: {startAt: newstartAt}});
  1723. }
  1724. if (req.body.hasOwnProperty('dueAt')) {
  1725. const newdueAt = req.body.dueAt;
  1726. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1727. {$set: {dueAt: newdueAt}});
  1728. }
  1729. if (req.body.hasOwnProperty('endAt')) {
  1730. const newendAt = req.body.endAt;
  1731. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1732. {$set: {endAt: newendAt}});
  1733. }
  1734. if (req.body.hasOwnProperty('spentTime')) {
  1735. const newspentTime = req.body.spentTime;
  1736. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1737. {$set: {spentTime: newspentTime}});
  1738. }
  1739. if (req.body.hasOwnProperty('isOverTime')) {
  1740. const newisOverTime = req.body.isOverTime;
  1741. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1742. {$set: {isOverTime: newisOverTime}});
  1743. }
  1744. if (req.body.hasOwnProperty('customFields')) {
  1745. const newcustomFields = req.body.customFields;
  1746. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1747. {$set: {customFields: newcustomFields}});
  1748. }
  1749. if (req.body.hasOwnProperty('members')) {
  1750. let newmembers = req.body.members;
  1751. if (_.isString(newmembers)) {
  1752. if (newmembers === '') {
  1753. newmembers = null;
  1754. }
  1755. else {
  1756. newmembers = [newmembers];
  1757. }
  1758. }
  1759. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1760. {$set: {members: newmembers}});
  1761. }
  1762. if (req.body.hasOwnProperty('swimlaneId')) {
  1763. const newParamSwimlaneId = req.body.swimlaneId;
  1764. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1765. {$set: {swimlaneId: newParamSwimlaneId}});
  1766. }
  1767. JsonRoutes.sendResult(res, {
  1768. code: 200,
  1769. data: {
  1770. _id: paramCardId,
  1771. },
  1772. });
  1773. });
  1774. /**
  1775. * @operation delete_card
  1776. * @summary Delete a card from a board
  1777. *
  1778. * @description This operation **deletes** a card, and therefore the card
  1779. * is not put in the recycle bin.
  1780. *
  1781. * @param {string} boardId the board ID of the card
  1782. * @param {string} list the list ID of the card
  1783. * @param {string} cardId the ID of the card
  1784. * @return_type {_id: string}
  1785. */
  1786. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1787. Authentication.checkUserId(req.userId);
  1788. const paramBoardId = req.params.boardId;
  1789. const paramListId = req.params.listId;
  1790. const paramCardId = req.params.cardId;
  1791. Cards.direct.remove({
  1792. _id: paramCardId,
  1793. listId: paramListId,
  1794. boardId: paramBoardId,
  1795. });
  1796. const card = Cards.find({
  1797. _id: paramCardId,
  1798. });
  1799. cardRemover(req.body.authorId, card);
  1800. JsonRoutes.sendResult(res, {
  1801. code: 200,
  1802. data: {
  1803. _id: paramCardId,
  1804. },
  1805. });
  1806. });
  1807. }