cards.js 26 KB

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