cards.js 101 KB

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