cards.js 51 KB

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