cards.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  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. move(boardId, swimlaneId, listId, sort) {
  954. // Copy Custom Fields
  955. if (this.boardId !== boardId) {
  956. CustomFields.find({
  957. _id: {$in: this.customFields.map((cf) => { return cf._id; })},
  958. }).forEach((cf) => {
  959. if (!_.contains(cf.boardIds, boardId))
  960. cf.addBoard(boardId);
  961. });
  962. }
  963. // Get label names
  964. const oldBoard = Boards.findOne(this.boardId);
  965. const oldBoardLabels = oldBoard.labels;
  966. const oldCardLabels = _.pluck(_.filter(oldBoardLabels, (label) => {
  967. return _.contains(this.labelIds, label._id);
  968. }), 'name');
  969. const newBoard = Boards.findOne(boardId);
  970. const newBoardLabels = newBoard.labels;
  971. const newCardLabelIds = _.pluck(_.filter(newBoardLabels, (label) => {
  972. return label.name && _.contains(oldCardLabels, label.name);
  973. }), '_id');
  974. const mutatedFields = {
  975. boardId,
  976. swimlaneId,
  977. listId,
  978. sort,
  979. labelIds: newCardLabelIds,
  980. };
  981. Cards.update(this._id, {
  982. $set: mutatedFields,
  983. });
  984. },
  985. addLabel(labelId) {
  986. return {
  987. $addToSet: {
  988. labelIds: labelId,
  989. },
  990. };
  991. },
  992. removeLabel(labelId) {
  993. return {
  994. $pull: {
  995. labelIds: labelId,
  996. },
  997. };
  998. },
  999. toggleLabel(labelId) {
  1000. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1001. return this.removeLabel(labelId);
  1002. } else {
  1003. return this.addLabel(labelId);
  1004. }
  1005. },
  1006. setColor(newColor) {
  1007. if (newColor === 'white') {
  1008. newColor = null;
  1009. }
  1010. return {
  1011. $set: {
  1012. color: newColor,
  1013. },
  1014. };
  1015. },
  1016. assignMember(memberId) {
  1017. return {
  1018. $addToSet: {
  1019. members: memberId,
  1020. },
  1021. };
  1022. },
  1023. unassignMember(memberId) {
  1024. return {
  1025. $pull: {
  1026. members: memberId,
  1027. },
  1028. };
  1029. },
  1030. toggleMember(memberId) {
  1031. if (this.members && this.members.indexOf(memberId) > -1) {
  1032. return this.unassignMember(memberId);
  1033. } else {
  1034. return this.assignMember(memberId);
  1035. }
  1036. },
  1037. assignCustomField(customFieldId) {
  1038. return {
  1039. $addToSet: {
  1040. customFields: {
  1041. _id: customFieldId,
  1042. value: null,
  1043. },
  1044. },
  1045. };
  1046. },
  1047. unassignCustomField(customFieldId) {
  1048. return {
  1049. $pull: {
  1050. customFields: {
  1051. _id: customFieldId,
  1052. },
  1053. },
  1054. };
  1055. },
  1056. toggleCustomField(customFieldId) {
  1057. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1058. return this.unassignCustomField(customFieldId);
  1059. } else {
  1060. return this.assignCustomField(customFieldId);
  1061. }
  1062. },
  1063. setCustomField(customFieldId, value) {
  1064. // todo
  1065. const index = this.customFieldIndex(customFieldId);
  1066. if (index > -1) {
  1067. const update = {
  1068. $set: {},
  1069. };
  1070. update.$set[`customFields.${index}.value`] = value;
  1071. return update;
  1072. }
  1073. // TODO
  1074. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1075. return null;
  1076. },
  1077. setCover(coverId) {
  1078. return {
  1079. $set: {
  1080. coverId,
  1081. },
  1082. };
  1083. },
  1084. unsetCover() {
  1085. return {
  1086. $unset: {
  1087. coverId: '',
  1088. },
  1089. };
  1090. },
  1091. setReceived(receivedAt) {
  1092. return {
  1093. $set: {
  1094. receivedAt,
  1095. },
  1096. };
  1097. },
  1098. unsetReceived() {
  1099. return {
  1100. $unset: {
  1101. receivedAt: '',
  1102. },
  1103. };
  1104. },
  1105. setStart(startAt) {
  1106. return {
  1107. $set: {
  1108. startAt,
  1109. },
  1110. };
  1111. },
  1112. unsetStart() {
  1113. return {
  1114. $unset: {
  1115. startAt: '',
  1116. },
  1117. };
  1118. },
  1119. setDue(dueAt) {
  1120. return {
  1121. $set: {
  1122. dueAt,
  1123. },
  1124. };
  1125. },
  1126. unsetDue() {
  1127. return {
  1128. $unset: {
  1129. dueAt: '',
  1130. },
  1131. };
  1132. },
  1133. setEnd(endAt) {
  1134. return {
  1135. $set: {
  1136. endAt,
  1137. },
  1138. };
  1139. },
  1140. unsetEnd() {
  1141. return {
  1142. $unset: {
  1143. endAt: '',
  1144. },
  1145. };
  1146. },
  1147. setOvertime(isOvertime) {
  1148. return {
  1149. $set: {
  1150. isOvertime,
  1151. },
  1152. };
  1153. },
  1154. setSpentTime(spentTime) {
  1155. return {
  1156. $set: {
  1157. spentTime,
  1158. },
  1159. };
  1160. },
  1161. unsetSpentTime() {
  1162. return {
  1163. $unset: {
  1164. spentTime: '',
  1165. isOvertime: false,
  1166. },
  1167. };
  1168. },
  1169. setParentId(parentId) {
  1170. return {
  1171. $set: {
  1172. parentId,
  1173. },
  1174. };
  1175. },
  1176. });
  1177. //FUNCTIONS FOR creation of Activities
  1178. function updateActivities(doc, fieldNames, modifier) {
  1179. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  1180. Activities.find({
  1181. activityType: 'addedLabel',
  1182. cardId: doc._id,
  1183. }).forEach((a) => {
  1184. const lidx = doc.labelIds.indexOf(a.labelId);
  1185. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  1186. Activities.update(a._id, {
  1187. $set: {
  1188. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  1189. boardId: modifier.$set.boardId,
  1190. },
  1191. });
  1192. } else {
  1193. Activities.remove(a._id);
  1194. }
  1195. });
  1196. } else if (_.contains(fieldNames, 'boardId')) {
  1197. Activities.remove({
  1198. activityType: 'addedLabel',
  1199. cardId: doc._id,
  1200. });
  1201. }
  1202. }
  1203. function cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId) {
  1204. if (_.contains(fieldNames, 'boardId') && (doc.boardId !== oldBoardId)) {
  1205. Activities.insert({
  1206. userId,
  1207. activityType: 'moveCardBoard',
  1208. boardName: Boards.findOne(doc.boardId).title,
  1209. boardId: doc.boardId,
  1210. oldBoardId,
  1211. oldBoardName: Boards.findOne(oldBoardId).title,
  1212. cardId: doc._id,
  1213. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1214. swimlaneId: doc.swimlaneId,
  1215. oldSwimlaneId,
  1216. });
  1217. } else if ((_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1218. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)){
  1219. Activities.insert({
  1220. userId,
  1221. oldListId,
  1222. activityType: 'moveCard',
  1223. listName: Lists.findOne(doc.listId).title,
  1224. listId: doc.listId,
  1225. boardId: doc.boardId,
  1226. cardId: doc._id,
  1227. cardTitle:doc.title,
  1228. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1229. swimlaneId: doc.swimlaneId,
  1230. oldSwimlaneId,
  1231. });
  1232. }
  1233. }
  1234. function cardState(userId, doc, fieldNames) {
  1235. if (_.contains(fieldNames, 'archived')) {
  1236. if (doc.archived) {
  1237. Activities.insert({
  1238. userId,
  1239. activityType: 'archivedCard',
  1240. listName: Lists.findOne(doc.listId).title,
  1241. boardId: doc.boardId,
  1242. listId: doc.listId,
  1243. cardId: doc._id,
  1244. });
  1245. } else {
  1246. Activities.insert({
  1247. userId,
  1248. activityType: 'restoredCard',
  1249. boardId: doc.boardId,
  1250. listName: Lists.findOne(doc.listId).title,
  1251. listId: doc.listId,
  1252. cardId: doc._id,
  1253. });
  1254. }
  1255. }
  1256. }
  1257. function cardMembers(userId, doc, fieldNames, modifier) {
  1258. if (!_.contains(fieldNames, 'members'))
  1259. return;
  1260. let memberId;
  1261. // Say hello to the new member
  1262. if (modifier.$addToSet && modifier.$addToSet.members) {
  1263. memberId = modifier.$addToSet.members;
  1264. const username = Users.findOne(memberId).username;
  1265. if (!_.contains(doc.members, memberId)) {
  1266. Activities.insert({
  1267. userId,
  1268. username,
  1269. activityType: 'joinMember',
  1270. boardId: doc.boardId,
  1271. cardId: doc._id,
  1272. memberId,
  1273. listId: doc.listId,
  1274. swimlaneId: doc.swimlaneId,
  1275. });
  1276. }
  1277. }
  1278. // Say goodbye to the former member
  1279. if (modifier.$pull && modifier.$pull.members) {
  1280. memberId = modifier.$pull.members;
  1281. const username = Users.findOne(memberId).username;
  1282. // Check that the former member is member of the card
  1283. if (_.contains(doc.members, memberId)) {
  1284. Activities.insert({
  1285. userId,
  1286. username,
  1287. activityType: 'unjoinMember',
  1288. boardId: doc.boardId,
  1289. cardId: doc._id,
  1290. memberId,
  1291. listId: doc.listId,
  1292. swimlaneId: doc.swimlaneId,
  1293. });
  1294. }
  1295. }
  1296. }
  1297. function cardLabels(userId, doc, fieldNames, modifier) {
  1298. if (!_.contains(fieldNames, 'labelIds'))
  1299. return;
  1300. let labelId;
  1301. // Say hello to the new label
  1302. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1303. labelId = modifier.$addToSet.labelIds;
  1304. if (!_.contains(doc.labelIds, labelId)) {
  1305. const act = {
  1306. userId,
  1307. labelId,
  1308. activityType: 'addedLabel',
  1309. boardId: doc.boardId,
  1310. cardId: doc._id,
  1311. };
  1312. Activities.insert(act);
  1313. }
  1314. }
  1315. // Say goodbye to the label
  1316. if (modifier.$pull && modifier.$pull.labelIds) {
  1317. labelId = modifier.$pull.labelIds;
  1318. // Check that the former member is member of the card
  1319. if (_.contains(doc.labelIds, labelId)) {
  1320. Activities.insert({
  1321. userId,
  1322. labelId,
  1323. activityType: 'removedLabel',
  1324. boardId: doc.boardId,
  1325. cardId: doc._id,
  1326. });
  1327. }
  1328. }
  1329. }
  1330. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1331. if (!_.contains(fieldNames, 'customFields'))
  1332. return;
  1333. // Say hello to the new customField value
  1334. if (modifier.$set) {
  1335. _.each(modifier.$set, (value, key) => {
  1336. if (key.startsWith('customFields')) {
  1337. const dotNotation = key.split('.');
  1338. // only individual changes are registered
  1339. if (dotNotation.length > 1) {
  1340. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1341. const act = {
  1342. userId,
  1343. customFieldId,
  1344. value,
  1345. activityType: 'setCustomField',
  1346. boardId: doc.boardId,
  1347. cardId: doc._id,
  1348. };
  1349. Activities.insert(act);
  1350. }
  1351. }
  1352. });
  1353. }
  1354. // Say goodbye to the former customField value
  1355. if (modifier.$unset) {
  1356. _.each(modifier.$unset, (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. activityType: 'unsetCustomField',
  1366. boardId: doc.boardId,
  1367. cardId: doc._id,
  1368. };
  1369. Activities.insert(act);
  1370. }
  1371. }
  1372. });
  1373. }
  1374. }
  1375. function cardCreation(userId, doc) {
  1376. Activities.insert({
  1377. userId,
  1378. activityType: 'createCard',
  1379. boardId: doc.boardId,
  1380. listName: Lists.findOne(doc.listId).title,
  1381. listId: doc.listId,
  1382. cardId: doc._id,
  1383. cardTitle:doc.title,
  1384. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1385. swimlaneId: doc.swimlaneId,
  1386. });
  1387. }
  1388. function cardRemover(userId, doc) {
  1389. Activities.remove({
  1390. cardId: doc._id,
  1391. });
  1392. Checklists.remove({
  1393. cardId: doc._id,
  1394. });
  1395. Cards.remove({
  1396. parentId: doc._id,
  1397. });
  1398. CardComments.remove({
  1399. cardId: doc._id,
  1400. });
  1401. Attachments.remove({
  1402. cardId: doc._id,
  1403. });
  1404. }
  1405. if (Meteor.isServer) {
  1406. // Cards are often fetched within a board, so we create an index to make these
  1407. // queries more efficient.
  1408. Meteor.startup(() => {
  1409. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  1410. // https://github.com/wekan/wekan/issues/1863
  1411. // Swimlane added a new field in the cards collection of mongodb named parentId.
  1412. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  1413. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  1414. // To correct it, add Index to parentId:
  1415. Cards._collection._ensureIndex({parentId: 1});
  1416. });
  1417. Cards.after.insert((userId, doc) => {
  1418. cardCreation(userId, doc);
  1419. });
  1420. // New activity for card (un)archivage
  1421. Cards.after.update((userId, doc, fieldNames) => {
  1422. cardState(userId, doc, fieldNames);
  1423. });
  1424. //New activity for card moves
  1425. Cards.after.update(function(userId, doc, fieldNames) {
  1426. const oldListId = this.previous.listId;
  1427. const oldSwimlaneId = this.previous.swimlaneId;
  1428. const oldBoardId = this.previous.boardId;
  1429. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  1430. });
  1431. // Add a new activity if we add or remove a member to the card
  1432. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1433. cardMembers(userId, doc, fieldNames, modifier);
  1434. updateActivities(doc, fieldNames, modifier);
  1435. });
  1436. // Add a new activity if we add or remove a label to the card
  1437. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1438. cardLabels(userId, doc, fieldNames, modifier);
  1439. });
  1440. // Add a new activity if we edit a custom field
  1441. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1442. cardCustomFields(userId, doc, fieldNames, modifier);
  1443. });
  1444. // Remove all activities associated with a card if we remove the card
  1445. // Remove also card_comments / checklists / attachments
  1446. Cards.after.remove((userId, doc) => {
  1447. cardRemover(userId, doc);
  1448. });
  1449. }
  1450. //SWIMLANES REST API
  1451. if (Meteor.isServer) {
  1452. /**
  1453. * @operation get_swimlane_cards
  1454. * @summary get all cards attached to a swimlane
  1455. *
  1456. * @param {string} boardId the board ID
  1457. * @param {string} swimlaneId the swimlane ID
  1458. * @return_type [{_id: string,
  1459. * title: string,
  1460. * description: string,
  1461. * listId: string}]
  1462. */
  1463. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId/cards', function(req, res) {
  1464. const paramBoardId = req.params.boardId;
  1465. const paramSwimlaneId = req.params.swimlaneId;
  1466. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1467. JsonRoutes.sendResult(res, {
  1468. code: 200,
  1469. data: Cards.find({
  1470. boardId: paramBoardId,
  1471. swimlaneId: paramSwimlaneId,
  1472. archived: false,
  1473. }).map(function(doc) {
  1474. return {
  1475. _id: doc._id,
  1476. title: doc.title,
  1477. description: doc.description,
  1478. listId: doc.listId,
  1479. };
  1480. }),
  1481. });
  1482. });
  1483. }
  1484. //LISTS REST API
  1485. if (Meteor.isServer) {
  1486. /**
  1487. * @operation get_all_cards
  1488. * @summary Get all Cards attached to a List
  1489. *
  1490. * @param {string} boardId the board ID
  1491. * @param {string} listId the list ID
  1492. * @return_type [{_id: string,
  1493. * title: string,
  1494. * description: string}]
  1495. */
  1496. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1497. const paramBoardId = req.params.boardId;
  1498. const paramListId = req.params.listId;
  1499. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1500. JsonRoutes.sendResult(res, {
  1501. code: 200,
  1502. data: Cards.find({
  1503. boardId: paramBoardId,
  1504. listId: paramListId,
  1505. archived: false,
  1506. }).map(function(doc) {
  1507. return {
  1508. _id: doc._id,
  1509. title: doc.title,
  1510. description: doc.description,
  1511. };
  1512. }),
  1513. });
  1514. });
  1515. /**
  1516. * @operation get_card
  1517. * @summary Get a Card
  1518. *
  1519. * @param {string} boardId the board ID
  1520. * @param {string} listId the list ID of the card
  1521. * @param {string} cardId the card ID
  1522. * @return_type Cards
  1523. */
  1524. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1525. const paramBoardId = req.params.boardId;
  1526. const paramListId = req.params.listId;
  1527. const paramCardId = req.params.cardId;
  1528. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1529. JsonRoutes.sendResult(res, {
  1530. code: 200,
  1531. data: Cards.findOne({
  1532. _id: paramCardId,
  1533. listId: paramListId,
  1534. boardId: paramBoardId,
  1535. archived: false,
  1536. }),
  1537. });
  1538. });
  1539. /**
  1540. * @operation new_card
  1541. * @summary Create a new Card
  1542. *
  1543. * @param {string} boardId the board ID of the new card
  1544. * @param {string} listId the list ID of the new card
  1545. * @param {string} authorID the user ID of the person owning the card
  1546. * @param {string} title the title of the new card
  1547. * @param {string} description the description of the new card
  1548. * @param {string} swimlaneId the swimlane ID of the new card
  1549. * @param {string} [members] the member IDs list of the new card
  1550. * @return_type {_id: string}
  1551. */
  1552. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1553. Authentication.checkUserId(req.userId);
  1554. const paramBoardId = req.params.boardId;
  1555. const paramListId = req.params.listId;
  1556. const currentCards = Cards.find({
  1557. listId: paramListId,
  1558. archived: false,
  1559. }, { sort: ['sort'] });
  1560. const check = Users.findOne({
  1561. _id: req.body.authorId,
  1562. });
  1563. const members = req.body.members || [req.body.authorId];
  1564. if (typeof check !== 'undefined') {
  1565. const id = Cards.direct.insert({
  1566. title: req.body.title,
  1567. boardId: paramBoardId,
  1568. listId: paramListId,
  1569. description: req.body.description,
  1570. userId: req.body.authorId,
  1571. swimlaneId: req.body.swimlaneId,
  1572. sort: currentCards.count(),
  1573. members,
  1574. });
  1575. JsonRoutes.sendResult(res, {
  1576. code: 200,
  1577. data: {
  1578. _id: id,
  1579. },
  1580. });
  1581. const card = Cards.findOne({
  1582. _id: id,
  1583. });
  1584. cardCreation(req.body.authorId, card);
  1585. } else {
  1586. JsonRoutes.sendResult(res, {
  1587. code: 401,
  1588. });
  1589. }
  1590. });
  1591. /*
  1592. * Note for the JSDoc:
  1593. * 'list' will be interpreted as the path parameter
  1594. * 'listID' will be interpreted as the body parameter
  1595. */
  1596. /**
  1597. * @operation edit_card
  1598. * @summary Edit Fields in a Card
  1599. *
  1600. * @description Edit a card
  1601. *
  1602. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  1603. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  1604. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  1605. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  1606. * `indigo`:
  1607. *
  1608. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  1609. *
  1610. * Note: setting the color to white has the same effect than removing it.
  1611. *
  1612. * @param {string} boardId the board ID of the card
  1613. * @param {string} list the list ID of the card
  1614. * @param {string} cardId the ID of the card
  1615. * @param {string} [title] the new title of the card
  1616. * @param {string} [listId] the new list ID of the card (move operation)
  1617. * @param {string} [description] the new description of the card
  1618. * @param {string} [authorId] change the owner of the card
  1619. * @param {string} [labelIds] the new list of label IDs attached to the card
  1620. * @param {string} [swimlaneId] the new swimlane ID of the card
  1621. * @param {string} [members] the new list of member IDs attached to the card
  1622. * @param {string} [requestedBy] the new requestedBy field of the card
  1623. * @param {string} [assignedBy] the new assignedBy field of the card
  1624. * @param {string} [receivedAt] the new receivedAt field of the card
  1625. * @param {string} [assignBy] the new assignBy field of the card
  1626. * @param {string} [startAt] the new startAt field of the card
  1627. * @param {string} [dueAt] the new dueAt field of the card
  1628. * @param {string} [endAt] the new endAt field of the card
  1629. * @param {string} [spentTime] the new spentTime field of the card
  1630. * @param {boolean} [isOverTime] the new isOverTime field of the card
  1631. * @param {string} [customFields] the new customFields value of the card
  1632. * @param {string} [color] the new color of the card
  1633. * @return_type {_id: string}
  1634. */
  1635. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1636. Authentication.checkUserId(req.userId);
  1637. const paramBoardId = req.params.boardId;
  1638. const paramCardId = req.params.cardId;
  1639. const paramListId = req.params.listId;
  1640. if (req.body.hasOwnProperty('title')) {
  1641. const newTitle = req.body.title;
  1642. Cards.direct.update({
  1643. _id: paramCardId,
  1644. listId: paramListId,
  1645. boardId: paramBoardId,
  1646. archived: false,
  1647. }, {
  1648. $set: {
  1649. title: newTitle,
  1650. },
  1651. });
  1652. }
  1653. if (req.body.hasOwnProperty('listId')) {
  1654. const newParamListId = req.body.listId;
  1655. Cards.direct.update({
  1656. _id: paramCardId,
  1657. listId: paramListId,
  1658. boardId: paramBoardId,
  1659. archived: false,
  1660. }, {
  1661. $set: {
  1662. listId: newParamListId,
  1663. },
  1664. });
  1665. const card = Cards.findOne({
  1666. _id: paramCardId,
  1667. });
  1668. cardMove(req.body.authorId, card, {
  1669. fieldName: 'listId',
  1670. }, paramListId);
  1671. }
  1672. if (req.body.hasOwnProperty('description')) {
  1673. const newDescription = req.body.description;
  1674. Cards.direct.update({
  1675. _id: paramCardId,
  1676. listId: paramListId,
  1677. boardId: paramBoardId,
  1678. archived: false,
  1679. }, {
  1680. $set: {
  1681. description: newDescription,
  1682. },
  1683. });
  1684. }
  1685. if (req.body.hasOwnProperty('color')) {
  1686. const newColor = req.body.color;
  1687. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1688. {$set: {color: newColor}});
  1689. }
  1690. if (req.body.hasOwnProperty('labelIds')) {
  1691. let newlabelIds = req.body.labelIds;
  1692. if (_.isString(newlabelIds)) {
  1693. if (newlabelIds === '') {
  1694. newlabelIds = null;
  1695. }
  1696. else {
  1697. newlabelIds = [newlabelIds];
  1698. }
  1699. }
  1700. Cards.direct.update({
  1701. _id: paramCardId,
  1702. listId: paramListId,
  1703. boardId: paramBoardId,
  1704. archived: false,
  1705. }, {
  1706. $set: {
  1707. labelIds: newlabelIds,
  1708. },
  1709. });
  1710. }
  1711. if (req.body.hasOwnProperty('requestedBy')) {
  1712. const newrequestedBy = req.body.requestedBy;
  1713. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1714. {$set: {requestedBy: newrequestedBy}});
  1715. }
  1716. if (req.body.hasOwnProperty('assignedBy')) {
  1717. const newassignedBy = req.body.assignedBy;
  1718. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1719. {$set: {assignedBy: newassignedBy}});
  1720. }
  1721. if (req.body.hasOwnProperty('receivedAt')) {
  1722. const newreceivedAt = req.body.receivedAt;
  1723. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1724. {$set: {receivedAt: newreceivedAt}});
  1725. }
  1726. if (req.body.hasOwnProperty('startAt')) {
  1727. const newstartAt = req.body.startAt;
  1728. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1729. {$set: {startAt: newstartAt}});
  1730. }
  1731. if (req.body.hasOwnProperty('dueAt')) {
  1732. const newdueAt = req.body.dueAt;
  1733. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1734. {$set: {dueAt: newdueAt}});
  1735. }
  1736. if (req.body.hasOwnProperty('endAt')) {
  1737. const newendAt = req.body.endAt;
  1738. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1739. {$set: {endAt: newendAt}});
  1740. }
  1741. if (req.body.hasOwnProperty('spentTime')) {
  1742. const newspentTime = req.body.spentTime;
  1743. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1744. {$set: {spentTime: newspentTime}});
  1745. }
  1746. if (req.body.hasOwnProperty('isOverTime')) {
  1747. const newisOverTime = req.body.isOverTime;
  1748. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1749. {$set: {isOverTime: newisOverTime}});
  1750. }
  1751. if (req.body.hasOwnProperty('customFields')) {
  1752. const newcustomFields = req.body.customFields;
  1753. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1754. {$set: {customFields: newcustomFields}});
  1755. }
  1756. if (req.body.hasOwnProperty('members')) {
  1757. let newmembers = req.body.members;
  1758. if (_.isString(newmembers)) {
  1759. if (newmembers === '') {
  1760. newmembers = null;
  1761. }
  1762. else {
  1763. newmembers = [newmembers];
  1764. }
  1765. }
  1766. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1767. {$set: {members: newmembers}});
  1768. }
  1769. if (req.body.hasOwnProperty('swimlaneId')) {
  1770. const newParamSwimlaneId = req.body.swimlaneId;
  1771. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1772. {$set: {swimlaneId: newParamSwimlaneId}});
  1773. }
  1774. JsonRoutes.sendResult(res, {
  1775. code: 200,
  1776. data: {
  1777. _id: paramCardId,
  1778. },
  1779. });
  1780. });
  1781. /**
  1782. * @operation delete_card
  1783. * @summary Delete a card from a board
  1784. *
  1785. * @description This operation **deletes** a card, and therefore the card
  1786. * is not put in the recycle bin.
  1787. *
  1788. * @param {string} boardId the board ID of the card
  1789. * @param {string} list the list ID of the card
  1790. * @param {string} cardId the ID of the card
  1791. * @return_type {_id: string}
  1792. */
  1793. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1794. Authentication.checkUserId(req.userId);
  1795. const paramBoardId = req.params.boardId;
  1796. const paramListId = req.params.listId;
  1797. const paramCardId = req.params.cardId;
  1798. Cards.direct.remove({
  1799. _id: paramCardId,
  1800. listId: paramListId,
  1801. boardId: paramBoardId,
  1802. });
  1803. const card = Cards.find({
  1804. _id: paramCardId,
  1805. });
  1806. cardRemover(req.body.authorId, card);
  1807. JsonRoutes.sendResult(res, {
  1808. code: 200,
  1809. data: {
  1810. _id: paramCardId,
  1811. },
  1812. });
  1813. });
  1814. }