cards.js 95 KB

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