cards.js 79 KB

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