cards.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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) {
  833. if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
  834. Activities.insert({
  835. userId,
  836. oldListId,
  837. activityType: 'moveCard',
  838. listId: doc.listId,
  839. boardId: doc.boardId,
  840. cardId: doc._id,
  841. });
  842. }
  843. }
  844. function cardState(userId, doc, fieldNames) {
  845. if (_.contains(fieldNames, 'archived')) {
  846. if (doc.archived) {
  847. Activities.insert({
  848. userId,
  849. activityType: 'archivedCard',
  850. boardId: doc.boardId,
  851. listId: doc.listId,
  852. cardId: doc._id,
  853. });
  854. } else {
  855. Activities.insert({
  856. userId,
  857. activityType: 'restoredCard',
  858. boardId: doc.boardId,
  859. listId: doc.listId,
  860. cardId: doc._id,
  861. });
  862. }
  863. }
  864. }
  865. function cardMembers(userId, doc, fieldNames, modifier) {
  866. if (!_.contains(fieldNames, 'members'))
  867. return;
  868. let memberId;
  869. // Say hello to the new member
  870. if (modifier.$addToSet && modifier.$addToSet.members) {
  871. memberId = modifier.$addToSet.members;
  872. if (!_.contains(doc.members, memberId)) {
  873. Activities.insert({
  874. userId,
  875. memberId,
  876. activityType: 'joinMember',
  877. boardId: doc.boardId,
  878. cardId: doc._id,
  879. });
  880. }
  881. }
  882. // Say goodbye to the former member
  883. if (modifier.$pull && modifier.$pull.members) {
  884. memberId = modifier.$pull.members;
  885. // Check that the former member is member of the card
  886. if (_.contains(doc.members, memberId)) {
  887. Activities.insert({
  888. userId,
  889. memberId,
  890. activityType: 'unjoinMember',
  891. boardId: doc.boardId,
  892. cardId: doc._id,
  893. });
  894. }
  895. }
  896. }
  897. function cardCreation(userId, doc) {
  898. Activities.insert({
  899. userId,
  900. activityType: 'createCard',
  901. boardId: doc.boardId,
  902. listId: doc.listId,
  903. cardId: doc._id,
  904. });
  905. }
  906. function cardRemover(userId, doc) {
  907. Activities.remove({
  908. cardId: doc._id,
  909. });
  910. Checklists.remove({
  911. cardId: doc._id,
  912. });
  913. Subtasks.remove({
  914. cardId: doc._id,
  915. });
  916. CardComments.remove({
  917. cardId: doc._id,
  918. });
  919. Attachments.remove({
  920. cardId: doc._id,
  921. });
  922. }
  923. if (Meteor.isServer) {
  924. // Cards are often fetched within a board, so we create an index to make these
  925. // queries more efficient.
  926. Meteor.startup(() => {
  927. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  928. // https://github.com/wekan/wekan/issues/1863
  929. // Swimlane added a new field in the cards collection of mongodb named parentId.
  930. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  931. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  932. // To correct it, add Index to parentId:
  933. Cards._collection._ensureIndex({parentId: 1});
  934. });
  935. Cards.after.insert((userId, doc) => {
  936. cardCreation(userId, doc);
  937. });
  938. // New activity for card (un)archivage
  939. Cards.after.update((userId, doc, fieldNames) => {
  940. cardState(userId, doc, fieldNames);
  941. });
  942. //New activity for card moves
  943. Cards.after.update(function (userId, doc, fieldNames) {
  944. const oldListId = this.previous.listId;
  945. cardMove(userId, doc, fieldNames, oldListId);
  946. });
  947. // Add a new activity if we add or remove a member to the card
  948. Cards.before.update((userId, doc, fieldNames, modifier) => {
  949. cardMembers(userId, doc, fieldNames, modifier);
  950. });
  951. // Remove all activities associated with a card if we remove the card
  952. // Remove also card_comments / checklists / attachments
  953. Cards.after.remove((userId, doc) => {
  954. cardRemover(userId, doc);
  955. });
  956. }
  957. //LISTS REST API
  958. if (Meteor.isServer) {
  959. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  960. const paramBoardId = req.params.boardId;
  961. const paramListId = req.params.listId;
  962. Authentication.checkBoardAccess(req.userId, paramBoardId);
  963. JsonRoutes.sendResult(res, {
  964. code: 200,
  965. data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
  966. return {
  967. _id: doc._id,
  968. title: doc.title,
  969. description: doc.description,
  970. };
  971. }),
  972. });
  973. });
  974. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  975. const paramBoardId = req.params.boardId;
  976. const paramListId = req.params.listId;
  977. const paramCardId = req.params.cardId;
  978. Authentication.checkBoardAccess(req.userId, paramBoardId);
  979. JsonRoutes.sendResult(res, {
  980. code: 200,
  981. data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
  982. });
  983. });
  984. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
  985. Authentication.checkUserId(req.userId);
  986. const paramBoardId = req.params.boardId;
  987. const paramListId = req.params.listId;
  988. const check = Users.findOne({_id: req.body.authorId});
  989. const members = req.body.members || [req.body.authorId];
  990. if (typeof check !== 'undefined') {
  991. const id = Cards.direct.insert({
  992. title: req.body.title,
  993. boardId: paramBoardId,
  994. listId: paramListId,
  995. description: req.body.description,
  996. userId: req.body.authorId,
  997. swimlaneId: req.body.swimlaneId,
  998. sort: 0,
  999. members,
  1000. });
  1001. JsonRoutes.sendResult(res, {
  1002. code: 200,
  1003. data: {
  1004. _id: id,
  1005. },
  1006. });
  1007. const card = Cards.findOne({_id:id});
  1008. cardCreation(req.body.authorId, card);
  1009. } else {
  1010. JsonRoutes.sendResult(res, {
  1011. code: 401,
  1012. });
  1013. }
  1014. });
  1015. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  1016. Authentication.checkUserId(req.userId);
  1017. const paramBoardId = req.params.boardId;
  1018. const paramCardId = req.params.cardId;
  1019. const paramListId = req.params.listId;
  1020. if (req.body.hasOwnProperty('title')) {
  1021. const newTitle = req.body.title;
  1022. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1023. {$set: {title: newTitle}});
  1024. }
  1025. if (req.body.hasOwnProperty('listId')) {
  1026. const newParamListId = req.body.listId;
  1027. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1028. {$set: {listId: newParamListId}});
  1029. const card = Cards.findOne({_id: paramCardId} );
  1030. cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
  1031. }
  1032. if (req.body.hasOwnProperty('description')) {
  1033. const newDescription = req.body.description;
  1034. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1035. {$set: {description: newDescription}});
  1036. }
  1037. if (req.body.hasOwnProperty('labelIds')) {
  1038. const newlabelIds = req.body.labelIds;
  1039. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1040. {$set: {labelIds: newlabelIds}});
  1041. }
  1042. JsonRoutes.sendResult(res, {
  1043. code: 200,
  1044. data: {
  1045. _id: paramCardId,
  1046. },
  1047. });
  1048. });
  1049. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
  1050. Authentication.checkUserId(req.userId);
  1051. const paramBoardId = req.params.boardId;
  1052. const paramListId = req.params.listId;
  1053. const paramCardId = req.params.cardId;
  1054. Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
  1055. const card = Cards.find({_id: paramCardId} );
  1056. cardRemover(req.body.authorId, card);
  1057. JsonRoutes.sendResult(res, {
  1058. code: 200,
  1059. data: {
  1060. _id: paramCardId,
  1061. },
  1062. });
  1063. });
  1064. }