cards.js 105 KB

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