cards.js 79 KB

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