cards.js 93 KB

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