cards.js 99 KB

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