cards.js 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024
  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:', 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: new RegExp(query, 'i'),
  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: new RegExp(query, 'i'),
  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: new RegExp(query, 'i'),
  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. if (Meteor.isServer) {
  1755. Users.update(userId, {
  1756. $set: {
  1757. 'sessionData.totalHits': cards.count(),
  1758. 'sessionData.lastHit': cards.count() > 50 ? 50 : cards.count(),
  1759. },
  1760. });
  1761. }
  1762. return { cards, errors };
  1763. };
  1764. //FUNCTIONS FOR creation of Activities
  1765. function updateActivities(doc, fieldNames, modifier) {
  1766. if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
  1767. Activities.find({
  1768. activityType: 'addedLabel',
  1769. cardId: doc._id,
  1770. }).forEach(a => {
  1771. const lidx = doc.labelIds.indexOf(a.labelId);
  1772. if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
  1773. Activities.update(a._id, {
  1774. $set: {
  1775. labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
  1776. boardId: modifier.$set.boardId,
  1777. },
  1778. });
  1779. } else {
  1780. Activities.remove(a._id);
  1781. }
  1782. });
  1783. } else if (_.contains(fieldNames, 'boardId')) {
  1784. Activities.remove({
  1785. activityType: 'addedLabel',
  1786. cardId: doc._id,
  1787. });
  1788. }
  1789. }
  1790. function cardMove(
  1791. userId,
  1792. doc,
  1793. fieldNames,
  1794. oldListId,
  1795. oldSwimlaneId,
  1796. oldBoardId,
  1797. ) {
  1798. if (_.contains(fieldNames, 'boardId') && doc.boardId !== oldBoardId) {
  1799. Activities.insert({
  1800. userId,
  1801. activityType: 'moveCardBoard',
  1802. boardName: Boards.findOne(doc.boardId).title,
  1803. boardId: doc.boardId,
  1804. oldBoardId,
  1805. oldBoardName: Boards.findOne(oldBoardId).title,
  1806. cardId: doc._id,
  1807. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1808. swimlaneId: doc.swimlaneId,
  1809. oldSwimlaneId,
  1810. });
  1811. } else if (
  1812. (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
  1813. (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)
  1814. ) {
  1815. Activities.insert({
  1816. userId,
  1817. oldListId,
  1818. activityType: 'moveCard',
  1819. listName: Lists.findOne(doc.listId).title,
  1820. listId: doc.listId,
  1821. boardId: doc.boardId,
  1822. cardId: doc._id,
  1823. cardTitle: doc.title,
  1824. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  1825. swimlaneId: doc.swimlaneId,
  1826. oldSwimlaneId,
  1827. });
  1828. }
  1829. }
  1830. function cardState(userId, doc, fieldNames) {
  1831. if (_.contains(fieldNames, 'archived')) {
  1832. if (doc.archived) {
  1833. Activities.insert({
  1834. userId,
  1835. activityType: 'archivedCard',
  1836. listName: Lists.findOne(doc.listId).title,
  1837. boardId: doc.boardId,
  1838. listId: doc.listId,
  1839. cardId: doc._id,
  1840. swimlaneId: doc.swimlaneId,
  1841. });
  1842. } else {
  1843. Activities.insert({
  1844. userId,
  1845. activityType: 'restoredCard',
  1846. boardId: doc.boardId,
  1847. listName: Lists.findOne(doc.listId).title,
  1848. listId: doc.listId,
  1849. cardId: doc._id,
  1850. swimlaneId: doc.swimlaneId,
  1851. });
  1852. }
  1853. }
  1854. }
  1855. function cardMembers(userId, doc, fieldNames, modifier) {
  1856. if (!_.contains(fieldNames, 'members')) return;
  1857. let memberId;
  1858. // Say hello to the new member
  1859. if (modifier.$addToSet && modifier.$addToSet.members) {
  1860. memberId = modifier.$addToSet.members;
  1861. const username = Users.findOne(memberId).username;
  1862. if (!_.contains(doc.members, memberId)) {
  1863. Activities.insert({
  1864. userId,
  1865. username,
  1866. activityType: 'joinMember',
  1867. boardId: doc.boardId,
  1868. cardId: doc._id,
  1869. memberId,
  1870. listId: doc.listId,
  1871. swimlaneId: doc.swimlaneId,
  1872. });
  1873. }
  1874. }
  1875. // Say goodbye to the former member
  1876. if (modifier.$pull && modifier.$pull.members) {
  1877. memberId = modifier.$pull.members;
  1878. const username = Users.findOne(memberId).username;
  1879. // Check that the former member is member of the card
  1880. if (_.contains(doc.members, memberId)) {
  1881. Activities.insert({
  1882. userId,
  1883. username,
  1884. activityType: 'unjoinMember',
  1885. boardId: doc.boardId,
  1886. cardId: doc._id,
  1887. memberId,
  1888. listId: doc.listId,
  1889. swimlaneId: doc.swimlaneId,
  1890. });
  1891. }
  1892. }
  1893. }
  1894. function cardAssignees(userId, doc, fieldNames, modifier) {
  1895. if (!_.contains(fieldNames, 'assignees')) return;
  1896. let assigneeId;
  1897. // Say hello to the new assignee
  1898. if (modifier.$addToSet && modifier.$addToSet.assignees) {
  1899. assigneeId = modifier.$addToSet.assignees;
  1900. const username = Users.findOne(assigneeId).username;
  1901. if (!_.contains(doc.assignees, assigneeId)) {
  1902. Activities.insert({
  1903. userId,
  1904. username,
  1905. activityType: 'joinAssignee',
  1906. boardId: doc.boardId,
  1907. cardId: doc._id,
  1908. assigneeId,
  1909. listId: doc.listId,
  1910. swimlaneId: doc.swimlaneId,
  1911. });
  1912. }
  1913. }
  1914. // Say goodbye to the former assignee
  1915. if (modifier.$pull && modifier.$pull.assignees) {
  1916. assigneeId = modifier.$pull.assignees;
  1917. const username = Users.findOne(assigneeId).username;
  1918. // Check that the former assignee is assignee of the card
  1919. if (_.contains(doc.assignees, assigneeId)) {
  1920. Activities.insert({
  1921. userId,
  1922. username,
  1923. activityType: 'unjoinAssignee',
  1924. boardId: doc.boardId,
  1925. cardId: doc._id,
  1926. assigneeId,
  1927. listId: doc.listId,
  1928. swimlaneId: doc.swimlaneId,
  1929. });
  1930. }
  1931. }
  1932. }
  1933. function cardLabels(userId, doc, fieldNames, modifier) {
  1934. if (!_.contains(fieldNames, 'labelIds')) return;
  1935. let labelId;
  1936. // Say hello to the new label
  1937. if (modifier.$addToSet && modifier.$addToSet.labelIds) {
  1938. labelId = modifier.$addToSet.labelIds;
  1939. if (!_.contains(doc.labelIds, labelId)) {
  1940. const act = {
  1941. userId,
  1942. labelId,
  1943. activityType: 'addedLabel',
  1944. boardId: doc.boardId,
  1945. cardId: doc._id,
  1946. listId: doc.listId,
  1947. swimlaneId: doc.swimlaneId,
  1948. };
  1949. Activities.insert(act);
  1950. }
  1951. }
  1952. // Say goodbye to the label
  1953. if (modifier.$pull && modifier.$pull.labelIds) {
  1954. labelId = modifier.$pull.labelIds;
  1955. // Check that the former member is member of the card
  1956. if (_.contains(doc.labelIds, labelId)) {
  1957. Activities.insert({
  1958. userId,
  1959. labelId,
  1960. activityType: 'removedLabel',
  1961. boardId: doc.boardId,
  1962. cardId: doc._id,
  1963. listId: doc.listId,
  1964. swimlaneId: doc.swimlaneId,
  1965. });
  1966. }
  1967. }
  1968. }
  1969. function cardCustomFields(userId, doc, fieldNames, modifier) {
  1970. if (!_.contains(fieldNames, 'customFields')) return;
  1971. // Say hello to the new customField value
  1972. if (modifier.$set) {
  1973. _.each(modifier.$set, (value, key) => {
  1974. if (key.startsWith('customFields')) {
  1975. const dotNotation = key.split('.');
  1976. // only individual changes are registered
  1977. if (dotNotation.length > 1) {
  1978. const customFieldId = doc.customFields[dotNotation[1]]._id;
  1979. const act = {
  1980. userId,
  1981. customFieldId,
  1982. value,
  1983. activityType: 'setCustomField',
  1984. boardId: doc.boardId,
  1985. cardId: doc._id,
  1986. };
  1987. Activities.insert(act);
  1988. }
  1989. }
  1990. });
  1991. }
  1992. // Say goodbye to the former customField value
  1993. if (modifier.$unset) {
  1994. _.each(modifier.$unset, (value, key) => {
  1995. if (key.startsWith('customFields')) {
  1996. const dotNotation = key.split('.');
  1997. // only individual changes are registered
  1998. if (dotNotation.length > 1) {
  1999. const customFieldId = doc.customFields[dotNotation[1]]._id;
  2000. const act = {
  2001. userId,
  2002. customFieldId,
  2003. activityType: 'unsetCustomField',
  2004. boardId: doc.boardId,
  2005. cardId: doc._id,
  2006. };
  2007. Activities.insert(act);
  2008. }
  2009. }
  2010. });
  2011. }
  2012. }
  2013. function cardCreation(userId, doc) {
  2014. Activities.insert({
  2015. userId,
  2016. activityType: 'createCard',
  2017. boardId: doc.boardId,
  2018. listName: Lists.findOne(doc.listId).title,
  2019. listId: doc.listId,
  2020. cardId: doc._id,
  2021. cardTitle: doc.title,
  2022. swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
  2023. swimlaneId: doc.swimlaneId,
  2024. });
  2025. }
  2026. function cardRemover(userId, doc) {
  2027. ChecklistItems.remove({
  2028. cardId: doc._id,
  2029. });
  2030. Checklists.remove({
  2031. cardId: doc._id,
  2032. });
  2033. Cards.remove({
  2034. parentId: doc._id,
  2035. });
  2036. CardComments.remove({
  2037. cardId: doc._id,
  2038. });
  2039. Attachments.remove({
  2040. cardId: doc._id,
  2041. });
  2042. }
  2043. const findDueCards = days => {
  2044. const seekDue = ($from, $to, activityType) => {
  2045. Cards.find({
  2046. archived: false,
  2047. dueAt: { $gte: $from, $lt: $to },
  2048. }).forEach(card => {
  2049. const username = Users.findOne(card.userId).username;
  2050. const activity = {
  2051. userId: card.userId,
  2052. username,
  2053. activityType,
  2054. boardId: card.boardId,
  2055. cardId: card._id,
  2056. cardTitle: card.title,
  2057. listId: card.listId,
  2058. timeValue: card.dueAt,
  2059. swimlaneId: card.swimlaneId,
  2060. };
  2061. Activities.insert(activity);
  2062. });
  2063. };
  2064. const now = new Date(),
  2065. aday = 3600 * 24 * 1e3,
  2066. then = day => new Date(now.setHours(0, 0, 0, 0) + day * aday);
  2067. if (!days) return;
  2068. if (!days.map) days = [days];
  2069. days.map(day => {
  2070. let args = [];
  2071. if (day === 0) {
  2072. args = [then(0), then(1), 'duenow'];
  2073. } else if (day > 0) {
  2074. args = [then(1), then(day), 'almostdue'];
  2075. } else {
  2076. args = [then(day), now, 'pastdue'];
  2077. }
  2078. seekDue(...args);
  2079. });
  2080. };
  2081. const addCronJob = _.debounce(
  2082. Meteor.bindEnvironment(function findDueCardsDebounced() {
  2083. const envValue = process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER;
  2084. if (!envValue) {
  2085. return;
  2086. }
  2087. const notifydays = envValue
  2088. .split(',')
  2089. .map(value => {
  2090. const iValue = parseInt(value, 10);
  2091. if (!(iValue > 0 && iValue < 15)) {
  2092. // notifying due is disabled
  2093. return false;
  2094. } else {
  2095. return iValue;
  2096. }
  2097. })
  2098. .filter(Boolean);
  2099. 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
  2100. const defaultitvl = 8; // default every morning at 8am, if the passed env variable has parsing error use default
  2101. const itvl = parseInt(notifyitvl, 10) || defaultitvl;
  2102. const scheduler = (job => () => {
  2103. const now = new Date();
  2104. const hour = 3600 * 1e3;
  2105. if (now.getHours() === itvl) {
  2106. if (typeof job === 'function') {
  2107. job();
  2108. }
  2109. }
  2110. Meteor.setTimeout(scheduler, hour);
  2111. })(() => {
  2112. findDueCards(notifydays);
  2113. });
  2114. scheduler();
  2115. }),
  2116. 500,
  2117. );
  2118. if (Meteor.isServer) {
  2119. // Cards are often fetched within a board, so we create an index to make these
  2120. // queries more efficient.
  2121. Meteor.startup(() => {
  2122. Cards._collection._ensureIndex({ modifiedAt: -1 });
  2123. Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  2124. // https://github.com/wekan/wekan/issues/1863
  2125. // Swimlane added a new field in the cards collection of mongodb named parentId.
  2126. // When loading a board, mongodb is searching for every cards, the id of the parent (in the swinglanes collection).
  2127. // With a huge database, this result in a very slow app and high CPU on the mongodb side.
  2128. // To correct it, add Index to parentId:
  2129. Cards._collection._ensureIndex({ parentId: 1 });
  2130. // let notifydays = parseInt(process.env.NOTIFY_DUE_DAYS_BEFORE_AND_AFTER) || 2; // default as 2 days b4 and after
  2131. // let notifyitvl = parseInt(process.env.NOTIFY_DUE_AT_HOUR_OF_DAY) || 3600 * 24 * 1e3; // default interval as one day
  2132. // Meteor.call("findDueCards",notifydays,notifyitvl);
  2133. Meteor.defer(() => {
  2134. addCronJob();
  2135. });
  2136. });
  2137. Cards.after.insert((userId, doc) => {
  2138. cardCreation(userId, doc);
  2139. });
  2140. // New activity for card (un)archivage
  2141. Cards.after.update((userId, doc, fieldNames) => {
  2142. cardState(userId, doc, fieldNames);
  2143. });
  2144. //New activity for card moves
  2145. Cards.after.update(function(userId, doc, fieldNames) {
  2146. const oldListId = this.previous.listId;
  2147. const oldSwimlaneId = this.previous.swimlaneId;
  2148. const oldBoardId = this.previous.boardId;
  2149. cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
  2150. });
  2151. // Add a new activity if we add or remove a member to the card
  2152. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2153. cardMembers(userId, doc, fieldNames, modifier);
  2154. updateActivities(doc, fieldNames, modifier);
  2155. });
  2156. // Add a new activity if we add or remove a assignee to the card
  2157. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2158. cardAssignees(userId, doc, fieldNames, modifier);
  2159. updateActivities(doc, fieldNames, modifier);
  2160. });
  2161. // Add a new activity if we add or remove a label to the card
  2162. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2163. cardLabels(userId, doc, fieldNames, modifier);
  2164. });
  2165. // Add a new activity if we edit a custom field
  2166. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2167. cardCustomFields(userId, doc, fieldNames, modifier);
  2168. });
  2169. // Add a new activity if modify time related field like dueAt startAt etc
  2170. Cards.before.update((userId, doc, fieldNames, modifier) => {
  2171. const dla = 'dateLastActivity';
  2172. const fields = fieldNames.filter(name => name !== dla);
  2173. const timingaction = ['receivedAt', 'dueAt', 'startAt', 'endAt'];
  2174. const action = fields[0];
  2175. if (fields.length > 0 && _.contains(timingaction, action)) {
  2176. // add activities for user change these attributes
  2177. const value = modifier.$set[action];
  2178. const oldvalue = doc[action] || '';
  2179. const activityType = `a-${action}`;
  2180. const card = Cards.findOne(doc._id);
  2181. const list = card.list();
  2182. if (list) {
  2183. // 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
  2184. const modifiedAt = new Date(
  2185. new Date(value).getTime() -
  2186. (action === 'endAt' ? 365 * 24 * 3600 * 1e3 : 0),
  2187. ); // set it as 1 year before
  2188. const boardId = list.boardId;
  2189. Lists.direct.update(
  2190. {
  2191. _id: list._id,
  2192. },
  2193. {
  2194. $set: {
  2195. modifiedAt,
  2196. boardId,
  2197. },
  2198. },
  2199. );
  2200. }
  2201. const username = Users.findOne(userId).username;
  2202. const activity = {
  2203. userId,
  2204. username,
  2205. activityType,
  2206. boardId: doc.boardId,
  2207. cardId: doc._id,
  2208. cardTitle: doc.title,
  2209. timeKey: action,
  2210. timeValue: value,
  2211. timeOldValue: oldvalue,
  2212. listId: card.listId,
  2213. swimlaneId: card.swimlaneId,
  2214. };
  2215. Activities.insert(activity);
  2216. }
  2217. });
  2218. // Remove all activities associated with a card if we remove the card
  2219. // Remove also card_comments / checklists / attachments
  2220. Cards.before.remove((userId, doc) => {
  2221. cardRemover(userId, doc);
  2222. });
  2223. }
  2224. //SWIMLANES REST API
  2225. if (Meteor.isServer) {
  2226. /**
  2227. * @operation get_swimlane_cards
  2228. * @summary get all cards attached to a swimlane
  2229. *
  2230. * @param {string} boardId the board ID
  2231. * @param {string} swimlaneId the swimlane ID
  2232. * @return_type [{_id: string,
  2233. * title: string,
  2234. * description: string,
  2235. * listId: string}]
  2236. */
  2237. JsonRoutes.add(
  2238. 'GET',
  2239. '/api/boards/:boardId/swimlanes/:swimlaneId/cards',
  2240. function(req, res) {
  2241. const paramBoardId = req.params.boardId;
  2242. const paramSwimlaneId = req.params.swimlaneId;
  2243. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2244. JsonRoutes.sendResult(res, {
  2245. code: 200,
  2246. data: Cards.find({
  2247. boardId: paramBoardId,
  2248. swimlaneId: paramSwimlaneId,
  2249. archived: false,
  2250. }).map(function(doc) {
  2251. return {
  2252. _id: doc._id,
  2253. title: doc.title,
  2254. description: doc.description,
  2255. listId: doc.listId,
  2256. receivedAt: doc.receivedAt,
  2257. startAt: doc.startAt,
  2258. dueAt: doc.dueAt,
  2259. endAt: doc.endAt,
  2260. assignees: doc.assignees,
  2261. };
  2262. }),
  2263. });
  2264. },
  2265. );
  2266. }
  2267. //LISTS REST API
  2268. if (Meteor.isServer) {
  2269. /**
  2270. * @operation get_all_cards
  2271. * @summary Get all Cards attached to a List
  2272. *
  2273. * @param {string} boardId the board ID
  2274. * @param {string} listId the list ID
  2275. * @return_type [{_id: string,
  2276. * title: string,
  2277. * description: string}]
  2278. */
  2279. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function(
  2280. req,
  2281. res,
  2282. ) {
  2283. const paramBoardId = req.params.boardId;
  2284. const paramListId = req.params.listId;
  2285. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2286. JsonRoutes.sendResult(res, {
  2287. code: 200,
  2288. data: Cards.find({
  2289. boardId: paramBoardId,
  2290. listId: paramListId,
  2291. archived: false,
  2292. }).map(function(doc) {
  2293. return {
  2294. _id: doc._id,
  2295. title: doc.title,
  2296. description: doc.description,
  2297. receivedAt: doc.receivedAt,
  2298. startAt: doc.startAt,
  2299. dueAt: doc.dueAt,
  2300. endAt: doc.endAt,
  2301. assignees: doc.assignees,
  2302. };
  2303. }),
  2304. });
  2305. });
  2306. /**
  2307. * @operation get_card
  2308. * @summary Get a Card
  2309. *
  2310. * @param {string} boardId the board ID
  2311. * @param {string} listId the list ID of the card
  2312. * @param {string} cardId the card ID
  2313. * @return_type Cards
  2314. */
  2315. JsonRoutes.add(
  2316. 'GET',
  2317. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2318. function(req, res) {
  2319. const paramBoardId = req.params.boardId;
  2320. const paramListId = req.params.listId;
  2321. const paramCardId = req.params.cardId;
  2322. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2323. JsonRoutes.sendResult(res, {
  2324. code: 200,
  2325. data: Cards.findOne({
  2326. _id: paramCardId,
  2327. listId: paramListId,
  2328. boardId: paramBoardId,
  2329. archived: false,
  2330. }),
  2331. });
  2332. },
  2333. );
  2334. /**
  2335. * @operation new_card
  2336. * @summary Create a new Card
  2337. *
  2338. * @param {string} boardId the board ID of the new card
  2339. * @param {string} listId the list ID of the new card
  2340. * @param {string} authorID the user ID of the person owning the card
  2341. * @param {string} parentId the parent ID of the new card
  2342. * @param {string} title the title of the new card
  2343. * @param {string} description the description of the new card
  2344. * @param {string} swimlaneId the swimlane ID of the new card
  2345. * @param {string} [members] the member IDs list of the new card
  2346. * @param {string} [assignees] the array of maximum one ID of assignee of the new card
  2347. * @return_type {_id: string}
  2348. */
  2349. JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function(
  2350. req,
  2351. res,
  2352. ) {
  2353. // Check user is logged in
  2354. Authentication.checkLoggedIn(req.userId);
  2355. const paramBoardId = req.params.boardId;
  2356. // Check user has permission to add card to the board
  2357. const board = Boards.findOne({
  2358. _id: paramBoardId,
  2359. });
  2360. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  2361. Authentication.checkAdminOrCondition(req.userId, addPermission);
  2362. const paramListId = req.params.listId;
  2363. const paramParentId = req.params.parentId;
  2364. const currentCards = Cards.find(
  2365. {
  2366. listId: paramListId,
  2367. archived: false,
  2368. },
  2369. { sort: ['sort'] },
  2370. );
  2371. const check = Users.findOne({
  2372. _id: req.body.authorId,
  2373. });
  2374. const members = req.body.members;
  2375. const assignees = req.body.assignees;
  2376. if (typeof check !== 'undefined') {
  2377. const id = Cards.direct.insert({
  2378. title: req.body.title,
  2379. boardId: paramBoardId,
  2380. listId: paramListId,
  2381. parentId: paramParentId,
  2382. description: req.body.description,
  2383. userId: req.body.authorId,
  2384. swimlaneId: req.body.swimlaneId,
  2385. sort: currentCards.count(),
  2386. members,
  2387. assignees,
  2388. });
  2389. JsonRoutes.sendResult(res, {
  2390. code: 200,
  2391. data: {
  2392. _id: id,
  2393. },
  2394. });
  2395. const card = Cards.findOne({
  2396. _id: id,
  2397. });
  2398. cardCreation(req.body.authorId, card);
  2399. } else {
  2400. JsonRoutes.sendResult(res, {
  2401. code: 401,
  2402. });
  2403. }
  2404. });
  2405. /*
  2406. * Note for the JSDoc:
  2407. * 'list' will be interpreted as the path parameter
  2408. * 'listID' will be interpreted as the body parameter
  2409. */
  2410. /**
  2411. * @operation edit_card
  2412. * @summary Edit Fields in a Card
  2413. *
  2414. * @description Edit a card
  2415. *
  2416. * The color has to be chosen between `white`, `green`, `yellow`, `orange`,
  2417. * `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`,
  2418. * `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`,
  2419. * `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`,
  2420. * `indigo`:
  2421. *
  2422. * <img src="/card-colors.png" width="40%" alt="Wekan card colors" />
  2423. *
  2424. * Note: setting the color to white has the same effect than removing it.
  2425. *
  2426. * @param {string} boardId the board ID of the card
  2427. * @param {string} list the list ID of the card
  2428. * @param {string} cardId the ID of the card
  2429. * @param {string} [title] the new title of the card
  2430. * @param {string} [listId] the new list ID of the card (move operation)
  2431. * @param {string} [description] the new description of the card
  2432. * @param {string} [authorId] change the owner of the card
  2433. * @param {string} [parentId] change the parent of the card
  2434. * @param {string} [labelIds] the new list of label IDs attached to the card
  2435. * @param {string} [swimlaneId] the new swimlane ID of the card
  2436. * @param {string} [members] the new list of member IDs attached to the card
  2437. * @param {string} [assignees] the array of maximum one ID of assignee attached to the card
  2438. * @param {string} [requestedBy] the new requestedBy field of the card
  2439. * @param {string} [assignedBy] the new assignedBy field of the card
  2440. * @param {string} [receivedAt] the new receivedAt field of the card
  2441. * @param {string} [assignBy] the new assignBy field of the card
  2442. * @param {string} [startAt] the new startAt field of the card
  2443. * @param {string} [dueAt] the new dueAt field of the card
  2444. * @param {string} [endAt] the new endAt field of the card
  2445. * @param {string} [spentTime] the new spentTime field of the card
  2446. * @param {boolean} [isOverTime] the new isOverTime field of the card
  2447. * @param {string} [customFields] the new customFields value of the card
  2448. * @param {string} [color] the new color of the card
  2449. * @param {Object} [vote] the vote object
  2450. * @param {string} vote.question the vote question
  2451. * @param {boolean} vote.public show who voted what
  2452. * @param {boolean} vote.allowNonBoardMembers allow all logged in users to vote?
  2453. * @return_type {_id: string}
  2454. */
  2455. JsonRoutes.add(
  2456. 'PUT',
  2457. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2458. function(req, res) {
  2459. Authentication.checkUserId(req.userId);
  2460. const paramBoardId = req.params.boardId;
  2461. const paramCardId = req.params.cardId;
  2462. const paramListId = req.params.listId;
  2463. if (req.body.hasOwnProperty('title')) {
  2464. const newTitle = req.body.title;
  2465. Cards.direct.update(
  2466. {
  2467. _id: paramCardId,
  2468. listId: paramListId,
  2469. boardId: paramBoardId,
  2470. archived: false,
  2471. },
  2472. {
  2473. $set: {
  2474. title: newTitle,
  2475. },
  2476. },
  2477. );
  2478. }
  2479. if (req.body.hasOwnProperty('parentId')) {
  2480. const newParentId = req.body.parentId;
  2481. Cards.direct.update(
  2482. {
  2483. _id: paramCardId,
  2484. listId: paramListId,
  2485. boardId: paramBoardId,
  2486. archived: false,
  2487. },
  2488. {
  2489. $set: {
  2490. parentId: newParentId,
  2491. },
  2492. },
  2493. );
  2494. }
  2495. if (req.body.hasOwnProperty('description')) {
  2496. const newDescription = req.body.description;
  2497. Cards.direct.update(
  2498. {
  2499. _id: paramCardId,
  2500. listId: paramListId,
  2501. boardId: paramBoardId,
  2502. archived: false,
  2503. },
  2504. {
  2505. $set: {
  2506. description: newDescription,
  2507. },
  2508. },
  2509. );
  2510. }
  2511. if (req.body.hasOwnProperty('color')) {
  2512. const newColor = req.body.color;
  2513. Cards.direct.update(
  2514. {
  2515. _id: paramCardId,
  2516. listId: paramListId,
  2517. boardId: paramBoardId,
  2518. archived: false,
  2519. },
  2520. { $set: { color: newColor } },
  2521. );
  2522. }
  2523. if (req.body.hasOwnProperty('vote')) {
  2524. const newVote = req.body.vote;
  2525. newVote.positive = [];
  2526. newVote.negative = [];
  2527. if (!newVote.hasOwnProperty('public')) newVote.public = false;
  2528. if (!newVote.hasOwnProperty('allowNonBoardMembers'))
  2529. newVote.allowNonBoardMembers = false;
  2530. Cards.direct.update(
  2531. {
  2532. _id: paramCardId,
  2533. listId: paramListId,
  2534. boardId: paramBoardId,
  2535. archived: false,
  2536. },
  2537. { $set: { vote: newVote } },
  2538. );
  2539. }
  2540. if (req.body.hasOwnProperty('labelIds')) {
  2541. let newlabelIds = req.body.labelIds;
  2542. if (_.isString(newlabelIds)) {
  2543. if (newlabelIds === '') {
  2544. newlabelIds = null;
  2545. } else {
  2546. newlabelIds = [newlabelIds];
  2547. }
  2548. }
  2549. Cards.direct.update(
  2550. {
  2551. _id: paramCardId,
  2552. listId: paramListId,
  2553. boardId: paramBoardId,
  2554. archived: false,
  2555. },
  2556. {
  2557. $set: {
  2558. labelIds: newlabelIds,
  2559. },
  2560. },
  2561. );
  2562. }
  2563. if (req.body.hasOwnProperty('requestedBy')) {
  2564. const newrequestedBy = req.body.requestedBy;
  2565. Cards.direct.update(
  2566. {
  2567. _id: paramCardId,
  2568. listId: paramListId,
  2569. boardId: paramBoardId,
  2570. archived: false,
  2571. },
  2572. { $set: { requestedBy: newrequestedBy } },
  2573. );
  2574. }
  2575. if (req.body.hasOwnProperty('assignedBy')) {
  2576. const newassignedBy = req.body.assignedBy;
  2577. Cards.direct.update(
  2578. {
  2579. _id: paramCardId,
  2580. listId: paramListId,
  2581. boardId: paramBoardId,
  2582. archived: false,
  2583. },
  2584. { $set: { assignedBy: newassignedBy } },
  2585. );
  2586. }
  2587. if (req.body.hasOwnProperty('receivedAt')) {
  2588. const newreceivedAt = req.body.receivedAt;
  2589. Cards.direct.update(
  2590. {
  2591. _id: paramCardId,
  2592. listId: paramListId,
  2593. boardId: paramBoardId,
  2594. archived: false,
  2595. },
  2596. { $set: { receivedAt: newreceivedAt } },
  2597. );
  2598. }
  2599. if (req.body.hasOwnProperty('startAt')) {
  2600. const newstartAt = req.body.startAt;
  2601. Cards.direct.update(
  2602. {
  2603. _id: paramCardId,
  2604. listId: paramListId,
  2605. boardId: paramBoardId,
  2606. archived: false,
  2607. },
  2608. { $set: { startAt: newstartAt } },
  2609. );
  2610. }
  2611. if (req.body.hasOwnProperty('dueAt')) {
  2612. const newdueAt = req.body.dueAt;
  2613. Cards.direct.update(
  2614. {
  2615. _id: paramCardId,
  2616. listId: paramListId,
  2617. boardId: paramBoardId,
  2618. archived: false,
  2619. },
  2620. { $set: { dueAt: newdueAt } },
  2621. );
  2622. }
  2623. if (req.body.hasOwnProperty('endAt')) {
  2624. const newendAt = req.body.endAt;
  2625. Cards.direct.update(
  2626. {
  2627. _id: paramCardId,
  2628. listId: paramListId,
  2629. boardId: paramBoardId,
  2630. archived: false,
  2631. },
  2632. { $set: { endAt: newendAt } },
  2633. );
  2634. }
  2635. if (req.body.hasOwnProperty('spentTime')) {
  2636. const newspentTime = req.body.spentTime;
  2637. Cards.direct.update(
  2638. {
  2639. _id: paramCardId,
  2640. listId: paramListId,
  2641. boardId: paramBoardId,
  2642. archived: false,
  2643. },
  2644. { $set: { spentTime: newspentTime } },
  2645. );
  2646. }
  2647. if (req.body.hasOwnProperty('isOverTime')) {
  2648. const newisOverTime = req.body.isOverTime;
  2649. Cards.direct.update(
  2650. {
  2651. _id: paramCardId,
  2652. listId: paramListId,
  2653. boardId: paramBoardId,
  2654. archived: false,
  2655. },
  2656. { $set: { isOverTime: newisOverTime } },
  2657. );
  2658. }
  2659. if (req.body.hasOwnProperty('customFields')) {
  2660. const newcustomFields = req.body.customFields;
  2661. Cards.direct.update(
  2662. {
  2663. _id: paramCardId,
  2664. listId: paramListId,
  2665. boardId: paramBoardId,
  2666. archived: false,
  2667. },
  2668. { $set: { customFields: newcustomFields } },
  2669. );
  2670. }
  2671. if (req.body.hasOwnProperty('members')) {
  2672. let newmembers = req.body.members;
  2673. if (_.isString(newmembers)) {
  2674. if (newmembers === '') {
  2675. newmembers = null;
  2676. } else {
  2677. newmembers = [newmembers];
  2678. }
  2679. }
  2680. Cards.direct.update(
  2681. {
  2682. _id: paramCardId,
  2683. listId: paramListId,
  2684. boardId: paramBoardId,
  2685. archived: false,
  2686. },
  2687. { $set: { members: newmembers } },
  2688. );
  2689. }
  2690. if (req.body.hasOwnProperty('assignees')) {
  2691. let newassignees = req.body.assignees;
  2692. if (_.isString(newassignees)) {
  2693. if (newassignees === '') {
  2694. newassignees = null;
  2695. } else {
  2696. newassignees = [newassignees];
  2697. }
  2698. }
  2699. Cards.direct.update(
  2700. {
  2701. _id: paramCardId,
  2702. listId: paramListId,
  2703. boardId: paramBoardId,
  2704. archived: false,
  2705. },
  2706. { $set: { assignees: newassignees } },
  2707. );
  2708. }
  2709. if (req.body.hasOwnProperty('swimlaneId')) {
  2710. const newParamSwimlaneId = req.body.swimlaneId;
  2711. Cards.direct.update(
  2712. {
  2713. _id: paramCardId,
  2714. listId: paramListId,
  2715. boardId: paramBoardId,
  2716. archived: false,
  2717. },
  2718. { $set: { swimlaneId: newParamSwimlaneId } },
  2719. );
  2720. }
  2721. if (req.body.hasOwnProperty('listId')) {
  2722. const newParamListId = req.body.listId;
  2723. Cards.direct.update(
  2724. {
  2725. _id: paramCardId,
  2726. listId: paramListId,
  2727. boardId: paramBoardId,
  2728. archived: false,
  2729. },
  2730. {
  2731. $set: {
  2732. listId: newParamListId,
  2733. },
  2734. },
  2735. );
  2736. const card = Cards.findOne({
  2737. _id: paramCardId,
  2738. });
  2739. cardMove(
  2740. req.body.authorId,
  2741. card,
  2742. {
  2743. fieldName: 'listId',
  2744. },
  2745. paramListId,
  2746. );
  2747. }
  2748. JsonRoutes.sendResult(res, {
  2749. code: 200,
  2750. data: {
  2751. _id: paramCardId,
  2752. },
  2753. });
  2754. },
  2755. );
  2756. /**
  2757. * @operation delete_card
  2758. * @summary Delete a card from a board
  2759. *
  2760. * @description This operation **deletes** a card, and therefore the card
  2761. * is not put in the recycle bin.
  2762. *
  2763. * @param {string} boardId the board ID of the card
  2764. * @param {string} list the list ID of the card
  2765. * @param {string} cardId the ID of the card
  2766. * @return_type {_id: string}
  2767. */
  2768. JsonRoutes.add(
  2769. 'DELETE',
  2770. '/api/boards/:boardId/lists/:listId/cards/:cardId',
  2771. function(req, res) {
  2772. Authentication.checkUserId(req.userId);
  2773. const paramBoardId = req.params.boardId;
  2774. const paramListId = req.params.listId;
  2775. const paramCardId = req.params.cardId;
  2776. const card = Cards.findOne({
  2777. _id: paramCardId,
  2778. });
  2779. Cards.direct.remove({
  2780. _id: paramCardId,
  2781. listId: paramListId,
  2782. boardId: paramBoardId,
  2783. });
  2784. cardRemover(req.body.authorId, card);
  2785. JsonRoutes.sendResult(res, {
  2786. code: 200,
  2787. data: {
  2788. _id: paramCardId,
  2789. },
  2790. });
  2791. },
  2792. );
  2793. /**
  2794. * @operation get_cards_by_custom_field
  2795. * @summary Get all Cards that matchs a value of a specific custom field
  2796. *
  2797. * @param {string} boardId the board ID
  2798. * @param {string} customFieldId the list ID
  2799. * @param {string} customFieldValue the value to look for
  2800. * @return_type [{_id: string,
  2801. * title: string,
  2802. * description: string,
  2803. * listId: string,
  2804. * swinlaneId: string}]
  2805. */
  2806. JsonRoutes.add(
  2807. 'GET',
  2808. '/api/boards/:boardId/cardsByCustomField/:customFieldId/:customFieldValue',
  2809. function(req, res) {
  2810. const paramBoardId = req.params.boardId;
  2811. const paramCustomFieldId = req.params.customFieldId;
  2812. const paramCustomFieldValue = req.params.customFieldValue;
  2813. Authentication.checkBoardAccess(req.userId, paramBoardId);
  2814. JsonRoutes.sendResult(res, {
  2815. code: 200,
  2816. data: Cards.find({
  2817. boardId: paramBoardId,
  2818. customFields: {
  2819. $elemMatch: {
  2820. _id: paramCustomFieldId,
  2821. value: paramCustomFieldValue,
  2822. },
  2823. },
  2824. archived: false,
  2825. }).fetch(),
  2826. });
  2827. },
  2828. );
  2829. }
  2830. export default Cards;