cards.js 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015
  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. return card.members;
  803. } else if (this.isLinkedBoard()) {
  804. const board = Boards.findOne({ _id: this.linkedId });
  805. return board.activeMembers().map(member => {
  806. return member.userId;
  807. });
  808. } else {
  809. return this.members;
  810. }
  811. },
  812. getAssignees() {
  813. if (this.isLinkedCard()) {
  814. const card = Cards.findOne({ _id: this.linkedId });
  815. return card.assignees;
  816. } else if (this.isLinkedBoard()) {
  817. const board = Boards.findOne({ _id: this.linkedId });
  818. return board.activeMembers().map(assignee => {
  819. return assignee.userId;
  820. });
  821. } else {
  822. return this.assignees;
  823. }
  824. },
  825. assignMember(memberId) {
  826. if (this.isLinkedCard()) {
  827. return Cards.update(
  828. { _id: this.linkedId },
  829. { $addToSet: { members: memberId } },
  830. );
  831. } else if (this.isLinkedBoard()) {
  832. const board = Boards.findOne({ _id: this.linkedId });
  833. return board.addMember(memberId);
  834. } else {
  835. return Cards.update(
  836. { _id: this._id },
  837. { $addToSet: { members: memberId } },
  838. );
  839. }
  840. },
  841. assignAssignee(assigneeId) {
  842. if (this.isLinkedCard()) {
  843. return Cards.update(
  844. { _id: this.linkedId },
  845. { $addToSet: { assignees: assigneeId } },
  846. );
  847. } else if (this.isLinkedBoard()) {
  848. const board = Boards.findOne({ _id: this.linkedId });
  849. return board.addAssignee(assigneeId);
  850. } else {
  851. return Cards.update(
  852. { _id: this._id },
  853. { $addToSet: { assignees: assigneeId } },
  854. );
  855. }
  856. },
  857. unassignMember(memberId) {
  858. if (this.isLinkedCard()) {
  859. return Cards.update(
  860. { _id: this.linkedId },
  861. { $pull: { members: memberId } },
  862. );
  863. } else if (this.isLinkedBoard()) {
  864. const board = Boards.findOne({ _id: this.linkedId });
  865. return board.removeMember(memberId);
  866. } else {
  867. return Cards.update({ _id: this._id }, { $pull: { members: memberId } });
  868. }
  869. },
  870. unassignAssignee(assigneeId) {
  871. if (this.isLinkedCard()) {
  872. return Cards.update(
  873. { _id: this.linkedId },
  874. { $pull: { assignees: assigneeId } },
  875. );
  876. } else if (this.isLinkedBoard()) {
  877. const board = Boards.findOne({ _id: this.linkedId });
  878. return board.removeAssignee(assigneeId);
  879. } else {
  880. return Cards.update(
  881. { _id: this._id },
  882. { $pull: { assignees: assigneeId } },
  883. );
  884. }
  885. },
  886. toggleMember(memberId) {
  887. if (this.getMembers() && this.getMembers().indexOf(memberId) > -1) {
  888. return this.unassignMember(memberId);
  889. } else {
  890. return this.assignMember(memberId);
  891. }
  892. },
  893. toggleAssignee(assigneeId) {
  894. if (this.getAssignees() && this.getAssignees().indexOf(assigneeId) > -1) {
  895. return this.unassignAssignee(assigneeId);
  896. } else {
  897. return this.assignAssignee(assigneeId);
  898. }
  899. },
  900. getReceived() {
  901. if (this.isLinkedCard()) {
  902. const card = Cards.findOne({ _id: this.linkedId });
  903. return card.receivedAt;
  904. } else {
  905. return this.receivedAt;
  906. }
  907. },
  908. setReceived(receivedAt) {
  909. if (this.isLinkedCard()) {
  910. return Cards.update({ _id: this.linkedId }, { $set: { receivedAt } });
  911. } else {
  912. return Cards.update({ _id: this._id }, { $set: { receivedAt } });
  913. }
  914. },
  915. getStart() {
  916. if (this.isLinkedCard()) {
  917. const card = Cards.findOne({ _id: this.linkedId });
  918. return card.startAt;
  919. } else if (this.isLinkedBoard()) {
  920. const board = Boards.findOne({ _id: this.linkedId });
  921. return board.startAt;
  922. } else {
  923. return this.startAt;
  924. }
  925. },
  926. setStart(startAt) {
  927. if (this.isLinkedCard()) {
  928. return Cards.update({ _id: this.linkedId }, { $set: { startAt } });
  929. } else if (this.isLinkedBoard()) {
  930. return Boards.update({ _id: this.linkedId }, { $set: { startAt } });
  931. } else {
  932. return Cards.update({ _id: this._id }, { $set: { startAt } });
  933. }
  934. },
  935. getDue() {
  936. if (this.isLinkedCard()) {
  937. const card = Cards.findOne({ _id: this.linkedId });
  938. return card.dueAt;
  939. } else if (this.isLinkedBoard()) {
  940. const board = Boards.findOne({ _id: this.linkedId });
  941. return board.dueAt;
  942. } else {
  943. return this.dueAt;
  944. }
  945. },
  946. setDue(dueAt) {
  947. if (this.isLinkedCard()) {
  948. return Cards.update({ _id: this.linkedId }, { $set: { dueAt } });
  949. } else if (this.isLinkedBoard()) {
  950. return Boards.update({ _id: this.linkedId }, { $set: { dueAt } });
  951. } else {
  952. return Cards.update({ _id: this._id }, { $set: { dueAt } });
  953. }
  954. },
  955. getEnd() {
  956. if (this.isLinkedCard()) {
  957. const card = Cards.findOne({ _id: this.linkedId });
  958. return card.endAt;
  959. } else if (this.isLinkedBoard()) {
  960. const board = Boards.findOne({ _id: this.linkedId });
  961. return board.endAt;
  962. } else {
  963. return this.endAt;
  964. }
  965. },
  966. setEnd(endAt) {
  967. if (this.isLinkedCard()) {
  968. return Cards.update({ _id: this.linkedId }, { $set: { endAt } });
  969. } else if (this.isLinkedBoard()) {
  970. return Boards.update({ _id: this.linkedId }, { $set: { endAt } });
  971. } else {
  972. return Cards.update({ _id: this._id }, { $set: { endAt } });
  973. }
  974. },
  975. getIsOvertime() {
  976. if (this.isLinkedCard()) {
  977. const card = Cards.findOne({ _id: this.linkedId });
  978. return card.isOvertime;
  979. } else if (this.isLinkedBoard()) {
  980. const board = Boards.findOne({ _id: this.linkedId });
  981. return board.isOvertime;
  982. } else {
  983. return this.isOvertime;
  984. }
  985. },
  986. setIsOvertime(isOvertime) {
  987. if (this.isLinkedCard()) {
  988. return Cards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  989. } else if (this.isLinkedBoard()) {
  990. return Boards.update({ _id: this.linkedId }, { $set: { isOvertime } });
  991. } else {
  992. return Cards.update({ _id: this._id }, { $set: { isOvertime } });
  993. }
  994. },
  995. getSpentTime() {
  996. if (this.isLinkedCard()) {
  997. const card = Cards.findOne({ _id: this.linkedId });
  998. return card.spentTime;
  999. } else if (this.isLinkedBoard()) {
  1000. const board = Boards.findOne({ _id: this.linkedId });
  1001. return board.spentTime;
  1002. } else {
  1003. return this.spentTime;
  1004. }
  1005. },
  1006. setSpentTime(spentTime) {
  1007. if (this.isLinkedCard()) {
  1008. return Cards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1009. } else if (this.isLinkedBoard()) {
  1010. return Boards.update({ _id: this.linkedId }, { $set: { spentTime } });
  1011. } else {
  1012. return Cards.update({ _id: this._id }, { $set: { spentTime } });
  1013. }
  1014. },
  1015. getVoteQuestion() {
  1016. if (this.isLinkedCard()) {
  1017. const card = Cards.findOne({ _id: this.linkedId });
  1018. if (card && card.vote) return card.vote.question;
  1019. else return null;
  1020. } else if (this.isLinkedBoard()) {
  1021. const board = Boards.findOne({ _id: this.linkedId });
  1022. if (board && board.vote) return board.vote.question;
  1023. else return null;
  1024. } else if (this.vote) {
  1025. return this.vote.question;
  1026. } else {
  1027. return null;
  1028. }
  1029. },
  1030. getVotePublic() {
  1031. if (this.isLinkedCard()) {
  1032. const card = Cards.findOne({ _id: this.linkedId });
  1033. if (card && card.vote) return card.vote.public;
  1034. else return null;
  1035. } else if (this.isLinkedBoard()) {
  1036. const board = Boards.findOne({ _id: this.linkedId });
  1037. if (board && board.vote) return board.vote.public;
  1038. else return null;
  1039. } else if (this.vote) {
  1040. return this.vote.public;
  1041. } else {
  1042. return null;
  1043. }
  1044. },
  1045. getVoteEnd() {
  1046. if (this.isLinkedCard()) {
  1047. const card = Cards.findOne({ _id: this.linkedId });
  1048. if (card && card.vote) return card.vote.end;
  1049. else return null;
  1050. } else if (this.isLinkedBoard()) {
  1051. const board = Boards.findOne({ _id: this.linkedId });
  1052. if (board && board.vote) return board.vote.end;
  1053. else return null;
  1054. } else if (this.vote) {
  1055. return this.vote.end;
  1056. } else {
  1057. return null;
  1058. }
  1059. },
  1060. expiredVote() {
  1061. let end = this.getVoteEnd();
  1062. if (end) {
  1063. end = moment(end);
  1064. return end.isBefore(new Date());
  1065. }
  1066. return false;
  1067. },
  1068. voteMemberPositive() {
  1069. if (this.vote && this.vote.positive)
  1070. return Users.find({ _id: { $in: this.vote.positive } });
  1071. return [];
  1072. },
  1073. voteMemberNegative() {
  1074. if (this.vote && this.vote.negative)
  1075. return Users.find({ _id: { $in: this.vote.negative } });
  1076. return [];
  1077. },
  1078. voteState() {
  1079. const userId = Meteor.userId();
  1080. let state;
  1081. if (this.vote) {
  1082. if (this.vote.positive) {
  1083. state = _.contains(this.vote.positive, userId);
  1084. if (state === true) return true;
  1085. }
  1086. if (this.vote.negative) {
  1087. state = _.contains(this.vote.negative, userId);
  1088. if (state === true) return false;
  1089. }
  1090. }
  1091. return null;
  1092. },
  1093. getId() {
  1094. if (this.isLinked()) {
  1095. return this.linkedId;
  1096. } else {
  1097. return this._id;
  1098. }
  1099. },
  1100. getTitle() {
  1101. if (this.isLinkedCard()) {
  1102. const card = Cards.findOne({ _id: this.linkedId });
  1103. return card.title;
  1104. } else if (this.isLinkedBoard()) {
  1105. const board = Boards.findOne({ _id: this.linkedId });
  1106. return board.title;
  1107. } else {
  1108. return this.title;
  1109. }
  1110. },
  1111. getBoardTitle() {
  1112. if (this.isLinkedCard()) {
  1113. const card = Cards.findOne({ _id: this.linkedId });
  1114. const board = Boards.findOne({ _id: card.boardId });
  1115. return board.title;
  1116. } else if (this.isLinkedBoard()) {
  1117. const board = Boards.findOne({ _id: this.linkedId });
  1118. return board.title;
  1119. } else {
  1120. const board = Boards.findOne({ _id: this.boardId });
  1121. return board.title;
  1122. }
  1123. },
  1124. setTitle(title) {
  1125. if (this.isLinkedCard()) {
  1126. return Cards.update({ _id: this.linkedId }, { $set: { title } });
  1127. } else if (this.isLinkedBoard()) {
  1128. return Boards.update({ _id: this.linkedId }, { $set: { title } });
  1129. } else {
  1130. return Cards.update({ _id: this._id }, { $set: { title } });
  1131. }
  1132. },
  1133. getArchived() {
  1134. if (this.isLinkedCard()) {
  1135. const card = Cards.findOne({ _id: this.linkedId });
  1136. return card.archived;
  1137. } else if (this.isLinkedBoard()) {
  1138. const board = Boards.findOne({ _id: this.linkedId });
  1139. return board.archived;
  1140. } else {
  1141. return this.archived;
  1142. }
  1143. },
  1144. setRequestedBy(requestedBy) {
  1145. if (this.isLinkedCard()) {
  1146. return Cards.update({ _id: this.linkedId }, { $set: { requestedBy } });
  1147. } else {
  1148. return Cards.update({ _id: this._id }, { $set: { requestedBy } });
  1149. }
  1150. },
  1151. getRequestedBy() {
  1152. if (this.isLinkedCard()) {
  1153. const card = Cards.findOne({ _id: this.linkedId });
  1154. return card.requestedBy;
  1155. } else {
  1156. return this.requestedBy;
  1157. }
  1158. },
  1159. setAssignedBy(assignedBy) {
  1160. if (this.isLinkedCard()) {
  1161. return Cards.update({ _id: this.linkedId }, { $set: { assignedBy } });
  1162. } else {
  1163. return Cards.update({ _id: this._id }, { $set: { assignedBy } });
  1164. }
  1165. },
  1166. getAssignedBy() {
  1167. if (this.isLinkedCard()) {
  1168. const card = Cards.findOne({ _id: this.linkedId });
  1169. return card.assignedBy;
  1170. } else {
  1171. return this.assignedBy;
  1172. }
  1173. },
  1174. isTemplateCard() {
  1175. return this.type === 'template-card';
  1176. },
  1177. votePublic() {
  1178. if (this.vote) return this.vote.public;
  1179. return null;
  1180. },
  1181. voteAllowNonBoardMembers() {
  1182. if (this.vote) return this.vote.allowNonBoardMembers;
  1183. return null;
  1184. },
  1185. voteCountNegative() {
  1186. if (this.vote && this.vote.negative) return this.vote.negative.length;
  1187. return null;
  1188. },
  1189. voteCountPositive() {
  1190. if (this.vote && this.vote.positive) return this.vote.positive.length;
  1191. return null;
  1192. },
  1193. voteCount() {
  1194. return this.voteCountPositive() + this.voteCountNegative();
  1195. },
  1196. });
  1197. Cards.mutations({
  1198. applyToChildren(funct) {
  1199. Cards.find({
  1200. parentId: this._id,
  1201. }).forEach(card => {
  1202. funct(card);
  1203. });
  1204. },
  1205. archive() {
  1206. this.applyToChildren(card => {
  1207. return card.archive();
  1208. });
  1209. return {
  1210. $set: {
  1211. archived: true,
  1212. },
  1213. };
  1214. },
  1215. restore() {
  1216. this.applyToChildren(card => {
  1217. return card.restore();
  1218. });
  1219. return {
  1220. $set: {
  1221. archived: false,
  1222. },
  1223. };
  1224. },
  1225. moveToEndOfList({ listId } = {}) {
  1226. let swimlaneId = this.swimlaneId;
  1227. const boardId = this.boardId;
  1228. let sortIndex = 0;
  1229. // This should never happen, but there was a bug that was fixed in commit
  1230. // ea0239538a68e225c867411a4f3e0d27c158383.
  1231. if (!swimlaneId) {
  1232. const board = Boards.findOne(boardId);
  1233. swimlaneId = board.getDefaultSwimline()._id;
  1234. }
  1235. // Move the minicard to the end of the target list
  1236. let parentElementDom = $(`#swimlane-${this.swimlaneId}`).get(0);
  1237. if (!parentElementDom) parentElementDom = $(':root');
  1238. const lastCardDom = $(parentElementDom)
  1239. .find(`#js-list-${listId} .js-minicard:last`)
  1240. .get(0);
  1241. if (lastCardDom) sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  1242. return this.moveOptionalArgs({
  1243. boardId,
  1244. swimlaneId,
  1245. listId,
  1246. sort: sortIndex,
  1247. });
  1248. },
  1249. moveOptionalArgs({ boardId, swimlaneId, listId, sort } = {}) {
  1250. boardId = boardId || this.boardId;
  1251. swimlaneId = swimlaneId || this.swimlaneId;
  1252. // This should never happen, but there was a bug that was fixed in commit
  1253. // ea0239538a68e225c867411a4f3e0d27c158383.
  1254. if (!swimlaneId) {
  1255. const board = Boards.findOne(boardId);
  1256. swimlaneId = board.getDefaultSwimline()._id;
  1257. }
  1258. listId = listId || this.listId;
  1259. if (sort === undefined || sort === null) sort = this.sort;
  1260. return this.move(boardId, swimlaneId, listId, sort);
  1261. },
  1262. move(boardId, swimlaneId, listId, sort) {
  1263. const mutatedFields = {
  1264. boardId,
  1265. swimlaneId,
  1266. listId,
  1267. sort,
  1268. };
  1269. // we must only copy the labels and custom fields if the target board
  1270. // differs from the source board
  1271. if (this.boardId !== boardId) {
  1272. // Get label names
  1273. const oldBoard = Boards.findOne(this.boardId);
  1274. const oldBoardLabels = oldBoard.labels;
  1275. const oldCardLabels = _.pluck(
  1276. _.filter(oldBoardLabels, label => {
  1277. return _.contains(this.labelIds, label._id);
  1278. }),
  1279. 'name',
  1280. );
  1281. const newBoard = Boards.findOne(boardId);
  1282. const newBoardLabels = newBoard.labels;
  1283. const newCardLabelIds = _.pluck(
  1284. _.filter(newBoardLabels, label => {
  1285. return label.name && _.contains(oldCardLabels, label.name);
  1286. }),
  1287. '_id',
  1288. );
  1289. Object.assign(mutatedFields, {
  1290. labelIds: newCardLabelIds,
  1291. });
  1292. // Copy custom fields
  1293. CustomFields.find({
  1294. _id: {
  1295. $in: this.customFields.map(cf => {
  1296. return cf._id;
  1297. }),
  1298. },
  1299. }).forEach(cf => {
  1300. if (!_.contains(cf.boardIds, boardId)) cf.addBoard(boardId);
  1301. });
  1302. }
  1303. Cards.update(this._id, {
  1304. $set: mutatedFields,
  1305. });
  1306. },
  1307. addLabel(labelId) {
  1308. return {
  1309. $addToSet: {
  1310. labelIds: labelId,
  1311. },
  1312. };
  1313. },
  1314. removeLabel(labelId) {
  1315. return {
  1316. $pull: {
  1317. labelIds: labelId,
  1318. },
  1319. };
  1320. },
  1321. toggleLabel(labelId) {
  1322. if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
  1323. return this.removeLabel(labelId);
  1324. } else {
  1325. return this.addLabel(labelId);
  1326. }
  1327. },
  1328. setColor(newColor) {
  1329. if (newColor === 'white') {
  1330. newColor = null;
  1331. }
  1332. return {
  1333. $set: {
  1334. color: newColor,
  1335. },
  1336. };
  1337. },
  1338. assignMember(memberId) {
  1339. return {
  1340. $addToSet: {
  1341. members: memberId,
  1342. },
  1343. };
  1344. },
  1345. assignAssignee(assigneeId) {
  1346. // If there is not any assignee, allow one assignee, not more.
  1347. /*
  1348. if (this.getAssignees().length === 0) {
  1349. return {
  1350. $addToSet: {
  1351. assignees: assigneeId,
  1352. },
  1353. };
  1354. */
  1355. // Allow more that one assignee:
  1356. // https://github.com/wekan/wekan/issues/3302
  1357. return {
  1358. $addToSet: {
  1359. assignees: assigneeId,
  1360. },
  1361. };
  1362. //} else {
  1363. // return false,
  1364. //}
  1365. },
  1366. unassignMember(memberId) {
  1367. return {
  1368. $pull: {
  1369. members: memberId,
  1370. },
  1371. };
  1372. },
  1373. unassignAssignee(assigneeId) {
  1374. return {
  1375. $pull: {
  1376. assignees: assigneeId,
  1377. },
  1378. };
  1379. },
  1380. toggleMember(memberId) {
  1381. if (this.members && this.members.indexOf(memberId) > -1) {
  1382. return this.unassignMember(memberId);
  1383. } else {
  1384. return this.assignMember(memberId);
  1385. }
  1386. },
  1387. toggleAssignee(assigneeId) {
  1388. if (this.assignees && this.assignees.indexOf(assigneeId) > -1) {
  1389. return this.unassignAssignee(assigneeId);
  1390. } else {
  1391. return this.assignAssignee(assigneeId);
  1392. }
  1393. },
  1394. assignCustomField(customFieldId) {
  1395. return {
  1396. $addToSet: {
  1397. customFields: {
  1398. _id: customFieldId,
  1399. value: null,
  1400. },
  1401. },
  1402. };
  1403. },
  1404. unassignCustomField(customFieldId) {
  1405. return {
  1406. $pull: {
  1407. customFields: {
  1408. _id: customFieldId,
  1409. },
  1410. },
  1411. };
  1412. },
  1413. toggleCustomField(customFieldId) {
  1414. if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
  1415. return this.unassignCustomField(customFieldId);
  1416. } else {
  1417. return this.assignCustomField(customFieldId);
  1418. }
  1419. },
  1420. setCustomField(customFieldId, value) {
  1421. // todo
  1422. const index = this.customFieldIndex(customFieldId);
  1423. if (index > -1) {
  1424. const update = {
  1425. $set: {},
  1426. };
  1427. update.$set[`customFields.${index}.value`] = value;
  1428. return update;
  1429. }
  1430. // TODO
  1431. // Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
  1432. return null;
  1433. },
  1434. setCover(coverId) {
  1435. return {
  1436. $set: {
  1437. coverId,
  1438. },
  1439. };
  1440. },
  1441. unsetCover() {
  1442. return {
  1443. $unset: {
  1444. coverId: '',
  1445. },
  1446. };
  1447. },
  1448. setReceived(receivedAt) {
  1449. return {
  1450. $set: {
  1451. receivedAt,
  1452. },
  1453. };
  1454. },
  1455. unsetReceived() {
  1456. return {
  1457. $unset: {
  1458. receivedAt: '',
  1459. },
  1460. };
  1461. },
  1462. setStart(startAt) {
  1463. return {
  1464. $set: {
  1465. startAt,
  1466. },
  1467. };
  1468. },
  1469. unsetStart() {
  1470. return {
  1471. $unset: {
  1472. startAt: '',
  1473. },
  1474. };
  1475. },
  1476. setDue(dueAt) {
  1477. return {
  1478. $set: {
  1479. dueAt,
  1480. },
  1481. };
  1482. },
  1483. unsetDue() {
  1484. return {
  1485. $unset: {
  1486. dueAt: '',
  1487. },
  1488. };
  1489. },
  1490. setEnd(endAt) {
  1491. return {
  1492. $set: {
  1493. endAt,
  1494. },
  1495. };
  1496. },
  1497. unsetEnd() {
  1498. return {
  1499. $unset: {
  1500. endAt: '',
  1501. },
  1502. };
  1503. },
  1504. setOvertime(isOvertime) {
  1505. return {
  1506. $set: {
  1507. isOvertime,
  1508. },
  1509. };
  1510. },
  1511. setSpentTime(spentTime) {
  1512. return {
  1513. $set: {
  1514. spentTime,
  1515. },
  1516. };
  1517. },
  1518. unsetSpentTime() {
  1519. return {
  1520. $unset: {
  1521. spentTime: '',
  1522. isOvertime: false,
  1523. },
  1524. };
  1525. },
  1526. setParentId(parentId) {
  1527. return {
  1528. $set: {
  1529. parentId,
  1530. },
  1531. };
  1532. },
  1533. setVoteQuestion(question, publicVote, allowNonBoardMembers) {
  1534. return {
  1535. $set: {
  1536. vote: {
  1537. question,
  1538. public: publicVote,
  1539. allowNonBoardMembers,
  1540. positive: [],
  1541. negative: [],
  1542. },
  1543. },
  1544. };
  1545. },
  1546. unsetVote() {
  1547. return {
  1548. $unset: {
  1549. vote: '',
  1550. },
  1551. };
  1552. },
  1553. setVoteEnd(end) {
  1554. return {
  1555. $set: { 'vote.end': end },
  1556. };
  1557. },
  1558. unsetVoteEnd() {
  1559. return {
  1560. $unset: { 'vote.end': '' },
  1561. };
  1562. },
  1563. setVote(userId, forIt) {
  1564. switch (forIt) {
  1565. case true:
  1566. // vote for it
  1567. return {
  1568. $pull: {
  1569. 'vote.negative': userId,
  1570. },
  1571. $addToSet: {
  1572. 'vote.positive': userId,
  1573. },
  1574. };
  1575. case false:
  1576. // vote against
  1577. return {
  1578. $pull: {
  1579. 'vote.positive': userId,
  1580. },
  1581. $addToSet: {
  1582. 'vote.negative': userId,
  1583. },
  1584. };
  1585. default:
  1586. // Remove votes
  1587. return {
  1588. $pull: {
  1589. 'vote.positive': userId,
  1590. 'vote.negative': userId,
  1591. },
  1592. };
  1593. }
  1594. },
  1595. });
  1596. Cards.globalSearch = queryParams => {
  1597. const userId = Meteor.userId;
  1598. // eslint-disable-next-line no-console
  1599. console.log('userId:', this.userId);
  1600. const errors = {
  1601. notFound: {
  1602. boards: [],
  1603. swimlanes: [],
  1604. lists: [],
  1605. users: [],
  1606. },
  1607. };
  1608. const selector = {
  1609. archived: false,
  1610. type: 'cardType-card',
  1611. boardId: { $in: Boards.userBoardIds(userId) },
  1612. swimlaneId: { $nin: Swimlanes.archivedSwimlaneIds() },
  1613. listId: { $nin: Lists.archivedListIds() },
  1614. };
  1615. if (queryParams.boards.length) {
  1616. const queryBoards = [];
  1617. queryParams.boards.forEach(query => {
  1618. const boards = Boards.userSearch(userId, {
  1619. title: query,
  1620. });
  1621. if (boards.count()) {
  1622. boards.forEach(board => {
  1623. queryBoards.push(board._id);
  1624. });
  1625. } else {
  1626. errors.notFound.boards.push(query);
  1627. }
  1628. });
  1629. selector.boardId.$in = queryBoards;
  1630. }
  1631. if (queryParams.swimlanes.length) {
  1632. const querySwimlanes = [];
  1633. queryParams.swimlanes.forEach(query => {
  1634. const swimlanes = Swimlanes.find({
  1635. title: query,
  1636. });
  1637. if (swimlanes.count()) {
  1638. swimlanes.forEach(swim => {
  1639. querySwimlanes.push(swim._id);
  1640. });
  1641. } else {
  1642. errors.notFound.swimlanes.push(query);
  1643. }
  1644. });
  1645. selector.swimlaneId.$in = querySwimlanes;
  1646. }
  1647. if (queryParams.lists.length) {
  1648. const queryLists = [];
  1649. queryParams.lists.forEach(query => {
  1650. const lists = Lists.find({
  1651. title: query,
  1652. });
  1653. if (lists.count()) {
  1654. lists.forEach(list => {
  1655. queryLists.push(list._id);
  1656. });
  1657. } else {
  1658. errors.notFound.lists.push(query);
  1659. }
  1660. });
  1661. selector.listId.$in = queryLists;
  1662. }
  1663. if (queryParams.users.length) {
  1664. const queryUsers = [];
  1665. queryParams.users.forEach(query => {
  1666. const users = Users.find({
  1667. username: query,
  1668. });
  1669. if (users.count()) {
  1670. users.forEach(user => {
  1671. queryUsers.push(user._id);
  1672. });
  1673. } else {
  1674. errors.notFound.users.push(query);
  1675. }
  1676. });
  1677. selector.$or = [
  1678. { members: { $in: queryUsers } },
  1679. { assignees: { $in: queryUsers } },
  1680. ];
  1681. }
  1682. if (queryParams.labels.length) {
  1683. queryParams.labels.forEach(label => {
  1684. const queryLabels = [];
  1685. let boards = Boards.userSearch(userId, {
  1686. labels: { $elemMatch: { color: label.toLowerCase() } },
  1687. });
  1688. if (boards.count()) {
  1689. boards.forEach(board => {
  1690. // eslint-disable-next-line no-console
  1691. console.log('board:', board);
  1692. // eslint-disable-next-line no-console
  1693. console.log('board.labels:', board.labels);
  1694. board.labels
  1695. .filter(boardLabel => {
  1696. return boardLabel.color === label.toLowerCase();
  1697. })
  1698. .forEach(boardLabel => {
  1699. queryLabels.push(boardLabel._id);
  1700. });
  1701. });
  1702. } else {
  1703. const reLabel = new RegExp(label, 'i');
  1704. boards = Boards.userSearch(userId, {
  1705. labels: { $elemMatch: { name: reLabel } },
  1706. });
  1707. if (boards.count()) {
  1708. boards.forEach(board => {
  1709. board.labels
  1710. .filter(boardLabel => {
  1711. return boardLabel.name.match(reLabel);
  1712. })
  1713. .forEach(boardLabel => {
  1714. queryLabels.push(boardLabel._id);
  1715. });
  1716. });
  1717. } else {
  1718. errors.notFound.labels.push({ tag: 'label', value: label });
  1719. }
  1720. }
  1721. selector.labelIds = { $in: queryLabels };
  1722. });
  1723. }
  1724. if (queryParams.text) {
  1725. const regex = new RegExp(queryParams.text, 'i');
  1726. selector.$or = [
  1727. { title: regex },
  1728. { description: regex },
  1729. { customFields: { $elemMatch: { value: regex } } },
  1730. ];
  1731. }
  1732. // eslint-disable-next-line no-console
  1733. console.log('selector:', selector);
  1734. const cards = Cards.find(selector, {
  1735. fields: {
  1736. _id: 1,
  1737. archived: 1,
  1738. boardId: 1,
  1739. swimlaneId: 1,
  1740. listId: 1,
  1741. title: 1,
  1742. type: 1,
  1743. sort: 1,
  1744. members: 1,
  1745. assignees: 1,
  1746. colors: 1,
  1747. dueAt: 1,
  1748. labelIds: 1,
  1749. },
  1750. limit: 50,
  1751. });
  1752. // eslint-disable-next-line no-console
  1753. console.log('count:', cards.count());
  1754. return { cards, errors };
  1755. };
  1756. //FUNCTIONS FOR creation of Activities
  1757. function updateActivities(doc, fieldNames, modifier) {
  1758. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  1759. Activities.find({
  1760. activityType: 'addedLabel',
  1761. cardId: doc._id,
  1762. }).forEach(a => {
  1763. const lidx = doc.labelIds.indexOf(a.labelId);
  1764. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  1765. Activities.update(a._id, {
  1766. $set: {
  1767. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  1768. boardId: modifier.$set.boardId,
  1769. },
  1770. });
  1771. } else {
  1772. Activities.remove(a._id);
  1773. }
  1774. });
  1775. } else if (_.contains(fieldNames, 'boardId')) {
  1776. Activities.remove({
  1777. activityType: 'addedLabel',
  1778. cardId: doc._id,
  1779. });
  1780. }
  1781. }
  1782. function cardMove(
  1783. userId,
  1784. doc,
  1785. fieldNames,
  1786. oldListId,
  1787. oldSwimlaneId,
  1788. oldBoardId,
  1789. ) {
  1790. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  1791. Activities.insert({
  1792. userId,
  1793. activityType: 'moveCardBoard',
  1794. boardName: Boards.findOne(doc.boardId).title,
  1795. boardId: doc.boardId,
  1796. oldBoardId,
  1797. oldBoardName: Boards.findOne(oldBoardId).title,
  1798. cardId: doc._id,
  1799. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1800. swimlaneId: doc.swimlaneId,
  1801. oldSwimlaneId,
  1802. });
  1803. } else if (
  1804. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1805. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  1806. ) {
  1807. Activities.insert({
  1808. userId,
  1809. oldListId,
  1810. activityType: 'moveCard',
  1811. listName: Lists.findOne(doc.listId).title,
  1812. listId: doc.listId,
  1813. boardId: doc.boardId,
  1814. cardId: doc._id,
  1815. cardTitle: doc.title,
  1816. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1817. swimlaneId: doc.swimlaneId,
  1818. oldSwimlaneId,
  1819. });
  1820. }
  1821. }
  1822. function cardState(userId, doc, fieldNames) {
  1823. if (_.contains(fieldNames, 'archived')) {
  1824. if (doc.archived) {
  1825. Activities.insert({
  1826. userId,
  1827. activityType: 'archivedCard',
  1828. listName: Lists.findOne(doc.listId).title,
  1829. boardId: doc.boardId,
  1830. listId: doc.listId,
  1831. cardId: doc._id,
  1832. swimlaneId: doc.swimlaneId,
  1833. });
  1834. } else {
  1835. Activities.insert({
  1836. userId,
  1837. activityType: 'restoredCard',
  1838. boardId: doc.boardId,
  1839. listName: Lists.findOne(doc.listId).title,
  1840. listId: doc.listId,
  1841. cardId: doc._id,
  1842. swimlaneId: doc.swimlaneId,
  1843. });
  1844. }
  1845. }
  1846. }
  1847. function cardMembers(userId, doc, fieldNames, modifier) {
  1848. if (!_.contains(fieldNames, 'members')) return;
  1849. let memberId;
  1850. // Say hello to the new member
  1851. if (modifier.$addToSet && modifier.$addToSet.members) {
  1852. memberId = modifier.$addToSet.members;
  1853. const username = Users.findOne(memberId).username;
  1854. if (!_.contains(doc.members, memberId)) {
  1855. Activities.insert({
  1856. userId,
  1857. username,
  1858. activityType: 'joinMember',
  1859. boardId: doc.boardId,
  1860. cardId: doc._id,
  1861. memberId,
  1862. listId: doc.listId,
  1863. swimlaneId: doc.swimlaneId,
  1864. });
  1865. }
  1866. }
  1867. // Say goodbye to the former member
  1868. if (modifier.$pull && modifier.$pull.members) {
  1869. memberId = modifier.$pull.members;
  1870. const username = Users.findOne(memberId).username;
  1871. // Check that the former member is member of the card
  1872. if (_.contains(doc.members, memberId)) {
  1873. Activities.insert({
  1874. userId,
  1875. username,
  1876. activityType: 'unjoinMember',
  1877. boardId: doc.boardId,
  1878. cardId: doc._id,
  1879. memberId,
  1880. listId: doc.listId,
  1881. swimlaneId: doc.swimlaneId,
  1882. });
  1883. }
  1884. }
  1885. }
  1886. function cardAssignees(userId, doc, fieldNames, modifier) {
  1887. if (!_.contains(fieldNames, 'assignees')) return;
  1888. let assigneeId;
  1889. // Say hello to the new assignee
  1890. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  1891. assigneeId = modifier.$addToSet.assignees;
  1892. const username = Users.findOne(assigneeId).username;
  1893. if (!_.contains(doc.assignees, assigneeId)) {
  1894. Activities.insert({
  1895. userId,
  1896. username,
  1897. activityType: 'joinAssignee',
  1898. boardId: doc.boardId,
  1899. cardId: doc._id,
  1900. assigneeId,
  1901. listId: doc.listId,
  1902. swimlaneId: doc.swimlaneId,
  1903. });
  1904. }
  1905. }
  1906. // Say goodbye to the former assignee
  1907. if (modifier.$pull && modifier.$pull.assignees) {
  1908. assigneeId = modifier.$pull.assignees;
  1909. const username = Users.findOne(assigneeId).username;
  1910. // Check that the former assignee is assignee of the card
  1911. if (_.contains(doc.assignees, assigneeId)) {
  1912. Activities.insert({
  1913. userId,
  1914. username,
  1915. activityType: 'unjoinAssignee',
  1916. boardId: doc.boardId,
  1917. cardId: doc._id,
  1918. assigneeId,
  1919. listId: doc.listId,
  1920. swimlaneId: doc.swimlaneId,
  1921. });
  1922. }
  1923. }
  1924. }
  1925. function cardLabels(userId, doc, fieldNames, modifier) {
  1926. if (!_.contains(fieldNames, 'labelIds')) return;
  1927. let labelId;
  1928. // Say hello to the new label
  1929. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1930. labelId = modifier.$addToSet.labelIds;
  1931. if (!_.contains(doc.labelIds, labelId)) {
  1932. const act = {
  1933. userId,
  1934. labelId,
  1935. activityType: 'addedLabel',
  1936. boardId: doc.boardId,
  1937. cardId: doc._id,
  1938. listId: doc.listId,
  1939. swimlaneId: doc.swimlaneId,
  1940. };
  1941. Activities.insert(act);
  1942. }
  1943. }
  1944. // Say goodbye to the label
  1945. if (modifier.$pull && modifier.$pull.labelIds) {
  1946. labelId = modifier.$pull.labelIds;
  1947. // Check that the former member is member of the card
  1948. if (_.contains(doc.labelIds, labelId)) {
  1949. Activities.insert({
  1950. userId,
  1951. labelId,
  1952. activityType: 'removedLabel',
  1953. boardId: doc.boardId,
  1954. cardId: doc._id,
  1955. listId: doc.listId,
  1956. swimlaneId: doc.swimlaneId,
  1957. });
  1958. }
  1959. }
  1960. }
  1961. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1962. if (!_.contains(fieldNames, 'customFields')) return;
  1963. // Say hello to the new customField value
  1964. if (modifier.$set) {
  1965. _.each(modifier.$set, (value, key) => {
  1966. if (key.startsWith('customFields')) {
  1967. const dotNotation = key.split('.');
  1968. // only individual changes are registered
  1969. if (dotNotation.length > 1) {
  1970. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1971. const act = {
  1972. userId,
  1973. customFieldId,
  1974. value,
  1975. activityType: 'setCustomField',
  1976. boardId: doc.boardId,
  1977. cardId: doc._id,
  1978. };
  1979. Activities.insert(act);
  1980. }
  1981. }
  1982. });
  1983. }
  1984. // Say goodbye to the former customField value
  1985. if (modifier.$unset) {
  1986. _.each(modifier.$unset, (value, key) => {
  1987. if (key.startsWith('customFields')) {
  1988. const dotNotation = key.split('.');
  1989. // only individual changes are registered
  1990. if (dotNotation.length > 1) {
  1991. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1992. const act = {
  1993. userId,
  1994. customFieldId,
  1995. activityType: 'unsetCustomField',
  1996. boardId: doc.boardId,
  1997. cardId: doc._id,
  1998. };
  1999. Activities.insert(act);
  2000. }
  2001. }
  2002. });
  2003. }
  2004. }
  2005. function cardCreation(userId, doc) {
  2006. Activities.insert({
  2007. userId,
  2008. activityType: 'createCard',
  2009. boardId: doc.boardId,
  2010. listName: Lists.findOne(doc.listId).title,
  2011. listId: doc.listId,
  2012. cardId: doc._id,
  2013. cardTitle: doc.title,
  2014. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2015. swimlaneId: doc.swimlaneId,
  2016. });
  2017. }
  2018. function cardRemover(userId, doc) {
  2019. ChecklistItems.remove({
  2020. cardId: doc._id,
  2021. });
  2022. Checklists.remove({
  2023. cardId: doc._id,
  2024. });
  2025. Cards.remove({
  2026. parentId: doc._id,
  2027. });
  2028. CardComments.remove({
  2029. cardId: doc._id,
  2030. });
  2031. Attachments.remove({
  2032. cardId: doc._id,
  2033. });
  2034. }
  2035. const findDueCards = days => {
  2036. const seekDue = ($from, $to, activityType) => {
  2037. Cards.find({
  2038. archived: false,
  2039. dueAt: { $gte: $from, $lt: $to },
  2040. }).forEach(card => {
  2041. const username = Users.findOne(card.userId).username;
  2042. const activity = {
  2043. userId: card.userId,
  2044. username,
  2045. activityType,
  2046. boardId: card.boardId,
  2047. cardId: card._id,
  2048. cardTitle: card.title,
  2049. listId: card.listId,
  2050. timeValue: card.dueAt,
  2051. swimlaneId: card.swimlaneId,
  2052. };
  2053. Activities.insert(activity);
  2054. });
  2055. };
  2056. const now = new Date(),
  2057. aday = 3600 * 24 * 1e3,
  2058. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  2059. if (!days) return;
  2060. if (!days.map) days = [days];
  2061. days.map(day => {
  2062. let args = [];
  2063. if (day === 0) {
  2064. args = [then(0), then(1), 'duenow'];
  2065. } else if (day > 0) {
  2066. args = [then(1), then(day), 'almostdue'];
  2067. } else {
  2068. args = [then(day), now, 'pastdue'];
  2069. }
  2070. seekDue(...args);
  2071. });
  2072. };
  2073. const addCronJob = _.debounce(
  2074. Meteor.bindEnvironment(function findDueCardsDebounced() {
  2075. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  2076. if (!envValue) {
  2077. return;
  2078. }
  2079. const notifydays = envValue
  2080. .split(',')
  2081. .map(value => {
  2082. const iValue = parseInt(value, 10);
  2083. if (!(iValue > 0 && iValue < 15)) {
  2084. // notifying due is disabled
  2085. return false;
  2086. } else {
  2087. return iValue;
  2088. }
  2089. })
  2090. .filter(Boolean);
  2091. 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
  2092. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  2093. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  2094. const scheduler = (job => () => {
  2095. const now = new Date();
  2096. const hour = 3600 * 1e3;
  2097. if (now.getHours() === itvl) {
  2098. if (typeof job === 'function') {
  2099. job();
  2100. }
  2101. }
  2102. Meteor.setTimeout(scheduler, hour);
  2103. })(() => {
  2104. findDueCards(notifydays);
  2105. });
  2106. scheduler();
  2107. }),
  2108. 500,
  2109. );
  2110. if (Meteor.isServer) {
  2111. // Cards are often fetched within a board, so we create an index to make these
  2112. // queries more efficient.
  2113. Meteor.startup(() => {
  2114. Cards._collection._ensureIndex({ modifiedAt: -1 });
  2115. Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  2116. // https://github.com/wekan/wekan/issues/1863
  2117. // Swimlane added a new field in the cards collection of mongodb named parentId.
  2118. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  2119. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  2120. // To correct it, add Index to parentId:
  2121. Cards._collection._ensureIndex({ parentId: 1 });
  2122. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  2123. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  2124. // Meteor.call("findDueCards",notifydays,notifyitvl);
  2125. Meteor.defer(() => {
  2126. addCronJob();
  2127. });
  2128. });
  2129. Cards.after.insert((userId, doc) => {
  2130. cardCreation(userId, doc);
  2131. });
  2132. // New activity for card (un)archivage
  2133. Cards.after.update((userId, doc, fieldNames) => {
  2134. cardState(userId, doc, fieldNames);
  2135. });
  2136. //New activity for card moves
  2137. Cards.after.update(function(userId, doc, fieldNames) {
  2138. const oldListId = this.previous.listId;
  2139. const oldSwimlaneId = this.previous.swimlaneId;
  2140. const oldBoardId = this.previous.boardId;
  2141. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  2142. });
  2143. // Add a new activity if we add or remove a member to the card
  2144. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2145. cardMembers(userId, doc, fieldNames, modifier);
  2146. updateActivities(doc, fieldNames, modifier);
  2147. });
  2148. // Add a new activity if we add or remove a assignee to the card
  2149. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2150. cardAssignees(userId, doc, fieldNames, modifier);
  2151. updateActivities(doc, fieldNames, modifier);
  2152. });
  2153. // Add a new activity if we add or remove a label to the card
  2154. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2155. cardLabels(userId, doc, fieldNames, modifier);
  2156. });
  2157. // Add a new activity if we edit a custom field
  2158. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2159. cardCustomFields(userId, doc, fieldNames, modifier);
  2160. });
  2161. // Add a new activity if modify time related field like dueAt startAt etc
  2162. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2163. const dla = 'dateLastActivity';
  2164. const fields = fieldNames.filter(name => name !== dla);
  2165. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  2166. const action = fields[0];
  2167. if (fields.length > 0 && _.contains(timingaction, action)) {
  2168. // add activities for user change these attributes
  2169. const value = modifier.$set[action];
  2170. const oldvalue = doc[action] || '';
  2171. const activityType = `a-${action}`;
  2172. const card = Cards.findOne(doc._id);
  2173. const list = card.list();
  2174. if (list) {
  2175. // 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
  2176. const modifiedAt = new Date(
  2177. new Date(value).getTime() -
  2178. (action === 'endAt' ? 365 * 24 * 3600 * 1e3 : 0),
  2179. ); // set it as 1 year before
  2180. const boardId = list.boardId;
  2181. Lists.direct.update(
  2182. {
  2183. _id: list._id,
  2184. },
  2185. {
  2186. $set: {
  2187. modifiedAt,
  2188. boardId,
  2189. },
  2190. },
  2191. );
  2192. }
  2193. const username = Users.findOne(userId).username;
  2194. const activity = {
  2195. userId,
  2196. username,
  2197. activityType,
  2198. boardId: doc.boardId,
  2199. cardId: doc._id,
  2200. cardTitle: doc.title,
  2201. timeKey: action,
  2202. timeValue: value,
  2203. timeOldValue: oldvalue,
  2204. listId: card.listId,
  2205. swimlaneId: card.swimlaneId,
  2206. };
  2207. Activities.insert(activity);
  2208. }
  2209. });
  2210. // Remove all activities associated with a card if we remove the card
  2211. // Remove also card_comments / checklists / attachments
  2212. Cards.before.remove((userId, doc) => {
  2213. cardRemover(userId, doc);
  2214. });
  2215. }
  2216. //SWIMLANES REST API
  2217. if (Meteor.isServer) {
  2218. /**
  2219. * @operation get_swimlane_cards
  2220. * @summary get all cards attached to a swimlane
  2221. *
  2222. * @param {string} boardId the board ID
  2223. * @param {string} swimlaneId the swimlane ID
  2224. * @return_type [{_id: string,
  2225. * title: string,
  2226. * description: string,
  2227. * listId: string}]
  2228. */
  2229. JsonRoutes.add(
  2230. 'GET',
  2231. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  2232. function(req, res) {
  2233. const paramBoardId = req.params.boardId;
  2234. const paramSwimlaneId = req.params.swimlaneId;
  2235. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2236. JsonRoutes.sendResult(res, {
  2237. code: 200,
  2238. data: Cards.find({
  2239. boardId: paramBoardId,
  2240. swimlaneId: paramSwimlaneId,
  2241. archived: false,
  2242. }).map(function(doc) {
  2243. return {
  2244. _id: doc._id,
  2245. title: doc.title,
  2246. description: doc.description,
  2247. listId: doc.listId,
  2248. receivedAt: doc.receivedAt,
  2249. startAt: doc.startAt,
  2250. dueAt: doc.dueAt,
  2251. endAt: doc.endAt,
  2252. assignees: doc.assignees,
  2253. };
  2254. }),
  2255. });
  2256. },
  2257. );
  2258. }
  2259. //LISTS REST API
  2260. if (Meteor.isServer) {
  2261. /**
  2262. * @operation get_all_cards
  2263. * @summary Get all Cards attached to a List
  2264. *
  2265. * @param {string} boardId the board ID
  2266. * @param {string} listId the list ID
  2267. * @return_type [{_id: string,
  2268. * title: string,
  2269. * description: string}]
  2270. */
  2271. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  2272. req,
  2273. res,
  2274. ) {
  2275. const paramBoardId = req.params.boardId;
  2276. const paramListId = req.params.listId;
  2277. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2278. JsonRoutes.sendResult(res, {
  2279. code: 200,
  2280. data: Cards.find({
  2281. boardId: paramBoardId,
  2282. listId: paramListId,
  2283. archived: false,
  2284. }).map(function(doc) {
  2285. return {
  2286. _id: doc._id,
  2287. title: doc.title,
  2288. description: doc.description,
  2289. receivedAt: doc.receivedAt,
  2290. startAt: doc.startAt,
  2291. dueAt: doc.dueAt,
  2292. endAt: doc.endAt,
  2293. assignees: doc.assignees,
  2294. };
  2295. }),
  2296. });
  2297. });
  2298. /**
  2299. * @operation get_card
  2300. * @summary Get a Card
  2301. *
  2302. * @param {string} boardId the board ID
  2303. * @param {string} listId the list ID of the card
  2304. * @param {string} cardId the card ID
  2305. * @return_type Cards
  2306. */
  2307. JsonRoutes.add(
  2308. 'GET',
  2309. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2310. function(req, res) {
  2311. const paramBoardId = req.params.boardId;
  2312. const paramListId = req.params.listId;
  2313. const paramCardId = req.params.cardId;
  2314. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2315. JsonRoutes.sendResult(res, {
  2316. code: 200,
  2317. data: Cards.findOne({
  2318. _id: paramCardId,
  2319. listId: paramListId,
  2320. boardId: paramBoardId,
  2321. archived: false,
  2322. }),
  2323. });
  2324. },
  2325. );
  2326. /**
  2327. * @operation new_card
  2328. * @summary Create a new Card
  2329. *
  2330. * @param {string} boardId the board ID of the new card
  2331. * @param {string} listId the list ID of the new card
  2332. * @param {string} authorID the user ID of the person owning the card
  2333. * @param {string} parentId the parent ID of the new card
  2334. * @param {string} title the title of the new card
  2335. * @param {string} description the description of the new card
  2336. * @param {string} swimlaneId the swimlane ID of the new card
  2337. * @param {string} [members] the member IDs list of the new card
  2338. * @param {string} [assignees] the array of maximum one ID of assignee of the new card
  2339. * @return_type {_id: string}
  2340. */
  2341. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  2342. req,
  2343. res,
  2344. ) {
  2345. // Check user is logged in
  2346. Authentication.checkLoggedIn(req.userId);
  2347. const paramBoardId = req.params.boardId;
  2348. // Check user has permission to add card to the board
  2349. const board = Boards.findOne({
  2350. _id: paramBoardId,
  2351. });
  2352. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  2353. Authentication.checkAdminOrCondition(req.userId, addPermission);
  2354. const paramListId = req.params.listId;
  2355. const paramParentId = req.params.parentId;
  2356. const currentCards = Cards.find(
  2357. {
  2358. listId: paramListId,
  2359. archived: false,
  2360. },
  2361. { sort: ['sort'] },
  2362. );
  2363. const check = Users.findOne({
  2364. _id: req.body.authorId,
  2365. });
  2366. const members = req.body.members;
  2367. const assignees = req.body.assignees;
  2368. if (typeof check !== 'undefined') {
  2369. const id = Cards.direct.insert({
  2370. title: req.body.title,
  2371. boardId: paramBoardId,
  2372. listId: paramListId,
  2373. parentId: paramParentId,
  2374. description: req.body.description,
  2375. userId: req.body.authorId,
  2376. swimlaneId: req.body.swimlaneId,
  2377. sort: currentCards.count(),
  2378. members,
  2379. assignees,
  2380. });
  2381. JsonRoutes.sendResult(res, {
  2382. code: 200,
  2383. data: {
  2384. _id: id,
  2385. },
  2386. });
  2387. const card = Cards.findOne({
  2388. _id: id,
  2389. });
  2390. cardCreation(req.body.authorId, card);
  2391. } else {
  2392. JsonRoutes.sendResult(res, {
  2393. code: 401,
  2394. });
  2395. }
  2396. });
  2397. /*
  2398. * Note for the JSDoc:
  2399. * 'list' will be interpreted as the path parameter
  2400. * 'listID' will be interpreted as the body parameter
  2401. */
  2402. /**
  2403. * @operation edit_card
  2404. * @summary Edit Fields in a Card
  2405. *
  2406. * @description Edit a card
  2407. *
  2408. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  2409. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  2410. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  2411. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  2412. * `indigo`:
  2413. *
  2414. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  2415. *
  2416. * Note: setting the color to white has the same effect than removing it.
  2417. *
  2418. * @param {string} boardId the board ID of the card
  2419. * @param {string} list the list ID of the card
  2420. * @param {string} cardId the ID of the card
  2421. * @param {string} [title] the new title of the card
  2422. * @param {string} [listId] the new list ID of the card (move operation)
  2423. * @param {string} [description] the new description of the card
  2424. * @param {string} [authorId] change the owner of the card
  2425. * @param {string} [parentId] change the parent of the card
  2426. * @param {string} [labelIds] the new list of label IDs attached to the card
  2427. * @param {string} [swimlaneId] the new swimlane ID of the card
  2428. * @param {string} [members] the new list of member IDs attached to the card
  2429. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  2430. * @param {string} [requestedBy] the new requestedBy field of the card
  2431. * @param {string} [assignedBy] the new assignedBy field of the card
  2432. * @param {string} [receivedAt] the new receivedAt field of the card
  2433. * @param {string} [assignBy] the new assignBy field of the card
  2434. * @param {string} [startAt] the new startAt field of the card
  2435. * @param {string} [dueAt] the new dueAt field of the card
  2436. * @param {string} [endAt] the new endAt field of the card
  2437. * @param {string} [spentTime] the new spentTime field of the card
  2438. * @param {boolean} [isOverTime] the new isOverTime field of the card
  2439. * @param {string} [customFields] the new customFields value of the card
  2440. * @param {string} [color] the new color of the card
  2441. * @param {Object} [vote] the vote object
  2442. * @param {string} vote.question the vote question
  2443. * @param {boolean} vote.public show who voted what
  2444. * @param {boolean} vote.allowNonBoardMembers allow all logged in users to vote?
  2445. * @return_type {_id: string}
  2446. */
  2447. JsonRoutes.add(
  2448. 'PUT',
  2449. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2450. function(req, res) {
  2451. Authentication.checkUserId(req.userId);
  2452. const paramBoardId = req.params.boardId;
  2453. const paramCardId = req.params.cardId;
  2454. const paramListId = req.params.listId;
  2455. if (req.body.hasOwnProperty('title')) {
  2456. const newTitle = req.body.title;
  2457. Cards.direct.update(
  2458. {
  2459. _id: paramCardId,
  2460. listId: paramListId,
  2461. boardId: paramBoardId,
  2462. archived: false,
  2463. },
  2464. {
  2465. $set: {
  2466. title: newTitle,
  2467. },
  2468. },
  2469. );
  2470. }
  2471. if (req.body.hasOwnProperty('parentId')) {
  2472. const newParentId = req.body.parentId;
  2473. Cards.direct.update(
  2474. {
  2475. _id: paramCardId,
  2476. listId: paramListId,
  2477. boardId: paramBoardId,
  2478. archived: false,
  2479. },
  2480. {
  2481. $set: {
  2482. parentId: newParentId,
  2483. },
  2484. },
  2485. );
  2486. }
  2487. if (req.body.hasOwnProperty('description')) {
  2488. const newDescription = req.body.description;
  2489. Cards.direct.update(
  2490. {
  2491. _id: paramCardId,
  2492. listId: paramListId,
  2493. boardId: paramBoardId,
  2494. archived: false,
  2495. },
  2496. {
  2497. $set: {
  2498. description: newDescription,
  2499. },
  2500. },
  2501. );
  2502. }
  2503. if (req.body.hasOwnProperty('color')) {
  2504. const newColor = req.body.color;
  2505. Cards.direct.update(
  2506. {
  2507. _id: paramCardId,
  2508. listId: paramListId,
  2509. boardId: paramBoardId,
  2510. archived: false,
  2511. },
  2512. { $set: { color: newColor } },
  2513. );
  2514. }
  2515. if (req.body.hasOwnProperty('vote')) {
  2516. const newVote = req.body.vote;
  2517. newVote.positive = [];
  2518. newVote.negative = [];
  2519. if (!newVote.hasOwnProperty('public')) newVote.public = false;
  2520. if (!newVote.hasOwnProperty('allowNonBoardMembers'))
  2521. newVote.allowNonBoardMembers = false;
  2522. Cards.direct.update(
  2523. {
  2524. _id: paramCardId,
  2525. listId: paramListId,
  2526. boardId: paramBoardId,
  2527. archived: false,
  2528. },
  2529. { $set: { vote: newVote } },
  2530. );
  2531. }
  2532. if (req.body.hasOwnProperty('labelIds')) {
  2533. let newlabelIds = req.body.labelIds;
  2534. if (_.isString(newlabelIds)) {
  2535. if (newlabelIds === '') {
  2536. newlabelIds = null;
  2537. } else {
  2538. newlabelIds = [newlabelIds];
  2539. }
  2540. }
  2541. Cards.direct.update(
  2542. {
  2543. _id: paramCardId,
  2544. listId: paramListId,
  2545. boardId: paramBoardId,
  2546. archived: false,
  2547. },
  2548. {
  2549. $set: {
  2550. labelIds: newlabelIds,
  2551. },
  2552. },
  2553. );
  2554. }
  2555. if (req.body.hasOwnProperty('requestedBy')) {
  2556. const newrequestedBy = req.body.requestedBy;
  2557. Cards.direct.update(
  2558. {
  2559. _id: paramCardId,
  2560. listId: paramListId,
  2561. boardId: paramBoardId,
  2562. archived: false,
  2563. },
  2564. { $set: { requestedBy: newrequestedBy } },
  2565. );
  2566. }
  2567. if (req.body.hasOwnProperty('assignedBy')) {
  2568. const newassignedBy = req.body.assignedBy;
  2569. Cards.direct.update(
  2570. {
  2571. _id: paramCardId,
  2572. listId: paramListId,
  2573. boardId: paramBoardId,
  2574. archived: false,
  2575. },
  2576. { $set: { assignedBy: newassignedBy } },
  2577. );
  2578. }
  2579. if (req.body.hasOwnProperty('receivedAt')) {
  2580. const newreceivedAt = req.body.receivedAt;
  2581. Cards.direct.update(
  2582. {
  2583. _id: paramCardId,
  2584. listId: paramListId,
  2585. boardId: paramBoardId,
  2586. archived: false,
  2587. },
  2588. { $set: { receivedAt: newreceivedAt } },
  2589. );
  2590. }
  2591. if (req.body.hasOwnProperty('startAt')) {
  2592. const newstartAt = req.body.startAt;
  2593. Cards.direct.update(
  2594. {
  2595. _id: paramCardId,
  2596. listId: paramListId,
  2597. boardId: paramBoardId,
  2598. archived: false,
  2599. },
  2600. { $set: { startAt: newstartAt } },
  2601. );
  2602. }
  2603. if (req.body.hasOwnProperty('dueAt')) {
  2604. const newdueAt = req.body.dueAt;
  2605. Cards.direct.update(
  2606. {
  2607. _id: paramCardId,
  2608. listId: paramListId,
  2609. boardId: paramBoardId,
  2610. archived: false,
  2611. },
  2612. { $set: { dueAt: newdueAt } },
  2613. );
  2614. }
  2615. if (req.body.hasOwnProperty('endAt')) {
  2616. const newendAt = req.body.endAt;
  2617. Cards.direct.update(
  2618. {
  2619. _id: paramCardId,
  2620. listId: paramListId,
  2621. boardId: paramBoardId,
  2622. archived: false,
  2623. },
  2624. { $set: { endAt: newendAt } },
  2625. );
  2626. }
  2627. if (req.body.hasOwnProperty('spentTime')) {
  2628. const newspentTime = req.body.spentTime;
  2629. Cards.direct.update(
  2630. {
  2631. _id: paramCardId,
  2632. listId: paramListId,
  2633. boardId: paramBoardId,
  2634. archived: false,
  2635. },
  2636. { $set: { spentTime: newspentTime } },
  2637. );
  2638. }
  2639. if (req.body.hasOwnProperty('isOverTime')) {
  2640. const newisOverTime = req.body.isOverTime;
  2641. Cards.direct.update(
  2642. {
  2643. _id: paramCardId,
  2644. listId: paramListId,
  2645. boardId: paramBoardId,
  2646. archived: false,
  2647. },
  2648. { $set: { isOverTime: newisOverTime } },
  2649. );
  2650. }
  2651. if (req.body.hasOwnProperty('customFields')) {
  2652. const newcustomFields = req.body.customFields;
  2653. Cards.direct.update(
  2654. {
  2655. _id: paramCardId,
  2656. listId: paramListId,
  2657. boardId: paramBoardId,
  2658. archived: false,
  2659. },
  2660. { $set: { customFields: newcustomFields } },
  2661. );
  2662. }
  2663. if (req.body.hasOwnProperty('members')) {
  2664. let newmembers = req.body.members;
  2665. if (_.isString(newmembers)) {
  2666. if (newmembers === '') {
  2667. newmembers = null;
  2668. } else {
  2669. newmembers = [newmembers];
  2670. }
  2671. }
  2672. Cards.direct.update(
  2673. {
  2674. _id: paramCardId,
  2675. listId: paramListId,
  2676. boardId: paramBoardId,
  2677. archived: false,
  2678. },
  2679. { $set: { members: newmembers } },
  2680. );
  2681. }
  2682. if (req.body.hasOwnProperty('assignees')) {
  2683. let newassignees = req.body.assignees;
  2684. if (_.isString(newassignees)) {
  2685. if (newassignees === '') {
  2686. newassignees = null;
  2687. } else {
  2688. newassignees = [newassignees];
  2689. }
  2690. }
  2691. Cards.direct.update(
  2692. {
  2693. _id: paramCardId,
  2694. listId: paramListId,
  2695. boardId: paramBoardId,
  2696. archived: false,
  2697. },
  2698. { $set: { assignees: newassignees } },
  2699. );
  2700. }
  2701. if (req.body.hasOwnProperty('swimlaneId')) {
  2702. const newParamSwimlaneId = req.body.swimlaneId;
  2703. Cards.direct.update(
  2704. {
  2705. _id: paramCardId,
  2706. listId: paramListId,
  2707. boardId: paramBoardId,
  2708. archived: false,
  2709. },
  2710. { $set: { swimlaneId: newParamSwimlaneId } },
  2711. );
  2712. }
  2713. if (req.body.hasOwnProperty('listId')) {
  2714. const newParamListId = req.body.listId;
  2715. Cards.direct.update(
  2716. {
  2717. _id: paramCardId,
  2718. listId: paramListId,
  2719. boardId: paramBoardId,
  2720. archived: false,
  2721. },
  2722. {
  2723. $set: {
  2724. listId: newParamListId,
  2725. },
  2726. },
  2727. );
  2728. const card = Cards.findOne({
  2729. _id: paramCardId,
  2730. });
  2731. cardMove(
  2732. req.body.authorId,
  2733. card,
  2734. {
  2735. fieldName: 'listId',
  2736. },
  2737. paramListId,
  2738. );
  2739. }
  2740. JsonRoutes.sendResult(res, {
  2741. code: 200,
  2742. data: {
  2743. _id: paramCardId,
  2744. },
  2745. });
  2746. },
  2747. );
  2748. /**
  2749. * @operation delete_card
  2750. * @summary Delete a card from a board
  2751. *
  2752. * @description This operation **deletes** a card, and therefore the card
  2753. * is not put in the recycle bin.
  2754. *
  2755. * @param {string} boardId the board ID of the card
  2756. * @param {string} list the list ID of the card
  2757. * @param {string} cardId the ID of the card
  2758. * @return_type {_id: string}
  2759. */
  2760. JsonRoutes.add(
  2761. 'DELETE',
  2762. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2763. function(req, res) {
  2764. Authentication.checkUserId(req.userId);
  2765. const paramBoardId = req.params.boardId;
  2766. const paramListId = req.params.listId;
  2767. const paramCardId = req.params.cardId;
  2768. const card = Cards.findOne({
  2769. _id: paramCardId,
  2770. });
  2771. Cards.direct.remove({
  2772. _id: paramCardId,
  2773. listId: paramListId,
  2774. boardId: paramBoardId,
  2775. });
  2776. cardRemover(req.body.authorId, card);
  2777. JsonRoutes.sendResult(res, {
  2778. code: 200,
  2779. data: {
  2780. _id: paramCardId,
  2781. },
  2782. });
  2783. },
  2784. );
  2785. /**
  2786. * @operation get_cards_by_custom_field
  2787. * @summary Get all Cards that matchs a value of a specific custom field
  2788. *
  2789. * @param {string} boardId the board ID
  2790. * @param {string} customFieldId the list ID
  2791. * @param {string} customFieldValue the value to look for
  2792. * @return_type [{_id: string,
  2793. * title: string,
  2794. * description: string,
  2795. * listId: string,
  2796. * swinlaneId: string}]
  2797. */
  2798. JsonRoutes.add(
  2799. 'GET',
  2800. '/api/boards/:boardId/cardsByCustomField/:customFieldId/:customFieldValue',
  2801. function(req, res) {
  2802. const paramBoardId = req.params.boardId;
  2803. const paramCustomFieldId = req.params.customFieldId;
  2804. const paramCustomFieldValue = req.params.customFieldValue;
  2805. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2806. JsonRoutes.sendResult(res, {
  2807. code: 200,
  2808. data: Cards.find({
  2809. boardId: paramBoardId,
  2810. customFields: {
  2811. $elemMatch: {
  2812. _id: paramCustomFieldId,
  2813. value: paramCustomFieldValue,
  2814. },
  2815. },
  2816. archived: false,
  2817. }).fetch(),
  2818. });
  2819. },
  2820. );
  2821. }
  2822. export default Cards;