cards.js 93 KB

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