cards.js 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001
  1. import {
  2. ALLOWED_COLORS,
  3. TYPE_CARD,
  4. TYPE_LINKED_BOARD,
  5. TYPE_LINKED_CARD,
  6. } from '../config/const';
  7. Cards = new Mongo.Collection('cards');
  8. // XXX To improve pub/sub performances a card document should include a
  9. // de-normalized number of comments so we don't have to publish the whole list
  10. // of comments just to display the number of them in the board view.
  11. Cards.attachSchema(
  12. new SimpleSchema({
  13. title: {
  14. /**
  15. * the title of the card
  16. */
  17. type: String,
  18. optional: true,
  19. defaultValue: '',
  20. },
  21. archived: {
  22. /**
  23. * is the card archived
  24. */
  25. type: Boolean,
  26. // eslint-disable-next-line consistent-return
  27. autoValue() {
  28. // eslint-disable-line consistent-return
  29. if (this.isInsert && !this.isSet) {
  30. return false;
  31. }
  32. },
  33. },
  34. archivedAt: {
  35. /**
  36. * latest archiving date
  37. */
  38. type: Date,
  39. optional: true,
  40. },
  41. parentId: {
  42. /**
  43. * ID of the parent card
  44. */
  45. type: String,
  46. optional: true,
  47. defaultValue: '',
  48. },
  49. listId: {
  50. /**
  51. * List ID where the card is
  52. */
  53. type: String,
  54. optional: true,
  55. defaultValue: '',
  56. },
  57. swimlaneId: {
  58. /**
  59. * Swimlane ID where the card is
  60. */
  61. type: String,
  62. },
  63. // The system could work without this `boardId` information (we could deduce
  64. // the board identifier from the card), but it would make the system more
  65. // difficult to manage and less efficient.
  66. boardId: {
  67. /**
  68. * Board ID of the card
  69. */
  70. type: String,
  71. optional: true,
  72. defaultValue: '',
  73. },
  74. coverId: {
  75. /**
  76. * Cover ID of the card
  77. */
  78. type: String,
  79. optional: true,
  80. defaultValue: '',
  81. },
  82. color: {
  83. type: String,
  84. optional: true,
  85. allowedValues: ALLOWED_COLORS,
  86. },
  87. createdAt: {
  88. /**
  89. * creation date
  90. */
  91. type: Date,
  92. // eslint-disable-next-line consistent-return
  93. autoValue() {
  94. if (this.isInsert) {
  95. return new Date();
  96. } else if (this.isUpsert) {
  97. return { $setOnInsert: new Date() };
  98. } else {
  99. this.unset();
  100. }
  101. },
  102. },
  103. modifiedAt: {
  104. type: Date,
  105. denyUpdate: false,
  106. // eslint-disable-next-line consistent-return
  107. autoValue() {
  108. if (this.isInsert || this.isUpsert || this.isUpdate) {
  109. return new Date();
  110. } else {
  111. this.unset();
  112. }
  113. },
  114. },
  115. customFields: {
  116. /**
  117. * list of custom fields
  118. */
  119. type: [Object],
  120. optional: true,
  121. defaultValue: [],
  122. },
  123. 'customFields.$': {
  124. type: new SimpleSchema({
  125. _id: {
  126. /**
  127. * the ID of the related custom field
  128. */
  129. type: String,
  130. optional: true,
  131. defaultValue: '',
  132. },
  133. value: {
  134. /**
  135. * value attached to the custom field
  136. */
  137. type: Match.OneOf(String, Number, Boolean, Date, [String]),
  138. optional: true,
  139. defaultValue: '',
  140. },
  141. 'value.$': {
  142. type: String,
  143. optional: true,
  144. },
  145. }),
  146. },
  147. dateLastActivity: {
  148. /**
  149. * Date of last activity
  150. */
  151. type: Date,
  152. autoValue() {
  153. return new Date();
  154. },
  155. },
  156. description: {
  157. /**
  158. * description of the card
  159. */
  160. type: String,
  161. optional: true,
  162. defaultValue: '',
  163. },
  164. requestedBy: {
  165. /**
  166. * who requested the card (ID of the user)
  167. */
  168. type: String,
  169. optional: true,
  170. defaultValue: '',
  171. },
  172. assignedBy: {
  173. /**
  174. * who assigned the card (ID of the user)
  175. */
  176. type: String,
  177. optional: true,
  178. defaultValue: '',
  179. },
  180. labelIds: {
  181. /**
  182. * list of labels ID the card has
  183. */
  184. type: [String],
  185. optional: true,
  186. defaultValue: [],
  187. },
  188. members: {
  189. /**
  190. * list of members (user IDs)
  191. */
  192. type: [String],
  193. optional: true,
  194. defaultValue: [],
  195. },
  196. assignees: {
  197. /**
  198. * who is assignee of the card (user ID),
  199. * maximum one ID of assignee in array.
  200. */
  201. type: [String],
  202. optional: true,
  203. defaultValue: [],
  204. },
  205. receivedAt: {
  206. /**
  207. * Date the card was received
  208. */
  209. type: Date,
  210. optional: true,
  211. },
  212. startAt: {
  213. /**
  214. * Date the card was started to be worked on
  215. */
  216. type: Date,
  217. optional: true,
  218. },
  219. dueAt: {
  220. /**
  221. * Date the card is due
  222. */
  223. type: Date,
  224. optional: true,
  225. },
  226. endAt: {
  227. /**
  228. * Date the card ended
  229. */
  230. type: Date,
  231. optional: true,
  232. },
  233. spentTime: {
  234. /**
  235. * How much time has been spent on this
  236. */
  237. type: Number,
  238. decimal: true,
  239. optional: true,
  240. defaultValue: 0,
  241. },
  242. isOvertime: {
  243. /**
  244. * is the card over time?
  245. */
  246. type: Boolean,
  247. defaultValue: false,
  248. optional: true,
  249. },
  250. // XXX Should probably be called `authorId`. Is it even needed since we have
  251. // the `members` field?
  252. userId: {
  253. /**
  254. * user ID of the author of the card
  255. */
  256. type: String,
  257. // eslint-disable-next-line consistent-return
  258. autoValue() {
  259. // eslint-disable-line consistent-return
  260. if (this.isInsert && !this.isSet) {
  261. return this.userId;
  262. }
  263. },
  264. },
  265. sort: {
  266. /**
  267. * Sort value
  268. */
  269. type: Number,
  270. decimal: true,
  271. defaultValue: 0,
  272. },
  273. subtaskSort: {
  274. /**
  275. * subtask sort value
  276. */
  277. type: Number,
  278. decimal: true,
  279. defaultValue: -1,
  280. optional: true,
  281. },
  282. type: {
  283. /**
  284. * type of the card
  285. */
  286. type: String,
  287. defaultValue: TYPE_CARD,
  288. // allowedValues: [TYPE_CARD, TYPE_LINKED_CARD, TYPE_LINKED_BOARD, TYPE_TEMPLATE_CARD],
  289. },
  290. linkedId: {
  291. /**
  292. * ID of the linked card
  293. */
  294. type: String,
  295. optional: true,
  296. defaultValue: '',
  297. },
  298. vote: {
  299. /**
  300. * vote object, see below
  301. */
  302. type: Object,
  303. optional: true,
  304. },
  305. 'vote.question': {
  306. type: String,
  307. defaultValue: '',
  308. },
  309. 'vote.positive': {
  310. /**
  311. * list of members (user IDs)
  312. */
  313. type: [String],
  314. optional: true,
  315. defaultValue: [],
  316. },
  317. 'vote.negative': {
  318. /**
  319. * list of members (user IDs)
  320. */
  321. type: [String],
  322. optional: true,
  323. defaultValue: [],
  324. },
  325. 'vote.end': {
  326. type: Date,
  327. optional: true,
  328. defaultValue: null,
  329. },
  330. 'vote.public': {
  331. type: Boolean,
  332. defaultValue: false,
  333. },
  334. 'vote.allowNonBoardMembers': {
  335. type: Boolean,
  336. defaultValue: false,
  337. },
  338. }),
  339. );
  340. Cards.allow({
  341. insert(userId, doc) {
  342. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  343. },
  344. update(userId, doc, fields) {
  345. // Allow board members or logged in users if only vote get's changed
  346. return (
  347. allowIsBoardMember(userId, Boards.findOne(doc.boardId)) ||
  348. (_.isEqual(fields, ['vote', 'modifiedAt', 'dateLastActivity']) &&
  349. !!userId)
  350. );
  351. },
  352. remove(userId, doc) {
  353. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  354. },
  355. fetch: ['boardId'],
  356. });
  357. Cards.helpers({
  358. mapCustomFieldsToBoard(boardId) {
  359. // Map custom fields to new board
  360. return this.customFields.map(cf => {
  361. const oldCf = CustomFields.findOne(cf._id);
  362. const newCf = CustomFields.findOne({
  363. boardIds: boardId,
  364. name: oldCf.name,
  365. type: oldCf.type,
  366. });
  367. if (newCf) {
  368. cf._id = newCf._id;
  369. } else if (!_.contains(oldCf.boardIds, boardId)) {
  370. oldCf.addBoard(boardId);
  371. }
  372. return cf;
  373. });
  374. },
  375. copy(boardId, swimlaneId, listId) {
  376. const oldId = this._id;
  377. const oldCard = Cards.findOne(oldId);
  378. // we must only copy the labels and custom fields if the target board
  379. // differs from the source board
  380. if (this.boardId !== boardId) {
  381. const oldBoard = Boards.findOne(this.boardId);
  382. const oldBoardLabels = oldBoard.labels;
  383. // Get old label names
  384. const oldCardLabels = _.pluck(
  385. _.filter(oldBoardLabels, label => {
  386. return _.contains(this.labelIds, label._id);
  387. }),
  388. 'name',
  389. );
  390. const newBoard = Boards.findOne(boardId);
  391. const newBoardLabels = newBoard.labels;
  392. const newCardLabels = _.pluck(
  393. _.filter(newBoardLabels, label => {
  394. return _.contains(oldCardLabels, label.name);
  395. }),
  396. '_id',
  397. );
  398. // now set the new label ids
  399. delete this.labelIds;
  400. this.labelIds = newCardLabels;
  401. this.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  402. }
  403. delete this._id;
  404. this.boardId = boardId;
  405. this.swimlaneId = swimlaneId;
  406. this.listId = listId;
  407. const _id = Cards.insert(this);
  408. // Copy attachments
  409. oldCard.attachments().forEach(att => {
  410. att.cardId = _id;
  411. delete att._id;
  412. return Attachments.insert(att);
  413. });
  414. // copy checklists
  415. Checklists.find({ cardId: oldId }).forEach(ch => {
  416. ch.copy(_id);
  417. });
  418. // copy subtasks
  419. Cards.find({ parentId: oldId }).forEach(subtask => {
  420. subtask.parentId = _id;
  421. subtask._id = null;
  422. Cards.insert(subtask);
  423. });
  424. // copy card comments
  425. CardComments.find({ cardId: oldId }).forEach(cmt => {
  426. cmt.copy(_id);
  427. });
  428. return _id;
  429. },
  430. link(boardId, swimlaneId, listId) {
  431. // TODO is there a better method to create a deepcopy?
  432. linkCard = JSON.parse(JSON.stringify(this));
  433. // TODO is this how it is meant to be?
  434. linkCard.linkedId = linkCard.linkedId || linkCard._id;
  435. linkCard.boardId = boardId;
  436. linkCard.swimlaneId = swimlaneId;
  437. linkCard.listId = listId;
  438. linkCard.type = 'cardType-linkedCard';
  439. delete linkCard._id;
  440. // TODO shall we copy the labels for a linked card?!
  441. delete linkCard.labelIds;
  442. return Cards.insert(linkCard);
  443. },
  444. list() {
  445. return Lists.findOne(this.listId);
  446. },
  447. swimlane() {
  448. return Swimlanes.findOne(this.swimlaneId);
  449. },
  450. board() {
  451. return Boards.findOne(this.boardId);
  452. },
  453. getList() {
  454. const list = this.list();
  455. if (!list) {
  456. return {
  457. _id: this.listId,
  458. title: 'Undefined List',
  459. archived: false,
  460. colorClass: '',
  461. };
  462. }
  463. return list;
  464. },
  465. getSwimlane() {
  466. const swimlane = this.swimlane();
  467. if (!swimlane) {
  468. return {
  469. _id: this.swimlaneId,
  470. title: 'Undefined Swimlane',
  471. archived: false,
  472. colorClass: '',
  473. };
  474. }
  475. return swimlane;
  476. },
  477. getBoard() {
  478. const board = this.board();
  479. if (!board) {
  480. return {
  481. _id: this.boardId,
  482. title: 'Undefined Board',
  483. archived: false,
  484. colorClass: '',
  485. };
  486. }
  487. return board;
  488. },
  489. labels() {
  490. const boardLabels = this.board().labels;
  491. const cardLabels = _.filter(boardLabels, label => {
  492. return _.contains(this.labelIds, label._id);
  493. });
  494. return cardLabels;
  495. },
  496. hasLabel(labelId) {
  497. return _.contains(this.labelIds, labelId);
  498. },
  499. user() {
  500. return Users.findOne(this.userId);
  501. },
  502. isAssigned(memberId) {
  503. return _.contains(this.getMembers(), memberId);
  504. },
  505. isAssignee(assigneeId) {
  506. return _.contains(this.getAssignees(), assigneeId);
  507. },
  508. activities() {
  509. if (this.isLinkedCard()) {
  510. return Activities.find(
  511. { cardId: this.linkedId },
  512. { sort: { createdAt: -1 } },
  513. );
  514. } else if (this.isLinkedBoard()) {
  515. return Activities.find(
  516. { boardId: this.linkedId },
  517. { sort: { createdAt: -1 } },
  518. );
  519. } else {
  520. return Activities.find({ cardId: this._id }, { sort: { createdAt: -1 } });
  521. }
  522. },
  523. comments() {
  524. if (this.isLinkedCard()) {
  525. return CardComments.find(
  526. { cardId: this.linkedId },
  527. { sort: { createdAt: -1 } },
  528. );
  529. } else {
  530. return CardComments.find(
  531. { cardId: this._id },
  532. { sort: { createdAt: -1 } },
  533. );
  534. }
  535. },
  536. attachments() {
  537. if (this.isLinkedCard()) {
  538. return Attachments.find(
  539. { cardId: this.linkedId },
  540. { sort: { uploadedAt: -1 } },
  541. );
  542. } else {
  543. return Attachments.find(
  544. { cardId: this._id },
  545. { sort: { uploadedAt: -1 } },
  546. );
  547. }
  548. },
  549. cover() {
  550. if (!this.coverId) return false;
  551. const cover = Attachments.findOne(this.coverId);
  552. // if we return a cover before it is fully stored, we will get errors when we try to display it
  553. // todo XXX we could return a default "upload pending" image in the meantime?
  554. return cover && cover.url() && cover;
  555. },
  556. checklists() {
  557. if (this.isLinkedCard()) {
  558. return Checklists.find({ cardId: this.linkedId }, { sort: { sort: 1 } });
  559. } else {
  560. return Checklists.find({ cardId: this._id }, { sort: { sort: 1 } });
  561. }
  562. },
  563. checklistItemCount() {
  564. const checklists = this.checklists().fetch();
  565. return checklists
  566. .map(checklist => {
  567. return checklist.itemCount();
  568. })
  569. .reduce((prev, next) => {
  570. return prev + next;
  571. }, 0);
  572. },
  573. checklistFinishedCount() {
  574. const checklists = this.checklists().fetch();
  575. return checklists
  576. .map(checklist => {
  577. return checklist.finishedCount();
  578. })
  579. .reduce((prev, next) => {
  580. return prev + next;
  581. }, 0);
  582. },
  583. checklistFinished() {
  584. return (
  585. this.hasChecklist() &&
  586. this.checklistItemCount() === this.checklistFinishedCount()
  587. );
  588. },
  589. hasChecklist() {
  590. return this.checklistItemCount() !== 0;
  591. },
  592. subtasks() {
  593. return Cards.find(
  594. {
  595. parentId: this._id,
  596. archived: false,
  597. },
  598. {
  599. sort: {
  600. sort: 1,
  601. },
  602. },
  603. );
  604. },
  605. allSubtasks() {
  606. return Cards.find(
  607. {
  608. parentId: this._id,
  609. archived: false,
  610. },
  611. {
  612. sort: {
  613. sort: 1,
  614. },
  615. },
  616. );
  617. },
  618. subtasksCount() {
  619. return Cards.find({
  620. parentId: this._id,
  621. archived: false,
  622. }).count();
  623. },
  624. subtasksFinishedCount() {
  625. return Cards.find({
  626. parentId: this._id,
  627. archived: true,
  628. }).count();
  629. },
  630. subtasksFinished() {
  631. const finishCount = this.subtasksFinishedCount();
  632. return finishCount > 0 && this.subtasksCount() === finishCount;
  633. },
  634. allowsSubtasks() {
  635. return this.subtasksCount() !== 0;
  636. },
  637. customFieldIndex(customFieldId) {
  638. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  639. },
  640. // customFields with definitions
  641. customFieldsWD() {
  642. // get all definitions
  643. const definitions = CustomFields.find({
  644. boardIds: { $in: [this.boardId] },
  645. }).fetch();
  646. if (!definitions) {
  647. return {};
  648. }
  649. // match right definition to each field
  650. if (!this.customFields) return [];
  651. const ret = this.customFields.map(customField => {
  652. const definition = definitions.find(definition => {
  653. return definition._id === customField._id;
  654. });
  655. if (!definition) {
  656. return {};
  657. }
  658. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  659. let trueValue = customField.value;
  660. if (
  661. definition.settings.dropdownItems &&
  662. definition.settings.dropdownItems.length > 0
  663. ) {
  664. for (let i = 0; i < definition.settings.dropdownItems.length; i++) {
  665. if (definition.settings.dropdownItems[i]._id === customField.value) {
  666. trueValue = definition.settings.dropdownItems[i].name;
  667. }
  668. }
  669. }
  670. return {
  671. _id: customField._id,
  672. value: customField.value,
  673. trueValue,
  674. definition,
  675. };
  676. });
  677. // at linked cards custom fields definition is not found
  678. ret.sort(
  679. (a, b) =>
  680. a.definition !== undefined &&
  681. b.definition !== undefined &&
  682. a.definition.name !== undefined &&
  683. b.definition.name !== undefined &&
  684. a.definition.name.localeCompare(b.definition.name),
  685. );
  686. return ret;
  687. },
  688. colorClass() {
  689. if (this.color) return this.color;
  690. return '';
  691. },
  692. absoluteUrl() {
  693. const board = this.board();
  694. return FlowRouter.url('card', {
  695. boardId: board._id,
  696. slug: board.slug,
  697. cardId: this._id,
  698. });
  699. },
  700. originRelativeUrl() {
  701. const board = this.board();
  702. return FlowRouter.path('card', {
  703. boardId: board._id,
  704. slug: board.slug,
  705. cardId: this._id,
  706. });
  707. },
  708. canBeRestored() {
  709. const list = Lists.findOne({
  710. _id: this.listId,
  711. });
  712. if (
  713. !list.getWipLimit('soft') &&
  714. list.getWipLimit('enabled') &&
  715. list.getWipLimit('value') === list.cards().count()
  716. ) {
  717. return false;
  718. }
  719. return true;
  720. },
  721. parentCard() {
  722. if (this.parentId === '') {
  723. return null;
  724. }
  725. return Cards.findOne(this.parentId);
  726. },
  727. parentCardName() {
  728. let result = '';
  729. if (this.parentId !== '') {
  730. const card = Cards.findOne(this.parentId);
  731. if (card) {
  732. result = card.title;
  733. }
  734. }
  735. return result;
  736. },
  737. parentListId() {
  738. const result = [];
  739. let crtParentId = this.parentId;
  740. while (crtParentId !== '') {
  741. const crt = Cards.findOne(crtParentId);
  742. if (crt === null || crt === undefined) {
  743. // maybe it has been deleted
  744. break;
  745. }
  746. if (crtParentId in result) {
  747. // circular reference
  748. break;
  749. }
  750. result.unshift(crtParentId);
  751. crtParentId = crt.parentId;
  752. }
  753. return result;
  754. },
  755. parentList() {
  756. const resultId = [];
  757. const result = [];
  758. let crtParentId = this.parentId;
  759. while (crtParentId !== '') {
  760. const crt = Cards.findOne(crtParentId);
  761. if (crt === null || crt === undefined) {
  762. // maybe it has been deleted
  763. break;
  764. }
  765. if (crtParentId in resultId) {
  766. // circular reference
  767. break;
  768. }
  769. resultId.unshift(crtParentId);
  770. result.unshift(crt);
  771. crtParentId = crt.parentId;
  772. }
  773. return result;
  774. },
  775. parentString(sep) {
  776. return this.parentList()
  777. .map(function(elem) {
  778. return elem.title;
  779. })
  780. .join(sep);
  781. },
  782. isTopLevel() {
  783. return this.parentId === '';
  784. },
  785. isLinkedCard() {
  786. return this.type === 'cardType-linkedCard';
  787. },
  788. isLinkedBoard() {
  789. return this.type === 'cardType-linkedBoard';
  790. },
  791. isLinked() {
  792. return this.isLinkedCard() || this.isLinkedBoard();
  793. },
  794. setDescription(description) {
  795. if (this.isLinkedCard()) {
  796. return Cards.update({ _id: this.linkedId }, { $set: { description } });
  797. } else if (this.isLinkedBoard()) {
  798. return Boards.update({ _id: this.linkedId }, { $set: { description } });
  799. } else {
  800. return Cards.update({ _id: this._id }, { $set: { description } });
  801. }
  802. },
  803. getDescription() {
  804. if (this.isLinkedCard()) {
  805. const card = Cards.findOne({ _id: this.linkedId });
  806. if (card && card.description) return card.description;
  807. else return null;
  808. } else if (this.isLinkedBoard()) {
  809. const board = Boards.findOne({ _id: this.linkedId });
  810. if (board && board.description) return board.description;
  811. else return null;
  812. } else if (this.description) {
  813. return this.description;
  814. } else {
  815. return null;
  816. }
  817. },
  818. getMembers() {
  819. if (this.isLinkedCard()) {
  820. const card = Cards.findOne({ _id: this.linkedId });
  821. if (card === undefined) {
  822. return null;
  823. } else {
  824. return card.members;
  825. }
  826. } else if (this.isLinkedBoard()) {
  827. const board = Boards.findOne({ _id: this.linkedId });
  828. if (board === undefined) {
  829. return null;
  830. } else {
  831. return board.activeMembers().map(member => {
  832. return member.userId;
  833. });
  834. }
  835. } else {
  836. return this.members;
  837. }
  838. },
  839. getAssignees() {
  840. if (this.isLinkedCard()) {
  841. const card = Cards.findOne({ _id: this.linkedId });
  842. if (card === undefined) {
  843. return null;
  844. } else {
  845. return card.assignees;
  846. }
  847. } else if (this.isLinkedBoard()) {
  848. const board = Boards.findOne({ _id: this.linkedId });
  849. if (board === undefined) {
  850. return null;
  851. } else {
  852. return board.activeMembers().map(assignee => {
  853. return assignee.userId;
  854. });
  855. }
  856. } else {
  857. return this.assignees;
  858. }
  859. },
  860. assignMember(memberId) {
  861. if (this.isLinkedCard()) {
  862. return Cards.update(
  863. { _id: this.linkedId },
  864. { $addToSet: { members: memberId } },
  865. );
  866. } else if (this.isLinkedBoard()) {
  867. const board = Boards.findOne({ _id: this.linkedId });
  868. return board.addMember(memberId);
  869. } else {
  870. return Cards.update(
  871. { _id: this._id },
  872. { $addToSet: { members: memberId } },
  873. );
  874. }
  875. },
  876. assignAssignee(assigneeId) {
  877. if (this.isLinkedCard()) {
  878. return Cards.update(
  879. { _id: this.linkedId },
  880. { $addToSet: { assignees: assigneeId } },
  881. );
  882. } else if (this.isLinkedBoard()) {
  883. const board = Boards.findOne({ _id: this.linkedId });
  884. return board.addAssignee(assigneeId);
  885. } else {
  886. return Cards.update(
  887. { _id: this._id },
  888. { $addToSet: { assignees: assigneeId } },
  889. );
  890. }
  891. },
  892. unassignMember(memberId) {
  893. if (this.isLinkedCard()) {
  894. return Cards.update(
  895. { _id: this.linkedId },
  896. { $pull: { members: memberId } },
  897. );
  898. } else if (this.isLinkedBoard()) {
  899. const board = Boards.findOne({ _id: this.linkedId });
  900. return board.removeMember(memberId);
  901. } else {
  902. return Cards.update({ _id: this._id }, { $pull: { members: memberId } });
  903. }
  904. },
  905. unassignAssignee(assigneeId) {
  906. if (this.isLinkedCard()) {
  907. return Cards.update(
  908. { _id: this.linkedId },
  909. { $pull: { assignees: assigneeId } },
  910. );
  911. } else if (this.isLinkedBoard()) {
  912. const board = Boards.findOne({ _id: this.linkedId });
  913. return board.removeAssignee(assigneeId);
  914. } else {
  915. return Cards.update(
  916. { _id: this._id },
  917. { $pull: { assignees: assigneeId } },
  918. );
  919. }
  920. },
  921. toggleMember(memberId) {
  922. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  923. return this.unassignMember(memberId);
  924. } else {
  925. return this.assignMember(memberId);
  926. }
  927. },
  928. toggleAssignee(assigneeId) {
  929. if (this.getAssignees() && this.getAssignees().indexOf(assigneeId) > -1) {
  930. return this.unassignAssignee(assigneeId);
  931. } else {
  932. return this.assignAssignee(assigneeId);
  933. }
  934. },
  935. getReceived() {
  936. if (this.isLinkedCard()) {
  937. const card = Cards.findOne({ _id: this.linkedId });
  938. if (card === undefined) {
  939. return null;
  940. } else {
  941. return card.receivedAt;
  942. }
  943. } else {
  944. return this.receivedAt;
  945. }
  946. },
  947. setReceived(receivedAt) {
  948. if (this.isLinkedCard()) {
  949. return Cards.update({ _id: this.linkedId }, { $set: { receivedAt } });
  950. } else {
  951. return Cards.update({ _id: this._id }, { $set: { receivedAt } });
  952. }
  953. },
  954. getStart() {
  955. if (this.isLinkedCard()) {
  956. const card = Cards.findOne({ _id: this.linkedId });
  957. if (card === undefined) {
  958. return null;
  959. } else {
  960. return card.startAt;
  961. }
  962. } else if (this.isLinkedBoard()) {
  963. const board = Boards.findOne({ _id: this.linkedId });
  964. if (board === undefined) {
  965. return null;
  966. } else {
  967. return board.startAt;
  968. }
  969. } else {
  970. return this.startAt;
  971. }
  972. },
  973. setStart(startAt) {
  974. if (this.isLinkedCard()) {
  975. return Cards.update({ _id: this.linkedId }, { $set: { startAt } });
  976. } else if (this.isLinkedBoard()) {
  977. return Boards.update({ _id: this.linkedId }, { $set: { startAt } });
  978. } else {
  979. return Cards.update({ _id: this._id }, { $set: { startAt } });
  980. }
  981. },
  982. getDue() {
  983. if (this.isLinkedCard()) {
  984. const card = Cards.findOne({ _id: this.linkedId });
  985. if (card === undefined) {
  986. return null;
  987. } else {
  988. return card.dueAt;
  989. }
  990. } else if (this.isLinkedBoard()) {
  991. const board = Boards.findOne({ _id: this.linkedId });
  992. if (board === undefined) {
  993. return null;
  994. } else {
  995. return board.dueAt;
  996. }
  997. } else {
  998. return this.dueAt;
  999. }
  1000. },
  1001. setDue(dueAt) {
  1002. if (this.isLinkedCard()) {
  1003. return Cards.update({ _id: this.linkedId }, { $set: { dueAt } });
  1004. } else if (this.isLinkedBoard()) {
  1005. return Boards.update({ _id: this.linkedId }, { $set: { dueAt } });
  1006. } else {
  1007. return Cards.update({ _id: this._id }, { $set: { dueAt } });
  1008. }
  1009. },
  1010. getEnd() {
  1011. if (this.isLinkedCard()) {
  1012. const card = Cards.findOne({ _id: this.linkedId });
  1013. if (card === undefined) {
  1014. return null;
  1015. } else {
  1016. return card.endAt;
  1017. }
  1018. } else if (this.isLinkedBoard()) {
  1019. const board = Boards.findOne({ _id: this.linkedId });
  1020. if (board === undefined) {
  1021. return null;
  1022. } else {
  1023. return board.endAt;
  1024. }
  1025. } else {
  1026. return this.endAt;
  1027. }
  1028. },
  1029. setEnd(endAt) {
  1030. if (this.isLinkedCard()) {
  1031. return Cards.update({ _id: this.linkedId }, { $set: { endAt } });
  1032. } else if (this.isLinkedBoard()) {
  1033. return Boards.update({ _id: this.linkedId }, { $set: { endAt } });
  1034. } else {
  1035. return Cards.update({ _id: this._id }, { $set: { endAt } });
  1036. }
  1037. },
  1038. getIsOvertime() {
  1039. if (this.isLinkedCard()) {
  1040. const card = Cards.findOne({ _id: this.linkedId });
  1041. if (card === undefined) {
  1042. return null;
  1043. } else {
  1044. return card.isOvertime;
  1045. }
  1046. } else if (this.isLinkedBoard()) {
  1047. const board = Boards.findOne({ _id: this.linkedId });
  1048. if (board === undefined) {
  1049. return null;
  1050. } else {
  1051. return board.isOvertime;
  1052. }
  1053. } else {
  1054. return this.isOvertime;
  1055. }
  1056. },
  1057. setIsOvertime(isOvertime) {
  1058. if (this.isLinkedCard()) {
  1059. return Cards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  1060. } else if (this.isLinkedBoard()) {
  1061. return Boards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  1062. } else {
  1063. return Cards.update({ _id: this._id }, { $set: { isOvertime } });
  1064. }
  1065. },
  1066. getSpentTime() {
  1067. if (this.isLinkedCard()) {
  1068. const card = Cards.findOne({ _id: this.linkedId });
  1069. if (card === undefined) {
  1070. return null;
  1071. } else {
  1072. return card.spentTime;
  1073. }
  1074. } else if (this.isLinkedBoard()) {
  1075. const board = Boards.findOne({ _id: this.linkedId });
  1076. if (board === undefined) {
  1077. return null;
  1078. } else {
  1079. return board.spentTime;
  1080. }
  1081. } else {
  1082. return this.spentTime;
  1083. }
  1084. },
  1085. setSpentTime(spentTime) {
  1086. if (this.isLinkedCard()) {
  1087. return Cards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1088. } else if (this.isLinkedBoard()) {
  1089. return Boards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1090. } else {
  1091. return Cards.update({ _id: this._id }, { $set: { spentTime } });
  1092. }
  1093. },
  1094. getVoteQuestion() {
  1095. if (this.isLinkedCard()) {
  1096. const card = Cards.findOne({ _id: this.linkedId });
  1097. if (card === undefined) {
  1098. return null;
  1099. } else if (card && card.vote) {
  1100. return card.vote.question;
  1101. } else {
  1102. return null;
  1103. }
  1104. } else if (this.isLinkedBoard()) {
  1105. const board = Boards.findOne({ _id: this.linkedId });
  1106. if (board === undefined) {
  1107. return null;
  1108. } else if (board && board.vote) {
  1109. return board.vote.question;
  1110. } else {
  1111. return null;
  1112. }
  1113. } else if (this.vote) {
  1114. return this.vote.question;
  1115. } else {
  1116. return null;
  1117. }
  1118. },
  1119. getVotePublic() {
  1120. if (this.isLinkedCard()) {
  1121. const card = Cards.findOne({ _id: this.linkedId });
  1122. if (card === undefined) {
  1123. return null;
  1124. } else if (card && card.vote) {
  1125. return card.vote.public;
  1126. } else {
  1127. return null;
  1128. }
  1129. } else if (this.isLinkedBoard()) {
  1130. const board = Boards.findOne({ _id: this.linkedId });
  1131. if (board === undefined) {
  1132. return null;
  1133. } else if (board && board.vote) {
  1134. return board.vote.public;
  1135. } else {
  1136. return null;
  1137. }
  1138. } else if (this.vote) {
  1139. return this.vote.public;
  1140. } else {
  1141. return null;
  1142. }
  1143. },
  1144. getVoteEnd() {
  1145. if (this.isLinkedCard()) {
  1146. const card = Cards.findOne({ _id: this.linkedId });
  1147. if (card === undefined) {
  1148. return null;
  1149. } else if (card && card.vote) {
  1150. return card.vote.end;
  1151. } else {
  1152. return null;
  1153. }
  1154. } else if (this.isLinkedBoard()) {
  1155. const board = Boards.findOne({ _id: this.linkedId });
  1156. if (board === undefined) {
  1157. return null;
  1158. } else if (board && board.vote) {
  1159. return board.vote.end;
  1160. } else {
  1161. return null;
  1162. }
  1163. } else if (this.vote) {
  1164. return this.vote.end;
  1165. } else {
  1166. return null;
  1167. }
  1168. },
  1169. expiredVote() {
  1170. let end = this.getVoteEnd();
  1171. if (end) {
  1172. end = moment(end);
  1173. return end.isBefore(new Date());
  1174. }
  1175. return false;
  1176. },
  1177. voteMemberPositive() {
  1178. if (this.vote && this.vote.positive)
  1179. return Users.find({ _id: { $in: this.vote.positive } });
  1180. return [];
  1181. },
  1182. voteMemberNegative() {
  1183. if (this.vote && this.vote.negative)
  1184. return Users.find({ _id: { $in: this.vote.negative } });
  1185. return [];
  1186. },
  1187. voteState() {
  1188. const userId = Meteor.userId();
  1189. let state;
  1190. if (this.vote) {
  1191. if (this.vote.positive) {
  1192. state = _.contains(this.vote.positive, userId);
  1193. if (state === true) return true;
  1194. }
  1195. if (this.vote.negative) {
  1196. state = _.contains(this.vote.negative, userId);
  1197. if (state === true) return false;
  1198. }
  1199. }
  1200. return null;
  1201. },
  1202. getId() {
  1203. if (this.isLinked()) {
  1204. return this.linkedId;
  1205. } else {
  1206. return this._id;
  1207. }
  1208. },
  1209. getTitle() {
  1210. if (this.isLinkedCard()) {
  1211. const card = Cards.findOne({ _id: this.linkedId });
  1212. if (card === undefined) {
  1213. return null;
  1214. } else {
  1215. return card.title;
  1216. }
  1217. } else if (this.isLinkedBoard()) {
  1218. const board = Boards.findOne({ _id: this.linkedId });
  1219. if (board === undefined) {
  1220. return null;
  1221. } else {
  1222. return board.title;
  1223. }
  1224. } else if (this.title === undefined) {
  1225. return null;
  1226. } else {
  1227. return this.title;
  1228. }
  1229. },
  1230. getBoardTitle() {
  1231. if (this.isLinkedCard()) {
  1232. const card = Cards.findOne({ _id: this.linkedId });
  1233. if (card === undefined) {
  1234. return null;
  1235. }
  1236. const board = Boards.findOne({ _id: card.boardId });
  1237. if (board === undefined) {
  1238. return null;
  1239. } else {
  1240. return board.title;
  1241. }
  1242. } else if (this.isLinkedBoard()) {
  1243. const board = Boards.findOne({ _id: this.linkedId });
  1244. if (board === undefined) {
  1245. return null;
  1246. } else {
  1247. return board.title;
  1248. }
  1249. } else {
  1250. const board = Boards.findOne({ _id: this.boardId });
  1251. if (board === undefined) {
  1252. return null;
  1253. } else {
  1254. return board.title;
  1255. }
  1256. }
  1257. },
  1258. setTitle(title) {
  1259. if (this.isLinkedCard()) {
  1260. return Cards.update({ _id: this.linkedId }, { $set: { title } });
  1261. } else if (this.isLinkedBoard()) {
  1262. return Boards.update({ _id: this.linkedId }, { $set: { title } });
  1263. } else {
  1264. return Cards.update({ _id: this._id }, { $set: { title } });
  1265. }
  1266. },
  1267. getArchived() {
  1268. if (this.isLinkedCard()) {
  1269. const card = Cards.findOne({ _id: this.linkedId });
  1270. if (card === undefined) {
  1271. return null;
  1272. } else {
  1273. return card.archived;
  1274. }
  1275. } else if (this.isLinkedBoard()) {
  1276. const board = Boards.findOne({ _id: this.linkedId });
  1277. if (board === undefined) {
  1278. return null;
  1279. } else {
  1280. return board.archived;
  1281. }
  1282. } else {
  1283. return this.archived;
  1284. }
  1285. },
  1286. setRequestedBy(requestedBy) {
  1287. if (this.isLinkedCard()) {
  1288. return Cards.update({ _id: this.linkedId }, { $set: { requestedBy } });
  1289. } else {
  1290. return Cards.update({ _id: this._id }, { $set: { requestedBy } });
  1291. }
  1292. },
  1293. getRequestedBy() {
  1294. if (this.isLinkedCard()) {
  1295. const card = Cards.findOne({ _id: this.linkedId });
  1296. if (card === undefined) {
  1297. return null;
  1298. } else {
  1299. return card.requestedBy;
  1300. }
  1301. } else {
  1302. return this.requestedBy;
  1303. }
  1304. },
  1305. setAssignedBy(assignedBy) {
  1306. if (this.isLinkedCard()) {
  1307. return Cards.update({ _id: this.linkedId }, { $set: { assignedBy } });
  1308. } else {
  1309. return Cards.update({ _id: this._id }, { $set: { assignedBy } });
  1310. }
  1311. },
  1312. getAssignedBy() {
  1313. if (this.isLinkedCard()) {
  1314. const card = Cards.findOne({ _id: this.linkedId });
  1315. if (card === undefined) {
  1316. return null;
  1317. } else {
  1318. return card.assignedBy;
  1319. }
  1320. } else {
  1321. return this.assignedBy;
  1322. }
  1323. },
  1324. isTemplateCard() {
  1325. return this.type === 'template-card';
  1326. },
  1327. votePublic() {
  1328. if (this.vote) return this.vote.public;
  1329. return null;
  1330. },
  1331. voteAllowNonBoardMembers() {
  1332. if (this.vote) return this.vote.allowNonBoardMembers;
  1333. return null;
  1334. },
  1335. voteCountNegative() {
  1336. if (this.vote && this.vote.negative) return this.vote.negative.length;
  1337. return null;
  1338. },
  1339. voteCountPositive() {
  1340. if (this.vote && this.vote.positive) return this.vote.positive.length;
  1341. return null;
  1342. },
  1343. voteCount() {
  1344. return this.voteCountPositive() + this.voteCountNegative();
  1345. },
  1346. });
  1347. Cards.mutations({
  1348. applyToChildren(funct) {
  1349. Cards.find({
  1350. parentId: this._id,
  1351. }).forEach(card => {
  1352. funct(card);
  1353. });
  1354. },
  1355. archive() {
  1356. this.applyToChildren(card => {
  1357. return card.archive();
  1358. });
  1359. return {
  1360. $set: {
  1361. archived: true,
  1362. archivedAt: new Date(),
  1363. },
  1364. };
  1365. },
  1366. restore() {
  1367. this.applyToChildren(card => {
  1368. return card.restore();
  1369. });
  1370. return {
  1371. $set: {
  1372. archived: false,
  1373. },
  1374. };
  1375. },
  1376. moveToEndOfList({ listId } = {}) {
  1377. let swimlaneId = this.swimlaneId;
  1378. const boardId = this.boardId;
  1379. let sortIndex = 0;
  1380. // This should never happen, but there was a bug that was fixed in commit
  1381. // ea0239538a68e225c867411a4f3e0d27c158383.
  1382. if (!swimlaneId) {
  1383. const board = Boards.findOne(boardId);
  1384. swimlaneId = board.getDefaultSwimline()._id;
  1385. }
  1386. // Move the minicard to the end of the target list
  1387. let parentElementDom = $(`#swimlane-${this.swimlaneId}`).get(0);
  1388. if (!parentElementDom) parentElementDom = $(':root');
  1389. const lastCardDom = $(parentElementDom)
  1390. .find(`#js-list-${listId} .js-minicard:last`)
  1391. .get(0);
  1392. if (lastCardDom) sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  1393. return this.moveOptionalArgs({
  1394. boardId,
  1395. swimlaneId,
  1396. listId,
  1397. sort: sortIndex,
  1398. });
  1399. },
  1400. moveOptionalArgs({ boardId, swimlaneId, listId, sort } = {}) {
  1401. boardId = boardId || this.boardId;
  1402. swimlaneId = swimlaneId || this.swimlaneId;
  1403. // This should never happen, but there was a bug that was fixed in commit
  1404. // ea0239538a68e225c867411a4f3e0d27c158383.
  1405. if (!swimlaneId) {
  1406. const board = Boards.findOne(boardId);
  1407. swimlaneId = board.getDefaultSwimline()._id;
  1408. }
  1409. listId = listId || this.listId;
  1410. if (sort === undefined || sort === null) sort = this.sort;
  1411. return this.move(boardId, swimlaneId, listId, sort);
  1412. },
  1413. move(boardId, swimlaneId, listId, sort = null) {
  1414. const mutatedFields = {
  1415. boardId,
  1416. swimlaneId,
  1417. listId,
  1418. };
  1419. if (sort !== null) {
  1420. mutatedFields.sort = sort;
  1421. }
  1422. // we must only copy the labels and custom fields if the target board
  1423. // differs from the source board
  1424. if (this.boardId !== boardId) {
  1425. // Get label names
  1426. const oldBoard = Boards.findOne(this.boardId);
  1427. const oldBoardLabels = oldBoard.labels;
  1428. const oldCardLabels = _.pluck(
  1429. _.filter(oldBoardLabels, label => {
  1430. return _.contains(this.labelIds, label._id);
  1431. }),
  1432. 'name',
  1433. );
  1434. const newBoard = Boards.findOne(boardId);
  1435. const newBoardLabels = newBoard.labels;
  1436. const newCardLabelIds = _.pluck(
  1437. _.filter(newBoardLabels, label => {
  1438. return label.name && _.contains(oldCardLabels, label.name);
  1439. }),
  1440. '_id',
  1441. );
  1442. Object.assign(mutatedFields, {
  1443. labelIds: newCardLabelIds,
  1444. });
  1445. mutatedFields.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  1446. }
  1447. Cards.update(this._id, {
  1448. $set: mutatedFields,
  1449. });
  1450. },
  1451. addLabel(labelId) {
  1452. return {
  1453. $addToSet: {
  1454. labelIds: labelId,
  1455. },
  1456. };
  1457. },
  1458. removeLabel(labelId) {
  1459. return {
  1460. $pull: {
  1461. labelIds: labelId,
  1462. },
  1463. };
  1464. },
  1465. toggleLabel(labelId) {
  1466. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1467. return this.removeLabel(labelId);
  1468. } else {
  1469. return this.addLabel(labelId);
  1470. }
  1471. },
  1472. setColor(newColor) {
  1473. if (newColor === 'white') {
  1474. newColor = null;
  1475. }
  1476. return {
  1477. $set: {
  1478. color: newColor,
  1479. },
  1480. };
  1481. },
  1482. assignMember(memberId) {
  1483. return {
  1484. $addToSet: {
  1485. members: memberId,
  1486. },
  1487. };
  1488. },
  1489. assignAssignee(assigneeId) {
  1490. // If there is not any assignee, allow one assignee, not more.
  1491. /*
  1492. if (this.getAssignees().length === 0) {
  1493. return {
  1494. $addToSet: {
  1495. assignees: assigneeId,
  1496. },
  1497. };
  1498. */
  1499. // Allow more that one assignee:
  1500. // https://github.com/wekan/wekan/issues/3302
  1501. return {
  1502. $addToSet: {
  1503. assignees: assigneeId,
  1504. },
  1505. };
  1506. //} else {
  1507. // return false,
  1508. //}
  1509. },
  1510. unassignMember(memberId) {
  1511. return {
  1512. $pull: {
  1513. members: memberId,
  1514. },
  1515. };
  1516. },
  1517. unassignAssignee(assigneeId) {
  1518. return {
  1519. $pull: {
  1520. assignees: assigneeId,
  1521. },
  1522. };
  1523. },
  1524. toggleMember(memberId) {
  1525. if (this.members && this.members.indexOf(memberId) > -1) {
  1526. return this.unassignMember(memberId);
  1527. } else {
  1528. return this.assignMember(memberId);
  1529. }
  1530. },
  1531. toggleAssignee(assigneeId) {
  1532. if (this.assignees && this.assignees.indexOf(assigneeId) > -1) {
  1533. return this.unassignAssignee(assigneeId);
  1534. } else {
  1535. return this.assignAssignee(assigneeId);
  1536. }
  1537. },
  1538. assignCustomField(customFieldId) {
  1539. return {
  1540. $addToSet: {
  1541. customFields: {
  1542. _id: customFieldId,
  1543. value: null,
  1544. },
  1545. },
  1546. };
  1547. },
  1548. unassignCustomField(customFieldId) {
  1549. return {
  1550. $pull: {
  1551. customFields: {
  1552. _id: customFieldId,
  1553. },
  1554. },
  1555. };
  1556. },
  1557. toggleCustomField(customFieldId) {
  1558. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1559. return this.unassignCustomField(customFieldId);
  1560. } else {
  1561. return this.assignCustomField(customFieldId);
  1562. }
  1563. },
  1564. setCustomField(customFieldId, value) {
  1565. // todo
  1566. const index = this.customFieldIndex(customFieldId);
  1567. if (index > -1) {
  1568. const update = {
  1569. $set: {},
  1570. };
  1571. update.$set[`customFields.${index}.value`] = value;
  1572. return update;
  1573. }
  1574. // TODO
  1575. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1576. return null;
  1577. },
  1578. setCover(coverId) {
  1579. return {
  1580. $set: {
  1581. coverId,
  1582. },
  1583. };
  1584. },
  1585. unsetCover() {
  1586. return {
  1587. $unset: {
  1588. coverId: '',
  1589. },
  1590. };
  1591. },
  1592. setReceived(receivedAt) {
  1593. return {
  1594. $set: {
  1595. receivedAt,
  1596. },
  1597. };
  1598. },
  1599. unsetReceived() {
  1600. return {
  1601. $unset: {
  1602. receivedAt: '',
  1603. },
  1604. };
  1605. },
  1606. setStart(startAt) {
  1607. return {
  1608. $set: {
  1609. startAt,
  1610. },
  1611. };
  1612. },
  1613. unsetStart() {
  1614. return {
  1615. $unset: {
  1616. startAt: '',
  1617. },
  1618. };
  1619. },
  1620. setDue(dueAt) {
  1621. return {
  1622. $set: {
  1623. dueAt,
  1624. },
  1625. };
  1626. },
  1627. unsetDue() {
  1628. return {
  1629. $unset: {
  1630. dueAt: '',
  1631. },
  1632. };
  1633. },
  1634. setEnd(endAt) {
  1635. return {
  1636. $set: {
  1637. endAt,
  1638. },
  1639. };
  1640. },
  1641. unsetEnd() {
  1642. return {
  1643. $unset: {
  1644. endAt: '',
  1645. },
  1646. };
  1647. },
  1648. setOvertime(isOvertime) {
  1649. return {
  1650. $set: {
  1651. isOvertime,
  1652. },
  1653. };
  1654. },
  1655. setSpentTime(spentTime) {
  1656. return {
  1657. $set: {
  1658. spentTime,
  1659. },
  1660. };
  1661. },
  1662. unsetSpentTime() {
  1663. return {
  1664. $unset: {
  1665. spentTime: '',
  1666. isOvertime: false,
  1667. },
  1668. };
  1669. },
  1670. setParentId(parentId) {
  1671. return {
  1672. $set: {
  1673. parentId,
  1674. },
  1675. };
  1676. },
  1677. setVoteQuestion(question, publicVote, allowNonBoardMembers) {
  1678. return {
  1679. $set: {
  1680. vote: {
  1681. question,
  1682. public: publicVote,
  1683. allowNonBoardMembers,
  1684. positive: [],
  1685. negative: [],
  1686. },
  1687. },
  1688. };
  1689. },
  1690. unsetVote() {
  1691. return {
  1692. $unset: {
  1693. vote: '',
  1694. },
  1695. };
  1696. },
  1697. setVoteEnd(end) {
  1698. return {
  1699. $set: { 'vote.end': end },
  1700. };
  1701. },
  1702. unsetVoteEnd() {
  1703. return {
  1704. $unset: { 'vote.end': '' },
  1705. };
  1706. },
  1707. setVote(userId, forIt) {
  1708. switch (forIt) {
  1709. case true:
  1710. // vote for it
  1711. return {
  1712. $pull: {
  1713. 'vote.negative': userId,
  1714. },
  1715. $addToSet: {
  1716. 'vote.positive': userId,
  1717. },
  1718. };
  1719. case false:
  1720. // vote against
  1721. return {
  1722. $pull: {
  1723. 'vote.positive': userId,
  1724. },
  1725. $addToSet: {
  1726. 'vote.negative': userId,
  1727. },
  1728. };
  1729. default:
  1730. // Remove votes
  1731. return {
  1732. $pull: {
  1733. 'vote.positive': userId,
  1734. 'vote.negative': userId,
  1735. },
  1736. };
  1737. }
  1738. },
  1739. });
  1740. //FUNCTIONS FOR creation of Activities
  1741. function updateActivities(doc, fieldNames, modifier) {
  1742. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  1743. Activities.find({
  1744. activityType: 'addedLabel',
  1745. cardId: doc._id,
  1746. }).forEach(a => {
  1747. const lidx = doc.labelIds.indexOf(a.labelId);
  1748. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  1749. Activities.update(a._id, {
  1750. $set: {
  1751. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  1752. boardId: modifier.$set.boardId,
  1753. },
  1754. });
  1755. } else {
  1756. Activities.remove(a._id);
  1757. }
  1758. });
  1759. } else if (_.contains(fieldNames, 'boardId')) {
  1760. Activities.remove({
  1761. activityType: 'addedLabel',
  1762. cardId: doc._id,
  1763. });
  1764. }
  1765. }
  1766. function cardMove(
  1767. userId,
  1768. doc,
  1769. fieldNames,
  1770. oldListId,
  1771. oldSwimlaneId,
  1772. oldBoardId,
  1773. ) {
  1774. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  1775. Activities.insert({
  1776. userId,
  1777. activityType: 'moveCardBoard',
  1778. boardName: Boards.findOne(doc.boardId).title,
  1779. boardId: doc.boardId,
  1780. oldBoardId,
  1781. oldBoardName: Boards.findOne(oldBoardId).title,
  1782. cardId: doc._id,
  1783. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1784. swimlaneId: doc.swimlaneId,
  1785. oldSwimlaneId,
  1786. });
  1787. } else if (
  1788. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1789. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  1790. ) {
  1791. Activities.insert({
  1792. userId,
  1793. oldListId,
  1794. activityType: 'moveCard',
  1795. listName: Lists.findOne(doc.listId).title,
  1796. listId: doc.listId,
  1797. boardId: doc.boardId,
  1798. cardId: doc._id,
  1799. cardTitle: doc.title,
  1800. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1801. swimlaneId: doc.swimlaneId,
  1802. oldSwimlaneId,
  1803. });
  1804. }
  1805. }
  1806. function cardState(userId, doc, fieldNames) {
  1807. if (_.contains(fieldNames, 'archived')) {
  1808. if (doc.archived) {
  1809. Activities.insert({
  1810. userId,
  1811. activityType: 'archivedCard',
  1812. listName: Lists.findOne(doc.listId).title,
  1813. boardId: doc.boardId,
  1814. listId: doc.listId,
  1815. cardId: doc._id,
  1816. swimlaneId: doc.swimlaneId,
  1817. });
  1818. } else {
  1819. Activities.insert({
  1820. userId,
  1821. activityType: 'restoredCard',
  1822. boardId: doc.boardId,
  1823. listName: Lists.findOne(doc.listId).title,
  1824. listId: doc.listId,
  1825. cardId: doc._id,
  1826. swimlaneId: doc.swimlaneId,
  1827. });
  1828. }
  1829. }
  1830. }
  1831. function cardMembers(userId, doc, fieldNames, modifier) {
  1832. if (!_.contains(fieldNames, 'members')) return;
  1833. let memberId;
  1834. // Say hello to the new member
  1835. if (modifier.$addToSet && modifier.$addToSet.members) {
  1836. memberId = modifier.$addToSet.members;
  1837. const username = Users.findOne(memberId).username;
  1838. if (!_.contains(doc.members, memberId)) {
  1839. Activities.insert({
  1840. userId,
  1841. username,
  1842. activityType: 'joinMember',
  1843. boardId: doc.boardId,
  1844. cardId: doc._id,
  1845. memberId,
  1846. listId: doc.listId,
  1847. swimlaneId: doc.swimlaneId,
  1848. });
  1849. }
  1850. }
  1851. // Say goodbye to the former member
  1852. if (modifier.$pull && modifier.$pull.members) {
  1853. memberId = modifier.$pull.members;
  1854. const username = Users.findOne(memberId).username;
  1855. // Check that the former member is member of the card
  1856. if (_.contains(doc.members, memberId)) {
  1857. Activities.insert({
  1858. userId,
  1859. username,
  1860. activityType: 'unjoinMember',
  1861. boardId: doc.boardId,
  1862. cardId: doc._id,
  1863. memberId,
  1864. listId: doc.listId,
  1865. swimlaneId: doc.swimlaneId,
  1866. });
  1867. }
  1868. }
  1869. }
  1870. function cardAssignees(userId, doc, fieldNames, modifier) {
  1871. if (!_.contains(fieldNames, 'assignees')) return;
  1872. let assigneeId;
  1873. // Say hello to the new assignee
  1874. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  1875. assigneeId = modifier.$addToSet.assignees;
  1876. const username = Users.findOne(assigneeId).username;
  1877. if (!_.contains(doc.assignees, assigneeId)) {
  1878. Activities.insert({
  1879. userId,
  1880. username,
  1881. activityType: 'joinAssignee',
  1882. boardId: doc.boardId,
  1883. cardId: doc._id,
  1884. assigneeId,
  1885. listId: doc.listId,
  1886. swimlaneId: doc.swimlaneId,
  1887. });
  1888. }
  1889. }
  1890. // Say goodbye to the former assignee
  1891. if (modifier.$pull && modifier.$pull.assignees) {
  1892. assigneeId = modifier.$pull.assignees;
  1893. const username = Users.findOne(assigneeId).username;
  1894. // Check that the former assignee is assignee of the card
  1895. if (_.contains(doc.assignees, assigneeId)) {
  1896. Activities.insert({
  1897. userId,
  1898. username,
  1899. activityType: 'unjoinAssignee',
  1900. boardId: doc.boardId,
  1901. cardId: doc._id,
  1902. assigneeId,
  1903. listId: doc.listId,
  1904. swimlaneId: doc.swimlaneId,
  1905. });
  1906. }
  1907. }
  1908. }
  1909. function cardLabels(userId, doc, fieldNames, modifier) {
  1910. if (!_.contains(fieldNames, 'labelIds')) return;
  1911. let labelId;
  1912. // Say hello to the new label
  1913. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1914. labelId = modifier.$addToSet.labelIds;
  1915. if (!_.contains(doc.labelIds, labelId)) {
  1916. const act = {
  1917. userId,
  1918. labelId,
  1919. activityType: 'addedLabel',
  1920. boardId: doc.boardId,
  1921. cardId: doc._id,
  1922. listId: doc.listId,
  1923. swimlaneId: doc.swimlaneId,
  1924. };
  1925. Activities.insert(act);
  1926. }
  1927. }
  1928. // Say goodbye to the label
  1929. if (modifier.$pull && modifier.$pull.labelIds) {
  1930. labelId = modifier.$pull.labelIds;
  1931. // Check that the former member is member of the card
  1932. if (_.contains(doc.labelIds, labelId)) {
  1933. Activities.insert({
  1934. userId,
  1935. labelId,
  1936. activityType: 'removedLabel',
  1937. boardId: doc.boardId,
  1938. cardId: doc._id,
  1939. listId: doc.listId,
  1940. swimlaneId: doc.swimlaneId,
  1941. });
  1942. }
  1943. }
  1944. }
  1945. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1946. if (!_.contains(fieldNames, 'customFields')) return;
  1947. // Say hello to the new customField value
  1948. if (modifier.$set) {
  1949. _.each(modifier.$set, (value, key) => {
  1950. if (key.startsWith('customFields')) {
  1951. const dotNotation = key.split('.');
  1952. // only individual changes are registered
  1953. if (dotNotation.length > 1) {
  1954. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1955. const act = {
  1956. userId,
  1957. customFieldId,
  1958. value,
  1959. activityType: 'setCustomField',
  1960. boardId: doc.boardId,
  1961. cardId: doc._id,
  1962. listId: doc.listId,
  1963. swimlaneId: doc.swimlaneId,
  1964. };
  1965. Activities.insert(act);
  1966. }
  1967. }
  1968. });
  1969. }
  1970. // Say goodbye to the former customField value
  1971. if (modifier.$unset) {
  1972. _.each(modifier.$unset, (value, key) => {
  1973. if (key.startsWith('customFields')) {
  1974. const dotNotation = key.split('.');
  1975. // only individual changes are registered
  1976. if (dotNotation.length > 1) {
  1977. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1978. const act = {
  1979. userId,
  1980. customFieldId,
  1981. activityType: 'unsetCustomField',
  1982. boardId: doc.boardId,
  1983. cardId: doc._id,
  1984. };
  1985. Activities.insert(act);
  1986. }
  1987. }
  1988. });
  1989. }
  1990. }
  1991. function cardCreation(userId, doc) {
  1992. Activities.insert({
  1993. userId,
  1994. activityType: 'createCard',
  1995. boardId: doc.boardId,
  1996. listName: Lists.findOne(doc.listId).title,
  1997. listId: doc.listId,
  1998. cardId: doc._id,
  1999. cardTitle: doc.title,
  2000. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2001. swimlaneId: doc.swimlaneId,
  2002. });
  2003. }
  2004. function cardRemover(userId, doc) {
  2005. ChecklistItems.remove({
  2006. cardId: doc._id,
  2007. });
  2008. Checklists.remove({
  2009. cardId: doc._id,
  2010. });
  2011. Cards.remove({
  2012. parentId: doc._id,
  2013. });
  2014. CardComments.remove({
  2015. cardId: doc._id,
  2016. });
  2017. Attachments.remove({
  2018. cardId: doc._id,
  2019. });
  2020. }
  2021. const findDueCards = days => {
  2022. const seekDue = ($from, $to, activityType) => {
  2023. Cards.find({
  2024. archived: false,
  2025. dueAt: { $gte: $from, $lt: $to },
  2026. }).forEach(card => {
  2027. const username = Users.findOne(card.userId).username;
  2028. const activity = {
  2029. userId: card.userId,
  2030. username,
  2031. activityType,
  2032. boardId: card.boardId,
  2033. cardId: card._id,
  2034. cardTitle: card.title,
  2035. listId: card.listId,
  2036. timeValue: card.dueAt,
  2037. swimlaneId: card.swimlaneId,
  2038. };
  2039. Activities.insert(activity);
  2040. });
  2041. };
  2042. const now = new Date(),
  2043. aday = 3600 * 24 * 1e3,
  2044. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  2045. if (!days) return;
  2046. if (!days.map) days = [days];
  2047. days.map(day => {
  2048. let args = [];
  2049. if (day === 0) {
  2050. args = [then(0), then(1), 'duenow'];
  2051. } else if (day > 0) {
  2052. args = [then(1), then(day), 'almostdue'];
  2053. } else {
  2054. args = [then(day), now, 'pastdue'];
  2055. }
  2056. seekDue(...args);
  2057. });
  2058. };
  2059. const addCronJob = _.debounce(
  2060. Meteor.bindEnvironment(function findDueCardsDebounced() {
  2061. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  2062. if (!envValue) {
  2063. return;
  2064. }
  2065. const notifydays = envValue
  2066. .split(',')
  2067. .map(value => {
  2068. const iValue = parseInt(value, 10);
  2069. if (!(iValue > 0 && iValue < 15)) {
  2070. // notifying due is disabled
  2071. return false;
  2072. } else {
  2073. return iValue;
  2074. }
  2075. })
  2076. .filter(Boolean);
  2077. const notifyitvl = process.env.NOTIFY_DUE_AT_HOUR_OF_DAY; //passed in the itvl has to be a number standing for the hour of current time
  2078. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  2079. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  2080. const scheduler = (job => () => {
  2081. const now = new Date();
  2082. const hour = 3600 * 1e3;
  2083. if (now.getHours() === itvl) {
  2084. if (typeof job === 'function') {
  2085. job();
  2086. }
  2087. }
  2088. Meteor.setTimeout(scheduler, hour);
  2089. })(() => {
  2090. findDueCards(notifydays);
  2091. });
  2092. scheduler();
  2093. }),
  2094. 500,
  2095. );
  2096. if (Meteor.isServer) {
  2097. // Cards are often fetched within a board, so we create an index to make these
  2098. // queries more efficient.
  2099. Meteor.startup(() => {
  2100. Cards._collection._ensureIndex({ modifiedAt: -1 });
  2101. Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  2102. // https://github.com/wekan/wekan/issues/1863
  2103. // Swimlane added a new field in the cards collection of mongodb named parentId.
  2104. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  2105. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  2106. // To correct it, add Index to parentId:
  2107. Cards._collection._ensureIndex({ parentId: 1 });
  2108. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  2109. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  2110. // Meteor.call("findDueCards",notifydays,notifyitvl);
  2111. Meteor.defer(() => {
  2112. addCronJob();
  2113. });
  2114. });
  2115. Cards.after.insert((userId, doc) => {
  2116. cardCreation(userId, doc);
  2117. });
  2118. // New activity for card (un)archivage
  2119. Cards.after.update((userId, doc, fieldNames) => {
  2120. cardState(userId, doc, fieldNames);
  2121. });
  2122. //New activity for card moves
  2123. Cards.after.update(function(userId, doc, fieldNames) {
  2124. const oldListId = this.previous.listId;
  2125. const oldSwimlaneId = this.previous.swimlaneId;
  2126. const oldBoardId = this.previous.boardId;
  2127. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  2128. });
  2129. // Add a new activity if we add or remove a member to the card
  2130. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2131. cardMembers(userId, doc, fieldNames, modifier);
  2132. updateActivities(doc, fieldNames, modifier);
  2133. });
  2134. // Add a new activity if we add or remove a assignee to the card
  2135. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2136. cardAssignees(userId, doc, fieldNames, modifier);
  2137. updateActivities(doc, fieldNames, modifier);
  2138. });
  2139. // Add a new activity if we add or remove a label to the card
  2140. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2141. cardLabels(userId, doc, fieldNames, modifier);
  2142. });
  2143. // Add a new activity if we edit a custom field
  2144. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2145. cardCustomFields(userId, doc, fieldNames, modifier);
  2146. });
  2147. // Add a new activity if modify time related field like dueAt startAt etc
  2148. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2149. const dla = 'dateLastActivity';
  2150. const fields = fieldNames.filter(name => name !== dla);
  2151. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  2152. const action = fields[0];
  2153. if (fields.length > 0 && _.contains(timingaction, action)) {
  2154. // add activities for user change these attributes
  2155. const value = modifier.$set[action];
  2156. const oldvalue = doc[action] || '';
  2157. const activityType = `a-${action}`;
  2158. const card = Cards.findOne(doc._id);
  2159. const list = card.list();
  2160. if (list) {
  2161. // change list modifiedAt, when user modified the key values in
  2162. // timingaction array, if it's endAt, put the modifiedAt of list
  2163. // back to one year ago for sorting purpose
  2164. const modifiedAt = moment()
  2165. .subtract(1, 'year')
  2166. .toISOString();
  2167. const boardId = list.boardId;
  2168. Lists.direct.update(
  2169. {
  2170. _id: list._id,
  2171. },
  2172. {
  2173. $set: {
  2174. modifiedAt,
  2175. boardId,
  2176. },
  2177. },
  2178. );
  2179. }
  2180. const username = Users.findOne(userId).username;
  2181. const activity = {
  2182. userId,
  2183. username,
  2184. activityType,
  2185. boardId: doc.boardId,
  2186. cardId: doc._id,
  2187. cardTitle: doc.title,
  2188. timeKey: action,
  2189. timeValue: value,
  2190. timeOldValue: oldvalue,
  2191. listId: card.listId,
  2192. swimlaneId: card.swimlaneId,
  2193. };
  2194. Activities.insert(activity);
  2195. }
  2196. });
  2197. // Remove all activities associated with a card if we remove the card
  2198. // Remove also card_comments / checklists / attachments
  2199. Cards.before.remove((userId, doc) => {
  2200. cardRemover(userId, doc);
  2201. });
  2202. }
  2203. //SWIMLANES REST API
  2204. if (Meteor.isServer) {
  2205. /**
  2206. * @operation get_swimlane_cards
  2207. * @summary get all cards attached to a swimlane
  2208. *
  2209. * @param {string} boardId the board ID
  2210. * @param {string} swimlaneId the swimlane ID
  2211. * @return_type [{_id: string,
  2212. * title: string,
  2213. * description: string,
  2214. * listId: string}]
  2215. */
  2216. JsonRoutes.add(
  2217. 'GET',
  2218. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  2219. function(req, res) {
  2220. const paramBoardId = req.params.boardId;
  2221. const paramSwimlaneId = req.params.swimlaneId;
  2222. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2223. JsonRoutes.sendResult(res, {
  2224. code: 200,
  2225. data: Cards.find({
  2226. boardId: paramBoardId,
  2227. swimlaneId: paramSwimlaneId,
  2228. archived: false,
  2229. }).map(function(doc) {
  2230. return {
  2231. _id: doc._id,
  2232. title: doc.title,
  2233. description: doc.description,
  2234. listId: doc.listId,
  2235. receivedAt: doc.receivedAt,
  2236. startAt: doc.startAt,
  2237. dueAt: doc.dueAt,
  2238. endAt: doc.endAt,
  2239. assignees: doc.assignees,
  2240. };
  2241. }),
  2242. });
  2243. },
  2244. );
  2245. }
  2246. //LISTS REST API
  2247. if (Meteor.isServer) {
  2248. /**
  2249. * @operation get_all_cards
  2250. * @summary Get all Cards attached to a List
  2251. *
  2252. * @param {string} boardId the board ID
  2253. * @param {string} listId the list ID
  2254. * @return_type [{_id: string,
  2255. * title: string,
  2256. * description: string}]
  2257. */
  2258. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  2259. req,
  2260. res,
  2261. ) {
  2262. const paramBoardId = req.params.boardId;
  2263. const paramListId = req.params.listId;
  2264. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2265. JsonRoutes.sendResult(res, {
  2266. code: 200,
  2267. data: Cards.find({
  2268. boardId: paramBoardId,
  2269. listId: paramListId,
  2270. archived: false,
  2271. }).map(function(doc) {
  2272. return {
  2273. _id: doc._id,
  2274. title: doc.title,
  2275. description: doc.description,
  2276. receivedAt: doc.receivedAt,
  2277. startAt: doc.startAt,
  2278. dueAt: doc.dueAt,
  2279. endAt: doc.endAt,
  2280. assignees: doc.assignees,
  2281. };
  2282. }),
  2283. });
  2284. });
  2285. /**
  2286. * @operation get_card
  2287. * @summary Get a Card
  2288. *
  2289. * @param {string} boardId the board ID
  2290. * @param {string} listId the list ID of the card
  2291. * @param {string} cardId the card ID
  2292. * @return_type Cards
  2293. */
  2294. JsonRoutes.add(
  2295. 'GET',
  2296. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2297. function(req, res) {
  2298. const paramBoardId = req.params.boardId;
  2299. const paramListId = req.params.listId;
  2300. const paramCardId = req.params.cardId;
  2301. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2302. JsonRoutes.sendResult(res, {
  2303. code: 200,
  2304. data: Cards.findOne({
  2305. _id: paramCardId,
  2306. listId: paramListId,
  2307. boardId: paramBoardId,
  2308. archived: false,
  2309. }),
  2310. });
  2311. },
  2312. );
  2313. /**
  2314. * @operation new_card
  2315. * @summary Create a new Card
  2316. *
  2317. * @param {string} boardId the board ID of the new card
  2318. * @param {string} listId the list ID of the new card
  2319. * @param {string} authorID the user ID of the person owning the card
  2320. * @param {string} parentId the parent ID of the new card
  2321. * @param {string} title the title of the new card
  2322. * @param {string} description the description of the new card
  2323. * @param {string} swimlaneId the swimlane ID of the new card
  2324. * @param {string} [members] the member IDs list of the new card
  2325. * @param {string} [assignees] the array of maximum one ID of assignee of the new card
  2326. * @return_type {_id: string}
  2327. */
  2328. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  2329. req,
  2330. res,
  2331. ) {
  2332. // Check user is logged in
  2333. Authentication.checkLoggedIn(req.userId);
  2334. const paramBoardId = req.params.boardId;
  2335. // Check user has permission to add card to the board
  2336. const board = Boards.findOne({
  2337. _id: paramBoardId,
  2338. });
  2339. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  2340. Authentication.checkAdminOrCondition(req.userId, addPermission);
  2341. const paramListId = req.params.listId;
  2342. const paramParentId = req.params.parentId;
  2343. const currentCards = Cards.find(
  2344. {
  2345. listId: paramListId,
  2346. archived: false,
  2347. },
  2348. { sort: ['sort'] },
  2349. );
  2350. const check = Users.findOne({
  2351. _id: req.body.authorId,
  2352. });
  2353. const members = req.body.members;
  2354. const assignees = req.body.assignees;
  2355. if (typeof check !== 'undefined') {
  2356. const id = Cards.direct.insert({
  2357. title: req.body.title,
  2358. boardId: paramBoardId,
  2359. listId: paramListId,
  2360. parentId: paramParentId,
  2361. description: req.body.description,
  2362. userId: req.body.authorId,
  2363. swimlaneId: req.body.swimlaneId,
  2364. sort: currentCards.count(),
  2365. members,
  2366. assignees,
  2367. });
  2368. JsonRoutes.sendResult(res, {
  2369. code: 200,
  2370. data: {
  2371. _id: id,
  2372. },
  2373. });
  2374. const card = Cards.findOne({
  2375. _id: id,
  2376. });
  2377. cardCreation(req.body.authorId, card);
  2378. } else {
  2379. JsonRoutes.sendResult(res, {
  2380. code: 401,
  2381. });
  2382. }
  2383. });
  2384. /*
  2385. * Note for the JSDoc:
  2386. * 'list' will be interpreted as the path parameter
  2387. * 'listID' will be interpreted as the body parameter
  2388. */
  2389. /**
  2390. * @operation edit_card
  2391. * @summary Edit Fields in a Card
  2392. *
  2393. * @description Edit a card
  2394. *
  2395. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  2396. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  2397. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  2398. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  2399. * `indigo`:
  2400. *
  2401. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  2402. *
  2403. * Note: setting the color to white has the same effect than removing it.
  2404. *
  2405. * @param {string} boardId the board ID of the card
  2406. * @param {string} list the list ID of the card
  2407. * @param {string} cardId the ID of the card
  2408. * @param {string} [title] the new title of the card
  2409. * @param {string} [sort] the new sort value of the card
  2410. * @param {string} [listId] the new list ID of the card (move operation)
  2411. * @param {string} [description] the new description of the card
  2412. * @param {string} [authorId] change the owner of the card
  2413. * @param {string} [parentId] change the parent of the card
  2414. * @param {string} [labelIds] the new list of label IDs attached to the card
  2415. * @param {string} [swimlaneId] the new swimlane ID of the card
  2416. * @param {string} [members] the new list of member IDs attached to the card
  2417. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  2418. * @param {string} [requestedBy] the new requestedBy field of the card
  2419. * @param {string} [assignedBy] the new assignedBy field of the card
  2420. * @param {string} [receivedAt] the new receivedAt field of the card
  2421. * @param {string} [assignBy] the new assignBy field of the card
  2422. * @param {string} [startAt] the new startAt field of the card
  2423. * @param {string} [dueAt] the new dueAt field of the card
  2424. * @param {string} [endAt] the new endAt field of the card
  2425. * @param {string} [spentTime] the new spentTime field of the card
  2426. * @param {boolean} [isOverTime] the new isOverTime field of the card
  2427. * @param {string} [customFields] the new customFields value of the card
  2428. * @param {string} [color] the new color of the card
  2429. * @param {Object} [vote] the vote object
  2430. * @param {string} vote.question the vote question
  2431. * @param {boolean} vote.public show who voted what
  2432. * @param {boolean} vote.allowNonBoardMembers allow all logged in users to vote?
  2433. * @return_type {_id: string}
  2434. */
  2435. JsonRoutes.add(
  2436. 'PUT',
  2437. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2438. function(req, res) {
  2439. Authentication.checkUserId(req.userId);
  2440. const paramBoardId = req.params.boardId;
  2441. const paramCardId = req.params.cardId;
  2442. const paramListId = req.params.listId;
  2443. if (req.body.hasOwnProperty('title')) {
  2444. const newTitle = req.body.title;
  2445. Cards.direct.update(
  2446. {
  2447. _id: paramCardId,
  2448. listId: paramListId,
  2449. boardId: paramBoardId,
  2450. archived: false,
  2451. },
  2452. {
  2453. $set: {
  2454. title: newTitle,
  2455. },
  2456. },
  2457. );
  2458. }
  2459. if (req.body.hasOwnProperty('sort')) {
  2460. const newSort = req.body.sort;
  2461. Cards.direct.update(
  2462. {
  2463. _id: paramCardId,
  2464. listId: paramListId,
  2465. boardId: paramBoardId,
  2466. archived: false,
  2467. },
  2468. {
  2469. $set: {
  2470. sort: newSort,
  2471. },
  2472. },
  2473. );
  2474. }
  2475. if (req.body.hasOwnProperty('parentId')) {
  2476. const newParentId = req.body.parentId;
  2477. Cards.direct.update(
  2478. {
  2479. _id: paramCardId,
  2480. listId: paramListId,
  2481. boardId: paramBoardId,
  2482. archived: false,
  2483. },
  2484. {
  2485. $set: {
  2486. parentId: newParentId,
  2487. },
  2488. },
  2489. );
  2490. }
  2491. if (req.body.hasOwnProperty('description')) {
  2492. const newDescription = req.body.description;
  2493. Cards.direct.update(
  2494. {
  2495. _id: paramCardId,
  2496. listId: paramListId,
  2497. boardId: paramBoardId,
  2498. archived: false,
  2499. },
  2500. {
  2501. $set: {
  2502. description: newDescription,
  2503. },
  2504. },
  2505. );
  2506. }
  2507. if (req.body.hasOwnProperty('color')) {
  2508. const newColor = req.body.color;
  2509. Cards.direct.update(
  2510. {
  2511. _id: paramCardId,
  2512. listId: paramListId,
  2513. boardId: paramBoardId,
  2514. archived: false,
  2515. },
  2516. { $set: { color: newColor } },
  2517. );
  2518. }
  2519. if (req.body.hasOwnProperty('vote')) {
  2520. const newVote = req.body.vote;
  2521. newVote.positive = [];
  2522. newVote.negative = [];
  2523. if (!newVote.hasOwnProperty('public')) newVote.public = false;
  2524. if (!newVote.hasOwnProperty('allowNonBoardMembers'))
  2525. newVote.allowNonBoardMembers = false;
  2526. Cards.direct.update(
  2527. {
  2528. _id: paramCardId,
  2529. listId: paramListId,
  2530. boardId: paramBoardId,
  2531. archived: false,
  2532. },
  2533. { $set: { vote: newVote } },
  2534. );
  2535. }
  2536. if (req.body.hasOwnProperty('labelIds')) {
  2537. let newlabelIds = req.body.labelIds;
  2538. if (_.isString(newlabelIds)) {
  2539. if (newlabelIds === '') {
  2540. newlabelIds = null;
  2541. } else {
  2542. newlabelIds = [newlabelIds];
  2543. }
  2544. }
  2545. Cards.direct.update(
  2546. {
  2547. _id: paramCardId,
  2548. listId: paramListId,
  2549. boardId: paramBoardId,
  2550. archived: false,
  2551. },
  2552. {
  2553. $set: {
  2554. labelIds: newlabelIds,
  2555. },
  2556. },
  2557. );
  2558. }
  2559. if (req.body.hasOwnProperty('requestedBy')) {
  2560. const newrequestedBy = req.body.requestedBy;
  2561. Cards.direct.update(
  2562. {
  2563. _id: paramCardId,
  2564. listId: paramListId,
  2565. boardId: paramBoardId,
  2566. archived: false,
  2567. },
  2568. { $set: { requestedBy: newrequestedBy } },
  2569. );
  2570. }
  2571. if (req.body.hasOwnProperty('assignedBy')) {
  2572. const newassignedBy = req.body.assignedBy;
  2573. Cards.direct.update(
  2574. {
  2575. _id: paramCardId,
  2576. listId: paramListId,
  2577. boardId: paramBoardId,
  2578. archived: false,
  2579. },
  2580. { $set: { assignedBy: newassignedBy } },
  2581. );
  2582. }
  2583. if (req.body.hasOwnProperty('receivedAt')) {
  2584. const newreceivedAt = req.body.receivedAt;
  2585. Cards.direct.update(
  2586. {
  2587. _id: paramCardId,
  2588. listId: paramListId,
  2589. boardId: paramBoardId,
  2590. archived: false,
  2591. },
  2592. { $set: { receivedAt: newreceivedAt } },
  2593. );
  2594. }
  2595. if (req.body.hasOwnProperty('startAt')) {
  2596. const newstartAt = req.body.startAt;
  2597. Cards.direct.update(
  2598. {
  2599. _id: paramCardId,
  2600. listId: paramListId,
  2601. boardId: paramBoardId,
  2602. archived: false,
  2603. },
  2604. { $set: { startAt: newstartAt } },
  2605. );
  2606. }
  2607. if (req.body.hasOwnProperty('dueAt')) {
  2608. const newdueAt = req.body.dueAt;
  2609. Cards.direct.update(
  2610. {
  2611. _id: paramCardId,
  2612. listId: paramListId,
  2613. boardId: paramBoardId,
  2614. archived: false,
  2615. },
  2616. { $set: { dueAt: newdueAt } },
  2617. );
  2618. }
  2619. if (req.body.hasOwnProperty('endAt')) {
  2620. const newendAt = req.body.endAt;
  2621. Cards.direct.update(
  2622. {
  2623. _id: paramCardId,
  2624. listId: paramListId,
  2625. boardId: paramBoardId,
  2626. archived: false,
  2627. },
  2628. { $set: { endAt: newendAt } },
  2629. );
  2630. }
  2631. if (req.body.hasOwnProperty('spentTime')) {
  2632. const newspentTime = req.body.spentTime;
  2633. Cards.direct.update(
  2634. {
  2635. _id: paramCardId,
  2636. listId: paramListId,
  2637. boardId: paramBoardId,
  2638. archived: false,
  2639. },
  2640. { $set: { spentTime: newspentTime } },
  2641. );
  2642. }
  2643. if (req.body.hasOwnProperty('isOverTime')) {
  2644. const newisOverTime = req.body.isOverTime;
  2645. Cards.direct.update(
  2646. {
  2647. _id: paramCardId,
  2648. listId: paramListId,
  2649. boardId: paramBoardId,
  2650. archived: false,
  2651. },
  2652. { $set: { isOverTime: newisOverTime } },
  2653. );
  2654. }
  2655. if (req.body.hasOwnProperty('customFields')) {
  2656. const newcustomFields = req.body.customFields;
  2657. Cards.direct.update(
  2658. {
  2659. _id: paramCardId,
  2660. listId: paramListId,
  2661. boardId: paramBoardId,
  2662. archived: false,
  2663. },
  2664. { $set: { customFields: newcustomFields } },
  2665. );
  2666. }
  2667. if (req.body.hasOwnProperty('members')) {
  2668. let newmembers = req.body.members;
  2669. if (_.isString(newmembers)) {
  2670. if (newmembers === '') {
  2671. newmembers = null;
  2672. } else {
  2673. newmembers = [newmembers];
  2674. }
  2675. }
  2676. Cards.direct.update(
  2677. {
  2678. _id: paramCardId,
  2679. listId: paramListId,
  2680. boardId: paramBoardId,
  2681. archived: false,
  2682. },
  2683. { $set: { members: newmembers } },
  2684. );
  2685. }
  2686. if (req.body.hasOwnProperty('assignees')) {
  2687. let newassignees = req.body.assignees;
  2688. if (_.isString(newassignees)) {
  2689. if (newassignees === '') {
  2690. newassignees = null;
  2691. } else {
  2692. newassignees = [newassignees];
  2693. }
  2694. }
  2695. Cards.direct.update(
  2696. {
  2697. _id: paramCardId,
  2698. listId: paramListId,
  2699. boardId: paramBoardId,
  2700. archived: false,
  2701. },
  2702. { $set: { assignees: newassignees } },
  2703. );
  2704. }
  2705. if (req.body.hasOwnProperty('swimlaneId')) {
  2706. const newParamSwimlaneId = req.body.swimlaneId;
  2707. Cards.direct.update(
  2708. {
  2709. _id: paramCardId,
  2710. listId: paramListId,
  2711. boardId: paramBoardId,
  2712. archived: false,
  2713. },
  2714. { $set: { swimlaneId: newParamSwimlaneId } },
  2715. );
  2716. }
  2717. if (req.body.hasOwnProperty('listId')) {
  2718. const newParamListId = req.body.listId;
  2719. Cards.direct.update(
  2720. {
  2721. _id: paramCardId,
  2722. listId: paramListId,
  2723. boardId: paramBoardId,
  2724. archived: false,
  2725. },
  2726. {
  2727. $set: {
  2728. listId: newParamListId,
  2729. },
  2730. },
  2731. );
  2732. const card = Cards.findOne({
  2733. _id: paramCardId,
  2734. });
  2735. cardMove(
  2736. req.body.authorId,
  2737. card,
  2738. {
  2739. fieldName: 'listId',
  2740. },
  2741. paramListId,
  2742. );
  2743. }
  2744. JsonRoutes.sendResult(res, {
  2745. code: 200,
  2746. data: {
  2747. _id: paramCardId,
  2748. },
  2749. });
  2750. },
  2751. );
  2752. /**
  2753. * @operation delete_card
  2754. * @summary Delete a card from a board
  2755. *
  2756. * @description This operation **deletes** a card, and therefore the card
  2757. * is not put in the recycle bin.
  2758. *
  2759. * @param {string} boardId the board ID of the card
  2760. * @param {string} list the list ID of the card
  2761. * @param {string} cardId the ID of the card
  2762. * @return_type {_id: string}
  2763. */
  2764. JsonRoutes.add(
  2765. 'DELETE',
  2766. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2767. function(req, res) {
  2768. Authentication.checkUserId(req.userId);
  2769. const paramBoardId = req.params.boardId;
  2770. const paramListId = req.params.listId;
  2771. const paramCardId = req.params.cardId;
  2772. const card = Cards.findOne({
  2773. _id: paramCardId,
  2774. });
  2775. Cards.direct.remove({
  2776. _id: paramCardId,
  2777. listId: paramListId,
  2778. boardId: paramBoardId,
  2779. });
  2780. cardRemover(req.body.authorId, card);
  2781. JsonRoutes.sendResult(res, {
  2782. code: 200,
  2783. data: {
  2784. _id: paramCardId,
  2785. },
  2786. });
  2787. },
  2788. );
  2789. /**
  2790. * @operation get_cards_by_custom_field
  2791. * @summary Get all Cards that matchs a value of a specific custom field
  2792. *
  2793. * @param {string} boardId the board ID
  2794. * @param {string} customFieldId the list ID
  2795. * @param {string} customFieldValue the value to look for
  2796. * @return_type [{_id: string,
  2797. * title: string,
  2798. * description: string,
  2799. * listId: string,
  2800. * swinlaneId: string}]
  2801. */
  2802. JsonRoutes.add(
  2803. 'GET',
  2804. '/api/boards/:boardId/cardsByCustomField/:customFieldId/:customFieldValue',
  2805. function(req, res) {
  2806. const paramBoardId = req.params.boardId;
  2807. const paramCustomFieldId = req.params.customFieldId;
  2808. const paramCustomFieldValue = req.params.customFieldValue;
  2809. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2810. JsonRoutes.sendResult(res, {
  2811. code: 200,
  2812. data: Cards.find({
  2813. boardId: paramBoardId,
  2814. customFields: {
  2815. $elemMatch: {
  2816. _id: paramCustomFieldId,
  2817. value: paramCustomFieldValue,
  2818. },
  2819. },
  2820. archived: false,
  2821. }).fetch(),
  2822. });
  2823. },
  2824. );
  2825. }
  2826. export default Cards;