cards.js 48 KB

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