cards.js 79 KB

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