cards.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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. type: String,
  8. optional: true,
  9. defaultValue: '',
  10. },
  11. archived: {
  12. type: Boolean,
  13. autoValue() { // eslint-disable-line consistent-return
  14. if (this.isInsert && !this.isSet) {
  15. return false;
  16. }
  17. },
  18. },
  19. parentId: {
  20. type: String,
  21. optional: true,
  22. defaultValue: '',
  23. },
  24. listId: {
  25. type: String,
  26. optional: true,
  27. defaultValue: '',
  28. },
  29. swimlaneId: {
  30. type: String,
  31. },
  32. // The system could work without this `boardId` information (we could deduce
  33. // the board identifier from the card), but it would make the system more
  34. // difficult to manage and less efficient.
  35. boardId: {
  36. type: String,
  37. optional: true,
  38. defaultValue: '',
  39. },
  40. coverId: {
  41. type: String,
  42. optional: true,
  43. defaultValue: '',
  44. },
  45. createdAt: {
  46. type: Date,
  47. autoValue() { // eslint-disable-line consistent-return
  48. if (this.isInsert) {
  49. return new Date();
  50. } else {
  51. this.unset();
  52. }
  53. },
  54. },
  55. customFields: {
  56. type: [Object],
  57. optional: true,
  58. defaultValue: [],
  59. },
  60. 'customFields.$': {
  61. type: new SimpleSchema({
  62. _id: {
  63. type: String,
  64. optional: true,
  65. defaultValue: '',
  66. },
  67. value: {
  68. type: Match.OneOf(String, Number, Boolean, Date),
  69. optional: true,
  70. defaultValue: '',
  71. },
  72. }),
  73. },
  74. dateLastActivity: {
  75. type: Date,
  76. autoValue() {
  77. return new Date();
  78. },
  79. },
  80. description: {
  81. type: String,
  82. optional: true,
  83. defaultValue: '',
  84. },
  85. requestedBy: {
  86. type: String,
  87. optional: true,
  88. defaultValue: '',
  89. },
  90. assignedBy: {
  91. type: String,
  92. optional: true,
  93. defaultValue: '',
  94. },
  95. labelIds: {
  96. type: [String],
  97. optional: true,
  98. defaultValue: [],
  99. },
  100. members: {
  101. type: [String],
  102. optional: true,
  103. defaultValue: [],
  104. },
  105. receivedAt: {
  106. type: Date,
  107. optional: true,
  108. },
  109. startAt: {
  110. type: Date,
  111. optional: true,
  112. },
  113. dueAt: {
  114. type: Date,
  115. optional: true,
  116. },
  117. endAt: {
  118. type: Date,
  119. optional: true,
  120. },
  121. spentTime: {
  122. type: Number,
  123. decimal: true,
  124. optional: true,
  125. defaultValue: 0,
  126. },
  127. isOvertime: {
  128. type: Boolean,
  129. defaultValue: false,
  130. optional: true,
  131. },
  132. // XXX Should probably be called `authorId`. Is it even needed since we have
  133. // the `members` field?
  134. userId: {
  135. type: String,
  136. autoValue() { // eslint-disable-line consistent-return
  137. if (this.isInsert && !this.isSet) {
  138. return this.userId;
  139. }
  140. },
  141. },
  142. sort: {
  143. type: Number,
  144. decimal: true,
  145. defaultValue: '',
  146. },
  147. subtaskSort: {
  148. type: Number,
  149. decimal: true,
  150. defaultValue: -1,
  151. optional: true,
  152. },
  153. type: {
  154. type: String,
  155. defaultValue: '',
  156. },
  157. linkedId: {
  158. type: String,
  159. optional: true,
  160. defaultValue: '',
  161. },
  162. }));
  163. Cards.allow({
  164. insert(userId, doc) {
  165. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  166. },
  167. update(userId, doc) {
  168. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  169. },
  170. remove(userId, doc) {
  171. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  172. },
  173. fetch: ['boardId'],
  174. });
  175. Cards.helpers({
  176. list() {
  177. return Lists.findOne(this.listId);
  178. },
  179. board() {
  180. return Boards.findOne(this.boardId);
  181. },
  182. labels() {
  183. const boardLabels = this.board().labels;
  184. const cardLabels = _.filter(boardLabels, (label) => {
  185. return _.contains(this.labelIds, label._id);
  186. });
  187. return cardLabels;
  188. },
  189. hasLabel(labelId) {
  190. return _.contains(this.labelIds, labelId);
  191. },
  192. user() {
  193. return Users.findOne(this.userId);
  194. },
  195. isAssigned(memberId) {
  196. return _.contains(this.getMembers(), memberId);
  197. },
  198. activities() {
  199. if (this.isLinkedCard()) {
  200. return Activities.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  201. } else if (this.isLinkedBoard()) {
  202. return Activities.find({boardId: this.linkedId}, {sort: {createdAt: -1}});
  203. } else {
  204. return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
  205. }
  206. },
  207. comments() {
  208. if (this.isLinkedCard()) {
  209. return CardComments.find({cardId: this.linkedId}, {sort: {createdAt: -1}});
  210. } else {
  211. return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
  212. }
  213. },
  214. attachments() {
  215. if (this.isLinkedCard()) {
  216. return Attachments.find({cardId: this.linkedId}, {sort: {uploadedAt: -1}});
  217. } else {
  218. return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
  219. }
  220. },
  221. cover() {
  222. const cover = Attachments.findOne(this.coverId);
  223. // if we return a cover before it is fully stored, we will get errors when we try to display it
  224. // todo XXX we could return a default "upload pending" image in the meantime?
  225. return cover && cover.url() && cover;
  226. },
  227. checklists() {
  228. if (this.isLinkedCard()) {
  229. return Checklists.find({cardId: this.linkedId}, {sort: { sort: 1 } });
  230. } else {
  231. return Checklists.find({cardId: this._id}, {sort: { sort: 1 } });
  232. }
  233. },
  234. checklistItemCount() {
  235. const checklists = this.checklists().fetch();
  236. return checklists.map((checklist) => {
  237. return checklist.itemCount();
  238. }).reduce((prev, next) => {
  239. return prev + next;
  240. }, 0);
  241. },
  242. checklistFinishedCount() {
  243. const checklists = this.checklists().fetch();
  244. return checklists.map((checklist) => {
  245. return checklist.finishedCount();
  246. }).reduce((prev, next) => {
  247. return prev + next;
  248. }, 0);
  249. },
  250. checklistFinished() {
  251. return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
  252. },
  253. hasChecklist() {
  254. return this.checklistItemCount() !== 0;
  255. },
  256. subtasks() {
  257. return Cards.find({
  258. parentId: this._id,
  259. archived: false,
  260. }, {sort: { sort: 1 } });
  261. },
  262. allSubtasks() {
  263. return Cards.find({
  264. parentId: this._id,
  265. archived: false,
  266. }, {sort: { sort: 1 } });
  267. },
  268. subtasksCount() {
  269. return Cards.find({
  270. parentId: this._id,
  271. archived: false,
  272. }).count();
  273. },
  274. subtasksFinishedCount() {
  275. return Cards.find({
  276. parentId: this._id,
  277. archived: true}).count();
  278. },
  279. subtasksFinished() {
  280. const finishCount = this.subtasksFinishedCount();
  281. return finishCount > 0 && this.subtasksCount() === finishCount;
  282. },
  283. allowsSubtasks() {
  284. return this.subtasksCount() !== 0;
  285. },
  286. customFieldIndex(customFieldId) {
  287. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  288. },
  289. // customFields with definitions
  290. customFieldsWD() {
  291. // get all definitions
  292. const definitions = CustomFields.find({
  293. boardId: this.boardId,
  294. }).fetch();
  295. // match right definition to each field
  296. if (!this.customFields) return [];
  297. return this.customFields.map((customField) => {
  298. const definition = definitions.find((definition) => {
  299. return definition._id === customField._id;
  300. });
  301. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  302. let trueValue = customField.value;
  303. if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0)
  304. {
  305. for (let i = 0; i < definition.settings.dropdownItems.length; i++)
  306. {
  307. if (definition.settings.dropdownItems[i]._id === customField.value)
  308. {
  309. trueValue = definition.settings.dropdownItems[i].name;
  310. }
  311. }
  312. }
  313. return {
  314. _id: customField._id,
  315. value: customField.value,
  316. trueValue,
  317. definition,
  318. };
  319. });
  320. },
  321. absoluteUrl() {
  322. const board = this.board();
  323. return FlowRouter.url('card', {
  324. boardId: board._id,
  325. slug: board.slug,
  326. cardId: this._id,
  327. });
  328. },
  329. canBeRestored() {
  330. const list = Lists.findOne({_id: this.listId});
  331. if(!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()){
  332. return false;
  333. }
  334. return true;
  335. },
  336. parentCard() {
  337. if (this.parentId === '') {
  338. return null;
  339. }
  340. return Cards.findOne(this.parentId);
  341. },
  342. parentCardName() {
  343. let result = '';
  344. if (this.parentId !== '') {
  345. const card = Cards.findOne(this.parentId);
  346. if (card) {
  347. result = card.title;
  348. }
  349. }
  350. return result;
  351. },
  352. parentListId() {
  353. const result = [];
  354. let crtParentId = this.parentId;
  355. while (crtParentId !== '') {
  356. const crt = Cards.findOne(crtParentId);
  357. if ((crt === null) || (crt === undefined)) {
  358. // maybe it has been deleted
  359. break;
  360. }
  361. if (crtParentId in result) {
  362. // circular reference
  363. break;
  364. }
  365. result.unshift(crtParentId);
  366. crtParentId = crt.parentId;
  367. }
  368. return result;
  369. },
  370. parentList() {
  371. const resultId = [];
  372. const result = [];
  373. let crtParentId = this.parentId;
  374. while (crtParentId !== '') {
  375. const crt = Cards.findOne(crtParentId);
  376. if ((crt === null) || (crt === undefined)) {
  377. // maybe it has been deleted
  378. break;
  379. }
  380. if (crtParentId in resultId) {
  381. // circular reference
  382. break;
  383. }
  384. resultId.unshift(crtParentId);
  385. result.unshift(crt);
  386. crtParentId = crt.parentId;
  387. }
  388. return result;
  389. },
  390. parentString(sep) {
  391. return this.parentList().map(function(elem){
  392. return elem.title;
  393. }).join(sep);
  394. },
  395. isTopLevel() {
  396. return this.parentId === '';
  397. },
  398. isLinkedCard() {
  399. return this.type === 'cardType-linkedCard';
  400. },
  401. isLinkedBoard() {
  402. return this.type === 'cardType-linkedBoard';
  403. },
  404. isLinked() {
  405. return this.isLinkedCard() || this.isLinkedBoard();
  406. },
  407. setDescription(description) {
  408. if (this.isLinkedCard()) {
  409. return Cards.update({_id: this.linkedId}, {$set: {description}});
  410. } else if (this.isLinkedBoard()) {
  411. return Boards.update({_id: this.linkedId}, {$set: {description}});
  412. } else {
  413. return Cards.update(
  414. {_id: this._id},
  415. {$set: {description}}
  416. );
  417. }
  418. },
  419. getDescription() {
  420. if (this.isLinkedCard()) {
  421. const card = Cards.findOne({_id: this.linkedId});
  422. if (card && card.description)
  423. return card.description;
  424. else
  425. return null;
  426. } else if (this.isLinkedBoard()) {
  427. const board = Boards.findOne({_id: this.linkedId});
  428. if (board && board.description)
  429. return board.description;
  430. else
  431. return null;
  432. } else if (this.description) {
  433. return this.description;
  434. } else {
  435. return null;
  436. }
  437. },
  438. getMembers() {
  439. if (this.isLinkedCard()) {
  440. const card = Cards.findOne({_id: this.linkedId});
  441. return card.members;
  442. } else if (this.isLinkedBoard()) {
  443. const board = Boards.findOne({_id: this.linkedId});
  444. return board.activeMembers().map((member) => {
  445. return member.userId;
  446. });
  447. } else {
  448. return this.members;
  449. }
  450. },
  451. assignMember(memberId) {
  452. if (this.isLinkedCard()) {
  453. return Cards.update(
  454. { _id: this.linkedId },
  455. { $addToSet: { members: memberId }}
  456. );
  457. } else if (this.isLinkedBoard()) {
  458. const board = Boards.findOne({_id: this.linkedId});
  459. return board.addMember(memberId);
  460. } else {
  461. return Cards.update(
  462. { _id: this._id },
  463. { $addToSet: { members: memberId}}
  464. );
  465. }
  466. },
  467. unassignMember(memberId) {
  468. if (this.isLinkedCard()) {
  469. return Cards.update(
  470. { _id: this.linkedId },
  471. { $pull: { members: memberId }}
  472. );
  473. } else if (this.isLinkedBoard()) {
  474. const board = Boards.findOne({_id: this.linkedId});
  475. return board.removeMember(memberId);
  476. } else {
  477. return Cards.update(
  478. { _id: this._id },
  479. { $pull: { members: memberId}}
  480. );
  481. }
  482. },
  483. toggleMember(memberId) {
  484. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  485. return this.unassignMember(memberId);
  486. } else {
  487. return this.assignMember(memberId);
  488. }
  489. },
  490. getReceived() {
  491. if (this.isLinkedCard()) {
  492. const card = Cards.findOne({_id: this.linkedId});
  493. return card.receivedAt;
  494. } else {
  495. return this.receivedAt;
  496. }
  497. },
  498. setReceived(receivedAt) {
  499. if (this.isLinkedCard()) {
  500. return Cards.update(
  501. {_id: this.linkedId},
  502. {$set: {receivedAt}}
  503. );
  504. } else {
  505. return Cards.update(
  506. {_id: this._id},
  507. {$set: {receivedAt}}
  508. );
  509. }
  510. },
  511. getStart() {
  512. if (this.isLinkedCard()) {
  513. const card = Cards.findOne({_id: this.linkedId});
  514. return card.startAt;
  515. } else if (this.isLinkedBoard()) {
  516. const board = Boards.findOne({_id: this.linkedId});
  517. return board.startAt;
  518. } else {
  519. return this.startAt;
  520. }
  521. },
  522. setStart(startAt) {
  523. if (this.isLinkedCard()) {
  524. return Cards.update(
  525. { _id: this.linkedId },
  526. {$set: {startAt}}
  527. );
  528. } else if (this.isLinkedBoard()) {
  529. return Boards.update(
  530. {_id: this.linkedId},
  531. {$set: {startAt}}
  532. );
  533. } else {
  534. return Cards.update(
  535. {_id: this._id},
  536. {$set: {startAt}}
  537. );
  538. }
  539. },
  540. getDue() {
  541. if (this.isLinkedCard()) {
  542. const card = Cards.findOne({_id: this.linkedId});
  543. return card.dueAt;
  544. } else if (this.isLinkedBoard()) {
  545. const board = Boards.findOne({_id: this.linkedId});
  546. return board.dueAt;
  547. } else {
  548. return this.dueAt;
  549. }
  550. },
  551. setDue(dueAt) {
  552. if (this.isLinkedCard()) {
  553. return Cards.update(
  554. { _id: this.linkedId },
  555. {$set: {dueAt}}
  556. );
  557. } else if (this.isLinkedBoard()) {
  558. return Boards.update(
  559. {_id: this.linkedId},
  560. {$set: {dueAt}}
  561. );
  562. } else {
  563. return Cards.update(
  564. {_id: this._id},
  565. {$set: {dueAt}}
  566. );
  567. }
  568. },
  569. getEnd() {
  570. if (this.isLinkedCard()) {
  571. const card = Cards.findOne({_id: this.linkedId});
  572. return card.endAt;
  573. } else if (this.isLinkedBoard()) {
  574. const board = Boards.findOne({_id: this.linkedId});
  575. return board.endAt;
  576. } else {
  577. return this.endAt;
  578. }
  579. },
  580. setEnd(endAt) {
  581. if (this.isLinkedCard()) {
  582. return Cards.update(
  583. { _id: this.linkedId },
  584. {$set: {endAt}}
  585. );
  586. } else if (this.isLinkedBoard()) {
  587. return Boards.update(
  588. {_id: this.linkedId},
  589. {$set: {endAt}}
  590. );
  591. } else {
  592. return Cards.update(
  593. {_id: this._id},
  594. {$set: {endAt}}
  595. );
  596. }
  597. },
  598. getIsOvertime() {
  599. if (this.isLinkedCard()) {
  600. const card = Cards.findOne({ _id: this.linkedId });
  601. return card.isOvertime;
  602. } else if (this.isLinkedBoard()) {
  603. const board = Boards.findOne({ _id: this.linkedId});
  604. return board.isOvertime;
  605. } else {
  606. return this.isOvertime;
  607. }
  608. },
  609. setIsOvertime(isOvertime) {
  610. if (this.isLinkedCard()) {
  611. return Cards.update(
  612. { _id: this.linkedId },
  613. {$set: {isOvertime}}
  614. );
  615. } else if (this.isLinkedBoard()) {
  616. return Boards.update(
  617. {_id: this.linkedId},
  618. {$set: {isOvertime}}
  619. );
  620. } else {
  621. return Cards.update(
  622. {_id: this._id},
  623. {$set: {isOvertime}}
  624. );
  625. }
  626. },
  627. getSpentTime() {
  628. if (this.isLinkedCard()) {
  629. const card = Cards.findOne({ _id: this.linkedId });
  630. return card.spentTime;
  631. } else if (this.isLinkedBoard()) {
  632. const board = Boards.findOne({ _id: this.linkedId});
  633. return board.spentTime;
  634. } else {
  635. return this.spentTime;
  636. }
  637. },
  638. setSpentTime(spentTime) {
  639. if (this.isLinkedCard()) {
  640. return Cards.update(
  641. { _id: this.linkedId },
  642. {$set: {spentTime}}
  643. );
  644. } else if (this.isLinkedBoard()) {
  645. return Boards.update(
  646. {_id: this.linkedId},
  647. {$set: {spentTime}}
  648. );
  649. } else {
  650. return Cards.update(
  651. {_id: this._id},
  652. {$set: {spentTime}}
  653. );
  654. }
  655. },
  656. getId() {
  657. if (this.isLinked()) {
  658. return this.linkedId;
  659. } else {
  660. return this._id;
  661. }
  662. },
  663. getTitle() {
  664. if (this.isLinkedCard()) {
  665. const card = Cards.findOne({ _id: this.linkedId });
  666. return card.title;
  667. } else if (this.isLinkedBoard()) {
  668. const board = Boards.findOne({ _id: this.linkedId});
  669. return board.title;
  670. } else {
  671. return this.title;
  672. }
  673. },
  674. getBoardTitle() {
  675. if (this.isLinkedCard()) {
  676. const card = Cards.findOne({ _id: this.linkedId });
  677. const board = Boards.findOne({ _id: card.boardId });
  678. return board.title;
  679. } else if (this.isLinkedBoard()) {
  680. const board = Boards.findOne({ _id: this.linkedId});
  681. return board.title;
  682. } else {
  683. const board = Boards.findOne({ _id: this.boardId });
  684. return board.title;
  685. }
  686. },
  687. setTitle(title) {
  688. if (this.isLinkedCard()) {
  689. return Cards.update(
  690. { _id: this.linkedId },
  691. {$set: {title}}
  692. );
  693. } else if (this.isLinkedBoard()) {
  694. return Boards.update(
  695. {_id: this.linkedId},
  696. {$set: {title}}
  697. );
  698. } else {
  699. return Cards.update(
  700. {_id: this._id},
  701. {$set: {title}}
  702. );
  703. }
  704. },
  705. getArchived() {
  706. if (this.isLinkedCard()) {
  707. const card = Cards.findOne({ _id: this.linkedId });
  708. return card.archived;
  709. } else if (this.isLinkedBoard()) {
  710. const board = Boards.findOne({ _id: this.linkedId});
  711. return board.archived;
  712. } else {
  713. return this.archived;
  714. }
  715. },
  716. setRequestedBy(requestedBy) {
  717. if (this.isLinkedCard()) {
  718. return Cards.update(
  719. { _id: this.linkedId },
  720. {$set: {requestedBy}}
  721. );
  722. } else {
  723. return Cards.update(
  724. {_id: this._id},
  725. {$set: {requestedBy}}
  726. );
  727. }
  728. },
  729. getRequestedBy() {
  730. if (this.isLinkedCard()) {
  731. const card = Cards.findOne({ _id: this.linkedId });
  732. return card.requestedBy;
  733. } else {
  734. return this.requestedBy;
  735. }
  736. },
  737. setAssignedBy(assignedBy) {
  738. if (this.isLinkedCard()) {
  739. return Cards.update(
  740. { _id: this.linkedId },
  741. {$set: {assignedBy}}
  742. );
  743. } else {
  744. return Cards.update(
  745. {_id: this._id},
  746. {$set: {assignedBy}}
  747. );
  748. }
  749. },
  750. getAssignedBy() {
  751. if (this.isLinkedCard()) {
  752. const card = Cards.findOne({ _id: this.linkedId });
  753. return card.assignedBy;
  754. } else {
  755. return this.assignedBy;
  756. }
  757. },
  758. });
  759. Cards.mutations({
  760. applyToChildren(funct) {
  761. Cards.find({ parentId: this._id }).forEach((card) => {
  762. funct(card);
  763. });
  764. },
  765. archive() {
  766. this.applyToChildren((card) => { return card.archive(); });
  767. return {$set: {archived: true}};
  768. },
  769. restore() {
  770. this.applyToChildren((card) => { return card.restore(); });
  771. return {$set: {archived: false}};
  772. },
  773. move(swimlaneId, listId, sortIndex) {
  774. const list = Lists.findOne(listId);
  775. const mutatedFields = {
  776. swimlaneId,
  777. listId,
  778. boardId: list.boardId,
  779. sort: sortIndex,
  780. };
  781. return {$set: mutatedFields};
  782. },
  783. addLabel(labelId) {
  784. return {$addToSet: {labelIds: labelId}};
  785. },
  786. removeLabel(labelId) {
  787. return {$pull: {labelIds: labelId}};
  788. },
  789. toggleLabel(labelId) {
  790. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  791. return this.removeLabel(labelId);
  792. } else {
  793. return this.addLabel(labelId);
  794. }
  795. },
  796. assignCustomField(customFieldId) {
  797. return {$addToSet: {customFields: {_id: customFieldId, value: null}}};
  798. },
  799. unassignCustomField(customFieldId) {
  800. return {$pull: {customFields: {_id: customFieldId}}};
  801. },
  802. toggleCustomField(customFieldId) {
  803. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  804. return this.unassignCustomField(customFieldId);
  805. } else {
  806. return this.assignCustomField(customFieldId);
  807. }
  808. },
  809. setCustomField(customFieldId, value) {
  810. // todo
  811. const index = this.customFieldIndex(customFieldId);
  812. if (index > -1) {
  813. const update = {$set: {}};
  814. update.$set[`customFields.${index}.value`] = value;
  815. return update;
  816. }
  817. // TODO
  818. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  819. return null;
  820. },
  821. setCover(coverId) {
  822. return {$set: {coverId}};
  823. },
  824. unsetCover() {
  825. return {$unset: {coverId: ''}};
  826. },
  827. setParentId(parentId) {
  828. return {$set: {parentId}};
  829. },
  830. });
  831. //FUNCTIONS FOR creation of Activities
  832. function cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId) {
  833. if ((_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  834. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)){
  835. Activities.insert({
  836. userId,
  837. oldListId,
  838. activityType: 'moveCard',
  839. listId: doc.listId,
  840. boardId: doc.boardId,
  841. cardId: doc._id,
  842. swimlaneId: doc.swimlaneId,
  843. oldSwimlaneId,
  844. });
  845. }
  846. }
  847. function cardState(userId, doc, fieldNames) {
  848. if (_.contains(fieldNames, 'archived')) {
  849. if (doc.archived) {
  850. Activities.insert({
  851. userId,
  852. activityType: 'archivedCard',
  853. boardId: doc.boardId,
  854. listId: doc.listId,
  855. cardId: doc._id,
  856. });
  857. } else {
  858. Activities.insert({
  859. userId,
  860. activityType: 'restoredCard',
  861. boardId: doc.boardId,
  862. listId: doc.listId,
  863. cardId: doc._id,
  864. });
  865. }
  866. }
  867. }
  868. function cardMembers(userId, doc, fieldNames, modifier) {
  869. if (!_.contains(fieldNames, 'members'))
  870. return;
  871. let memberId;
  872. // Say hello to the new member
  873. if (modifier.$addToSet && modifier.$addToSet.members) {
  874. memberId = modifier.$addToSet.members;
  875. if (!_.contains(doc.members, memberId)) {
  876. Activities.insert({
  877. userId,
  878. memberId,
  879. activityType: 'joinMember',
  880. boardId: doc.boardId,
  881. cardId: doc._id,
  882. });
  883. }
  884. }
  885. // Say goodbye to the former member
  886. if (modifier.$pull && modifier.$pull.members) {
  887. memberId = modifier.$pull.members;
  888. // Check that the former member is member of the card
  889. if (_.contains(doc.members, memberId)) {
  890. Activities.insert({
  891. userId,
  892. memberId,
  893. activityType: 'unjoinMember',
  894. boardId: doc.boardId,
  895. cardId: doc._id,
  896. });
  897. }
  898. }
  899. }
  900. function cardCreation(userId, doc) {
  901. Activities.insert({
  902. userId,
  903. activityType: 'createCard',
  904. boardId: doc.boardId,
  905. listId: doc.listId,
  906. cardId: doc._id,
  907. swimlaneId: doc.swimlaneId,
  908. });
  909. }
  910. function cardRemover(userId, doc) {
  911. Activities.remove({
  912. cardId: doc._id,
  913. });
  914. Checklists.remove({
  915. cardId: doc._id,
  916. });
  917. Subtasks.remove({
  918. cardId: doc._id,
  919. });
  920. CardComments.remove({
  921. cardId: doc._id,
  922. });
  923. Attachments.remove({
  924. cardId: doc._id,
  925. });
  926. }
  927. if (Meteor.isServer) {
  928. // Cards are often fetched within a board, so we create an index to make these
  929. // queries more efficient.
  930. Meteor.startup(() => {
  931. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  932. // https://github.com/wekan/wekan/issues/1863
  933. // Swimlane added a new field in the cards collection of mongodb named parentId.
  934. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  935. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  936. // To correct it, add Index to parentId:
  937. Cards._collection._ensureIndex({parentId: 1});
  938. });
  939. Cards.after.insert((userId, doc) => {
  940. cardCreation(userId, doc);
  941. });
  942. // New activity for card (un)archivage
  943. Cards.after.update((userId, doc, fieldNames) => {
  944. cardState(userId, doc, fieldNames);
  945. });
  946. //New activity for card moves
  947. Cards.after.update(function (userId, doc, fieldNames) {
  948. const oldListId = this.previous.listId;
  949. const oldSwimlaneId = this.previous.swimlaneId;
  950. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId);
  951. });
  952. // Add a new activity if we add or remove a member to the card
  953. Cards.before.update((userId, doc, fieldNames, modifier) => {
  954. cardMembers(userId, doc, fieldNames, modifier);
  955. });
  956. // Remove all activities associated with a card if we remove the card
  957. // Remove also card_comments / checklists / attachments
  958. Cards.after.remove((userId, doc) => {
  959. cardRemover(userId, doc);
  960. });
  961. }
  962. //LISTS REST API
  963. if (Meteor.isServer) {
  964. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  965. const paramBoardId = req.params.boardId;
  966. const paramListId = req.params.listId;
  967. Authentication.checkBoardAccess(req.userId, paramBoardId);
  968. JsonRoutes.sendResult(res, {
  969. code: 200,
  970. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  971. return {
  972. _id: doc._id,
  973. title: doc.title,
  974. description: doc.description,
  975. };
  976. }),
  977. });
  978. });
  979. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  980. const paramBoardId = req.params.boardId;
  981. const paramListId = req.params.listId;
  982. const paramCardId = req.params.cardId;
  983. Authentication.checkBoardAccess(req.userId, paramBoardId);
  984. JsonRoutes.sendResult(res, {
  985. code: 200,
  986. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  987. });
  988. });
  989. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  990. Authentication.checkUserId(req.userId);
  991. const paramBoardId = req.params.boardId;
  992. const paramListId = req.params.listId;
  993. const check = Users.findOne({_id: req.body.authorId});
  994. const members = req.body.members || [req.body.authorId];
  995. if (typeof check !== 'undefined') {
  996. const id = Cards.direct.insert({
  997. title: req.body.title,
  998. boardId: paramBoardId,
  999. listId: paramListId,
  1000. description: req.body.description,
  1001. userId: req.body.authorId,
  1002. swimlaneId: req.body.swimlaneId,
  1003. sort: 0,
  1004. members,
  1005. });
  1006. JsonRoutes.sendResult(res, {
  1007. code: 200,
  1008. data: {
  1009. _id: id,
  1010. },
  1011. });
  1012. const card = Cards.findOne({_id:id});
  1013. cardCreation(req.body.authorId, card);
  1014. } else {
  1015. JsonRoutes.sendResult(res, {
  1016. code: 401,
  1017. });
  1018. }
  1019. });
  1020. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  1021. Authentication.checkUserId(req.userId);
  1022. const paramBoardId = req.params.boardId;
  1023. const paramCardId = req.params.cardId;
  1024. const paramListId = req.params.listId;
  1025. if (req.body.hasOwnProperty('title')) {
  1026. const newTitle = req.body.title;
  1027. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1028. {$set: {title: newTitle}});
  1029. }
  1030. if (req.body.hasOwnProperty('listId')) {
  1031. const newParamListId = req.body.listId;
  1032. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1033. {$set: {listId: newParamListId}});
  1034. const card = Cards.findOne({_id: paramCardId} );
  1035. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  1036. }
  1037. if (req.body.hasOwnProperty('description')) {
  1038. const newDescription = req.body.description;
  1039. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1040. {$set: {description: newDescription}});
  1041. }
  1042. if (req.body.hasOwnProperty('labelIds')) {
  1043. const newlabelIds = req.body.labelIds;
  1044. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1045. {$set: {labelIds: newlabelIds}});
  1046. }
  1047. if (req.body.hasOwnProperty('requestedBy')) {
  1048. const newrequestedBy = req.body.requestedBy;
  1049. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1050. {$set: {requestedBy: newrequestedBy}});
  1051. }
  1052. if (req.body.hasOwnProperty('assignedBy')) {
  1053. const newassignedBy = req.body.assignedBy;
  1054. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1055. {$set: {assignedBy: newassignedBy}});
  1056. }
  1057. if (req.body.hasOwnProperty('receivedAt')) {
  1058. const newreceivedAt = req.body.receivedAt;
  1059. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1060. {$set: {receivedAt: newreceivedAt}});
  1061. }
  1062. if (req.body.hasOwnProperty('startAt')) {
  1063. const newstartAt = req.body.startAt;
  1064. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1065. {$set: {startAt: newstartAt}});
  1066. }
  1067. if (req.body.hasOwnProperty('dueAt')) {
  1068. const newdueAt = req.body.dueAt;
  1069. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1070. {$set: {dueAt: newdueAt}});
  1071. }
  1072. if (req.body.hasOwnProperty('endAt')) {
  1073. const newendAt = req.body.endAt;
  1074. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1075. {$set: {endAt: newendAt}});
  1076. }
  1077. if (req.body.hasOwnProperty('spentTime')) {
  1078. const newspentTime = req.body.spentTime;
  1079. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1080. {$set: {spentTime: newspentTime}});
  1081. }
  1082. if (req.body.hasOwnProperty('isOverTime')) {
  1083. const newisOverTime = req.body.isOverTime;
  1084. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1085. {$set: {isOverTime: newisOverTime}});
  1086. }
  1087. if (req.body.hasOwnProperty('customFields')) {
  1088. const newcustomFields = req.body.customFields;
  1089. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1090. {$set: {customFields: newcustomFields}});
  1091. }
  1092. JsonRoutes.sendResult(res, {
  1093. code: 200,
  1094. data: {
  1095. _id: paramCardId,
  1096. },
  1097. });
  1098. });
  1099. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  1100. Authentication.checkUserId(req.userId);
  1101. const paramBoardId = req.params.boardId;
  1102. const paramListId = req.params.listId;
  1103. const paramCardId = req.params.cardId;
  1104. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  1105. const card = Cards.find({_id: paramCardId} );
  1106. cardRemover(req.body.authorId, card);
  1107. JsonRoutes.sendResult(res, {
  1108. code: 200,
  1109. data: {
  1110. _id: paramCardId,
  1111. },
  1112. });
  1113. });
  1114. }