cards.js 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import moment from 'moment/min/moment-with-locales';
  3. import {
  4. ALLOWED_COLORS,
  5. TYPE_CARD,
  6. TYPE_LINKED_BOARD,
  7. TYPE_LINKED_CARD,
  8. } from '../config/const';
  9. import Attachments, { fileStoreStrategyFactory } from "./attachments";
  10. import { copyFile } from './lib/fileStoreStrategy.js';
  11. import { DataCache } from 'meteor-reactive-cache';
  12. Cards = new Mongo.Collection('cards');
  13. // XXX To improve pub/sub performances a card document should include a
  14. // de-normalized number of comments so we don't have to publish the whole list
  15. // of comments just to display the number of them in the board view.
  16. Cards.attachSchema(
  17. new SimpleSchema({
  18. title: {
  19. /**
  20. * the title of the card
  21. */
  22. type: String,
  23. optional: true,
  24. defaultValue: '',
  25. },
  26. archived: {
  27. /**
  28. * is the card archived
  29. */
  30. type: Boolean,
  31. // eslint-disable-next-line consistent-return
  32. autoValue() {
  33. // eslint-disable-line consistent-return
  34. if (this.isInsert && !this.isSet) {
  35. return false;
  36. }
  37. },
  38. },
  39. archivedAt: {
  40. /**
  41. * latest archiving date
  42. */
  43. type: Date,
  44. optional: true,
  45. },
  46. parentId: {
  47. /**
  48. * ID of the parent card
  49. */
  50. type: String,
  51. optional: true,
  52. defaultValue: '',
  53. },
  54. listId: {
  55. /**
  56. * List ID where the card is
  57. */
  58. type: String,
  59. optional: true,
  60. defaultValue: '',
  61. },
  62. swimlaneId: {
  63. /**
  64. * Swimlane ID where the card is
  65. */
  66. type: String,
  67. },
  68. // The system could work without this `boardId` information (we could deduce
  69. // the board identifier from the card), but it would make the system more
  70. // difficult to manage and less efficient.
  71. boardId: {
  72. /**
  73. * Board ID of the card
  74. */
  75. type: String,
  76. optional: true,
  77. defaultValue: '',
  78. },
  79. coverId: {
  80. /**
  81. * Cover ID of the card
  82. */
  83. type: String,
  84. optional: true,
  85. defaultValue: '',
  86. },
  87. color: {
  88. type: String,
  89. optional: true,
  90. allowedValues: ALLOWED_COLORS,
  91. },
  92. createdAt: {
  93. /**
  94. * creation date
  95. */
  96. type: Date,
  97. // eslint-disable-next-line consistent-return
  98. autoValue() {
  99. if (this.isInsert) {
  100. return new Date();
  101. } else if (this.isUpsert) {
  102. return { $setOnInsert: new Date() };
  103. } else {
  104. this.unset();
  105. }
  106. },
  107. },
  108. modifiedAt: {
  109. type: Date,
  110. denyUpdate: false,
  111. // eslint-disable-next-line consistent-return
  112. autoValue() {
  113. if (this.isInsert || this.isUpsert || this.isUpdate) {
  114. return new Date();
  115. } else {
  116. this.unset();
  117. }
  118. },
  119. },
  120. customFields: {
  121. /**
  122. * list of custom fields
  123. */
  124. type: [Object],
  125. optional: true,
  126. defaultValue: [],
  127. },
  128. 'customFields.$': {
  129. type: new SimpleSchema({
  130. _id: {
  131. /**
  132. * the ID of the related custom field
  133. */
  134. type: String,
  135. optional: true,
  136. defaultValue: '',
  137. },
  138. value: {
  139. /**
  140. * value attached to the custom field
  141. */
  142. type: Match.OneOf(String, Number, Boolean, Date, [String]),
  143. optional: true,
  144. defaultValue: '',
  145. },
  146. 'value.$': {
  147. type: String,
  148. optional: true,
  149. },
  150. }),
  151. },
  152. dateLastActivity: {
  153. /**
  154. * Date of last activity
  155. */
  156. type: Date,
  157. autoValue() {
  158. return new Date();
  159. },
  160. },
  161. description: {
  162. /**
  163. * description of the card
  164. */
  165. type: String,
  166. optional: true,
  167. defaultValue: '',
  168. },
  169. requestedBy: {
  170. /**
  171. * who requested the card (ID of the user)
  172. */
  173. type: String,
  174. optional: true,
  175. defaultValue: '',
  176. },
  177. assignedBy: {
  178. /**
  179. * who assigned the card (ID of the user)
  180. */
  181. type: String,
  182. optional: true,
  183. defaultValue: '',
  184. },
  185. labelIds: {
  186. /**
  187. * list of labels ID the card has
  188. */
  189. type: [String],
  190. optional: true,
  191. defaultValue: [],
  192. },
  193. members: {
  194. /**
  195. * list of members (user IDs)
  196. */
  197. type: [String],
  198. optional: true,
  199. defaultValue: [],
  200. },
  201. assignees: {
  202. /**
  203. * who is assignee of the card (user ID),
  204. * maximum one ID of assignee in array.
  205. */
  206. type: [String],
  207. optional: true,
  208. defaultValue: [],
  209. },
  210. receivedAt: {
  211. /**
  212. * Date the card was received
  213. */
  214. type: Date,
  215. optional: true,
  216. },
  217. startAt: {
  218. /**
  219. * Date the card was started to be worked on
  220. */
  221. type: Date,
  222. optional: true,
  223. },
  224. dueAt: {
  225. /**
  226. * Date the card is due
  227. */
  228. type: Date,
  229. optional: true,
  230. },
  231. endAt: {
  232. /**
  233. * Date the card ended
  234. */
  235. type: Date,
  236. optional: true,
  237. },
  238. spentTime: {
  239. /**
  240. * How much time has been spent on this
  241. */
  242. type: Number,
  243. decimal: true,
  244. optional: true,
  245. defaultValue: 0,
  246. },
  247. isOvertime: {
  248. /**
  249. * is the card over time?
  250. */
  251. type: Boolean,
  252. defaultValue: false,
  253. optional: true,
  254. },
  255. // XXX Should probably be called `authorId`. Is it even needed since we have
  256. // the `members` field?
  257. userId: {
  258. /**
  259. * user ID of the author of the card
  260. */
  261. type: String,
  262. // eslint-disable-next-line consistent-return
  263. autoValue() {
  264. // eslint-disable-line consistent-return
  265. if (this.isInsert && !this.isSet) {
  266. return this.userId;
  267. }
  268. },
  269. },
  270. sort: {
  271. /**
  272. * Sort value
  273. */
  274. type: Number,
  275. decimal: true,
  276. defaultValue: 0,
  277. optional: true,
  278. },
  279. subtaskSort: {
  280. /**
  281. * subtask sort value
  282. */
  283. type: Number,
  284. decimal: true,
  285. defaultValue: -1,
  286. optional: true,
  287. },
  288. type: {
  289. /**
  290. * type of the card
  291. */
  292. type: String,
  293. defaultValue: TYPE_CARD,
  294. // allowedValues: [TYPE_CARD, TYPE_LINKED_CARD, TYPE_LINKED_BOARD, TYPE_TEMPLATE_CARD],
  295. },
  296. linkedId: {
  297. /**
  298. * ID of the linked card
  299. */
  300. type: String,
  301. optional: true,
  302. defaultValue: '',
  303. },
  304. vote: {
  305. /**
  306. * vote object, see below
  307. */
  308. type: Object,
  309. optional: true,
  310. },
  311. 'vote.question': {
  312. type: String,
  313. defaultValue: '',
  314. },
  315. 'vote.positive': {
  316. /**
  317. * list of members (user IDs)
  318. */
  319. type: [String],
  320. optional: true,
  321. defaultValue: [],
  322. },
  323. 'vote.negative': {
  324. /**
  325. * list of members (user IDs)
  326. */
  327. type: [String],
  328. optional: true,
  329. defaultValue: [],
  330. },
  331. 'vote.end': {
  332. type: Date,
  333. optional: true,
  334. defaultValue: null,
  335. },
  336. 'vote.public': {
  337. type: Boolean,
  338. defaultValue: false,
  339. },
  340. 'vote.allowNonBoardMembers': {
  341. type: Boolean,
  342. defaultValue: false,
  343. },
  344. poker: {
  345. /**
  346. * poker object, see below
  347. */
  348. type: Object,
  349. optional: true,
  350. },
  351. 'poker.question': {
  352. type: Boolean,
  353. defaultValue: false,
  354. },
  355. 'poker.one': {
  356. /**
  357. * poker card one
  358. */
  359. type: [String],
  360. optional: true,
  361. defaultValue: [],
  362. },
  363. 'poker.two': {
  364. /**
  365. * poker card two
  366. */
  367. type: [String],
  368. optional: true,
  369. defaultValue: [],
  370. },
  371. 'poker.three': {
  372. /**
  373. * poker card three
  374. */
  375. type: [String],
  376. optional: true,
  377. defaultValue: [],
  378. },
  379. 'poker.five': {
  380. /**
  381. * poker card five
  382. */
  383. type: [String],
  384. optional: true,
  385. defaultValue: [],
  386. },
  387. 'poker.eight': {
  388. /**
  389. * poker card eight
  390. */
  391. type: [String],
  392. optional: true,
  393. defaultValue: [],
  394. },
  395. 'poker.thirteen': {
  396. /**
  397. * poker card thirteen
  398. */
  399. type: [String],
  400. optional: true,
  401. defaultValue: [],
  402. },
  403. 'poker.twenty': {
  404. /**
  405. * poker card twenty
  406. */
  407. type: [String],
  408. optional: true,
  409. defaultValue: [],
  410. },
  411. 'poker.forty': {
  412. /**
  413. * poker card forty
  414. */
  415. type: [String],
  416. optional: true,
  417. defaultValue: [],
  418. },
  419. 'poker.oneHundred': {
  420. /**
  421. * poker card oneHundred
  422. */
  423. type: [String],
  424. optional: true,
  425. defaultValue: [],
  426. },
  427. 'poker.unsure': {
  428. /**
  429. * poker card unsure
  430. */
  431. type: [String],
  432. optional: true,
  433. defaultValue: [],
  434. },
  435. 'poker.end': {
  436. type: Date,
  437. optional: true,
  438. defaultValue: null,
  439. },
  440. 'poker.allowNonBoardMembers': {
  441. type: Boolean,
  442. defaultValue: false,
  443. },
  444. 'poker.estimation': {
  445. /**
  446. * poker estimation value
  447. */
  448. type: Number,
  449. optional: true,
  450. },
  451. targetId_gantt: {
  452. /**
  453. * ID of card which is the child link in gantt view
  454. */
  455. type: [String],
  456. optional: true,
  457. defaultValue: [],
  458. },
  459. linkType_gantt: {
  460. /**
  461. * ID of card which is the parent link in gantt view
  462. */
  463. type: [Number],
  464. decimal: false,
  465. optional: true,
  466. defaultValue: [],
  467. },
  468. linkId_gantt: {
  469. /**
  470. * ID of card which is the parent link in gantt view
  471. */
  472. type: [String],
  473. optional: true,
  474. defaultValue: [],
  475. },
  476. cardNumber: {
  477. /**
  478. * A boardwise sequentially increasing number that is assigned
  479. * to every newly created card
  480. */
  481. type: Number,
  482. decimal: true,
  483. optional: true,
  484. defaultValue: 0,
  485. },
  486. }),
  487. );
  488. Cards.allow({
  489. insert(userId, doc) {
  490. return allowIsBoardMember(userId, ReactiveCache.getBoard(doc.boardId));
  491. },
  492. update(userId, doc, fields) {
  493. // Allow board members or logged in users if only vote get's changed
  494. return (
  495. allowIsBoardMember(userId, ReactiveCache.getBoard(doc.boardId)) ||
  496. (_.isEqual(fields, ['vote', 'modifiedAt', 'dateLastActivity']) &&
  497. !!userId)
  498. );
  499. },
  500. remove(userId, doc) {
  501. return allowIsBoardMember(userId, ReactiveCache.getBoard(doc.boardId));
  502. },
  503. fetch: ['boardId'],
  504. });
  505. Cards.helpers({
  506. // Gantt https://github.com/wekan/wekan/issues/2870#issuecomment-857171127
  507. setGanttTargetId(sourceId, targetId, linkType, linkId){
  508. return Cards.update({ _id: sourceId}, {
  509. $push: {
  510. targetId_gantt: targetId,
  511. linkType_gantt : linkType,
  512. linkId_gantt: linkId
  513. }
  514. });
  515. },
  516. removeGanttTargetId(sourceId, targetId, linkType, linkId){
  517. return Cards.update({ _id: sourceId}, {
  518. $pull: {
  519. targetId_gantt: targetId,
  520. linkType_gantt : linkType,
  521. linkId_gantt: linkId
  522. }
  523. });
  524. },
  525. mapCustomFieldsToBoard(boardId) {
  526. // Map custom fields to new board
  527. return this.customFields.map(cf => {
  528. const oldCf = CustomFields.findOne(cf._id);
  529. const newCf = CustomFields.findOne({
  530. boardIds: boardId,
  531. name: oldCf.name,
  532. type: oldCf.type,
  533. });
  534. if (newCf) {
  535. cf._id = newCf._id;
  536. } else if (!_.contains(oldCf.boardIds, boardId)) {
  537. oldCf.addBoard(boardId);
  538. }
  539. return cf;
  540. });
  541. },
  542. copy(boardId, swimlaneId, listId) {
  543. const oldId = this._id;
  544. const oldCard = Cards.findOne(oldId);
  545. // we must only copy the labels and custom fields if the target board
  546. // differs from the source board
  547. if (this.boardId !== boardId) {
  548. const oldBoard = ReactiveCache.getBoard(this.boardId);
  549. const oldBoardLabels = oldBoard.labels;
  550. // Get old label names
  551. const oldCardLabels = _.pluck(
  552. _.filter(oldBoardLabels, label => {
  553. return _.contains(this.labelIds, label._id);
  554. }),
  555. 'name',
  556. );
  557. const newBoard = ReactiveCache.getBoard(boardId);
  558. const newBoardLabels = newBoard.labels;
  559. const newCardLabels = _.pluck(
  560. _.filter(newBoardLabels, label => {
  561. return _.contains(oldCardLabels, label.name);
  562. }),
  563. '_id',
  564. );
  565. // now set the new label ids
  566. delete this.labelIds;
  567. this.labelIds = newCardLabels;
  568. this.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  569. }
  570. delete this._id;
  571. this.boardId = boardId;
  572. this.cardNumber = ReactiveCache.getBoard(boardId).getNextCardNumber();
  573. this.swimlaneId = swimlaneId;
  574. this.listId = listId;
  575. const _id = Cards.insert(this);
  576. // Copy attachments
  577. oldCard.attachments()
  578. .map(att => att.get())
  579. .forEach(att => {
  580. copyFile(att, _id, fileStoreStrategyFactory);
  581. });
  582. // copy checklists
  583. Checklists.find({ cardId: oldId }).forEach(ch => {
  584. ch.copy(_id);
  585. });
  586. // copy subtasks
  587. Cards.find({ parentId: oldId }).forEach(subtask => {
  588. subtask.parentId = _id;
  589. subtask._id = null;
  590. Cards.insert(subtask);
  591. });
  592. // copy card comments
  593. CardComments.find({ cardId: oldId }).forEach(cmt => {
  594. cmt.copy(_id);
  595. });
  596. // restore the id, otherwise new copies will fail
  597. this._id = oldId;
  598. return _id;
  599. },
  600. link(boardId, swimlaneId, listId) {
  601. // TODO is there a better method to create a deepcopy?
  602. linkCard = JSON.parse(JSON.stringify(this));
  603. // TODO is this how it is meant to be?
  604. linkCard.linkedId = linkCard.linkedId || linkCard._id;
  605. linkCard.boardId = boardId;
  606. linkCard.swimlaneId = swimlaneId;
  607. linkCard.listId = listId;
  608. linkCard.type = 'cardType-linkedCard';
  609. delete linkCard._id;
  610. // TODO shall we copy the labels for a linked card?!
  611. delete linkCard.labelIds;
  612. return Cards.insert(linkCard);
  613. },
  614. list() {
  615. return ReactiveCache.getList(this.listId);
  616. },
  617. swimlane() {
  618. return ReactiveCache.getSwimlane(this.swimlaneId);
  619. },
  620. board() {
  621. const ret = ReactiveCache.getBoard(this.boardId);
  622. return ret;
  623. },
  624. getRealId() {
  625. if (!this.__id) {
  626. if (this.isLinkedCard()) {
  627. this.__id = this.linkedId;
  628. } else {
  629. this.__id = this._id;
  630. }
  631. }
  632. return this.__id;
  633. },
  634. getList() {
  635. const list = this.list();
  636. if (!list) {
  637. return {
  638. _id: this.listId,
  639. title: 'Undefined List',
  640. archived: false,
  641. colorClass: '',
  642. };
  643. }
  644. return list;
  645. },
  646. getSwimlane() {
  647. const swimlane = this.swimlane();
  648. if (!swimlane) {
  649. return {
  650. _id: this.swimlaneId,
  651. title: 'Undefined Swimlane',
  652. archived: false,
  653. colorClass: '',
  654. };
  655. }
  656. return swimlane;
  657. },
  658. getBoard() {
  659. const board = this.board();
  660. if (!board) {
  661. return {
  662. _id: this.boardId,
  663. title: 'Undefined Board',
  664. archived: false,
  665. colorClass: '',
  666. };
  667. }
  668. return board;
  669. },
  670. labels() {
  671. const boardLabels = this.board().labels;
  672. const cardLabels = _.filter(boardLabels, label => {
  673. return _.contains(this.labelIds, label._id);
  674. });
  675. return cardLabels;
  676. },
  677. hasLabel(labelId) {
  678. return _.contains(this.labelIds, labelId);
  679. },
  680. /** returns the sort number of a list
  681. * @param listId a list id
  682. * @param swimlaneId a swimlane id
  683. * top sorting of the card at the top if true, or from the bottom if false
  684. */
  685. getSort(listId, swimlaneId, top) {
  686. if (!_.isBoolean(top)) {
  687. top = true;
  688. }
  689. if (!listId) {
  690. listId = this.listId;
  691. }
  692. if (!swimlaneId) {
  693. swimlaneId = this.swimlaneId;
  694. }
  695. const selector = {
  696. listId: listId,
  697. swimlaneId: swimlaneId,
  698. archived: false,
  699. };
  700. const sorting = top ? 1 : -1;
  701. const card = Cards.findOne(selector, { sort: { sort: sorting } });
  702. let ret = null
  703. if (card) {
  704. ret = card.sort;
  705. }
  706. return ret;
  707. },
  708. /** returns the sort number of a list from the card at the top
  709. * @param listId a list id
  710. * @param swimlaneId a swimlane id
  711. */
  712. getMinSort(listId, swimlaneId) {
  713. const ret = this.getSort(listId, swimlaneId, true);
  714. return ret;
  715. },
  716. /** returns the sort number of a list from the card at the bottom
  717. * @param listId a list id
  718. * @param swimlaneId a swimlane id
  719. */
  720. getMaxSort(listId, swimlaneId) {
  721. const ret = this.getSort(listId, swimlaneId, false);
  722. return ret;
  723. },
  724. user() {
  725. return Users.findOne(this.userId);
  726. },
  727. isAssigned(memberId) {
  728. return _.contains(this.getMembers(), memberId);
  729. },
  730. isAssignee(assigneeId) {
  731. return _.contains(this.getAssignees(), assigneeId);
  732. },
  733. activities() {
  734. let ret;
  735. if (this.isLinkedBoard()) {
  736. ret = Activities.find(
  737. { boardId: this.linkedId },
  738. { sort: { createdAt: -1 } },
  739. );
  740. } else {
  741. ret = Activities.find({ cardId: this.getRealId() }, { sort: { createdAt: -1 } });
  742. }
  743. return ret;
  744. },
  745. comments() {
  746. let ret
  747. if (this.isLinkedBoard()) {
  748. ret = CardComments.find(
  749. { boardId: this.linkedId },
  750. { sort: { createdAt: -1 } },
  751. );
  752. } else {
  753. ret = CardComments.find(
  754. { cardId: this.getRealId() },
  755. { sort: { createdAt: -1 } },
  756. );
  757. }
  758. return ret;
  759. },
  760. attachments() {
  761. const ret = Attachments.find(
  762. { 'meta.cardId': this.getRealId() },
  763. { sort: { uploadedAt: -1 } },
  764. ).each();
  765. return ret;
  766. },
  767. cover() {
  768. if (!this.coverId) return false;
  769. const cover = Attachments.findOne(this.coverId);
  770. // if we return a cover before it is fully stored, we will get errors when we try to display it
  771. // todo XXX we could return a default "upload pending" image in the meantime?
  772. return cover && cover.link() && cover;
  773. },
  774. checklists() {
  775. if (!this._checklists) {
  776. this._checklists = new DataCache(() => Checklists.find({ cardId: this.getRealId() }, { sort: { sort: 1 } }).fetch(), 1000);
  777. }
  778. return this._checklists.get();
  779. },
  780. firstChecklist() {
  781. const checklists = this.checklists();
  782. const ret = _.first(checklists);
  783. return ret;
  784. },
  785. lastChecklist() {
  786. const checklists = this.checklists();
  787. const ret = _.last(checklists);
  788. return ret;
  789. },
  790. checklistItemCount() {
  791. const checklists = this.checklists();
  792. const ret = checklists
  793. .map(checklist => {
  794. return checklist.itemCount();
  795. })
  796. .reduce((prev, next) => {
  797. return prev + next;
  798. }, 0);
  799. return ret;
  800. },
  801. checklistFinishedCount() {
  802. const checklists = this.checklists();
  803. const ret = checklists
  804. .map(checklist => {
  805. return checklist.finishedCount();
  806. })
  807. .reduce((prev, next) => {
  808. return prev + next;
  809. }, 0);
  810. return ret;
  811. },
  812. checklistFinished() {
  813. return (
  814. this.hasChecklist() &&
  815. this.checklistItemCount() === this.checklistFinishedCount()
  816. );
  817. },
  818. hasChecklist() {
  819. return this.checklistItemCount() !== 0;
  820. },
  821. subtasks() {
  822. return Cards.find(
  823. {
  824. parentId: this._id,
  825. archived: false,
  826. },
  827. {
  828. sort: {
  829. sort: 1,
  830. },
  831. },
  832. );
  833. },
  834. subtasksFinished() {
  835. return Cards.find({
  836. parentId: this._id,
  837. archived: true,
  838. });
  839. },
  840. allSubtasks() {
  841. return Cards.find({
  842. parentId: this._id,
  843. });
  844. },
  845. subtasksCount() {
  846. const subtasks = this.subtasks();
  847. return subtasks.count();
  848. },
  849. subtasksFinishedCount() {
  850. const subtasksArchived = this.subtasksFinished();
  851. return subtasksArchived.count();
  852. },
  853. allSubtasksCount() {
  854. const allSubtasks = this.allSubtasks();
  855. return allSubtasks.count();
  856. },
  857. allowsSubtasks() {
  858. return this.subtasksCount() !== 0;
  859. },
  860. customFieldIndex(customFieldId) {
  861. return _.pluck(this.customFields, '_id').indexOf(customFieldId);
  862. },
  863. // customFields with definitions
  864. customFieldsWD() {
  865. // get all definitions
  866. const definitions = ReactiveCache.getCustomFields({
  867. boardIds: { $in: [this.boardId] },
  868. });
  869. if (!definitions) {
  870. return {};
  871. }
  872. // match right definition to each field
  873. if (!this.customFields) return [];
  874. const ret = this.customFields.map(customField => {
  875. const definition = definitions.find(definition => {
  876. return definition._id === customField._id;
  877. });
  878. if (!definition) {
  879. return {};
  880. }
  881. //search for "True Value" which is for DropDowns other then the Value (which is the id)
  882. let trueValue = customField.value;
  883. if (
  884. definition.settings.dropdownItems &&
  885. definition.settings.dropdownItems.length > 0
  886. ) {
  887. for (let i = 0; i < definition.settings.dropdownItems.length; i++) {
  888. if (definition.settings.dropdownItems[i]._id === customField.value) {
  889. trueValue = definition.settings.dropdownItems[i].name;
  890. }
  891. }
  892. }
  893. return {
  894. _id: customField._id,
  895. value: customField.value,
  896. trueValue,
  897. definition,
  898. };
  899. });
  900. // at linked cards custom fields definition is not found
  901. ret.sort(
  902. (a, b) =>
  903. a.definition !== undefined &&
  904. b.definition !== undefined &&
  905. a.definition.name !== undefined &&
  906. b.definition.name !== undefined &&
  907. a.definition.name.localeCompare(b.definition.name),
  908. );
  909. return ret;
  910. },
  911. colorClass() {
  912. if (this.color) return this.color;
  913. return '';
  914. },
  915. absoluteUrl() {
  916. const board = this.board();
  917. return FlowRouter.url('card', {
  918. boardId: board._id,
  919. slug: board.slug,
  920. cardId: this._id,
  921. });
  922. },
  923. originRelativeUrl() {
  924. const board = this.board();
  925. return FlowRouter.path('card', {
  926. boardId: board._id,
  927. slug: board.slug,
  928. cardId: this._id,
  929. });
  930. },
  931. canBeRestored() {
  932. const list = ReactiveCache.getList(this.listId);
  933. if (
  934. !list.getWipLimit('soft') &&
  935. list.getWipLimit('enabled') &&
  936. list.getWipLimit('value') === list.cards().count()
  937. ) {
  938. return false;
  939. }
  940. return true;
  941. },
  942. parentCard() {
  943. if (this.parentId === '') {
  944. return null;
  945. }
  946. return Cards.findOne(this.parentId);
  947. },
  948. parentCardName() {
  949. let result = '';
  950. if (this.parentId !== '') {
  951. const card = ReactiveCache.getCard(this.parentId);
  952. if (card) {
  953. result = card.title;
  954. }
  955. }
  956. return result;
  957. },
  958. parentListId() {
  959. const result = [];
  960. let crtParentId = this.parentId;
  961. while (crtParentId !== '') {
  962. const crt = Cards.findOne(crtParentId);
  963. if (crt === null || crt === undefined) {
  964. // maybe it has been deleted
  965. break;
  966. }
  967. if (crtParentId in result) {
  968. // circular reference
  969. break;
  970. }
  971. result.unshift(crtParentId);
  972. crtParentId = crt.parentId;
  973. }
  974. return result;
  975. },
  976. parentList() {
  977. const resultId = [];
  978. const result = [];
  979. let crtParentId = this.parentId;
  980. while (crtParentId !== '') {
  981. const crt = Cards.findOne(crtParentId);
  982. if (crt === null || crt === undefined) {
  983. // maybe it has been deleted
  984. break;
  985. }
  986. if (crtParentId in resultId) {
  987. // circular reference
  988. break;
  989. }
  990. resultId.unshift(crtParentId);
  991. result.unshift(crt);
  992. crtParentId = crt.parentId;
  993. }
  994. return result;
  995. },
  996. parentString(sep) {
  997. return this.parentList()
  998. .map(function(elem) {
  999. return elem.title;
  1000. })
  1001. .join(sep);
  1002. },
  1003. isTopLevel() {
  1004. return this.parentId === '';
  1005. },
  1006. isLinkedCard() {
  1007. return this.type === 'cardType-linkedCard';
  1008. },
  1009. isLinkedBoard() {
  1010. return this.type === 'cardType-linkedBoard';
  1011. },
  1012. isLinked() {
  1013. return this.isLinkedCard() || this.isLinkedBoard();
  1014. },
  1015. setDescription(description) {
  1016. if (this.isLinkedBoard()) {
  1017. return Boards.update({ _id: this.linkedId }, { $set: { description } });
  1018. } else {
  1019. return Cards.update({ _id: this.getRealId() }, { $set: { description } });
  1020. }
  1021. },
  1022. getDescription() {
  1023. if (this.isLinkedCard()) {
  1024. const card = Cards.findOne({ _id: this.linkedId });
  1025. if (card && card.description) return card.description;
  1026. else return null;
  1027. } else if (this.isLinkedBoard()) {
  1028. const board = ReactiveCache.getBoard(this.linkedId);
  1029. if (board && board.description) return board.description;
  1030. else return null;
  1031. } else if (this.description) {
  1032. return this.description;
  1033. } else {
  1034. return null;
  1035. }
  1036. },
  1037. getMembers() {
  1038. if (this.isLinkedCard()) {
  1039. const card = Cards.findOne({ _id: this.linkedId });
  1040. if (card === undefined) {
  1041. return null;
  1042. } else {
  1043. return card.members;
  1044. }
  1045. } else if (this.isLinkedBoard()) {
  1046. const board = ReactiveCache.getBoard(this.linkedId);
  1047. if (board === undefined) {
  1048. return null;
  1049. } else {
  1050. return board.activeMembers().map(member => {
  1051. return member.userId;
  1052. });
  1053. }
  1054. } else {
  1055. return this.members;
  1056. }
  1057. },
  1058. getAssignees() {
  1059. if (this.isLinkedCard()) {
  1060. const card = Cards.findOne({ _id: this.linkedId });
  1061. if (card === undefined) {
  1062. return null;
  1063. } else {
  1064. return card.assignees;
  1065. }
  1066. } else if (this.isLinkedBoard()) {
  1067. const board = ReactiveCache.getBoard(this.linkedId);
  1068. if (board === undefined) {
  1069. return null;
  1070. } else {
  1071. return board.activeMembers().map(assignee => {
  1072. return assignee.userId;
  1073. });
  1074. }
  1075. } else {
  1076. return this.assignees;
  1077. }
  1078. },
  1079. assignMember(memberId) {
  1080. let ret;
  1081. if (this.isLinkedBoard()) {
  1082. const board = ReactiveCache.getBoard(this.linkedId);
  1083. ret = board.addMember(memberId);
  1084. } else {
  1085. ret = Cards.update(
  1086. { _id: this.getRealId() },
  1087. { $addToSet: { members: memberId } },
  1088. );
  1089. }
  1090. return ret;
  1091. },
  1092. assignAssignee(assigneeId) {
  1093. if (this.isLinkedCard()) {
  1094. return Cards.update(
  1095. { _id: this.linkedId },
  1096. { $addToSet: { assignees: assigneeId } },
  1097. );
  1098. } else if (this.isLinkedBoard()) {
  1099. const board = ReactiveCache.getBoard(this.linkedId);
  1100. return board.addAssignee(assigneeId);
  1101. } else {
  1102. return Cards.update(
  1103. { _id: this._id },
  1104. { $addToSet: { assignees: assigneeId } },
  1105. );
  1106. }
  1107. },
  1108. unassignMember(memberId) {
  1109. if (this.isLinkedCard()) {
  1110. return Cards.update(
  1111. { _id: this.linkedId },
  1112. { $pull: { members: memberId } },
  1113. );
  1114. } else if (this.isLinkedBoard()) {
  1115. const board = ReactiveCache.getBoard(this.linkedId);
  1116. return board.removeMember(memberId);
  1117. } else {
  1118. return Cards.update({ _id: this._id }, { $pull: { members: memberId } });
  1119. }
  1120. },
  1121. unassignAssignee(assigneeId) {
  1122. if (this.isLinkedCard()) {
  1123. return Cards.update(
  1124. { _id: this.linkedId },
  1125. { $pull: { assignees: assigneeId } },
  1126. );
  1127. } else if (this.isLinkedBoard()) {
  1128. const board = ReactiveCache.getBoard(this.linkedId);
  1129. return board.removeAssignee(assigneeId);
  1130. } else {
  1131. return Cards.update(
  1132. { _id: this._id },
  1133. { $pull: { assignees: assigneeId } },
  1134. );
  1135. }
  1136. },
  1137. toggleMember(memberId) {
  1138. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  1139. return this.unassignMember(memberId);
  1140. } else {
  1141. return this.assignMember(memberId);
  1142. }
  1143. },
  1144. toggleAssignee(assigneeId) {
  1145. if (this.getAssignees() && this.getAssignees().indexOf(assigneeId) > -1) {
  1146. return this.unassignAssignee(assigneeId);
  1147. } else {
  1148. return this.assignAssignee(assigneeId);
  1149. }
  1150. },
  1151. getReceived() {
  1152. if (this.isLinkedCard()) {
  1153. const card = Cards.findOne({ _id: this.linkedId });
  1154. if (card === undefined) {
  1155. return null;
  1156. } else {
  1157. return card.receivedAt;
  1158. }
  1159. } else if (this.isLinkedBoard()) {
  1160. const board = ReactiveCache.getBoard(this.linkedId);
  1161. if (board === undefined) {
  1162. return null;
  1163. } else {
  1164. return board.receivedAt;
  1165. }
  1166. } else {
  1167. return this.receivedAt;
  1168. }
  1169. },
  1170. setReceived(receivedAt) {
  1171. if (this.isLinkedBoard()) {
  1172. return Boards.update({ _id: this.linkedId }, { $set: { receivedAt } });
  1173. } else {
  1174. return Cards.update({ _id: this.getRealId() }, { $set: { receivedAt } });
  1175. }
  1176. },
  1177. getStart() {
  1178. if (this.isLinkedCard()) {
  1179. const card = Cards.findOne({ _id: this.linkedId });
  1180. if (card === undefined) {
  1181. return null;
  1182. } else {
  1183. return card.startAt;
  1184. }
  1185. } else if (this.isLinkedBoard()) {
  1186. const board = ReactiveCache.getBoard(this.linkedId);
  1187. if (board === undefined) {
  1188. return null;
  1189. } else {
  1190. return board.startAt;
  1191. }
  1192. } else {
  1193. return this.startAt;
  1194. }
  1195. },
  1196. setStart(startAt) {
  1197. if (this.isLinkedBoard()) {
  1198. return Boards.update({ _id: this.linkedId }, { $set: { startAt } });
  1199. } else {
  1200. return Cards.update({ _id: this.getRealId() }, { $set: { startAt } });
  1201. }
  1202. },
  1203. getDue() {
  1204. if (this.isLinkedCard()) {
  1205. const card = Cards.findOne({ _id: this.linkedId });
  1206. if (card === undefined) {
  1207. return null;
  1208. } else {
  1209. return card.dueAt;
  1210. }
  1211. } else if (this.isLinkedBoard()) {
  1212. const board = ReactiveCache.getBoard(this.linkedId);
  1213. if (board === undefined) {
  1214. return null;
  1215. } else {
  1216. return board.dueAt;
  1217. }
  1218. } else {
  1219. return this.dueAt;
  1220. }
  1221. },
  1222. setDue(dueAt) {
  1223. if (this.isLinkedBoard()) {
  1224. return Boards.update({ _id: this.linkedId }, { $set: { dueAt } });
  1225. } else {
  1226. return Cards.update({ _id: this.getRealId() }, { $set: { dueAt } });
  1227. }
  1228. },
  1229. getEnd() {
  1230. if (this.isLinkedCard()) {
  1231. const card = Cards.findOne({ _id: this.linkedId });
  1232. if (card === undefined) {
  1233. return null;
  1234. } else {
  1235. return card.endAt;
  1236. }
  1237. } else if (this.isLinkedBoard()) {
  1238. const board = ReactiveCache.getBoard(this.linkedId);
  1239. if (board === undefined) {
  1240. return null;
  1241. } else {
  1242. return board.endAt;
  1243. }
  1244. } else {
  1245. return this.endAt;
  1246. }
  1247. },
  1248. setEnd(endAt) {
  1249. if (this.isLinkedBoard()) {
  1250. return Boards.update({ _id: this.linkedId }, { $set: { endAt } });
  1251. } else {
  1252. return Cards.update({ _id: this.getRealId() }, { $set: { endAt } });
  1253. }
  1254. },
  1255. getIsOvertime() {
  1256. if (this.isLinkedCard()) {
  1257. const card = Cards.findOne({ _id: this.linkedId });
  1258. if (card === undefined) {
  1259. return null;
  1260. } else {
  1261. return card.isOvertime;
  1262. }
  1263. } else if (this.isLinkedBoard()) {
  1264. const board = ReactiveCache.getBoard(this.linkedId);
  1265. if (board === undefined) {
  1266. return null;
  1267. } else {
  1268. return board.isOvertime;
  1269. }
  1270. } else {
  1271. return this.isOvertime;
  1272. }
  1273. },
  1274. setIsOvertime(isOvertime) {
  1275. if (this.isLinkedBoard()) {
  1276. return Boards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  1277. } else {
  1278. return Cards.update({ _id: this.getRealId() }, { $set: { isOvertime } });
  1279. }
  1280. },
  1281. getSpentTime() {
  1282. if (this.isLinkedCard()) {
  1283. const card = Cards.findOne({ _id: this.linkedId });
  1284. if (card === undefined) {
  1285. return null;
  1286. } else {
  1287. return card.spentTime;
  1288. }
  1289. } else if (this.isLinkedBoard()) {
  1290. const board = ReactiveCache.getBoard(this.linkedId);
  1291. if (board === undefined) {
  1292. return null;
  1293. } else {
  1294. return board.spentTime;
  1295. }
  1296. } else {
  1297. return this.spentTime;
  1298. }
  1299. },
  1300. setSpentTime(spentTime) {
  1301. if (this.isLinkedBoard()) {
  1302. return Boards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1303. } else {
  1304. return Cards.update({ _id: this.getRealId() }, { $set: { spentTime } });
  1305. }
  1306. },
  1307. getVoteQuestion() {
  1308. if (this.isLinkedCard()) {
  1309. const card = Cards.findOne({ _id: this.linkedId });
  1310. if (card === undefined) {
  1311. return null;
  1312. } else if (card && card.vote) {
  1313. return card.vote.question;
  1314. } else {
  1315. return null;
  1316. }
  1317. } else if (this.isLinkedBoard()) {
  1318. const board = ReactiveCache.getBoard(this.linkedId);
  1319. if (board === undefined) {
  1320. return null;
  1321. } else if (board && board.vote) {
  1322. return board.vote.question;
  1323. } else {
  1324. return null;
  1325. }
  1326. } else if (this.vote) {
  1327. return this.vote.question;
  1328. } else {
  1329. return null;
  1330. }
  1331. },
  1332. getVotePublic() {
  1333. if (this.isLinkedCard()) {
  1334. const card = Cards.findOne({ _id: this.linkedId });
  1335. if (card === undefined) {
  1336. return null;
  1337. } else if (card && card.vote) {
  1338. return card.vote.public;
  1339. } else {
  1340. return null;
  1341. }
  1342. } else if (this.isLinkedBoard()) {
  1343. const board = ReactiveCache.getBoard(this.linkedId);
  1344. if (board === undefined) {
  1345. return null;
  1346. } else if (board && board.vote) {
  1347. return board.vote.public;
  1348. } else {
  1349. return null;
  1350. }
  1351. } else if (this.vote) {
  1352. return this.vote.public;
  1353. } else {
  1354. return null;
  1355. }
  1356. },
  1357. getVoteEnd() {
  1358. if (this.isLinkedCard()) {
  1359. const card = Cards.findOne({ _id: this.linkedId });
  1360. if (card === undefined) {
  1361. return null;
  1362. } else if (card && card.vote) {
  1363. return card.vote.end;
  1364. } else {
  1365. return null;
  1366. }
  1367. } else if (this.isLinkedBoard()) {
  1368. const board = ReactiveCache.getBoard(this.linkedId);
  1369. if (board === undefined) {
  1370. return null;
  1371. } else if (board && board.vote) {
  1372. return board.vote.end;
  1373. } else {
  1374. return null;
  1375. }
  1376. } else if (this.vote) {
  1377. return this.vote.end;
  1378. } else {
  1379. return null;
  1380. }
  1381. },
  1382. expiredVote() {
  1383. let end = this.getVoteEnd();
  1384. if (end) {
  1385. end = moment(end);
  1386. return end.isBefore(new Date());
  1387. }
  1388. return false;
  1389. },
  1390. voteMemberPositive() {
  1391. if (this.vote && this.vote.positive)
  1392. return Users.find({ _id: { $in: this.vote.positive } });
  1393. return [];
  1394. },
  1395. voteMemberNegative() {
  1396. if (this.vote && this.vote.negative)
  1397. return Users.find({ _id: { $in: this.vote.negative } });
  1398. return [];
  1399. },
  1400. voteState() {
  1401. const userId = Meteor.userId();
  1402. let state;
  1403. if (this.vote) {
  1404. if (this.vote.positive) {
  1405. state = _.contains(this.vote.positive, userId);
  1406. if (state === true) return true;
  1407. }
  1408. if (this.vote.negative) {
  1409. state = _.contains(this.vote.negative, userId);
  1410. if (state === true) return false;
  1411. }
  1412. }
  1413. return null;
  1414. },
  1415. getPokerQuestion() {
  1416. if (this.isLinkedCard()) {
  1417. const card = Cards.findOne({ _id: this.linkedId });
  1418. if (card === undefined) {
  1419. return null;
  1420. } else if (card && card.poker) {
  1421. return card.poker.question;
  1422. } else {
  1423. return null;
  1424. }
  1425. } else if (this.isLinkedBoard()) {
  1426. const board = ReactiveCache.getBoard(this.linkedId);
  1427. if (board === undefined) {
  1428. return null;
  1429. } else if (board && board.poker) {
  1430. return board.poker.question;
  1431. } else {
  1432. return null;
  1433. }
  1434. } else if (this.poker) {
  1435. return this.poker.question;
  1436. } else {
  1437. return null;
  1438. }
  1439. },
  1440. getPokerEstimation() {
  1441. if (this.poker) {
  1442. return this.poker.estimation;
  1443. } else {
  1444. return null;
  1445. }
  1446. },
  1447. getPokerEnd() {
  1448. if (this.isLinkedCard()) {
  1449. const card = Cards.findOne({ _id: this.linkedId });
  1450. if (card === undefined) {
  1451. return null;
  1452. } else if (card && card.poker) {
  1453. return card.poker.end;
  1454. } else {
  1455. return null;
  1456. }
  1457. } else if (this.isLinkedBoard()) {
  1458. const board = ReactiveCache.getBoard(this.linkedId);
  1459. if (board === undefined) {
  1460. return null;
  1461. } else if (board && board.poker) {
  1462. return board.poker.end;
  1463. } else {
  1464. return null;
  1465. }
  1466. } else if (this.poker) {
  1467. return this.poker.end;
  1468. } else {
  1469. return null;
  1470. }
  1471. },
  1472. expiredPoker() {
  1473. let end = this.getPokerEnd();
  1474. if (end) {
  1475. end = moment(end);
  1476. return end.isBefore(new Date());
  1477. }
  1478. return false;
  1479. },
  1480. pokerMemberOne() {
  1481. if (this.poker && this.poker.one)
  1482. return Users.find({ _id: { $in: this.poker.one } });
  1483. return [];
  1484. },
  1485. pokerMemberTwo() {
  1486. if (this.poker && this.poker.two)
  1487. return Users.find({ _id: { $in: this.poker.two } });
  1488. return [];
  1489. },
  1490. pokerMemberThree() {
  1491. if (this.poker && this.poker.three)
  1492. return Users.find({ _id: { $in: this.poker.three } });
  1493. return [];
  1494. },
  1495. pokerMemberFive() {
  1496. if (this.poker && this.poker.five)
  1497. return Users.find({ _id: { $in: this.poker.five } });
  1498. return [];
  1499. },
  1500. pokerMemberEight() {
  1501. if (this.poker && this.poker.eight)
  1502. return Users.find({ _id: { $in: this.poker.eight } });
  1503. return [];
  1504. },
  1505. pokerMemberThirteen() {
  1506. if (this.poker && this.poker.thirteen)
  1507. return Users.find({ _id: { $in: this.poker.thirteen } });
  1508. return [];
  1509. },
  1510. pokerMemberTwenty() {
  1511. if (this.poker && this.poker.twenty)
  1512. return Users.find({ _id: { $in: this.poker.twenty } });
  1513. return [];
  1514. },
  1515. pokerMemberForty() {
  1516. if (this.poker && this.poker.forty)
  1517. return Users.find({ _id: { $in: this.poker.forty } });
  1518. return [];
  1519. },
  1520. pokerMemberOneHundred() {
  1521. if (this.poker && this.poker.oneHundred)
  1522. return Users.find({ _id: { $in: this.poker.oneHundred } });
  1523. return [];
  1524. },
  1525. pokerMemberUnsure() {
  1526. if (this.poker && this.poker.unsure)
  1527. return Users.find({ _id: { $in: this.poker.unsure } });
  1528. return [];
  1529. },
  1530. pokerState() {
  1531. const userId = Meteor.userId();
  1532. let state;
  1533. if (this.poker) {
  1534. if (this.poker.one) {
  1535. state = _.contains(this.poker.one, userId);
  1536. if (state === true) {
  1537. return 'one';
  1538. }
  1539. }
  1540. if (this.poker.two) {
  1541. state = _.contains(this.poker.two, userId);
  1542. if (state === true) {
  1543. return 'two';
  1544. }
  1545. }
  1546. if (this.poker.three) {
  1547. state = _.contains(this.poker.three, userId);
  1548. if (state === true) {
  1549. return 'three';
  1550. }
  1551. }
  1552. if (this.poker.five) {
  1553. state = _.contains(this.poker.five, userId);
  1554. if (state === true) {
  1555. return 'five';
  1556. }
  1557. }
  1558. if (this.poker.eight) {
  1559. state = _.contains(this.poker.eight, userId);
  1560. if (state === true) {
  1561. return 'eight';
  1562. }
  1563. }
  1564. if (this.poker.thirteen) {
  1565. state = _.contains(this.poker.thirteen, userId);
  1566. if (state === true) {
  1567. return 'thirteen';
  1568. }
  1569. }
  1570. if (this.poker.twenty) {
  1571. state = _.contains(this.poker.twenty, userId);
  1572. if (state === true) {
  1573. return 'twenty';
  1574. }
  1575. }
  1576. if (this.poker.forty) {
  1577. state = _.contains(this.poker.forty, userId);
  1578. if (state === true) {
  1579. return 'forty';
  1580. }
  1581. }
  1582. if (this.poker.oneHundred) {
  1583. state = _.contains(this.poker.oneHundred, userId);
  1584. if (state === true) {
  1585. return 'oneHundred';
  1586. }
  1587. }
  1588. if (this.poker.unsure) {
  1589. state = _.contains(this.poker.unsure, userId);
  1590. if (state === true) {
  1591. return 'unsure';
  1592. }
  1593. }
  1594. }
  1595. return null;
  1596. },
  1597. getTitle() {
  1598. if (this.isLinkedCard()) {
  1599. const card = Cards.findOne({ _id: this.linkedId });
  1600. if (card === undefined) {
  1601. return null;
  1602. } else {
  1603. return card.title;
  1604. }
  1605. } else if (this.isLinkedBoard()) {
  1606. const board = ReactiveCache.getBoard(this.linkedId);
  1607. if (board === undefined) {
  1608. return null;
  1609. } else {
  1610. return board.title;
  1611. }
  1612. } else if (this.title === undefined) {
  1613. return null;
  1614. } else {
  1615. return this.title;
  1616. }
  1617. },
  1618. getCardNumber() {
  1619. return this.cardNumber;
  1620. },
  1621. getBoardTitle() {
  1622. if (this.isLinkedCard()) {
  1623. const card = Cards.findOne({ _id: this.linkedId });
  1624. if (card === undefined) {
  1625. return null;
  1626. }
  1627. const board = ReactiveCache.getBoard(card.boardId);
  1628. if (board === undefined) {
  1629. return null;
  1630. } else {
  1631. return board.title;
  1632. }
  1633. } else if (this.isLinkedBoard()) {
  1634. const board = ReactiveCache.getBoard(this.linkedId);
  1635. if (board === undefined) {
  1636. return null;
  1637. } else {
  1638. return board.title;
  1639. }
  1640. } else {
  1641. const board = ReactiveCache.getBoard(this.boardId);
  1642. if (board === undefined) {
  1643. return null;
  1644. } else {
  1645. return board.title;
  1646. }
  1647. }
  1648. },
  1649. setTitle(title) {
  1650. if (this.isLinkedBoard()) {
  1651. return Boards.update({ _id: this.linkedId }, { $set: { title } });
  1652. } else {
  1653. return Cards.update({ _id: this.getRealId() }, { $set: { title } });
  1654. }
  1655. },
  1656. getArchived() {
  1657. if (this.isLinkedCard()) {
  1658. const card = Cards.findOne({ _id: this.linkedId });
  1659. if (card === undefined) {
  1660. return null;
  1661. } else {
  1662. return card.archived;
  1663. }
  1664. } else if (this.isLinkedBoard()) {
  1665. const board = ReactiveCache.getBoard(this.linkedId);
  1666. if (board === undefined) {
  1667. return null;
  1668. } else {
  1669. return board.archived;
  1670. }
  1671. } else {
  1672. return this.archived;
  1673. }
  1674. },
  1675. setRequestedBy(requestedBy) {
  1676. return Cards.update({ _id: this.getRealId() }, { $set: { requestedBy } });
  1677. },
  1678. getRequestedBy() {
  1679. if (this.isLinkedCard()) {
  1680. const card = Cards.findOne({ _id: this.linkedId });
  1681. if (card === undefined) {
  1682. return null;
  1683. } else {
  1684. return card.requestedBy;
  1685. }
  1686. } else {
  1687. return this.requestedBy;
  1688. }
  1689. },
  1690. setAssignedBy(assignedBy) {
  1691. return Cards.update({ _id: this.getRealId() }, { $set: { assignedBy } });
  1692. },
  1693. getAssignedBy() {
  1694. if (this.isLinkedCard()) {
  1695. const card = Cards.findOne({ _id: this.linkedId });
  1696. if (card === undefined) {
  1697. return null;
  1698. } else {
  1699. return card.assignedBy;
  1700. }
  1701. } else {
  1702. return this.assignedBy;
  1703. }
  1704. },
  1705. isTemplateCard() {
  1706. return this.type === 'template-card';
  1707. },
  1708. votePublic() {
  1709. if (this.vote) return this.vote.public;
  1710. return null;
  1711. },
  1712. voteAllowNonBoardMembers() {
  1713. if (this.vote) return this.vote.allowNonBoardMembers;
  1714. return null;
  1715. },
  1716. voteCountNegative() {
  1717. if (this.vote && this.vote.negative) return this.vote.negative.length;
  1718. return null;
  1719. },
  1720. voteCountPositive() {
  1721. if (this.vote && this.vote.positive) return this.vote.positive.length;
  1722. return null;
  1723. },
  1724. voteCount() {
  1725. return this.voteCountPositive() + this.voteCountNegative();
  1726. },
  1727. pokerAllowNonBoardMembers() {
  1728. if (this.poker) return this.poker.allowNonBoardMembers;
  1729. return null;
  1730. },
  1731. pokerCountOne() {
  1732. if (this.poker && this.poker.one) return this.poker.one.length;
  1733. return null;
  1734. },
  1735. pokerCountTwo() {
  1736. if (this.poker && this.poker.two) return this.poker.two.length;
  1737. return null;
  1738. },
  1739. pokerCountThree() {
  1740. if (this.poker && this.poker.three) return this.poker.three.length;
  1741. return null;
  1742. },
  1743. pokerCountFive() {
  1744. if (this.poker && this.poker.five) return this.poker.five.length;
  1745. return null;
  1746. },
  1747. pokerCountEight() {
  1748. if (this.poker && this.poker.eight) return this.poker.eight.length;
  1749. return null;
  1750. },
  1751. pokerCountThirteen() {
  1752. if (this.poker && this.poker.thirteen) return this.poker.thirteen.length;
  1753. return null;
  1754. },
  1755. pokerCountTwenty() {
  1756. if (this.poker && this.poker.twenty) return this.poker.twenty.length;
  1757. return null;
  1758. },
  1759. pokerCountForty() {
  1760. if (this.poker && this.poker.forty) return this.poker.forty.length;
  1761. return null;
  1762. },
  1763. pokerCountOneHundred() {
  1764. if (this.poker && this.poker.oneHundred) return this.poker.oneHundred.length;
  1765. return null;
  1766. },
  1767. pokerCountUnsure() {
  1768. if (this.poker && this.poker.unsure) return this.poker.unsure.length;
  1769. return null;
  1770. },
  1771. pokerCount() {
  1772. return (
  1773. this.pokerCountOne() +
  1774. this.pokerCountTwo() +
  1775. this.pokerCountThree() +
  1776. this.pokerCountFive() +
  1777. this.pokerCountEight() +
  1778. this.pokerCountThirteen() +
  1779. this.pokerCountTwenty() +
  1780. this.pokerCountForty() +
  1781. this.pokerCountOneHundred() +
  1782. this.pokerCountUnsure()
  1783. );
  1784. },
  1785. pokerWinner() {
  1786. const pokerListMaps = [];
  1787. let pokerWinnersListMap = [];
  1788. if (this.expiredPoker()) {
  1789. const one = { count: this.pokerCountOne(), pokerCard: 1 };
  1790. const two = { count: this.pokerCountTwo(), pokerCard: 2 };
  1791. const three = { count: this.pokerCountThree(), pokerCard: 3 };
  1792. const five = { count: this.pokerCountFive(), pokerCard: 5 };
  1793. const eight = { count: this.pokerCountEight(), pokerCard: 8 };
  1794. const thirteen = { count: this.pokerCountThirteen(), pokerCard: 13 };
  1795. const twenty = { count: this.pokerCountTwenty(), pokerCard: 20 };
  1796. const forty = { count: this.pokerCountForty(), pokerCard: 40 };
  1797. const oneHundred = { count: this.pokerCountOneHundred(), pokerCard: 100 };
  1798. const unsure = { count: this.pokerCountUnsure(), pokerCard: 'Unsure' };
  1799. pokerListMaps.push(one);
  1800. pokerListMaps.push(two);
  1801. pokerListMaps.push(three);
  1802. pokerListMaps.push(five);
  1803. pokerListMaps.push(eight);
  1804. pokerListMaps.push(thirteen);
  1805. pokerListMaps.push(twenty);
  1806. pokerListMaps.push(forty);
  1807. pokerListMaps.push(oneHundred);
  1808. pokerListMaps.push(unsure);
  1809. pokerListMaps.sort(function(a, b) {
  1810. return b.count - a.count;
  1811. });
  1812. const max = pokerListMaps[0].count;
  1813. pokerWinnersListMap = pokerListMaps.filter(task => task.count >= max);
  1814. pokerWinnersListMap.sort(function(a, b) {
  1815. return b.pokerCard - a.pokerCard;
  1816. });
  1817. }
  1818. return pokerWinnersListMap[0].pokerCard;
  1819. },
  1820. });
  1821. Cards.mutations({
  1822. applyToChildren(funct) {
  1823. Cards.find({
  1824. parentId: this._id,
  1825. }).forEach(card => {
  1826. funct(card);
  1827. });
  1828. },
  1829. archive() {
  1830. this.applyToChildren(card => {
  1831. return card.archive();
  1832. });
  1833. return {
  1834. $set: {
  1835. archived: true,
  1836. archivedAt: new Date(),
  1837. },
  1838. };
  1839. },
  1840. restore() {
  1841. this.applyToChildren(card => {
  1842. return card.restore();
  1843. });
  1844. return {
  1845. $set: {
  1846. archived: false,
  1847. },
  1848. };
  1849. },
  1850. moveToEndOfList({ listId } = {}) {
  1851. let swimlaneId = this.swimlaneId;
  1852. const boardId = this.boardId;
  1853. let sortIndex = 0;
  1854. // This should never happen, but there was a bug that was fixed in commit
  1855. // ea0239538a68e225c867411a4f3e0d27c158383.
  1856. if (!swimlaneId) {
  1857. const board = ReactiveCache.getBoard(boardId);
  1858. swimlaneId = board.getDefaultSwimline()._id;
  1859. }
  1860. // Move the minicard to the end of the target list
  1861. let parentElementDom = $(`#swimlane-${this.swimlaneId}`).get(0);
  1862. if (!parentElementDom) parentElementDom = $(':root');
  1863. const lastCardDom = $(parentElementDom)
  1864. .find(`#js-list-${listId} .js-minicard:last`)
  1865. .get(0);
  1866. if (lastCardDom) sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  1867. return this.moveOptionalArgs({
  1868. boardId,
  1869. swimlaneId,
  1870. listId,
  1871. sort: sortIndex,
  1872. });
  1873. },
  1874. moveOptionalArgs({ boardId, swimlaneId, listId, sort } = {}) {
  1875. boardId = boardId || this.boardId;
  1876. swimlaneId = swimlaneId || this.swimlaneId;
  1877. // This should never happen, but there was a bug that was fixed in commit
  1878. // ea0239538a68e225c867411a4f3e0d27c158383.
  1879. if (!swimlaneId) {
  1880. const board = ReactiveCache.getBoard(boardId);
  1881. swimlaneId = board.getDefaultSwimline()._id;
  1882. }
  1883. listId = listId || this.listId;
  1884. if (sort === undefined || sort === null) sort = this.sort;
  1885. return this.move(boardId, swimlaneId, listId, sort);
  1886. },
  1887. move(boardId, swimlaneId, listId, sort = null) {
  1888. const mutatedFields = {
  1889. boardId,
  1890. swimlaneId,
  1891. listId,
  1892. };
  1893. if (sort !== null) {
  1894. mutatedFields.sort = sort;
  1895. }
  1896. // we must only copy the labels and custom fields if the target board
  1897. // differs from the source board
  1898. if (this.boardId !== boardId) {
  1899. // Get label names
  1900. const oldBoard = ReactiveCache.getBoard(this.boardId);
  1901. const oldBoardLabels = oldBoard.labels;
  1902. const oldCardLabels = _.pluck(
  1903. _.filter(oldBoardLabels, label => {
  1904. return _.contains(this.labelIds, label._id);
  1905. }),
  1906. 'name',
  1907. );
  1908. const newBoard = ReactiveCache.getBoard(boardId);
  1909. const newBoardLabels = newBoard.labels;
  1910. const newCardLabelIds = _.pluck(
  1911. _.filter(newBoardLabels, label => {
  1912. return label.name && _.contains(oldCardLabels, label.name);
  1913. }),
  1914. '_id',
  1915. );
  1916. // assign the new card number from the target board
  1917. const newCardNumber = newBoard.getNextCardNumber();
  1918. Object.assign(mutatedFields, {
  1919. labelIds: newCardLabelIds,
  1920. cardNumber: newCardNumber
  1921. });
  1922. mutatedFields.customFields = this.mapCustomFieldsToBoard(newBoard._id);
  1923. }
  1924. Cards.update(this._id, {
  1925. $set: mutatedFields,
  1926. });
  1927. },
  1928. addLabel(labelId) {
  1929. this.labelIds.push(labelId);
  1930. return {
  1931. $addToSet: {
  1932. labelIds: labelId,
  1933. },
  1934. };
  1935. },
  1936. removeLabel(labelId) {
  1937. this.labelIds = _.without(this.labelIds, labelId);
  1938. return {
  1939. $pull: {
  1940. labelIds: labelId,
  1941. },
  1942. };
  1943. },
  1944. toggleLabel(labelId) {
  1945. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1946. return this.removeLabel(labelId);
  1947. } else {
  1948. return this.addLabel(labelId);
  1949. }
  1950. },
  1951. setColor(newColor) {
  1952. if (newColor === 'white') {
  1953. newColor = null;
  1954. }
  1955. return {
  1956. $set: {
  1957. color: newColor,
  1958. },
  1959. };
  1960. },
  1961. assignMember(memberId) {
  1962. return {
  1963. $addToSet: {
  1964. members: memberId,
  1965. },
  1966. };
  1967. },
  1968. assignAssignee(assigneeId) {
  1969. // If there is not any assignee, allow one assignee, not more.
  1970. /*
  1971. if (this.getAssignees().length === 0) {
  1972. return {
  1973. $addToSet: {
  1974. assignees: assigneeId,
  1975. },
  1976. };
  1977. */
  1978. // Allow more that one assignee:
  1979. // https://github.com/wekan/wekan/issues/3302
  1980. return {
  1981. $addToSet: {
  1982. assignees: assigneeId,
  1983. },
  1984. };
  1985. //} else {
  1986. // return false,
  1987. //}
  1988. },
  1989. unassignMember(memberId) {
  1990. return {
  1991. $pull: {
  1992. members: memberId,
  1993. },
  1994. };
  1995. },
  1996. unassignAssignee(assigneeId) {
  1997. return {
  1998. $pull: {
  1999. assignees: assigneeId,
  2000. },
  2001. };
  2002. },
  2003. toggleMember(memberId) {
  2004. if (this.members && this.members.indexOf(memberId) > -1) {
  2005. return this.unassignMember(memberId);
  2006. } else {
  2007. return this.assignMember(memberId);
  2008. }
  2009. },
  2010. toggleAssignee(assigneeId) {
  2011. if (this.assignees && this.assignees.indexOf(assigneeId) > -1) {
  2012. return this.unassignAssignee(assigneeId);
  2013. } else {
  2014. return this.assignAssignee(assigneeId);
  2015. }
  2016. },
  2017. assignCustomField(customFieldId) {
  2018. return {
  2019. $addToSet: {
  2020. customFields: {
  2021. _id: customFieldId,
  2022. value: null,
  2023. },
  2024. },
  2025. };
  2026. },
  2027. unassignCustomField(customFieldId) {
  2028. return {
  2029. $pull: {
  2030. customFields: {
  2031. _id: customFieldId,
  2032. },
  2033. },
  2034. };
  2035. },
  2036. toggleCustomField(customFieldId) {
  2037. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  2038. return this.unassignCustomField(customFieldId);
  2039. } else {
  2040. return this.assignCustomField(customFieldId);
  2041. }
  2042. },
  2043. setCustomField(customFieldId, value) {
  2044. // todo
  2045. const index = this.customFieldIndex(customFieldId);
  2046. if (index > -1) {
  2047. const update = {
  2048. $set: {},
  2049. };
  2050. update.$set[`customFields.${index}.value`] = value;
  2051. return update;
  2052. }
  2053. // TODO
  2054. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  2055. return null;
  2056. },
  2057. setCover(coverId) {
  2058. return {
  2059. $set: {
  2060. coverId,
  2061. },
  2062. };
  2063. },
  2064. unsetCover() {
  2065. return {
  2066. $unset: {
  2067. coverId: '',
  2068. },
  2069. };
  2070. },
  2071. //setReceived(receivedAt) {
  2072. // return {
  2073. // $set: {
  2074. // receivedAt,
  2075. // },
  2076. // };
  2077. //},
  2078. unsetReceived() {
  2079. return {
  2080. $unset: {
  2081. receivedAt: '',
  2082. },
  2083. };
  2084. },
  2085. //setStart(startAt) {
  2086. // return {
  2087. // $set: {
  2088. // startAt,
  2089. // },
  2090. // };
  2091. //},
  2092. unsetStart() {
  2093. return {
  2094. $unset: {
  2095. startAt: '',
  2096. },
  2097. };
  2098. },
  2099. //setDue(dueAt) {
  2100. // return {
  2101. // $set: {
  2102. // dueAt,
  2103. // },
  2104. // };
  2105. //},
  2106. unsetDue() {
  2107. return {
  2108. $unset: {
  2109. dueAt: '',
  2110. },
  2111. };
  2112. },
  2113. //setEnd(endAt) {
  2114. // return {
  2115. // $set: {
  2116. // endAt,
  2117. // },
  2118. // };
  2119. //},
  2120. unsetEnd() {
  2121. return {
  2122. $unset: {
  2123. endAt: '',
  2124. },
  2125. };
  2126. },
  2127. setOvertime(isOvertime) {
  2128. return {
  2129. $set: {
  2130. isOvertime,
  2131. },
  2132. };
  2133. },
  2134. setSpentTime(spentTime) {
  2135. return {
  2136. $set: {
  2137. spentTime,
  2138. },
  2139. };
  2140. },
  2141. unsetSpentTime() {
  2142. return {
  2143. $unset: {
  2144. spentTime: '',
  2145. isOvertime: false,
  2146. },
  2147. };
  2148. },
  2149. setParentId(parentId) {
  2150. return {
  2151. $set: {
  2152. parentId,
  2153. },
  2154. };
  2155. },
  2156. setVoteQuestion(question, publicVote, allowNonBoardMembers) {
  2157. return {
  2158. $set: {
  2159. vote: {
  2160. question,
  2161. public: publicVote,
  2162. allowNonBoardMembers,
  2163. positive: [],
  2164. negative: [],
  2165. },
  2166. },
  2167. };
  2168. },
  2169. unsetVote() {
  2170. return {
  2171. $unset: {
  2172. vote: '',
  2173. },
  2174. };
  2175. },
  2176. setVoteEnd(end) {
  2177. return {
  2178. $set: { 'vote.end': end },
  2179. };
  2180. },
  2181. unsetVoteEnd() {
  2182. return {
  2183. $unset: { 'vote.end': '' },
  2184. };
  2185. },
  2186. setVote(userId, forIt) {
  2187. switch (forIt) {
  2188. case true:
  2189. // vote for it
  2190. return {
  2191. $pull: {
  2192. 'vote.negative': userId,
  2193. },
  2194. $addToSet: {
  2195. 'vote.positive': userId,
  2196. },
  2197. };
  2198. case false:
  2199. // vote against
  2200. return {
  2201. $pull: {
  2202. 'vote.positive': userId,
  2203. },
  2204. $addToSet: {
  2205. 'vote.negative': userId,
  2206. },
  2207. };
  2208. default:
  2209. // Remove votes
  2210. return {
  2211. $pull: {
  2212. 'vote.positive': userId,
  2213. 'vote.negative': userId,
  2214. },
  2215. };
  2216. }
  2217. },
  2218. setPokerQuestion(question, allowNonBoardMembers) {
  2219. return {
  2220. $set: {
  2221. poker: {
  2222. question,
  2223. allowNonBoardMembers,
  2224. one: [],
  2225. two: [],
  2226. three: [],
  2227. five: [],
  2228. eight: [],
  2229. thirteen: [],
  2230. twenty: [],
  2231. forty: [],
  2232. oneHundred: [],
  2233. unsure: [],
  2234. },
  2235. },
  2236. };
  2237. },
  2238. setPokerEstimation(estimation) {
  2239. return {
  2240. $set: { 'poker.estimation': estimation },
  2241. };
  2242. },
  2243. unsetPokerEstimation() {
  2244. return {
  2245. $unset: { 'poker.estimation': '' },
  2246. };
  2247. },
  2248. unsetPoker() {
  2249. return {
  2250. $unset: {
  2251. poker: '',
  2252. },
  2253. };
  2254. },
  2255. setPokerEnd(end) {
  2256. return {
  2257. $set: { 'poker.end': end },
  2258. };
  2259. },
  2260. unsetPokerEnd() {
  2261. return {
  2262. $unset: { 'poker.end': '' },
  2263. };
  2264. },
  2265. setPoker(userId, state) {
  2266. switch (state) {
  2267. case 'one':
  2268. // poker one
  2269. return {
  2270. $pull: {
  2271. 'poker.two': userId,
  2272. 'poker.three': userId,
  2273. 'poker.five': userId,
  2274. 'poker.eight': userId,
  2275. 'poker.thirteen': userId,
  2276. 'poker.twenty': userId,
  2277. 'poker.forty': userId,
  2278. 'poker.oneHundred': userId,
  2279. 'poker.unsure': userId,
  2280. },
  2281. $addToSet: {
  2282. 'poker.one': userId,
  2283. },
  2284. };
  2285. case 'two':
  2286. // poker two
  2287. return {
  2288. $pull: {
  2289. 'poker.one': userId,
  2290. 'poker.three': userId,
  2291. 'poker.five': userId,
  2292. 'poker.eight': userId,
  2293. 'poker.thirteen': userId,
  2294. 'poker.twenty': userId,
  2295. 'poker.forty': userId,
  2296. 'poker.oneHundred': userId,
  2297. 'poker.unsure': userId,
  2298. },
  2299. $addToSet: {
  2300. 'poker.two': userId,
  2301. },
  2302. };
  2303. case 'three':
  2304. // poker three
  2305. return {
  2306. $pull: {
  2307. 'poker.one': userId,
  2308. 'poker.two': userId,
  2309. 'poker.five': userId,
  2310. 'poker.eight': userId,
  2311. 'poker.thirteen': userId,
  2312. 'poker.twenty': userId,
  2313. 'poker.forty': userId,
  2314. 'poker.oneHundred': userId,
  2315. 'poker.unsure': userId,
  2316. },
  2317. $addToSet: {
  2318. 'poker.three': userId,
  2319. },
  2320. };
  2321. case 'five':
  2322. // poker five
  2323. return {
  2324. $pull: {
  2325. 'poker.one': userId,
  2326. 'poker.two': userId,
  2327. 'poker.three': userId,
  2328. 'poker.eight': userId,
  2329. 'poker.thirteen': userId,
  2330. 'poker.twenty': userId,
  2331. 'poker.forty': userId,
  2332. 'poker.oneHundred': userId,
  2333. 'poker.unsure': userId,
  2334. },
  2335. $addToSet: {
  2336. 'poker.five': userId,
  2337. },
  2338. };
  2339. case 'eight':
  2340. // poker eight
  2341. return {
  2342. $pull: {
  2343. 'poker.one': userId,
  2344. 'poker.two': userId,
  2345. 'poker.three': userId,
  2346. 'poker.five': userId,
  2347. 'poker.thirteen': userId,
  2348. 'poker.twenty': userId,
  2349. 'poker.forty': userId,
  2350. 'poker.oneHundred': userId,
  2351. 'poker.unsure': userId,
  2352. },
  2353. $addToSet: {
  2354. 'poker.eight': userId,
  2355. },
  2356. };
  2357. case 'thirteen':
  2358. // poker thirteen
  2359. return {
  2360. $pull: {
  2361. 'poker.one': userId,
  2362. 'poker.two': userId,
  2363. 'poker.three': userId,
  2364. 'poker.five': userId,
  2365. 'poker.eight': userId,
  2366. 'poker.twenty': userId,
  2367. 'poker.forty': userId,
  2368. 'poker.oneHundred': userId,
  2369. 'poker.unsure': userId,
  2370. },
  2371. $addToSet: {
  2372. 'poker.thirteen': userId,
  2373. },
  2374. };
  2375. case 'twenty':
  2376. // poker twenty
  2377. return {
  2378. $pull: {
  2379. 'poker.one': userId,
  2380. 'poker.two': userId,
  2381. 'poker.three': userId,
  2382. 'poker.five': userId,
  2383. 'poker.eight': userId,
  2384. 'poker.thirteen': userId,
  2385. 'poker.forty': userId,
  2386. 'poker.oneHundred': userId,
  2387. 'poker.unsure': userId,
  2388. },
  2389. $addToSet: {
  2390. 'poker.twenty': userId,
  2391. },
  2392. };
  2393. case 'forty':
  2394. // poker forty
  2395. return {
  2396. $pull: {
  2397. 'poker.one': userId,
  2398. 'poker.two': userId,
  2399. 'poker.three': userId,
  2400. 'poker.five': userId,
  2401. 'poker.eight': userId,
  2402. 'poker.thirteen': userId,
  2403. 'poker.twenty': userId,
  2404. 'poker.oneHundred': userId,
  2405. 'poker.unsure': userId,
  2406. },
  2407. $addToSet: {
  2408. 'poker.forty': userId,
  2409. },
  2410. };
  2411. case 'oneHundred':
  2412. // poker one hundred
  2413. return {
  2414. $pull: {
  2415. 'poker.one': userId,
  2416. 'poker.two': userId,
  2417. 'poker.three': userId,
  2418. 'poker.five': userId,
  2419. 'poker.eight': userId,
  2420. 'poker.thirteen': userId,
  2421. 'poker.twenty': userId,
  2422. 'poker.forty': userId,
  2423. 'poker.unsure': userId,
  2424. },
  2425. $addToSet: {
  2426. 'poker.oneHundred': userId,
  2427. },
  2428. };
  2429. case 'unsure':
  2430. // poker unsure
  2431. return {
  2432. $pull: {
  2433. 'poker.one': userId,
  2434. 'poker.two': userId,
  2435. 'poker.three': userId,
  2436. 'poker.five': userId,
  2437. 'poker.eight': userId,
  2438. 'poker.thirteen': userId,
  2439. 'poker.twenty': userId,
  2440. 'poker.forty': userId,
  2441. 'poker.oneHundred': userId,
  2442. },
  2443. $addToSet: {
  2444. 'poker.unsure': userId,
  2445. },
  2446. };
  2447. default:
  2448. // Remove pokers
  2449. return {
  2450. $pull: {
  2451. 'poker.one': userId,
  2452. 'poker.two': userId,
  2453. 'poker.three': userId,
  2454. 'poker.five': userId,
  2455. 'poker.eight': userId,
  2456. 'poker.thirteen': userId,
  2457. 'poker.twenty': userId,
  2458. 'poker.forty': userId,
  2459. 'poker.oneHundred': userId,
  2460. 'poker.unsure': userId,
  2461. },
  2462. };
  2463. }
  2464. },
  2465. replayPoker() {
  2466. return {
  2467. $set: {
  2468. 'poker.one': [],
  2469. 'poker.two': [],
  2470. 'poker.three': [],
  2471. 'poker.five': [],
  2472. 'poker.eight': [],
  2473. 'poker.thirteen': [],
  2474. 'poker.twelve': [],
  2475. 'poker.forty': [],
  2476. 'poker.oneHundred': [],
  2477. 'poker.unsure': [],
  2478. },
  2479. };
  2480. },
  2481. });
  2482. //FUNCTIONS FOR creation of Activities
  2483. function updateActivities(doc, fieldNames, modifier) {
  2484. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  2485. Activities.find({
  2486. activityType: 'addedLabel',
  2487. cardId: doc._id,
  2488. }).forEach(a => {
  2489. const lidx = doc.labelIds.indexOf(a.labelId);
  2490. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  2491. Activities.update(a._id, {
  2492. $set: {
  2493. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  2494. boardId: modifier.$set.boardId,
  2495. },
  2496. });
  2497. } else {
  2498. Activities.remove(a._id);
  2499. }
  2500. });
  2501. } else if (_.contains(fieldNames, 'boardId')) {
  2502. Activities.remove({
  2503. activityType: 'addedLabel',
  2504. cardId: doc._id,
  2505. });
  2506. }
  2507. }
  2508. function cardMove(
  2509. userId,
  2510. doc,
  2511. fieldNames,
  2512. oldListId,
  2513. oldSwimlaneId,
  2514. oldBoardId,
  2515. ) {
  2516. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  2517. Activities.insert({
  2518. userId,
  2519. activityType: 'moveCardBoard',
  2520. boardName: ReactiveCache.getBoard(doc.boardId).title,
  2521. boardId: doc.boardId,
  2522. oldBoardId,
  2523. oldBoardName: ReactiveCache.getBoard(oldBoardId).title,
  2524. cardId: doc._id,
  2525. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2526. swimlaneId: doc.swimlaneId,
  2527. oldSwimlaneId,
  2528. });
  2529. } else if (
  2530. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  2531. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  2532. ) {
  2533. Activities.insert({
  2534. userId,
  2535. oldListId,
  2536. activityType: 'moveCard',
  2537. listName: ReactiveCache.getList(doc.listId).title,
  2538. listId: doc.listId,
  2539. boardId: doc.boardId,
  2540. cardId: doc._id,
  2541. cardTitle: doc.title,
  2542. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2543. swimlaneId: doc.swimlaneId,
  2544. oldSwimlaneId,
  2545. });
  2546. }
  2547. }
  2548. function cardState(userId, doc, fieldNames) {
  2549. if (_.contains(fieldNames, 'archived')) {
  2550. if (doc.archived) {
  2551. Activities.insert({
  2552. userId,
  2553. activityType: 'archivedCard',
  2554. listName: ReactiveCache.getList(doc.listId).title,
  2555. boardId: doc.boardId,
  2556. listId: doc.listId,
  2557. cardId: doc._id,
  2558. swimlaneId: doc.swimlaneId,
  2559. });
  2560. } else {
  2561. Activities.insert({
  2562. userId,
  2563. activityType: 'restoredCard',
  2564. boardId: doc.boardId,
  2565. listName: ReactiveCache.getList(doc.listId).title,
  2566. listId: doc.listId,
  2567. cardId: doc._id,
  2568. swimlaneId: doc.swimlaneId,
  2569. });
  2570. }
  2571. }
  2572. }
  2573. function cardMembers(userId, doc, fieldNames, modifier) {
  2574. if (!_.contains(fieldNames, 'members')) return;
  2575. let memberId;
  2576. // Say hello to the new member
  2577. if (modifier.$addToSet && modifier.$addToSet.members) {
  2578. memberId = modifier.$addToSet.members;
  2579. const username = Users.findOne(memberId).username;
  2580. if (!_.contains(doc.members, memberId)) {
  2581. Activities.insert({
  2582. userId,
  2583. username,
  2584. activityType: 'joinMember',
  2585. boardId: doc.boardId,
  2586. cardId: doc._id,
  2587. memberId,
  2588. listId: doc.listId,
  2589. swimlaneId: doc.swimlaneId,
  2590. });
  2591. }
  2592. }
  2593. // Say goodbye to the former member
  2594. if (modifier.$pull && modifier.$pull.members) {
  2595. memberId = modifier.$pull.members;
  2596. const username = Users.findOne(memberId).username;
  2597. // Check that the former member is member of the card
  2598. if (_.contains(doc.members, memberId)) {
  2599. Activities.insert({
  2600. userId,
  2601. username,
  2602. activityType: 'unjoinMember',
  2603. boardId: doc.boardId,
  2604. cardId: doc._id,
  2605. memberId,
  2606. listId: doc.listId,
  2607. swimlaneId: doc.swimlaneId,
  2608. });
  2609. }
  2610. }
  2611. }
  2612. function cardAssignees(userId, doc, fieldNames, modifier) {
  2613. if (!_.contains(fieldNames, 'assignees')) return;
  2614. let assigneeId;
  2615. // Say hello to the new assignee
  2616. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  2617. assigneeId = modifier.$addToSet.assignees;
  2618. const username = Users.findOne(assigneeId).username;
  2619. if (!_.contains(doc.assignees, assigneeId)) {
  2620. Activities.insert({
  2621. userId,
  2622. username,
  2623. activityType: 'joinAssignee',
  2624. boardId: doc.boardId,
  2625. cardId: doc._id,
  2626. assigneeId,
  2627. listId: doc.listId,
  2628. swimlaneId: doc.swimlaneId,
  2629. });
  2630. }
  2631. }
  2632. // Say goodbye to the former assignee
  2633. if (modifier.$pull && modifier.$pull.assignees) {
  2634. assigneeId = modifier.$pull.assignees;
  2635. const username = Users.findOne(assigneeId).username;
  2636. // Check that the former assignee is assignee of the card
  2637. if (_.contains(doc.assignees, assigneeId)) {
  2638. Activities.insert({
  2639. userId,
  2640. username,
  2641. activityType: 'unjoinAssignee',
  2642. boardId: doc.boardId,
  2643. cardId: doc._id,
  2644. assigneeId,
  2645. listId: doc.listId,
  2646. swimlaneId: doc.swimlaneId,
  2647. });
  2648. }
  2649. }
  2650. }
  2651. function cardLabels(userId, doc, fieldNames, modifier) {
  2652. if (!_.contains(fieldNames, 'labelIds')) return;
  2653. let labelId;
  2654. // Say hello to the new label
  2655. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  2656. labelId = modifier.$addToSet.labelIds;
  2657. if (!_.contains(doc.labelIds, labelId)) {
  2658. const act = {
  2659. userId,
  2660. labelId,
  2661. activityType: 'addedLabel',
  2662. boardId: doc.boardId,
  2663. cardId: doc._id,
  2664. listId: doc.listId,
  2665. swimlaneId: doc.swimlaneId,
  2666. };
  2667. Activities.insert(act);
  2668. }
  2669. }
  2670. // Say goodbye to the label
  2671. if (modifier.$pull && modifier.$pull.labelIds) {
  2672. labelId = modifier.$pull.labelIds;
  2673. // Check that the former member is member of the card
  2674. if (_.contains(doc.labelIds, labelId)) {
  2675. Activities.insert({
  2676. userId,
  2677. labelId,
  2678. activityType: 'removedLabel',
  2679. boardId: doc.boardId,
  2680. cardId: doc._id,
  2681. listId: doc.listId,
  2682. swimlaneId: doc.swimlaneId,
  2683. });
  2684. }
  2685. }
  2686. }
  2687. function cardCustomFields(userId, doc, fieldNames, modifier) {
  2688. if (!_.contains(fieldNames, 'customFields')) return;
  2689. // Say hello to the new customField value
  2690. if (modifier.$set) {
  2691. _.each(modifier.$set, (value, key) => {
  2692. if (key.startsWith('customFields')) {
  2693. const dotNotation = key.split('.');
  2694. // only individual changes are registered
  2695. if (dotNotation.length > 1) {
  2696. const customFieldId = doc.customFields[dotNotation[1]]._id;
  2697. const act = {
  2698. userId,
  2699. customFieldId,
  2700. value,
  2701. activityType: 'setCustomField',
  2702. boardId: doc.boardId,
  2703. cardId: doc._id,
  2704. listId: doc.listId,
  2705. swimlaneId: doc.swimlaneId,
  2706. };
  2707. Activities.insert(act);
  2708. }
  2709. }
  2710. });
  2711. }
  2712. // Say goodbye to the former customField value
  2713. if (modifier.$unset) {
  2714. _.each(modifier.$unset, (value, key) => {
  2715. if (key.startsWith('customFields')) {
  2716. const dotNotation = key.split('.');
  2717. // only individual changes are registered
  2718. if (dotNotation.length > 1) {
  2719. const customFieldId = doc.customFields[dotNotation[1]]._id;
  2720. const act = {
  2721. userId,
  2722. customFieldId,
  2723. activityType: 'unsetCustomField',
  2724. boardId: doc.boardId,
  2725. cardId: doc._id,
  2726. };
  2727. Activities.insert(act);
  2728. }
  2729. }
  2730. });
  2731. }
  2732. }
  2733. function cardCreation(userId, doc) {
  2734. Activities.insert({
  2735. userId,
  2736. activityType: 'createCard',
  2737. boardId: doc.boardId,
  2738. listName: ReactiveCache.getList(doc.listId).title,
  2739. listId: doc.listId,
  2740. cardId: doc._id,
  2741. cardTitle: doc.title,
  2742. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2743. swimlaneId: doc.swimlaneId,
  2744. });
  2745. }
  2746. Meteor.methods({
  2747. createCardWithDueDate: function(boardId, listId, title, dueDate, swimlaneId) {
  2748. check(boardId, String);
  2749. check(listId, String);
  2750. check(title, String);
  2751. check(dueDate, Date);
  2752. check(swimlaneId, String);
  2753. const card = {
  2754. title,
  2755. listId,
  2756. boardId,
  2757. swimlaneId,
  2758. createdAt: new Date(),
  2759. dueAt: dueDate,
  2760. sort: 0,
  2761. };
  2762. const cardId = Cards.insert(card);
  2763. return cardId;
  2764. },
  2765. });
  2766. function cardRemover(userId, doc) {
  2767. ChecklistItems.remove({
  2768. cardId: doc._id,
  2769. });
  2770. Checklists.remove({
  2771. cardId: doc._id,
  2772. });
  2773. Cards.remove({
  2774. parentId: doc._id,
  2775. });
  2776. CardComments.remove({
  2777. cardId: doc._id,
  2778. });
  2779. Attachments.remove({
  2780. cardId: doc._id,
  2781. });
  2782. }
  2783. const findDueCards = days => {
  2784. const seekDue = ($from, $to, activityType) => {
  2785. Cards.find({
  2786. archived: false,
  2787. dueAt: { $gte: $from, $lt: $to },
  2788. }).forEach(card => {
  2789. const username = Users.findOne(card.userId).username;
  2790. const activity = {
  2791. userId: card.userId,
  2792. username,
  2793. activityType,
  2794. boardId: card.boardId,
  2795. cardId: card._id,
  2796. cardTitle: card.title,
  2797. listId: card.listId,
  2798. timeValue: card.dueAt,
  2799. swimlaneId: card.swimlaneId,
  2800. };
  2801. Activities.insert(activity);
  2802. });
  2803. };
  2804. const now = new Date(),
  2805. aday = 3600 * 24 * 1e3,
  2806. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  2807. if (!days) return;
  2808. if (!days.map) days = [days];
  2809. days.map(day => {
  2810. let args = [];
  2811. if (day === 0) {
  2812. args = [then(0), then(1), 'duenow'];
  2813. } else if (day > 0) {
  2814. args = [then(1), then(day), 'almostdue'];
  2815. } else {
  2816. args = [then(day), now, 'pastdue'];
  2817. }
  2818. seekDue(...args);
  2819. });
  2820. };
  2821. const addCronJob = _.debounce(
  2822. Meteor.bindEnvironment(function findDueCardsDebounced() {
  2823. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  2824. if (!envValue) {
  2825. return;
  2826. }
  2827. const notifydays = envValue
  2828. .split(',')
  2829. .map(value => {
  2830. const iValue = parseInt(value, 10);
  2831. if (!(iValue > 0 && iValue < 15)) {
  2832. // notifying due is disabled
  2833. return false;
  2834. } else {
  2835. return iValue;
  2836. }
  2837. })
  2838. .filter(Boolean);
  2839. 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
  2840. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  2841. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  2842. const scheduler = (job => () => {
  2843. const now = new Date();
  2844. const hour = 3600 * 1e3;
  2845. if (now.getHours() === itvl) {
  2846. if (typeof job === 'function') {
  2847. job();
  2848. }
  2849. }
  2850. Meteor.setTimeout(scheduler, hour);
  2851. })(() => {
  2852. findDueCards(notifydays);
  2853. });
  2854. scheduler();
  2855. }),
  2856. 500,
  2857. );
  2858. if (Meteor.isServer) {
  2859. Meteor.methods({
  2860. /** copies a card
  2861. * <li> this method is needed on the server because attachments can only be copied on the server (access to file system)
  2862. * @param card id to copy
  2863. * @param boardId copy to this board
  2864. * @param swimlandeId copy to this swimlane id
  2865. * @param listId copy to this list id
  2866. * @param insertAtTop insert the card at the top?
  2867. * @param mergeCardValues this values into the copied card
  2868. * @return the new card id
  2869. */
  2870. copyCard(cardId, boardId, swimlaneId, listId, insertAtTop, mergeCardValues) {
  2871. check(cardId, String);
  2872. check(boardId, String);
  2873. check(swimlaneId, String);
  2874. check(listId, String);
  2875. check(insertAtTop, Boolean);
  2876. check(mergeCardValues, Object);
  2877. const card = Cards.findOne({_id: cardId});
  2878. Object.assign(card, mergeCardValues);
  2879. const sort = card.getSort(listId, swimlaneId, insertAtTop);
  2880. if (insertAtTop) {
  2881. card.sort = sort - 1;
  2882. } else
  2883. {
  2884. card.sort = sort + 1;
  2885. }
  2886. const ret = card.copy(boardId, swimlaneId, listId);
  2887. return ret;
  2888. },
  2889. });
  2890. // Cards are often fetched within a board, so we create an index to make these
  2891. // queries more efficient.
  2892. Meteor.startup(() => {
  2893. Cards._collection.createIndex({ modifiedAt: -1 });
  2894. Cards._collection.createIndex({ boardId: 1, createdAt: -1 });
  2895. // https://github.com/wekan/wekan/issues/1863
  2896. // Swimlane added a new field in the cards collection of mongodb named parentId.
  2897. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  2898. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  2899. // To correct it, add Index to parentId:
  2900. Cards._collection.createIndex({ parentId: 1 });
  2901. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  2902. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  2903. // Meteor.call("findDueCards",notifydays,notifyitvl);
  2904. Meteor.defer(() => {
  2905. addCronJob();
  2906. });
  2907. });
  2908. Cards.after.insert((userId, doc) => {
  2909. cardCreation(userId, doc);
  2910. });
  2911. // New activity for card (un)archivage
  2912. Cards.after.update((userId, doc, fieldNames) => {
  2913. cardState(userId, doc, fieldNames);
  2914. });
  2915. //New activity for card moves
  2916. Cards.after.update(function(userId, doc, fieldNames) {
  2917. const oldListId = this.previous.listId;
  2918. const oldSwimlaneId = this.previous.swimlaneId;
  2919. const oldBoardId = this.previous.boardId;
  2920. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  2921. });
  2922. // Add a new activity if we add or remove a member to the card
  2923. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2924. cardMembers(userId, doc, fieldNames, modifier);
  2925. updateActivities(doc, fieldNames, modifier);
  2926. });
  2927. // Add a new activity if we add or remove a assignee to the card
  2928. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2929. cardAssignees(userId, doc, fieldNames, modifier);
  2930. updateActivities(doc, fieldNames, modifier);
  2931. });
  2932. // Add a new activity if we add or remove a label to the card
  2933. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2934. cardLabels(userId, doc, fieldNames, modifier);
  2935. });
  2936. // Add a new activity if we edit a custom field
  2937. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2938. cardCustomFields(userId, doc, fieldNames, modifier);
  2939. });
  2940. // Add a new activity if modify time related field like dueAt startAt etc
  2941. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2942. const dla = 'dateLastActivity';
  2943. const fields = fieldNames.filter(name => name !== dla);
  2944. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  2945. const action = fields[0];
  2946. if (fields.length > 0 && _.contains(timingaction, action)) {
  2947. // add activities for user change these attributes
  2948. const value = modifier.$set[action];
  2949. const oldvalue = doc[action] || '';
  2950. const activityType = `a-${action}`;
  2951. const card = Cards.findOne(doc._id);
  2952. const list = card.list();
  2953. if (list) {
  2954. // change list modifiedAt, when user modified the key values in
  2955. // timingaction array, if it's endAt, put the modifiedAt of list
  2956. // back to one year ago for sorting purpose
  2957. const modifiedAt = moment()
  2958. .subtract(1, 'year')
  2959. .toISOString();
  2960. const boardId = list.boardId;
  2961. Lists.direct.update(
  2962. {
  2963. _id: list._id,
  2964. },
  2965. {
  2966. $set: {
  2967. modifiedAt,
  2968. boardId,
  2969. },
  2970. },
  2971. );
  2972. }
  2973. const username = Users.findOne(userId).username;
  2974. const activity = {
  2975. userId,
  2976. username,
  2977. activityType,
  2978. boardId: doc.boardId,
  2979. cardId: doc._id,
  2980. cardTitle: doc.title,
  2981. timeKey: action,
  2982. timeValue: value,
  2983. timeOldValue: oldvalue,
  2984. listId: card.listId,
  2985. swimlaneId: card.swimlaneId,
  2986. };
  2987. Activities.insert(activity);
  2988. }
  2989. });
  2990. // Remove all activities associated with a card if we remove the card
  2991. // Remove also card_comments / checklists / attachments
  2992. Cards.before.remove((userId, doc) => {
  2993. cardRemover(userId, doc);
  2994. });
  2995. }
  2996. //SWIMLANES REST API
  2997. if (Meteor.isServer) {
  2998. /**
  2999. * @operation get_swimlane_cards
  3000. * @summary get all cards attached to a swimlane
  3001. *
  3002. * @param {string} boardId the board ID
  3003. * @param {string} swimlaneId the swimlane ID
  3004. * @return_type [{_id: string,
  3005. * title: string,
  3006. * description: string,
  3007. * listId: string}]
  3008. */
  3009. JsonRoutes.add(
  3010. 'GET',
  3011. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  3012. function(req, res) {
  3013. const paramBoardId = req.params.boardId;
  3014. const paramSwimlaneId = req.params.swimlaneId;
  3015. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3016. JsonRoutes.sendResult(res, {
  3017. code: 200,
  3018. data: Cards.find({
  3019. boardId: paramBoardId,
  3020. swimlaneId: paramSwimlaneId,
  3021. archived: false,
  3022. }).map(function(doc) {
  3023. return {
  3024. _id: doc._id,
  3025. title: doc.title,
  3026. description: doc.description,
  3027. listId: doc.listId,
  3028. receivedAt: doc.receivedAt,
  3029. startAt: doc.startAt,
  3030. dueAt: doc.dueAt,
  3031. endAt: doc.endAt,
  3032. assignees: doc.assignees,
  3033. };
  3034. }),
  3035. });
  3036. },
  3037. );
  3038. }
  3039. //LISTS REST API
  3040. if (Meteor.isServer) {
  3041. /**
  3042. * @operation get_all_cards
  3043. * @summary Get all Cards attached to a List
  3044. *
  3045. * @param {string} boardId the board ID
  3046. * @param {string} listId the list ID
  3047. * @return_type [{_id: string,
  3048. * title: string,
  3049. * description: string}]
  3050. */
  3051. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  3052. req,
  3053. res,
  3054. ) {
  3055. const paramBoardId = req.params.boardId;
  3056. const paramListId = req.params.listId;
  3057. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3058. JsonRoutes.sendResult(res, {
  3059. code: 200,
  3060. data: Cards.find({
  3061. boardId: paramBoardId,
  3062. listId: paramListId,
  3063. archived: false,
  3064. }).map(function(doc) {
  3065. return {
  3066. _id: doc._id,
  3067. title: doc.title,
  3068. description: doc.description,
  3069. receivedAt: doc.receivedAt,
  3070. startAt: doc.startAt,
  3071. dueAt: doc.dueAt,
  3072. endAt: doc.endAt,
  3073. assignees: doc.assignees,
  3074. };
  3075. }),
  3076. });
  3077. });
  3078. /**
  3079. * @operation get_card
  3080. * @summary Get a Card
  3081. *
  3082. * @param {string} boardId the board ID
  3083. * @param {string} listId the list ID of the card
  3084. * @param {string} cardId the card ID
  3085. * @return_type Cards
  3086. */
  3087. JsonRoutes.add(
  3088. 'GET',
  3089. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  3090. function(req, res) {
  3091. const paramBoardId = req.params.boardId;
  3092. const paramListId = req.params.listId;
  3093. const paramCardId = req.params.cardId;
  3094. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3095. JsonRoutes.sendResult(res, {
  3096. code: 200,
  3097. data: Cards.findOne({
  3098. _id: paramCardId,
  3099. listId: paramListId,
  3100. boardId: paramBoardId,
  3101. archived: false,
  3102. }),
  3103. });
  3104. },
  3105. );
  3106. /**
  3107. * @operation new_card
  3108. * @summary Create a new Card
  3109. *
  3110. * @param {string} boardId the board ID of the new card
  3111. * @param {string} listId the list ID of the new card
  3112. * @param {string} authorID the user ID of the person owning the card
  3113. * @param {string} parentId the parent ID of the new card
  3114. * @param {string} title the title of the new card
  3115. * @param {string} description the description of the new card
  3116. * @param {string} swimlaneId the swimlane ID of the new card
  3117. * @param {string} [members] the member IDs list of the new card
  3118. * @param {string} [assignees] the array of maximum one ID of assignee of the new card
  3119. * @return_type {_id: string}
  3120. */
  3121. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  3122. req,
  3123. res,
  3124. ) {
  3125. // Check user is logged in
  3126. Authentication.checkLoggedIn(req.userId);
  3127. const paramBoardId = req.params.boardId;
  3128. // Check user has permission to add card to the board
  3129. const board = ReactiveCache.getBoard(paramBoardId);
  3130. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  3131. Authentication.checkAdminOrCondition(req.userId, addPermission);
  3132. const paramListId = req.params.listId;
  3133. const paramParentId = req.params.parentId;
  3134. const nextCardNumber = board.getNextCardNumber();
  3135. let customFieldsArr = [];
  3136. _.forEach(
  3137. CustomFields.find({'boardIds': paramBoardId}).fetch(),
  3138. function (field) {
  3139. if (field.automaticallyOnCard || field.alwaysOnCard)
  3140. customFieldsArr.push({ _id: field._id, value: null });
  3141. },
  3142. );
  3143. const currentCards = Cards.find(
  3144. {
  3145. listId: paramListId,
  3146. archived: false,
  3147. },
  3148. { sort: ['sort'] },
  3149. );
  3150. const check = Users.findOne({
  3151. _id: req.body.authorId,
  3152. });
  3153. const members = req.body.members;
  3154. const assignees = req.body.assignees;
  3155. if (typeof check !== 'undefined') {
  3156. const id = Cards.direct.insert({
  3157. title: req.body.title,
  3158. boardId: paramBoardId,
  3159. listId: paramListId,
  3160. parentId: paramParentId,
  3161. description: req.body.description,
  3162. userId: req.body.authorId,
  3163. swimlaneId: req.body.swimlaneId,
  3164. sort: currentCards.count(),
  3165. cardNumber: nextCardNumber,
  3166. customFields: customFieldsArr,
  3167. members,
  3168. assignees,
  3169. });
  3170. JsonRoutes.sendResult(res, {
  3171. code: 200,
  3172. data: {
  3173. _id: id,
  3174. },
  3175. });
  3176. const card = Cards.findOne({
  3177. _id: id,
  3178. });
  3179. cardCreation(req.body.authorId, card);
  3180. } else {
  3181. JsonRoutes.sendResult(res, {
  3182. code: 401,
  3183. });
  3184. }
  3185. });
  3186. /**
  3187. * @operation get_board_cards_count
  3188. * @summary Get a cards count to a board
  3189. *
  3190. * @param {string} boardId the board ID
  3191. * @return_type {board_cards_count: integer}
  3192. */
  3193. JsonRoutes.add('GET', '/api/boards/:boardId/cards_count', function(
  3194. req,
  3195. res,
  3196. ) {
  3197. try {
  3198. const paramBoardId = req.params.boardId;
  3199. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3200. JsonRoutes.sendResult(res, {
  3201. code: 200,
  3202. data: {
  3203. board_cards_count: Cards.find({
  3204. boardId: paramBoardId,
  3205. archived: false,
  3206. }).count(),
  3207. }
  3208. });
  3209. } catch (error) {
  3210. JsonRoutes.sendResult(res, {
  3211. code: 200,
  3212. data: error,
  3213. });
  3214. }
  3215. });
  3216. /**
  3217. * @operation get_list_cards_count
  3218. * @summary Get a cards count to a list
  3219. *
  3220. * @param {string} boardId the board ID
  3221. * @param {string} listId the List ID
  3222. * @return_type {list_cards_count: integer}
  3223. */
  3224. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards_count', function(
  3225. req,
  3226. res,
  3227. ) {
  3228. try {
  3229. const paramBoardId = req.params.boardId;
  3230. const paramListId = req.params.listId;
  3231. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3232. JsonRoutes.sendResult(res, {
  3233. code: 200,
  3234. data: {
  3235. list_cards_count: Cards.find({
  3236. boardId: paramBoardId,
  3237. listId: paramListId,
  3238. archived: false,
  3239. }).count(),
  3240. }
  3241. });
  3242. } catch (error) {
  3243. JsonRoutes.sendResult(res, {
  3244. code: 200,
  3245. data: error,
  3246. });
  3247. }
  3248. });
  3249. /*
  3250. * Note for the JSDoc:
  3251. * 'list' will be interpreted as the path parameter
  3252. * 'listID' will be interpreted as the body parameter
  3253. */
  3254. /**
  3255. * @operation edit_card
  3256. * @summary Edit Fields in a Card
  3257. *
  3258. * @description Edit a card
  3259. *
  3260. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  3261. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  3262. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  3263. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  3264. * `indigo`:
  3265. *
  3266. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  3267. *
  3268. * Note: setting the color to white has the same effect than removing it.
  3269. *
  3270. * @param {string} boardId the board ID of the card
  3271. * @param {string} list the list ID of the card
  3272. * @param {string} cardId the ID of the card
  3273. * @param {string} [title] the new title of the card
  3274. * @param {string} [sort] the new sort value of the card
  3275. * @param {string} [listId] the new list ID of the card (move operation)
  3276. * @param {string} [description] the new description of the card
  3277. * @param {string} [authorId] change the owner of the card
  3278. * @param {string} [parentId] change the parent of the card
  3279. * @param {string} [labelIds] the new list of label IDs attached to the card
  3280. * @param {string} [swimlaneId] the new swimlane ID of the card
  3281. * @param {string} [members] the new list of member IDs attached to the card
  3282. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  3283. * @param {string} [requestedBy] the new requestedBy field of the card
  3284. * @param {string} [assignedBy] the new assignedBy field of the card
  3285. * @param {string} [receivedAt] the new receivedAt field of the card
  3286. * @param {string} [assignBy] the new assignBy field of the card
  3287. * @param {string} [startAt] the new startAt field of the card
  3288. * @param {string} [dueAt] the new dueAt field of the card
  3289. * @param {string} [endAt] the new endAt field of the card
  3290. * @param {string} [spentTime] the new spentTime field of the card
  3291. * @param {boolean} [isOverTime] the new isOverTime field of the card
  3292. * @param {string} [customFields] the new customFields value of the card
  3293. * @param {string} [color] the new color of the card
  3294. * @param {Object} [vote] the vote object
  3295. * @param {string} vote.question the vote question
  3296. * @param {boolean} vote.public show who voted what
  3297. * @param {boolean} vote.allowNonBoardMembers allow all logged in users to vote?
  3298. * @param {Object} [poker] the poker object
  3299. * @param {string} poker.question the vote question
  3300. * @param {boolean} poker.allowNonBoardMembers allow all logged in users to vote?
  3301. * @return_type {_id: string}
  3302. */
  3303. JsonRoutes.add(
  3304. 'PUT',
  3305. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  3306. function(req, res) {
  3307. const paramBoardId = req.params.boardId;
  3308. const paramCardId = req.params.cardId;
  3309. const paramListId = req.params.listId;
  3310. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3311. if (req.body.title) {
  3312. const newTitle = req.body.title;
  3313. Cards.direct.update(
  3314. {
  3315. _id: paramCardId,
  3316. listId: paramListId,
  3317. boardId: paramBoardId,
  3318. archived: false,
  3319. },
  3320. {
  3321. $set: {
  3322. title: newTitle,
  3323. },
  3324. },
  3325. );
  3326. }
  3327. if (req.body.sort) {
  3328. const newSort = req.body.sort;
  3329. Cards.direct.update(
  3330. {
  3331. _id: paramCardId,
  3332. listId: paramListId,
  3333. boardId: paramBoardId,
  3334. archived: false,
  3335. },
  3336. {
  3337. $set: {
  3338. sort: newSort,
  3339. },
  3340. },
  3341. );
  3342. }
  3343. if (req.body.parentId) {
  3344. const newParentId = req.body.parentId;
  3345. Cards.direct.update(
  3346. {
  3347. _id: paramCardId,
  3348. listId: paramListId,
  3349. boardId: paramBoardId,
  3350. archived: false,
  3351. },
  3352. {
  3353. $set: {
  3354. parentId: newParentId,
  3355. },
  3356. },
  3357. );
  3358. }
  3359. if (req.body.description) {
  3360. const newDescription = req.body.description;
  3361. Cards.direct.update(
  3362. {
  3363. _id: paramCardId,
  3364. listId: paramListId,
  3365. boardId: paramBoardId,
  3366. archived: false,
  3367. },
  3368. {
  3369. $set: {
  3370. description: newDescription,
  3371. },
  3372. },
  3373. );
  3374. }
  3375. if (req.body.color) {
  3376. const newColor = req.body.color;
  3377. Cards.direct.update(
  3378. {
  3379. _id: paramCardId,
  3380. listId: paramListId,
  3381. boardId: paramBoardId,
  3382. archived: false,
  3383. },
  3384. { $set: { color: newColor } },
  3385. );
  3386. }
  3387. if (req.body.vote) {
  3388. const newVote = req.body.vote;
  3389. newVote.positive = [];
  3390. newVote.negative = [];
  3391. if (!newVote.hasOwnProperty('public')) newVote.public = false;
  3392. if (!newVote.hasOwnProperty('allowNonBoardMembers'))
  3393. newVote.allowNonBoardMembers = false;
  3394. Cards.direct.update(
  3395. {
  3396. _id: paramCardId,
  3397. listId: paramListId,
  3398. boardId: paramBoardId,
  3399. archived: false,
  3400. },
  3401. { $set: { vote: newVote } },
  3402. );
  3403. }
  3404. if (req.body.poker) {
  3405. const newPoker = req.body.poker;
  3406. newPoker.one = [];
  3407. newPoker.two = [];
  3408. newPoker.three = [];
  3409. newPoker.five = [];
  3410. newPoker.eight = [];
  3411. newPoker.thirteen = [];
  3412. newPoker.twenty = [];
  3413. newPoker.forty = [];
  3414. newPoker.oneHundred = [];
  3415. newPoker.unsure = [];
  3416. if (!newPoker.hasOwnProperty('allowNonBoardMembers'))
  3417. newPoker.allowNonBoardMembers = false;
  3418. Cards.direct.update(
  3419. {
  3420. _id: paramCardId,
  3421. listId: paramListId,
  3422. boardId: paramBoardId,
  3423. archived: false,
  3424. },
  3425. { $set: { poker: newPoker } },
  3426. );
  3427. }
  3428. if (req.body.labelIds) {
  3429. let newlabelIds = req.body.labelIds;
  3430. if (_.isString(newlabelIds)) {
  3431. if (newlabelIds === '') {
  3432. newlabelIds = null;
  3433. } else {
  3434. newlabelIds = [newlabelIds];
  3435. }
  3436. }
  3437. Cards.direct.update(
  3438. {
  3439. _id: paramCardId,
  3440. listId: paramListId,
  3441. boardId: paramBoardId,
  3442. archived: false,
  3443. },
  3444. {
  3445. $set: {
  3446. labelIds: newlabelIds,
  3447. },
  3448. },
  3449. );
  3450. }
  3451. if (req.body.requestedBy) {
  3452. const newrequestedBy = req.body.requestedBy;
  3453. Cards.direct.update(
  3454. {
  3455. _id: paramCardId,
  3456. listId: paramListId,
  3457. boardId: paramBoardId,
  3458. archived: false,
  3459. },
  3460. { $set: { requestedBy: newrequestedBy } },
  3461. );
  3462. }
  3463. if (req.body.assignedBy) {
  3464. const newassignedBy = req.body.assignedBy;
  3465. Cards.direct.update(
  3466. {
  3467. _id: paramCardId,
  3468. listId: paramListId,
  3469. boardId: paramBoardId,
  3470. archived: false,
  3471. },
  3472. { $set: { assignedBy: newassignedBy } },
  3473. );
  3474. }
  3475. if (req.body.receivedAt) {
  3476. const newreceivedAt = req.body.receivedAt;
  3477. Cards.direct.update(
  3478. {
  3479. _id: paramCardId,
  3480. listId: paramListId,
  3481. boardId: paramBoardId,
  3482. archived: false,
  3483. },
  3484. { $set: { receivedAt: newreceivedAt } },
  3485. );
  3486. }
  3487. if (req.body.startAt) {
  3488. const newstartAt = req.body.startAt;
  3489. Cards.direct.update(
  3490. {
  3491. _id: paramCardId,
  3492. listId: paramListId,
  3493. boardId: paramBoardId,
  3494. archived: false,
  3495. },
  3496. { $set: { startAt: newstartAt } },
  3497. );
  3498. }
  3499. if (req.body.dueAt) {
  3500. const newdueAt = req.body.dueAt;
  3501. Cards.direct.update(
  3502. {
  3503. _id: paramCardId,
  3504. listId: paramListId,
  3505. boardId: paramBoardId,
  3506. archived: false,
  3507. },
  3508. { $set: { dueAt: newdueAt } },
  3509. );
  3510. }
  3511. if (req.body.endAt) {
  3512. const newendAt = req.body.endAt;
  3513. Cards.direct.update(
  3514. {
  3515. _id: paramCardId,
  3516. listId: paramListId,
  3517. boardId: paramBoardId,
  3518. archived: false,
  3519. },
  3520. { $set: { endAt: newendAt } },
  3521. );
  3522. }
  3523. if (req.body.spentTime) {
  3524. const newspentTime = req.body.spentTime;
  3525. Cards.direct.update(
  3526. {
  3527. _id: paramCardId,
  3528. listId: paramListId,
  3529. boardId: paramBoardId,
  3530. archived: false,
  3531. },
  3532. { $set: { spentTime: newspentTime } },
  3533. );
  3534. }
  3535. if (req.body.isOverTime) {
  3536. const newisOverTime = req.body.isOverTime;
  3537. Cards.direct.update(
  3538. {
  3539. _id: paramCardId,
  3540. listId: paramListId,
  3541. boardId: paramBoardId,
  3542. archived: false,
  3543. },
  3544. { $set: { isOverTime: newisOverTime } },
  3545. );
  3546. }
  3547. if (req.body.customFields) {
  3548. const newcustomFields = req.body.customFields;
  3549. Cards.direct.update(
  3550. {
  3551. _id: paramCardId,
  3552. listId: paramListId,
  3553. boardId: paramBoardId,
  3554. archived: false,
  3555. },
  3556. { $set: { customFields: newcustomFields } },
  3557. );
  3558. }
  3559. if (req.body.members) {
  3560. let newmembers = req.body.members;
  3561. if (_.isString(newmembers)) {
  3562. if (newmembers === '') {
  3563. newmembers = null;
  3564. } else {
  3565. newmembers = [newmembers];
  3566. }
  3567. }
  3568. Cards.direct.update(
  3569. {
  3570. _id: paramCardId,
  3571. listId: paramListId,
  3572. boardId: paramBoardId,
  3573. archived: false,
  3574. },
  3575. { $set: { members: newmembers } },
  3576. );
  3577. }
  3578. if (req.body.assignees) {
  3579. let newassignees = req.body.assignees;
  3580. if (_.isString(newassignees)) {
  3581. if (newassignees === '') {
  3582. newassignees = null;
  3583. } else {
  3584. newassignees = [newassignees];
  3585. }
  3586. }
  3587. Cards.direct.update(
  3588. {
  3589. _id: paramCardId,
  3590. listId: paramListId,
  3591. boardId: paramBoardId,
  3592. archived: false,
  3593. },
  3594. { $set: { assignees: newassignees } },
  3595. );
  3596. }
  3597. if (req.body.swimlaneId) {
  3598. const newParamSwimlaneId = req.body.swimlaneId;
  3599. Cards.direct.update(
  3600. {
  3601. _id: paramCardId,
  3602. listId: paramListId,
  3603. boardId: paramBoardId,
  3604. archived: false,
  3605. },
  3606. { $set: { swimlaneId: newParamSwimlaneId } },
  3607. );
  3608. }
  3609. if (req.body.listId) {
  3610. const newParamListId = req.body.listId;
  3611. Cards.direct.update(
  3612. {
  3613. _id: paramCardId,
  3614. listId: paramListId,
  3615. boardId: paramBoardId,
  3616. archived: false,
  3617. },
  3618. {
  3619. $set: {
  3620. listId: newParamListId,
  3621. },
  3622. },
  3623. );
  3624. const card = Cards.findOne({
  3625. _id: paramCardId,
  3626. });
  3627. cardMove(
  3628. req.body.authorId,
  3629. card,
  3630. {
  3631. fieldName: 'listId',
  3632. },
  3633. paramListId,
  3634. );
  3635. }
  3636. JsonRoutes.sendResult(res, {
  3637. code: 200,
  3638. data: {
  3639. _id: paramCardId,
  3640. },
  3641. });
  3642. },
  3643. );
  3644. /**
  3645. * @operation delete_card
  3646. * @summary Delete a card from a board
  3647. *
  3648. * @description This operation **deletes** a card, and therefore the card
  3649. * is not put in the recycle bin.
  3650. *
  3651. * @param {string} boardId the board ID of the card
  3652. * @param {string} list the list ID of the card
  3653. * @param {string} cardId the ID of the card
  3654. * @return_type {_id: string}
  3655. */
  3656. JsonRoutes.add(
  3657. 'DELETE',
  3658. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  3659. function(req, res) {
  3660. const paramBoardId = req.params.boardId;
  3661. const paramListId = req.params.listId;
  3662. const paramCardId = req.params.cardId;
  3663. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3664. const card = Cards.findOne({
  3665. _id: paramCardId,
  3666. });
  3667. Cards.direct.remove({
  3668. _id: paramCardId,
  3669. listId: paramListId,
  3670. boardId: paramBoardId,
  3671. });
  3672. cardRemover(req.body.authorId, card);
  3673. JsonRoutes.sendResult(res, {
  3674. code: 200,
  3675. data: {
  3676. _id: paramCardId,
  3677. },
  3678. });
  3679. },
  3680. );
  3681. /**
  3682. * @operation get_cards_by_custom_field
  3683. * @summary Get all Cards that matchs a value of a specific custom field
  3684. *
  3685. * @param {string} boardId the board ID
  3686. * @param {string} customFieldId the list ID
  3687. * @param {string} customFieldValue the value to look for
  3688. * @return_type [{_id: string,
  3689. * title: string,
  3690. * description: string,
  3691. * listId: string,
  3692. * swinlaneId: string}]
  3693. */
  3694. JsonRoutes.add(
  3695. 'GET',
  3696. '/api/boards/:boardId/cardsByCustomField/:customFieldId/:customFieldValue',
  3697. function(req, res) {
  3698. const paramBoardId = req.params.boardId;
  3699. const paramCustomFieldId = req.params.customFieldId;
  3700. const paramCustomFieldValue = req.params.customFieldValue;
  3701. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3702. JsonRoutes.sendResult(res, {
  3703. code: 200,
  3704. data: Cards.find({
  3705. boardId: paramBoardId,
  3706. customFields: {
  3707. $elemMatch: {
  3708. _id: paramCustomFieldId,
  3709. value: paramCustomFieldValue,
  3710. },
  3711. },
  3712. archived: false,
  3713. }).fetch(),
  3714. });
  3715. },
  3716. );
  3717. /**
  3718. * @operation edit_card_custom_field
  3719. * @summary Edit Custom Field in a Card
  3720. *
  3721. * @description Edit a custom field value in a card
  3722. * @param {string} boardId the board ID of the card
  3723. * @param {string} listId the list ID of the card
  3724. * @param {string} cardId the ID of the card
  3725. * @param {string} customFieldId the ID of the custom field
  3726. * @param {string} value the new custom field value
  3727. * @return_type {_id: string, customFields: object}
  3728. */
  3729. JsonRoutes.add(
  3730. 'POST',
  3731. '/api/boards/:boardId/lists/:listId/cards/:cardId/customFields/:customFieldId',
  3732. function(req, res) {
  3733. const paramBoardId = req.params.boardId;
  3734. const paramCardId = req.params.cardId;
  3735. const paramListId = req.params.listId;
  3736. const paramCustomFieldId = req.params.customFieldId;
  3737. const paramCustomFieldValue = req.body.value;
  3738. Authentication.checkBoardAccess(req.userId, paramBoardId);
  3739. const card = Cards.findOne({
  3740. _id: paramCardId,
  3741. listId: paramListId,
  3742. boardId: paramBoardId,
  3743. archived: false,
  3744. });
  3745. if (!card) {
  3746. throw new Meteor.Error(404, 'Card not found');
  3747. }
  3748. const customFields = card.customFields || [];
  3749. const updatedCustomFields = customFields.map(cf => {
  3750. if (cf._id === paramCustomFieldId) {
  3751. return {
  3752. _id: cf._id,
  3753. value: paramCustomFieldValue,
  3754. };
  3755. }
  3756. return cf;
  3757. });
  3758. Cards.direct.update(
  3759. {
  3760. _id: paramCardId,
  3761. listId: paramListId,
  3762. boardId: paramBoardId,
  3763. archived: false,
  3764. },
  3765. { $set: { customFields: updatedCustomFields } },
  3766. );
  3767. JsonRoutes.sendResult(res, {
  3768. code: 200,
  3769. data: {
  3770. _id: paramCardId,
  3771. customFields: updatedCustomFields,
  3772. },
  3773. });
  3774. },
  3775. );
  3776. }
  3777. export default Cards;