cards.js 78 KB

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