cards.js 119 KB

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