cards.js 74 KB

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