cards.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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: '',
  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. list() {
  272. return Lists.findOne(this.listId);
  273. },
  274. board() {
  275. return Boards.findOne(this.boardId);
  276. },
  277. labels() {
  278. const boardLabels = this.board().labels;
  279. const cardLabels = _.filter(boardLabels, (label) => {
  280. return _.contains(this.labelIds, label._id);
  281. });
  282. return cardLabels;
  283. },
  284. hasLabel(labelId) {
  285. return _.contains(this.labelIds, labelId);
  286. },
  287. user() {
  288. return Users.findOne(this.userId);
  289. },
  290. isAssigned(memberId) {
  291. return _.contains(this.getMembers(), memberId);
  292. },
  293. activities() {
  294. if (this.isLinkedCard()) {
  295. return Activities.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  296. } else if (this.isLinkedBoard()) {
  297. return Activities.find({boardId: this.linkedId}, {sort: {createdAt: -1}});
  298. } else {
  299. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  300. }
  301. },
  302. comments() {
  303. if (this.isLinkedCard()) {
  304. return CardComments.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  305. } else {
  306. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  307. }
  308. },
  309. attachments() {
  310. if (this.isLinkedCard()) {
  311. return Attachments.find({cardId: this.linkedId}, {sort: {uploadedAt: -1}});
  312. } else {
  313. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  314. }
  315. },
  316. cover() {
  317. const cover = Attachments.findOne(this.coverId);
  318. // if we return a cover before it is fully stored, we will get errors when we try to display it
  319. // todo XXX we could return a default "upload pending" image in the meantime?
  320. return cover && cover.url() && cover;
  321. },
  322. checklists() {
  323. if (this.isLinkedCard()) {
  324. return Checklists.find({cardId: this.linkedId}, {sort: { sort: 1 } });
  325. } else {
  326. return Checklists.find({cardId: this._id}, {sort: { sort: 1 } });
  327. }
  328. },
  329. checklistItemCount() {
  330. const checklists = this.checklists().fetch();
  331. return checklists.map((checklist) => {
  332. return checklist.itemCount();
  333. }).reduce((prev, next) => {
  334. return prev + next;
  335. }, 0);
  336. },
  337. checklistFinishedCount() {
  338. const checklists = this.checklists().fetch();
  339. return checklists.map((checklist) => {
  340. return checklist.finishedCount();
  341. }).reduce((prev, next) => {
  342. return prev + next;
  343. }, 0);
  344. },
  345. checklistFinished() {
  346. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  347. },
  348. hasChecklist() {
  349. return this.checklistItemCount() !== 0;
  350. },
  351. subtasks() {
  352. return Cards.find({
  353. parentId: this._id,
  354. archived: false,
  355. }, {
  356. sort: {
  357. sort: 1,
  358. },
  359. });
  360. },
  361. allSubtasks() {
  362. return Cards.find({
  363. parentId: this._id,
  364. archived: false,
  365. }, {
  366. sort: {
  367. sort: 1,
  368. },
  369. });
  370. },
  371. subtasksCount() {
  372. return Cards.find({
  373. parentId: this._id,
  374. archived: false,
  375. }).count();
  376. },
  377. subtasksFinishedCount() {
  378. return Cards.find({
  379. parentId: this._id,
  380. archived: true,
  381. }).count();
  382. },
  383. subtasksFinished() {
  384. const finishCount = this.subtasksFinishedCount();
  385. return finishCount > 0 && this.subtasksCount() === finishCount;
  386. },
  387. allowsSubtasks() {
  388. return this.subtasksCount() !== 0;
  389. },
  390. customFieldIndex(customFieldId) {
  391. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  392. },
  393. // customFields with definitions
  394. customFieldsWD() {
  395. // get all definitions
  396. const definitions = CustomFields.find({
  397. boardId: this.boardId,
  398. }).fetch();
  399. // match right definition to each field
  400. if (!this.customFields) return [];
  401. return this.customFields.map((customField) => {
  402. const definition = definitions.find((definition) => {
  403. return definition._id === customField._id;
  404. });
  405. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  406. let trueValue = customField.value;
  407. if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0) {
  408. for (let i = 0; i < definition.settings.dropdownItems.length; i++) {
  409. if (definition.settings.dropdownItems[i]._id === customField.value) {
  410. trueValue = definition.settings.dropdownItems[i].name;
  411. }
  412. }
  413. }
  414. return {
  415. _id: customField._id,
  416. value: customField.value,
  417. trueValue,
  418. definition,
  419. };
  420. });
  421. },
  422. colorClass() {
  423. if (this.color)
  424. return this.color;
  425. return '';
  426. },
  427. absoluteUrl() {
  428. const board = this.board();
  429. return FlowRouter.url('card', {
  430. boardId: board._id,
  431. slug: board.slug,
  432. cardId: this._id,
  433. });
  434. },
  435. canBeRestored() {
  436. const list = Lists.findOne({
  437. _id: this.listId,
  438. });
  439. if (!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()) {
  440. return false;
  441. }
  442. return true;
  443. },
  444. parentCard() {
  445. if (this.parentId === '') {
  446. return null;
  447. }
  448. return Cards.findOne(this.parentId);
  449. },
  450. parentCardName() {
  451. let result = '';
  452. if (this.parentId !== '') {
  453. const card = Cards.findOne(this.parentId);
  454. if (card) {
  455. result = card.title;
  456. }
  457. }
  458. return result;
  459. },
  460. parentListId() {
  461. const result = [];
  462. let crtParentId = this.parentId;
  463. while (crtParentId !== '') {
  464. const crt = Cards.findOne(crtParentId);
  465. if ((crt === null) || (crt === undefined)) {
  466. // maybe it has been deleted
  467. break;
  468. }
  469. if (crtParentId in result) {
  470. // circular reference
  471. break;
  472. }
  473. result.unshift(crtParentId);
  474. crtParentId = crt.parentId;
  475. }
  476. return result;
  477. },
  478. parentList() {
  479. const resultId = [];
  480. const result = [];
  481. let crtParentId = this.parentId;
  482. while (crtParentId !== '') {
  483. const crt = Cards.findOne(crtParentId);
  484. if ((crt === null) || (crt === undefined)) {
  485. // maybe it has been deleted
  486. break;
  487. }
  488. if (crtParentId in resultId) {
  489. // circular reference
  490. break;
  491. }
  492. resultId.unshift(crtParentId);
  493. result.unshift(crt);
  494. crtParentId = crt.parentId;
  495. }
  496. return result;
  497. },
  498. parentString(sep) {
  499. return this.parentList().map(function(elem) {
  500. return elem.title;
  501. }).join(sep);
  502. },
  503. isTopLevel() {
  504. return this.parentId === '';
  505. },
  506. isLinkedCard() {
  507. return this.type === 'cardType-linkedCard';
  508. },
  509. isLinkedBoard() {
  510. return this.type === 'cardType-linkedBoard';
  511. },
  512. isLinked() {
  513. return this.isLinkedCard() || this.isLinkedBoard();
  514. },
  515. setDescription(description) {
  516. if (this.isLinkedCard()) {
  517. return Cards.update({_id: this.linkedId}, {$set: {description}});
  518. } else if (this.isLinkedBoard()) {
  519. return Boards.update({_id: this.linkedId}, {$set: {description}});
  520. } else {
  521. return Cards.update(
  522. {_id: this._id},
  523. {$set: {description}}
  524. );
  525. }
  526. },
  527. getDescription() {
  528. if (this.isLinkedCard()) {
  529. const card = Cards.findOne({_id: this.linkedId});
  530. if (card && card.description)
  531. return card.description;
  532. else
  533. return null;
  534. } else if (this.isLinkedBoard()) {
  535. const board = Boards.findOne({_id: this.linkedId});
  536. if (board && board.description)
  537. return board.description;
  538. else
  539. return null;
  540. } else if (this.description) {
  541. return this.description;
  542. } else {
  543. return null;
  544. }
  545. },
  546. getMembers() {
  547. if (this.isLinkedCard()) {
  548. const card = Cards.findOne({_id: this.linkedId});
  549. return card.members;
  550. } else if (this.isLinkedBoard()) {
  551. const board = Boards.findOne({_id: this.linkedId});
  552. return board.activeMembers().map((member) => {
  553. return member.userId;
  554. });
  555. } else {
  556. return this.members;
  557. }
  558. },
  559. assignMember(memberId) {
  560. if (this.isLinkedCard()) {
  561. return Cards.update(
  562. { _id: this.linkedId },
  563. { $addToSet: { members: memberId }}
  564. );
  565. } else if (this.isLinkedBoard()) {
  566. const board = Boards.findOne({_id: this.linkedId});
  567. return board.addMember(memberId);
  568. } else {
  569. return Cards.update(
  570. { _id: this._id },
  571. { $addToSet: { members: memberId}}
  572. );
  573. }
  574. },
  575. unassignMember(memberId) {
  576. if (this.isLinkedCard()) {
  577. return Cards.update(
  578. { _id: this.linkedId },
  579. { $pull: { members: memberId }}
  580. );
  581. } else if (this.isLinkedBoard()) {
  582. const board = Boards.findOne({_id: this.linkedId});
  583. return board.removeMember(memberId);
  584. } else {
  585. return Cards.update(
  586. { _id: this._id },
  587. { $pull: { members: memberId}}
  588. );
  589. }
  590. },
  591. toggleMember(memberId) {
  592. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  593. return this.unassignMember(memberId);
  594. } else {
  595. return this.assignMember(memberId);
  596. }
  597. },
  598. getReceived() {
  599. if (this.isLinkedCard()) {
  600. const card = Cards.findOne({_id: this.linkedId});
  601. return card.receivedAt;
  602. } else {
  603. return this.receivedAt;
  604. }
  605. },
  606. setReceived(receivedAt) {
  607. if (this.isLinkedCard()) {
  608. return Cards.update(
  609. {_id: this.linkedId},
  610. {$set: {receivedAt}}
  611. );
  612. } else {
  613. return Cards.update(
  614. {_id: this._id},
  615. {$set: {receivedAt}}
  616. );
  617. }
  618. },
  619. getStart() {
  620. if (this.isLinkedCard()) {
  621. const card = Cards.findOne({_id: this.linkedId});
  622. return card.startAt;
  623. } else if (this.isLinkedBoard()) {
  624. const board = Boards.findOne({_id: this.linkedId});
  625. return board.startAt;
  626. } else {
  627. return this.startAt;
  628. }
  629. },
  630. setStart(startAt) {
  631. if (this.isLinkedCard()) {
  632. return Cards.update(
  633. { _id: this.linkedId },
  634. {$set: {startAt}}
  635. );
  636. } else if (this.isLinkedBoard()) {
  637. return Boards.update(
  638. {_id: this.linkedId},
  639. {$set: {startAt}}
  640. );
  641. } else {
  642. return Cards.update(
  643. {_id: this._id},
  644. {$set: {startAt}}
  645. );
  646. }
  647. },
  648. getDue() {
  649. if (this.isLinkedCard()) {
  650. const card = Cards.findOne({_id: this.linkedId});
  651. return card.dueAt;
  652. } else if (this.isLinkedBoard()) {
  653. const board = Boards.findOne({_id: this.linkedId});
  654. return board.dueAt;
  655. } else {
  656. return this.dueAt;
  657. }
  658. },
  659. setDue(dueAt) {
  660. if (this.isLinkedCard()) {
  661. return Cards.update(
  662. { _id: this.linkedId },
  663. {$set: {dueAt}}
  664. );
  665. } else if (this.isLinkedBoard()) {
  666. return Boards.update(
  667. {_id: this.linkedId},
  668. {$set: {dueAt}}
  669. );
  670. } else {
  671. return Cards.update(
  672. {_id: this._id},
  673. {$set: {dueAt}}
  674. );
  675. }
  676. },
  677. getEnd() {
  678. if (this.isLinkedCard()) {
  679. const card = Cards.findOne({_id: this.linkedId});
  680. return card.endAt;
  681. } else if (this.isLinkedBoard()) {
  682. const board = Boards.findOne({_id: this.linkedId});
  683. return board.endAt;
  684. } else {
  685. return this.endAt;
  686. }
  687. },
  688. setEnd(endAt) {
  689. if (this.isLinkedCard()) {
  690. return Cards.update(
  691. { _id: this.linkedId },
  692. {$set: {endAt}}
  693. );
  694. } else if (this.isLinkedBoard()) {
  695. return Boards.update(
  696. {_id: this.linkedId},
  697. {$set: {endAt}}
  698. );
  699. } else {
  700. return Cards.update(
  701. {_id: this._id},
  702. {$set: {endAt}}
  703. );
  704. }
  705. },
  706. getIsOvertime() {
  707. if (this.isLinkedCard()) {
  708. const card = Cards.findOne({ _id: this.linkedId });
  709. return card.isOvertime;
  710. } else if (this.isLinkedBoard()) {
  711. const board = Boards.findOne({ _id: this.linkedId});
  712. return board.isOvertime;
  713. } else {
  714. return this.isOvertime;
  715. }
  716. },
  717. setIsOvertime(isOvertime) {
  718. if (this.isLinkedCard()) {
  719. return Cards.update(
  720. { _id: this.linkedId },
  721. {$set: {isOvertime}}
  722. );
  723. } else if (this.isLinkedBoard()) {
  724. return Boards.update(
  725. {_id: this.linkedId},
  726. {$set: {isOvertime}}
  727. );
  728. } else {
  729. return Cards.update(
  730. {_id: this._id},
  731. {$set: {isOvertime}}
  732. );
  733. }
  734. },
  735. getSpentTime() {
  736. if (this.isLinkedCard()) {
  737. const card = Cards.findOne({ _id: this.linkedId });
  738. return card.spentTime;
  739. } else if (this.isLinkedBoard()) {
  740. const board = Boards.findOne({ _id: this.linkedId});
  741. return board.spentTime;
  742. } else {
  743. return this.spentTime;
  744. }
  745. },
  746. setSpentTime(spentTime) {
  747. if (this.isLinkedCard()) {
  748. return Cards.update(
  749. { _id: this.linkedId },
  750. {$set: {spentTime}}
  751. );
  752. } else if (this.isLinkedBoard()) {
  753. return Boards.update(
  754. {_id: this.linkedId},
  755. {$set: {spentTime}}
  756. );
  757. } else {
  758. return Cards.update(
  759. {_id: this._id},
  760. {$set: {spentTime}}
  761. );
  762. }
  763. },
  764. getId() {
  765. if (this.isLinked()) {
  766. return this.linkedId;
  767. } else {
  768. return this._id;
  769. }
  770. },
  771. getTitle() {
  772. if (this.isLinkedCard()) {
  773. const card = Cards.findOne({ _id: this.linkedId });
  774. return card.title;
  775. } else if (this.isLinkedBoard()) {
  776. const board = Boards.findOne({ _id: this.linkedId});
  777. return board.title;
  778. } else {
  779. return this.title;
  780. }
  781. },
  782. getBoardTitle() {
  783. if (this.isLinkedCard()) {
  784. const card = Cards.findOne({ _id: this.linkedId });
  785. const board = Boards.findOne({ _id: card.boardId });
  786. return board.title;
  787. } else if (this.isLinkedBoard()) {
  788. const board = Boards.findOne({ _id: this.linkedId});
  789. return board.title;
  790. } else {
  791. const board = Boards.findOne({ _id: this.boardId });
  792. return board.title;
  793. }
  794. },
  795. setTitle(title) {
  796. if (this.isLinkedCard()) {
  797. return Cards.update(
  798. { _id: this.linkedId },
  799. {$set: {title}}
  800. );
  801. } else if (this.isLinkedBoard()) {
  802. return Boards.update(
  803. {_id: this.linkedId},
  804. {$set: {title}}
  805. );
  806. } else {
  807. return Cards.update(
  808. {_id: this._id},
  809. {$set: {title}}
  810. );
  811. }
  812. },
  813. getArchived() {
  814. if (this.isLinkedCard()) {
  815. const card = Cards.findOne({ _id: this.linkedId });
  816. return card.archived;
  817. } else if (this.isLinkedBoard()) {
  818. const board = Boards.findOne({ _id: this.linkedId});
  819. return board.archived;
  820. } else {
  821. return this.archived;
  822. }
  823. },
  824. setRequestedBy(requestedBy) {
  825. if (this.isLinkedCard()) {
  826. return Cards.update(
  827. { _id: this.linkedId },
  828. {$set: {requestedBy}}
  829. );
  830. } else {
  831. return Cards.update(
  832. {_id: this._id},
  833. {$set: {requestedBy}}
  834. );
  835. }
  836. },
  837. getRequestedBy() {
  838. if (this.isLinkedCard()) {
  839. const card = Cards.findOne({ _id: this.linkedId });
  840. return card.requestedBy;
  841. } else {
  842. return this.requestedBy;
  843. }
  844. },
  845. setAssignedBy(assignedBy) {
  846. if (this.isLinkedCard()) {
  847. return Cards.update(
  848. { _id: this.linkedId },
  849. {$set: {assignedBy}}
  850. );
  851. } else {
  852. return Cards.update(
  853. {_id: this._id},
  854. {$set: {assignedBy}}
  855. );
  856. }
  857. },
  858. getAssignedBy() {
  859. if (this.isLinkedCard()) {
  860. const card = Cards.findOne({ _id: this.linkedId });
  861. return card.assignedBy;
  862. } else {
  863. return this.assignedBy;
  864. }
  865. },
  866. });
  867. Cards.mutations({
  868. applyToChildren(funct) {
  869. Cards.find({
  870. parentId: this._id,
  871. }).forEach((card) => {
  872. funct(card);
  873. });
  874. },
  875. archive() {
  876. this.applyToChildren((card) => {
  877. return card.archive();
  878. });
  879. return {
  880. $set: {
  881. archived: true,
  882. },
  883. };
  884. },
  885. restore() {
  886. this.applyToChildren((card) => {
  887. return card.restore();
  888. });
  889. return {
  890. $set: {
  891. archived: false,
  892. },
  893. };
  894. },
  895. setTitle(title) {
  896. return {
  897. $set: {
  898. title,
  899. },
  900. };
  901. },
  902. setDescription(description) {
  903. return {
  904. $set: {
  905. description,
  906. },
  907. };
  908. },
  909. setRequestedBy(requestedBy) {
  910. return {
  911. $set: {
  912. requestedBy,
  913. },
  914. };
  915. },
  916. setAssignedBy(assignedBy) {
  917. return {
  918. $set: {
  919. assignedBy,
  920. },
  921. };
  922. },
  923. move(swimlaneId, listId, sortIndex) {
  924. const list = Lists.findOne(listId);
  925. const mutatedFields = {
  926. swimlaneId,
  927. listId,
  928. boardId: list.boardId,
  929. sort: sortIndex,
  930. };
  931. return {
  932. $set: mutatedFields,
  933. };
  934. },
  935. addLabel(labelId) {
  936. return {
  937. $addToSet: {
  938. labelIds: labelId,
  939. },
  940. };
  941. },
  942. removeLabel(labelId) {
  943. return {
  944. $pull: {
  945. labelIds: labelId,
  946. },
  947. };
  948. },
  949. toggleLabel(labelId) {
  950. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  951. return this.removeLabel(labelId);
  952. } else {
  953. return this.addLabel(labelId);
  954. }
  955. },
  956. setColor(newColor) {
  957. if (newColor === 'white') {
  958. newColor = null;
  959. }
  960. return {
  961. $set: {
  962. color: newColor,
  963. },
  964. };
  965. },
  966. assignMember(memberId) {
  967. return {
  968. $addToSet: {
  969. members: memberId,
  970. },
  971. };
  972. },
  973. unassignMember(memberId) {
  974. return {
  975. $pull: {
  976. members: memberId,
  977. },
  978. };
  979. },
  980. toggleMember(memberId) {
  981. if (this.members && this.members.indexOf(memberId) > -1) {
  982. return this.unassignMember(memberId);
  983. } else {
  984. return this.assignMember(memberId);
  985. }
  986. },
  987. assignCustomField(customFieldId) {
  988. return {
  989. $addToSet: {
  990. customFields: {
  991. _id: customFieldId,
  992. value: null,
  993. },
  994. },
  995. };
  996. },
  997. unassignCustomField(customFieldId) {
  998. return {
  999. $pull: {
  1000. customFields: {
  1001. _id: customFieldId,
  1002. },
  1003. },
  1004. };
  1005. },
  1006. toggleCustomField(customFieldId) {
  1007. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1008. return this.unassignCustomField(customFieldId);
  1009. } else {
  1010. return this.assignCustomField(customFieldId);
  1011. }
  1012. },
  1013. setCustomField(customFieldId, value) {
  1014. // todo
  1015. const index = this.customFieldIndex(customFieldId);
  1016. if (index > -1) {
  1017. const update = {
  1018. $set: {},
  1019. };
  1020. update.$set[`customFields.${index}.value`] = value;
  1021. return update;
  1022. }
  1023. // TODO
  1024. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1025. return null;
  1026. },
  1027. setCover(coverId) {
  1028. return {
  1029. $set: {
  1030. coverId,
  1031. },
  1032. };
  1033. },
  1034. unsetCover() {
  1035. return {
  1036. $unset: {
  1037. coverId: '',
  1038. },
  1039. };
  1040. },
  1041. setReceived(receivedAt) {
  1042. return {
  1043. $set: {
  1044. receivedAt,
  1045. },
  1046. };
  1047. },
  1048. unsetReceived() {
  1049. return {
  1050. $unset: {
  1051. receivedAt: '',
  1052. },
  1053. };
  1054. },
  1055. setStart(startAt) {
  1056. return {
  1057. $set: {
  1058. startAt,
  1059. },
  1060. };
  1061. },
  1062. unsetStart() {
  1063. return {
  1064. $unset: {
  1065. startAt: '',
  1066. },
  1067. };
  1068. },
  1069. setDue(dueAt) {
  1070. return {
  1071. $set: {
  1072. dueAt,
  1073. },
  1074. };
  1075. },
  1076. unsetDue() {
  1077. return {
  1078. $unset: {
  1079. dueAt: '',
  1080. },
  1081. };
  1082. },
  1083. setEnd(endAt) {
  1084. return {
  1085. $set: {
  1086. endAt,
  1087. },
  1088. };
  1089. },
  1090. unsetEnd() {
  1091. return {
  1092. $unset: {
  1093. endAt: '',
  1094. },
  1095. };
  1096. },
  1097. setOvertime(isOvertime) {
  1098. return {
  1099. $set: {
  1100. isOvertime,
  1101. },
  1102. };
  1103. },
  1104. setSpentTime(spentTime) {
  1105. return {
  1106. $set: {
  1107. spentTime,
  1108. },
  1109. };
  1110. },
  1111. unsetSpentTime() {
  1112. return {
  1113. $unset: {
  1114. spentTime: '',
  1115. isOvertime: false,
  1116. },
  1117. };
  1118. },
  1119. setParentId(parentId) {
  1120. return {
  1121. $set: {
  1122. parentId,
  1123. },
  1124. };
  1125. },
  1126. });
  1127. //FUNCTIONS FOR creation of Activities
  1128. function cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId) {
  1129. if ((_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1130. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)){
  1131. Activities.insert({
  1132. userId,
  1133. oldListId,
  1134. activityType: 'moveCard',
  1135. listName: Lists.findOne(doc.listId).title,
  1136. listId: doc.listId,
  1137. boardId: doc.boardId,
  1138. cardId: doc._id,
  1139. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1140. swimlaneId: doc.swimlaneId,
  1141. oldSwimlaneId,
  1142. });
  1143. }
  1144. }
  1145. function cardState(userId, doc, fieldNames) {
  1146. if (_.contains(fieldNames, 'archived')) {
  1147. if (doc.archived) {
  1148. Activities.insert({
  1149. userId,
  1150. activityType: 'archivedCard',
  1151. listName: Lists.findOne(doc.listId).title,
  1152. boardId: doc.boardId,
  1153. listId: doc.listId,
  1154. cardId: doc._id,
  1155. });
  1156. } else {
  1157. Activities.insert({
  1158. userId,
  1159. activityType: 'restoredCard',
  1160. boardId: doc.boardId,
  1161. listName: Lists.findOne(doc.listId).title,
  1162. listId: doc.listId,
  1163. cardId: doc._id,
  1164. });
  1165. }
  1166. }
  1167. }
  1168. function cardMembers(userId, doc, fieldNames, modifier) {
  1169. if (!_.contains(fieldNames, 'members'))
  1170. return;
  1171. let memberId;
  1172. // Say hello to the new member
  1173. if (modifier.$addToSet && modifier.$addToSet.members) {
  1174. memberId = modifier.$addToSet.members;
  1175. const username = Users.findOne(memberId).username;
  1176. if (!_.contains(doc.members, memberId)) {
  1177. Activities.insert({
  1178. userId,
  1179. username,
  1180. activityType: 'joinMember',
  1181. boardId: doc.boardId,
  1182. cardId: doc._id,
  1183. });
  1184. }
  1185. }
  1186. // Say goodbye to the former member
  1187. if (modifier.$pull && modifier.$pull.members) {
  1188. memberId = modifier.$pull.members;
  1189. const username = Users.findOne(memberId).username;
  1190. // Check that the former member is member of the card
  1191. if (_.contains(doc.members, memberId)) {
  1192. Activities.insert({
  1193. userId,
  1194. username,
  1195. activityType: 'unjoinMember',
  1196. boardId: doc.boardId,
  1197. cardId: doc._id,
  1198. });
  1199. }
  1200. }
  1201. }
  1202. function cardLabels(userId, doc, fieldNames, modifier) {
  1203. if (!_.contains(fieldNames, 'labelIds'))
  1204. return;
  1205. let labelId;
  1206. // Say hello to the new label
  1207. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1208. labelId = modifier.$addToSet.labelIds;
  1209. if (!_.contains(doc.labelIds, labelId)) {
  1210. const act = {
  1211. userId,
  1212. labelId,
  1213. activityType: 'addedLabel',
  1214. boardId: doc.boardId,
  1215. cardId: doc._id,
  1216. };
  1217. Activities.insert(act);
  1218. }
  1219. }
  1220. // Say goodbye to the label
  1221. if (modifier.$pull && modifier.$pull.labelIds) {
  1222. labelId = modifier.$pull.labelIds;
  1223. // Check that the former member is member of the card
  1224. if (_.contains(doc.labelIds, labelId)) {
  1225. Activities.insert({
  1226. userId,
  1227. labelId,
  1228. activityType: 'removedLabel',
  1229. boardId: doc.boardId,
  1230. cardId: doc._id,
  1231. });
  1232. }
  1233. }
  1234. }
  1235. function cardCreation(userId, doc) {
  1236. Activities.insert({
  1237. userId,
  1238. activityType: 'createCard',
  1239. boardId: doc.boardId,
  1240. listName: Lists.findOne(doc.listId).title,
  1241. listId: doc.listId,
  1242. cardId: doc._id,
  1243. cardTitle:doc.title,
  1244. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1245. swimlaneId: doc.swimlaneId,
  1246. });
  1247. }
  1248. function cardRemover(userId, doc) {
  1249. Activities.remove({
  1250. cardId: doc._id,
  1251. });
  1252. Checklists.remove({
  1253. cardId: doc._id,
  1254. });
  1255. Subtasks.remove({
  1256. cardId: doc._id,
  1257. });
  1258. CardComments.remove({
  1259. cardId: doc._id,
  1260. });
  1261. Attachments.remove({
  1262. cardId: doc._id,
  1263. });
  1264. }
  1265. if (Meteor.isServer) {
  1266. // Cards are often fetched within a board, so we create an index to make these
  1267. // queries more efficient.
  1268. Meteor.startup(() => {
  1269. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  1270. // https://github.com/wekan/wekan/issues/1863
  1271. // Swimlane added a new field in the cards collection of mongodb named parentId.
  1272. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  1273. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  1274. // To correct it, add Index to parentId:
  1275. Cards._collection._ensureIndex({parentId: 1});
  1276. });
  1277. Cards.after.insert((userId, doc) => {
  1278. cardCreation(userId, doc);
  1279. });
  1280. // New activity for card (un)archivage
  1281. Cards.after.update((userId, doc, fieldNames) => {
  1282. cardState(userId, doc, fieldNames);
  1283. });
  1284. //New activity for card moves
  1285. Cards.after.update(function(userId, doc, fieldNames) {
  1286. const oldListId = this.previous.listId;
  1287. const oldSwimlaneId = this.previous.swimlaneId;
  1288. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId);
  1289. });
  1290. // Add a new activity if we add or remove a member to the card
  1291. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1292. cardMembers(userId, doc, fieldNames, modifier);
  1293. });
  1294. // Add a new activity if we add or remove a label to the card
  1295. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1296. cardLabels(userId, doc, fieldNames, modifier);
  1297. });
  1298. // Remove all activities associated with a card if we remove the card
  1299. // Remove also card_comments / checklists / attachments
  1300. Cards.after.remove((userId, doc) => {
  1301. cardRemover(userId, doc);
  1302. });
  1303. }
  1304. //SWIMLANES REST API
  1305. if (Meteor.isServer) {
  1306. /**
  1307. * @operation get_swimlane_cards
  1308. * @summary get all cards attached to a swimlane
  1309. *
  1310. * @param {string} boardId the board ID
  1311. * @param {string} swimlaneId the swimlane ID
  1312. * @return_type [{_id: string,
  1313. * title: string,
  1314. * description: string,
  1315. * listId: string}]
  1316. */
  1317. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId/cards', function(req, res) {
  1318. const paramBoardId = req.params.boardId;
  1319. const paramSwimlaneId = req.params.swimlaneId;
  1320. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1321. JsonRoutes.sendResult(res, {
  1322. code: 200,
  1323. data: Cards.find({
  1324. boardId: paramBoardId,
  1325. swimlaneId: paramSwimlaneId,
  1326. archived: false,
  1327. }).map(function(doc) {
  1328. return {
  1329. _id: doc._id,
  1330. title: doc.title,
  1331. description: doc.description,
  1332. listId: doc.listId,
  1333. };
  1334. }),
  1335. });
  1336. });
  1337. }
  1338. //LISTS REST API
  1339. if (Meteor.isServer) {
  1340. /**
  1341. * @operation get_all_cards
  1342. * @summary Get all Cards attached to a List
  1343. *
  1344. * @param {string} boardId the board ID
  1345. * @param {string} listId the list ID
  1346. * @return_type [{_id: string,
  1347. * title: string,
  1348. * description: string}]
  1349. */
  1350. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1351. const paramBoardId = req.params.boardId;
  1352. const paramListId = req.params.listId;
  1353. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1354. JsonRoutes.sendResult(res, {
  1355. code: 200,
  1356. data: Cards.find({
  1357. boardId: paramBoardId,
  1358. listId: paramListId,
  1359. archived: false,
  1360. }).map(function(doc) {
  1361. return {
  1362. _id: doc._id,
  1363. title: doc.title,
  1364. description: doc.description,
  1365. };
  1366. }),
  1367. });
  1368. });
  1369. /**
  1370. * @operation get_card
  1371. * @summary Get a Card
  1372. *
  1373. * @param {string} boardId the board ID
  1374. * @param {string} listId the list ID of the card
  1375. * @param {string} cardId the card ID
  1376. * @return_type Cards
  1377. */
  1378. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1379. const paramBoardId = req.params.boardId;
  1380. const paramListId = req.params.listId;
  1381. const paramCardId = req.params.cardId;
  1382. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1383. JsonRoutes.sendResult(res, {
  1384. code: 200,
  1385. data: Cards.findOne({
  1386. _id: paramCardId,
  1387. listId: paramListId,
  1388. boardId: paramBoardId,
  1389. archived: false,
  1390. }),
  1391. });
  1392. });
  1393. /**
  1394. * @operation new_card
  1395. * @summary Create a new Card
  1396. *
  1397. * @param {string} boardId the board ID of the new card
  1398. * @param {string} listId the list ID of the new card
  1399. * @param {string} authorID the user ID of the person owning the card
  1400. * @param {string} title the title of the new card
  1401. * @param {string} description the description of the new card
  1402. * @param {string} swimlaneId the swimlane ID of the new card
  1403. * @param {string} [members] the member IDs list of the new card
  1404. * @return_type {_id: string}
  1405. */
  1406. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1407. Authentication.checkUserId(req.userId);
  1408. const paramBoardId = req.params.boardId;
  1409. const paramListId = req.params.listId;
  1410. const currentCards = Cards.find({
  1411. listId: paramListId,
  1412. archived: false,
  1413. }, { sort: ['sort'] });
  1414. const check = Users.findOne({
  1415. _id: req.body.authorId,
  1416. });
  1417. const members = req.body.members || [req.body.authorId];
  1418. if (typeof check !== 'undefined') {
  1419. const id = Cards.direct.insert({
  1420. title: req.body.title,
  1421. boardId: paramBoardId,
  1422. listId: paramListId,
  1423. description: req.body.description,
  1424. userId: req.body.authorId,
  1425. swimlaneId: req.body.swimlaneId,
  1426. sort: currentCards.count(),
  1427. members,
  1428. });
  1429. JsonRoutes.sendResult(res, {
  1430. code: 200,
  1431. data: {
  1432. _id: id,
  1433. },
  1434. });
  1435. const card = Cards.findOne({
  1436. _id: id,
  1437. });
  1438. cardCreation(req.body.authorId, card);
  1439. } else {
  1440. JsonRoutes.sendResult(res, {
  1441. code: 401,
  1442. });
  1443. }
  1444. });
  1445. /*
  1446. * Note for the JSDoc:
  1447. * 'list' will be interpreted as the path parameter
  1448. * 'listID' will be interpreted as the body parameter
  1449. */
  1450. /**
  1451. * @operation edit_card
  1452. * @summary Edit Fields in a Card
  1453. *
  1454. * @description Edit a card
  1455. *
  1456. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  1457. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  1458. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  1459. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  1460. * `indigo`:
  1461. *
  1462. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  1463. *
  1464. * Note: setting the color to white has the same effect than removing it.
  1465. *
  1466. * @param {string} boardId the board ID of the card
  1467. * @param {string} list the list ID of the card
  1468. * @param {string} cardId the ID of the card
  1469. * @param {string} [title] the new title of the card
  1470. * @param {string} [listId] the new list ID of the card (move operation)
  1471. * @param {string} [description] the new description of the card
  1472. * @param {string} [authorId] change the owner of the card
  1473. * @param {string} [labelIds] the new list of label IDs attached to the card
  1474. * @param {string} [swimlaneId] the new swimlane ID of the card
  1475. * @param {string} [members] the new list of member IDs attached to the card
  1476. * @param {string} [requestedBy] the new requestedBy field of the card
  1477. * @param {string} [assignedBy] the new assignedBy field of the card
  1478. * @param {string} [receivedAt] the new receivedAt field of the card
  1479. * @param {string} [assignBy] the new assignBy field of the card
  1480. * @param {string} [startAt] the new startAt field of the card
  1481. * @param {string} [dueAt] the new dueAt field of the card
  1482. * @param {string} [endAt] the new endAt field of the card
  1483. * @param {string} [spentTime] the new spentTime field of the card
  1484. * @param {boolean} [isOverTime] the new isOverTime field of the card
  1485. * @param {string} [customFields] the new customFields value of the card
  1486. * @param {string} [color] the new color of the card
  1487. * @return_type {_id: string}
  1488. */
  1489. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1490. Authentication.checkUserId(req.userId);
  1491. const paramBoardId = req.params.boardId;
  1492. const paramCardId = req.params.cardId;
  1493. const paramListId = req.params.listId;
  1494. if (req.body.hasOwnProperty('title')) {
  1495. const newTitle = req.body.title;
  1496. Cards.direct.update({
  1497. _id: paramCardId,
  1498. listId: paramListId,
  1499. boardId: paramBoardId,
  1500. archived: false,
  1501. }, {
  1502. $set: {
  1503. title: newTitle,
  1504. },
  1505. });
  1506. }
  1507. if (req.body.hasOwnProperty('listId')) {
  1508. const newParamListId = req.body.listId;
  1509. Cards.direct.update({
  1510. _id: paramCardId,
  1511. listId: paramListId,
  1512. boardId: paramBoardId,
  1513. archived: false,
  1514. }, {
  1515. $set: {
  1516. listId: newParamListId,
  1517. },
  1518. });
  1519. const card = Cards.findOne({
  1520. _id: paramCardId,
  1521. });
  1522. cardMove(req.body.authorId, card, {
  1523. fieldName: 'listId',
  1524. }, paramListId);
  1525. }
  1526. if (req.body.hasOwnProperty('description')) {
  1527. const newDescription = req.body.description;
  1528. Cards.direct.update({
  1529. _id: paramCardId,
  1530. listId: paramListId,
  1531. boardId: paramBoardId,
  1532. archived: false,
  1533. }, {
  1534. $set: {
  1535. description: newDescription,
  1536. },
  1537. });
  1538. }
  1539. if (req.body.hasOwnProperty('color')) {
  1540. const newColor = req.body.color;
  1541. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1542. {$set: {color: newColor}});
  1543. }
  1544. if (req.body.hasOwnProperty('labelIds')) {
  1545. let newlabelIds = req.body.labelIds;
  1546. if (_.isString(newlabelIds)) {
  1547. if (newlabelIds === '') {
  1548. newlabelIds = null;
  1549. }
  1550. else {
  1551. newlabelIds = [newlabelIds];
  1552. }
  1553. }
  1554. Cards.direct.update({
  1555. _id: paramCardId,
  1556. listId: paramListId,
  1557. boardId: paramBoardId,
  1558. archived: false,
  1559. }, {
  1560. $set: {
  1561. labelIds: newlabelIds,
  1562. },
  1563. });
  1564. }
  1565. if (req.body.hasOwnProperty('requestedBy')) {
  1566. const newrequestedBy = req.body.requestedBy;
  1567. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1568. {$set: {requestedBy: newrequestedBy}});
  1569. }
  1570. if (req.body.hasOwnProperty('assignedBy')) {
  1571. const newassignedBy = req.body.assignedBy;
  1572. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1573. {$set: {assignedBy: newassignedBy}});
  1574. }
  1575. if (req.body.hasOwnProperty('receivedAt')) {
  1576. const newreceivedAt = req.body.receivedAt;
  1577. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1578. {$set: {receivedAt: newreceivedAt}});
  1579. }
  1580. if (req.body.hasOwnProperty('startAt')) {
  1581. const newstartAt = req.body.startAt;
  1582. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1583. {$set: {startAt: newstartAt}});
  1584. }
  1585. if (req.body.hasOwnProperty('dueAt')) {
  1586. const newdueAt = req.body.dueAt;
  1587. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1588. {$set: {dueAt: newdueAt}});
  1589. }
  1590. if (req.body.hasOwnProperty('endAt')) {
  1591. const newendAt = req.body.endAt;
  1592. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1593. {$set: {endAt: newendAt}});
  1594. }
  1595. if (req.body.hasOwnProperty('spentTime')) {
  1596. const newspentTime = req.body.spentTime;
  1597. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1598. {$set: {spentTime: newspentTime}});
  1599. }
  1600. if (req.body.hasOwnProperty('isOverTime')) {
  1601. const newisOverTime = req.body.isOverTime;
  1602. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1603. {$set: {isOverTime: newisOverTime}});
  1604. }
  1605. if (req.body.hasOwnProperty('customFields')) {
  1606. const newcustomFields = req.body.customFields;
  1607. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1608. {$set: {customFields: newcustomFields}});
  1609. }
  1610. if (req.body.hasOwnProperty('members')) {
  1611. let newmembers = req.body.members;
  1612. if (_.isString(newmembers)) {
  1613. if (newmembers === '') {
  1614. newmembers = null;
  1615. }
  1616. else {
  1617. newmembers = [newmembers];
  1618. }
  1619. }
  1620. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1621. {$set: {members: newmembers}});
  1622. }
  1623. if (req.body.hasOwnProperty('swimlaneId')) {
  1624. const newParamSwimlaneId = req.body.swimlaneId;
  1625. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1626. {$set: {swimlaneId: newParamSwimlaneId}});
  1627. }
  1628. JsonRoutes.sendResult(res, {
  1629. code: 200,
  1630. data: {
  1631. _id: paramCardId,
  1632. },
  1633. });
  1634. });
  1635. /**
  1636. * @operation delete_card
  1637. * @summary Delete a card from a board
  1638. *
  1639. * @description This operation **deletes** a card, and therefore the card
  1640. * is not put in the recycle bin.
  1641. *
  1642. * @param {string} boardId the board ID of the card
  1643. * @param {string} list the list ID of the card
  1644. * @param {string} cardId the ID of the card
  1645. * @return_type {_id: string}
  1646. */
  1647. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1648. Authentication.checkUserId(req.userId);
  1649. const paramBoardId = req.params.boardId;
  1650. const paramListId = req.params.listId;
  1651. const paramCardId = req.params.cardId;
  1652. Cards.direct.remove({
  1653. _id: paramCardId,
  1654. listId: paramListId,
  1655. boardId: paramBoardId,
  1656. });
  1657. const card = Cards.find({
  1658. _id: paramCardId,
  1659. });
  1660. cardRemover(req.body.authorId, card);
  1661. JsonRoutes.sendResult(res, {
  1662. code: 200,
  1663. data: {
  1664. _id: paramCardId,
  1665. },
  1666. });
  1667. });
  1668. }