cards.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  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. swimlaneId: doc.swimlaneId,
  1245. });
  1246. } else {
  1247. Activities.insert({
  1248. userId,
  1249. activityType: 'restoredCard',
  1250. boardId: doc.boardId,
  1251. listName: Lists.findOne(doc.listId).title,
  1252. listId: doc.listId,
  1253. cardId: doc._id,
  1254. swimlaneId: doc.swimlaneId,
  1255. });
  1256. }
  1257. }
  1258. }
  1259. function cardMembers(userId, doc, fieldNames, modifier) {
  1260. if (!_.contains(fieldNames, 'members'))
  1261. return;
  1262. let memberId;
  1263. // Say hello to the new member
  1264. if (modifier.$addToSet && modifier.$addToSet.members) {
  1265. memberId = modifier.$addToSet.members;
  1266. const username = Users.findOne(memberId).username;
  1267. if (!_.contains(doc.members, memberId)) {
  1268. Activities.insert({
  1269. userId,
  1270. username,
  1271. activityType: 'joinMember',
  1272. boardId: doc.boardId,
  1273. cardId: doc._id,
  1274. memberId,
  1275. listId: doc.listId,
  1276. swimlaneId: doc.swimlaneId,
  1277. });
  1278. }
  1279. }
  1280. // Say goodbye to the former member
  1281. if (modifier.$pull && modifier.$pull.members) {
  1282. memberId = modifier.$pull.members;
  1283. const username = Users.findOne(memberId).username;
  1284. // Check that the former member is member of the card
  1285. if (_.contains(doc.members, memberId)) {
  1286. Activities.insert({
  1287. userId,
  1288. username,
  1289. activityType: 'unjoinMember',
  1290. boardId: doc.boardId,
  1291. cardId: doc._id,
  1292. memberId,
  1293. listId: doc.listId,
  1294. swimlaneId: doc.swimlaneId,
  1295. });
  1296. }
  1297. }
  1298. }
  1299. function cardLabels(userId, doc, fieldNames, modifier) {
  1300. if (!_.contains(fieldNames, 'labelIds'))
  1301. return;
  1302. let labelId;
  1303. // Say hello to the new label
  1304. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1305. labelId = modifier.$addToSet.labelIds;
  1306. if (!_.contains(doc.labelIds, labelId)) {
  1307. const act = {
  1308. userId,
  1309. labelId,
  1310. activityType: 'addedLabel',
  1311. boardId: doc.boardId,
  1312. cardId: doc._id,
  1313. listId: doc.listId,
  1314. swimlaneId: doc.swimlaneId,
  1315. };
  1316. Activities.insert(act);
  1317. }
  1318. }
  1319. // Say goodbye to the label
  1320. if (modifier.$pull && modifier.$pull.labelIds) {
  1321. labelId = modifier.$pull.labelIds;
  1322. // Check that the former member is member of the card
  1323. if (_.contains(doc.labelIds, labelId)) {
  1324. Activities.insert({
  1325. userId,
  1326. labelId,
  1327. activityType: 'removedLabel',
  1328. boardId: doc.boardId,
  1329. cardId: doc._id,
  1330. listId: doc.listId,
  1331. swimlaneId: doc.swimlaneId,
  1332. });
  1333. }
  1334. }
  1335. }
  1336. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1337. if (!_.contains(fieldNames, 'customFields'))
  1338. return;
  1339. // Say hello to the new customField value
  1340. if (modifier.$set) {
  1341. _.each(modifier.$set, (value, key) => {
  1342. if (key.startsWith('customFields')) {
  1343. const dotNotation = key.split('.');
  1344. // only individual changes are registered
  1345. if (dotNotation.length > 1) {
  1346. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1347. const act = {
  1348. userId,
  1349. customFieldId,
  1350. value,
  1351. activityType: 'setCustomField',
  1352. boardId: doc.boardId,
  1353. cardId: doc._id,
  1354. };
  1355. Activities.insert(act);
  1356. }
  1357. }
  1358. });
  1359. }
  1360. // Say goodbye to the former customField value
  1361. if (modifier.$unset) {
  1362. _.each(modifier.$unset, (value, key) => {
  1363. if (key.startsWith('customFields')) {
  1364. const dotNotation = key.split('.');
  1365. // only individual changes are registered
  1366. if (dotNotation.length > 1) {
  1367. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1368. const act = {
  1369. userId,
  1370. customFieldId,
  1371. activityType: 'unsetCustomField',
  1372. boardId: doc.boardId,
  1373. cardId: doc._id,
  1374. };
  1375. Activities.insert(act);
  1376. }
  1377. }
  1378. });
  1379. }
  1380. }
  1381. function cardCreation(userId, doc) {
  1382. Activities.insert({
  1383. userId,
  1384. activityType: 'createCard',
  1385. boardId: doc.boardId,
  1386. listName: Lists.findOne(doc.listId).title,
  1387. listId: doc.listId,
  1388. cardId: doc._id,
  1389. cardTitle:doc.title,
  1390. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1391. swimlaneId: doc.swimlaneId,
  1392. });
  1393. }
  1394. function cardRemover(userId, doc) {
  1395. Activities.remove({
  1396. cardId: doc._id,
  1397. });
  1398. Checklists.remove({
  1399. cardId: doc._id,
  1400. });
  1401. Cards.remove({
  1402. parentId: doc._id,
  1403. });
  1404. CardComments.remove({
  1405. cardId: doc._id,
  1406. });
  1407. Attachments.remove({
  1408. cardId: doc._id,
  1409. });
  1410. }
  1411. if (Meteor.isServer) {
  1412. // Cards are often fetched within a board, so we create an index to make these
  1413. // queries more efficient.
  1414. Meteor.startup(() => {
  1415. Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
  1416. // https://github.com/wekan/wekan/issues/1863
  1417. // Swimlane added a new field in the cards collection of mongodb named parentId.
  1418. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  1419. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  1420. // To correct it, add Index to parentId:
  1421. Cards._collection._ensureIndex({parentId: 1});
  1422. });
  1423. Cards.after.insert((userId, doc) => {
  1424. cardCreation(userId, doc);
  1425. });
  1426. // New activity for card (un)archivage
  1427. Cards.after.update((userId, doc, fieldNames) => {
  1428. cardState(userId, doc, fieldNames);
  1429. });
  1430. //New activity for card moves
  1431. Cards.after.update(function(userId, doc, fieldNames) {
  1432. const oldListId = this.previous.listId;
  1433. const oldSwimlaneId = this.previous.swimlaneId;
  1434. const oldBoardId = this.previous.boardId;
  1435. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  1436. });
  1437. // Add a new activity if we add or remove a member to the card
  1438. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1439. cardMembers(userId, doc, fieldNames, modifier);
  1440. updateActivities(doc, fieldNames, modifier);
  1441. });
  1442. // Add a new activity if we add or remove a label to the card
  1443. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1444. cardLabels(userId, doc, fieldNames, modifier);
  1445. });
  1446. // Add a new activity if we edit a custom field
  1447. Cards.before.update((userId, doc, fieldNames, modifier) => {
  1448. cardCustomFields(userId, doc, fieldNames, modifier);
  1449. });
  1450. // Remove all activities associated with a card if we remove the card
  1451. // Remove also card_comments / checklists / attachments
  1452. Cards.after.remove((userId, doc) => {
  1453. cardRemover(userId, doc);
  1454. });
  1455. }
  1456. //SWIMLANES REST API
  1457. if (Meteor.isServer) {
  1458. /**
  1459. * @operation get_swimlane_cards
  1460. * @summary get all cards attached to a swimlane
  1461. *
  1462. * @param {string} boardId the board ID
  1463. * @param {string} swimlaneId the swimlane ID
  1464. * @return_type [{_id: string,
  1465. * title: string,
  1466. * description: string,
  1467. * listId: string}]
  1468. */
  1469. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId/cards', function(req, res) {
  1470. const paramBoardId = req.params.boardId;
  1471. const paramSwimlaneId = req.params.swimlaneId;
  1472. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1473. JsonRoutes.sendResult(res, {
  1474. code: 200,
  1475. data: Cards.find({
  1476. boardId: paramBoardId,
  1477. swimlaneId: paramSwimlaneId,
  1478. archived: false,
  1479. }).map(function(doc) {
  1480. return {
  1481. _id: doc._id,
  1482. title: doc.title,
  1483. description: doc.description,
  1484. listId: doc.listId,
  1485. };
  1486. }),
  1487. });
  1488. });
  1489. }
  1490. //LISTS REST API
  1491. if (Meteor.isServer) {
  1492. /**
  1493. * @operation get_all_cards
  1494. * @summary Get all Cards attached to a List
  1495. *
  1496. * @param {string} boardId the board ID
  1497. * @param {string} listId the list ID
  1498. * @return_type [{_id: string,
  1499. * title: string,
  1500. * description: string}]
  1501. */
  1502. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1503. const paramBoardId = req.params.boardId;
  1504. const paramListId = req.params.listId;
  1505. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1506. JsonRoutes.sendResult(res, {
  1507. code: 200,
  1508. data: Cards.find({
  1509. boardId: paramBoardId,
  1510. listId: paramListId,
  1511. archived: false,
  1512. }).map(function(doc) {
  1513. return {
  1514. _id: doc._id,
  1515. title: doc.title,
  1516. description: doc.description,
  1517. };
  1518. }),
  1519. });
  1520. });
  1521. /**
  1522. * @operation get_card
  1523. * @summary Get a Card
  1524. *
  1525. * @param {string} boardId the board ID
  1526. * @param {string} listId the list ID of the card
  1527. * @param {string} cardId the card ID
  1528. * @return_type Cards
  1529. */
  1530. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1531. const paramBoardId = req.params.boardId;
  1532. const paramListId = req.params.listId;
  1533. const paramCardId = req.params.cardId;
  1534. Authentication.checkBoardAccess(req.userId, paramBoardId);
  1535. JsonRoutes.sendResult(res, {
  1536. code: 200,
  1537. data: Cards.findOne({
  1538. _id: paramCardId,
  1539. listId: paramListId,
  1540. boardId: paramBoardId,
  1541. archived: false,
  1542. }),
  1543. });
  1544. });
  1545. /**
  1546. * @operation new_card
  1547. * @summary Create a new Card
  1548. *
  1549. * @param {string} boardId the board ID of the new card
  1550. * @param {string} listId the list ID of the new card
  1551. * @param {string} authorID the user ID of the person owning the card
  1552. * @param {string} title the title of the new card
  1553. * @param {string} description the description of the new card
  1554. * @param {string} swimlaneId the swimlane ID of the new card
  1555. * @param {string} [members] the member IDs list of the new card
  1556. * @return_type {_id: string}
  1557. */
  1558. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(req, res) {
  1559. Authentication.checkUserId(req.userId);
  1560. const paramBoardId = req.params.boardId;
  1561. const paramListId = req.params.listId;
  1562. const currentCards = Cards.find({
  1563. listId: paramListId,
  1564. archived: false,
  1565. }, { sort: ['sort'] });
  1566. const check = Users.findOne({
  1567. _id: req.body.authorId,
  1568. });
  1569. const members = req.body.members || [req.body.authorId];
  1570. if (typeof check !== 'undefined') {
  1571. const id = Cards.direct.insert({
  1572. title: req.body.title,
  1573. boardId: paramBoardId,
  1574. listId: paramListId,
  1575. description: req.body.description,
  1576. userId: req.body.authorId,
  1577. swimlaneId: req.body.swimlaneId,
  1578. sort: currentCards.count(),
  1579. members,
  1580. });
  1581. JsonRoutes.sendResult(res, {
  1582. code: 200,
  1583. data: {
  1584. _id: id,
  1585. },
  1586. });
  1587. const card = Cards.findOne({
  1588. _id: id,
  1589. });
  1590. cardCreation(req.body.authorId, card);
  1591. } else {
  1592. JsonRoutes.sendResult(res, {
  1593. code: 401,
  1594. });
  1595. }
  1596. });
  1597. /*
  1598. * Note for the JSDoc:
  1599. * 'list' will be interpreted as the path parameter
  1600. * 'listID' will be interpreted as the body parameter
  1601. */
  1602. /**
  1603. * @operation edit_card
  1604. * @summary Edit Fields in a Card
  1605. *
  1606. * @description Edit a card
  1607. *
  1608. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  1609. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  1610. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  1611. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  1612. * `indigo`:
  1613. *
  1614. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  1615. *
  1616. * Note: setting the color to white has the same effect than removing it.
  1617. *
  1618. * @param {string} boardId the board ID of the card
  1619. * @param {string} list the list ID of the card
  1620. * @param {string} cardId the ID of the card
  1621. * @param {string} [title] the new title of the card
  1622. * @param {string} [listId] the new list ID of the card (move operation)
  1623. * @param {string} [description] the new description of the card
  1624. * @param {string} [authorId] change the owner of the card
  1625. * @param {string} [labelIds] the new list of label IDs attached to the card
  1626. * @param {string} [swimlaneId] the new swimlane ID of the card
  1627. * @param {string} [members] the new list of member IDs attached to the card
  1628. * @param {string} [requestedBy] the new requestedBy field of the card
  1629. * @param {string} [assignedBy] the new assignedBy field of the card
  1630. * @param {string} [receivedAt] the new receivedAt field of the card
  1631. * @param {string} [assignBy] the new assignBy field of the card
  1632. * @param {string} [startAt] the new startAt field of the card
  1633. * @param {string} [dueAt] the new dueAt field of the card
  1634. * @param {string} [endAt] the new endAt field of the card
  1635. * @param {string} [spentTime] the new spentTime field of the card
  1636. * @param {boolean} [isOverTime] the new isOverTime field of the card
  1637. * @param {string} [customFields] the new customFields value of the card
  1638. * @param {string} [color] the new color of the card
  1639. * @return_type {_id: string}
  1640. */
  1641. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1642. Authentication.checkUserId(req.userId);
  1643. const paramBoardId = req.params.boardId;
  1644. const paramCardId = req.params.cardId;
  1645. const paramListId = req.params.listId;
  1646. if (req.body.hasOwnProperty('title')) {
  1647. const newTitle = req.body.title;
  1648. Cards.direct.update({
  1649. _id: paramCardId,
  1650. listId: paramListId,
  1651. boardId: paramBoardId,
  1652. archived: false,
  1653. }, {
  1654. $set: {
  1655. title: newTitle,
  1656. },
  1657. });
  1658. }
  1659. if (req.body.hasOwnProperty('listId')) {
  1660. const newParamListId = req.body.listId;
  1661. Cards.direct.update({
  1662. _id: paramCardId,
  1663. listId: paramListId,
  1664. boardId: paramBoardId,
  1665. archived: false,
  1666. }, {
  1667. $set: {
  1668. listId: newParamListId,
  1669. },
  1670. });
  1671. const card = Cards.findOne({
  1672. _id: paramCardId,
  1673. });
  1674. cardMove(req.body.authorId, card, {
  1675. fieldName: 'listId',
  1676. }, paramListId);
  1677. }
  1678. if (req.body.hasOwnProperty('description')) {
  1679. const newDescription = req.body.description;
  1680. Cards.direct.update({
  1681. _id: paramCardId,
  1682. listId: paramListId,
  1683. boardId: paramBoardId,
  1684. archived: false,
  1685. }, {
  1686. $set: {
  1687. description: newDescription,
  1688. },
  1689. });
  1690. }
  1691. if (req.body.hasOwnProperty('color')) {
  1692. const newColor = req.body.color;
  1693. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1694. {$set: {color: newColor}});
  1695. }
  1696. if (req.body.hasOwnProperty('labelIds')) {
  1697. let newlabelIds = req.body.labelIds;
  1698. if (_.isString(newlabelIds)) {
  1699. if (newlabelIds === '') {
  1700. newlabelIds = null;
  1701. }
  1702. else {
  1703. newlabelIds = [newlabelIds];
  1704. }
  1705. }
  1706. Cards.direct.update({
  1707. _id: paramCardId,
  1708. listId: paramListId,
  1709. boardId: paramBoardId,
  1710. archived: false,
  1711. }, {
  1712. $set: {
  1713. labelIds: newlabelIds,
  1714. },
  1715. });
  1716. }
  1717. if (req.body.hasOwnProperty('requestedBy')) {
  1718. const newrequestedBy = req.body.requestedBy;
  1719. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1720. {$set: {requestedBy: newrequestedBy}});
  1721. }
  1722. if (req.body.hasOwnProperty('assignedBy')) {
  1723. const newassignedBy = req.body.assignedBy;
  1724. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1725. {$set: {assignedBy: newassignedBy}});
  1726. }
  1727. if (req.body.hasOwnProperty('receivedAt')) {
  1728. const newreceivedAt = req.body.receivedAt;
  1729. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1730. {$set: {receivedAt: newreceivedAt}});
  1731. }
  1732. if (req.body.hasOwnProperty('startAt')) {
  1733. const newstartAt = req.body.startAt;
  1734. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1735. {$set: {startAt: newstartAt}});
  1736. }
  1737. if (req.body.hasOwnProperty('dueAt')) {
  1738. const newdueAt = req.body.dueAt;
  1739. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1740. {$set: {dueAt: newdueAt}});
  1741. }
  1742. if (req.body.hasOwnProperty('endAt')) {
  1743. const newendAt = req.body.endAt;
  1744. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1745. {$set: {endAt: newendAt}});
  1746. }
  1747. if (req.body.hasOwnProperty('spentTime')) {
  1748. const newspentTime = req.body.spentTime;
  1749. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1750. {$set: {spentTime: newspentTime}});
  1751. }
  1752. if (req.body.hasOwnProperty('isOverTime')) {
  1753. const newisOverTime = req.body.isOverTime;
  1754. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1755. {$set: {isOverTime: newisOverTime}});
  1756. }
  1757. if (req.body.hasOwnProperty('customFields')) {
  1758. const newcustomFields = req.body.customFields;
  1759. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1760. {$set: {customFields: newcustomFields}});
  1761. }
  1762. if (req.body.hasOwnProperty('members')) {
  1763. let newmembers = req.body.members;
  1764. if (_.isString(newmembers)) {
  1765. if (newmembers === '') {
  1766. newmembers = null;
  1767. }
  1768. else {
  1769. newmembers = [newmembers];
  1770. }
  1771. }
  1772. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1773. {$set: {members: newmembers}});
  1774. }
  1775. if (req.body.hasOwnProperty('swimlaneId')) {
  1776. const newParamSwimlaneId = req.body.swimlaneId;
  1777. Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
  1778. {$set: {swimlaneId: newParamSwimlaneId}});
  1779. }
  1780. JsonRoutes.sendResult(res, {
  1781. code: 200,
  1782. data: {
  1783. _id: paramCardId,
  1784. },
  1785. });
  1786. });
  1787. /**
  1788. * @operation delete_card
  1789. * @summary Delete a card from a board
  1790. *
  1791. * @description This operation **deletes** a card, and therefore the card
  1792. * is not put in the recycle bin.
  1793. *
  1794. * @param {string} boardId the board ID of the card
  1795. * @param {string} list the list ID of the card
  1796. * @param {string} cardId the ID of the card
  1797. * @return_type {_id: string}
  1798. */
  1799. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function(req, res) {
  1800. Authentication.checkUserId(req.userId);
  1801. const paramBoardId = req.params.boardId;
  1802. const paramListId = req.params.listId;
  1803. const paramCardId = req.params.cardId;
  1804. Cards.direct.remove({
  1805. _id: paramCardId,
  1806. listId: paramListId,
  1807. boardId: paramBoardId,
  1808. });
  1809. const card = Cards.find({
  1810. _id: paramCardId,
  1811. });
  1812. cardRemover(req.body.authorId, card);
  1813. JsonRoutes.sendResult(res, {
  1814. code: 200,
  1815. data: {
  1816. _id: paramCardId,
  1817. },
  1818. });
  1819. });
  1820. }