invest card logic

Download Raw Clone


  1. const isActiveMoreThanThreshole = async (user, db) => {
  2. const lastThreeDaysStats = await db('user_stats')
  3. .where({
  4. user_id: user.id,
  5. })
  6. .orderByRaw('logged_date DESC NULLS LAST')
  7. .limit(3);
  8. if (
  9. R.isNil(lastThreeDaysStats) ||
  10. R.isEmpty(lastThreeDaysStats) ||
  11. lastThreeDaysStats.length < 3
  12. ) {
  13. return false;
  14. }
  15. const lastThreeDaysInvested = R.pluck('invested')(lastThreeDaysStats);
  16. if (R.includes(0, lastThreeDaysInvested)) {
  17. return false;
  18. }
  19. if (
  20. Interval.fromDateTimes(
  21. DateTime.fromJSDate(user.investmentTimeUpdatedDate, { zone: 'utc' }),
  22. DateTime.utc()
  23. ).length('days') <= 2
  24. ) {
  25. return false;
  26. }
  27. const userInvestmentTime = user.invest || 18;
  28. const maxInvested = R.reduce(R.max, -Infinity, lastThreeDaysInvested);
  29. const minInvested = R.reduce(R.min, Infinity, lastThreeDaysInvested);
  30. if (userInvestmentTime > maxInvested) {
  31. return {
  32. shouldIncreaseInvestmentTime: false,
  33. };
  34. }
  35. if (userInvestmentTime < minInvested) {
  36. return {
  37. shouldIncreaseInvestmentTime: true,
  38. };
  39. }
  40. return false;
  41. };

Raw paste data: