cards.js 95 KB

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