Skip to content

Commit

Permalink
fix: sync goals on calendar load
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed May 7, 2024
1 parent 539427f commit 53c1732
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 29 deletions.
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"sync-goals-badges": "cross-env NODE_ENV=development && node ./src/scripts/sync-goal-badges.js ",
"sync-goals-drinks": "cross-env NODE_ENV=development && node ./src/scripts/sync-drinks-badges.js ",
"sync-goals": "cross-env NODE_ENV=development && node ./src/scripts/sync-goals.js ",
"sync-goal": "cross-env NODE_ENV=development && node ./src/scripts/sync-goal.js ",
"dev-cron": "nodemon ./src/cronjobs.js ",
"create-migration": "cross-env NODE_ENV=development npx prisma migrate dev --name",
"start": "NODE_ENV=production npx prisma migrate deploy && NODE_ENV=production node ./src/server.js",
Expand Down
13 changes: 7 additions & 6 deletions api/src/controllers/consommation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const { catchErrors } = require("../middlewares/errors");
const router = express.Router();
const prisma = require("../prisma");
const { getBadgeCatalog } = require("../utils/badges");
const { syncBadgesWithGoals } = require("../utils/goals");
const { checksConsecutiveDays, syncBadgesWithConsos } = require("../utils/drinks");
const { syncGoalsWithConsos } = require("../utils/goals");
const { checksConsecutiveDays, syncDrinkBadgesWithConsos } = require("../utils/drinks");

router.post(
"/init",
Expand Down Expand Up @@ -68,7 +68,7 @@ router.post(
skipDuplicates: true,
});

await syncBadgesWithConsos(matomoId);
await syncDrinkBadgesWithConsos(matomoId);

return res.status(200).send({ ok: true });
})
Expand Down Expand Up @@ -104,20 +104,21 @@ router.post(
await prisma.consommation.delete({ where: { id: conso_id } });
}
} else {
await prisma.consommation.upsert({
const consoDB = await prisma.consommation.upsert({
where: { id: conso_id },
update: conso,
create: { ...conso, id: conso_id },
});
console.log("conso upserted", consoDB._id);
}

/* 2. SIDE EFFECTS */

// note: the `date` can be ANY date, not just today,
// because the user can update a conso from any date
await syncBadgesWithGoals(matomoId, date);
await syncGoalsWithConsos(matomoId, date);

const drinksBadgeToShow = await syncBadgesWithConsos(matomoId);
const drinksBadgeToShow = await syncDrinkBadgesWithConsos(matomoId);

if (drinksBadgeToShow) {
const allBadges = await prisma.badge.findMany({ where: { userId: user.id } });
Expand Down
34 changes: 22 additions & 12 deletions api/src/controllers/goal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const dayjs = require("dayjs");
const prisma = require("../prisma");
const { grabBadgeFromCatalog, getBadgeCatalog } = require("../utils/badges");
const { GoalStatus } = require("@prisma/client");
const { syncAllGoalsWithConsos, syncBadgesWithGoals } = require("../utils/goals");
const router = express.Router();

router.post(
Expand Down Expand Up @@ -117,24 +118,33 @@ router.get(
});

if (user.goal_isSetup) {
let currentGoal = {
id: `${user.id}_${dayjs().startOf("week").format("YYYY-MM-DD")}`,
userId: user.id,
date: dayjs().startOf("week").format("YYYY-MM-DD"),
daysWithGoalNoDrink: user.goal_daysWithGoalNoDrink,
dosesByDrinkingDay: user.goal_dosesByDrinkingDay,
dosesPerWeek: user.goal_dosesPerWeek,
status: GoalStatus.InProgress,
};
// if pas de goal pour cette semaine, on le crée
const thisWeekGoal = await prisma.goal.findFirst({
where: { userId: user.id, date: dayjs().startOf("week").format("YYYY-MM-DD") },
let thisWeekGoal = await prisma.goal.findFirst({
where: { id: currentGoal.id },
});
if (!thisWeekGoal) {
await prisma.goal.create({
data: {
id: `${user.id}_${dayjs().startOf("week").format("YYYY-MM-DD")}`,
userId: user.id,
date: dayjs().startOf("week").format("YYYY-MM-DD"),
daysWithGoalNoDrink: user.goal_daysWithGoalNoDrink,
dosesByDrinkingDay: user.goal_dosesByDrinkingDay,
dosesPerWeek: user.goal_dosesPerWeek,
status: GoalStatus.InProgress,
},
thisWeekGoal = await prisma.goal.upsert({
where: { id: currentGoal.id },
create: currentGoal,
data: currentGoal,
});
}
}

await syncAllGoalsWithConsos(matomoId, true);

// TODO: uncomment this line when the notifications for goals sync is sent
// await syncBadgesWithGoals(matomoId, true);

const goals = await prisma.goal.findMany({
where: { userId: user.id },
orderBy: { date: "desc" },
Expand Down
7 changes: 7 additions & 0 deletions api/src/scripts/sync-goal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require("dotenv").config({ path: "./.env" });

const dayjs = require("dayjs");
const prisma = require("../prisma");
const { syncAllGoalsWithConsos } = require("../utils/goals");

syncAllGoalsWithConsos("any matomo id", true, true);
14 changes: 7 additions & 7 deletions api/src/scripts/sync-goals.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async function syncBadges(fixGoals = false, debug = false) {
orderBy: { date: "desc" },
},
},
take: 1000,
skip: 1100,
take: 100,
// skip: 1100,
});
console.log("USERS", users.length);
let usersWithFuckedUpGoals = {};
Expand Down Expand Up @@ -65,7 +65,7 @@ async function syncBadges(fixGoals = false, debug = false) {
}
usersWithMoreOrLessSuccessThanExpected[user.matomo_id] = 0;
for (const [startOfWeek, weekConsos] of Object.entries(startOfWeeksForDrinks)) {
if (debug) console.log("CHECKING GOAL", user.id, startOfWeek, oldestGoalDate);
// if (debug) console.log("CHECKING GOAL", user.id, startOfWeek, oldestGoalDate);
if (oldestGoalDate && startOfWeek < oldestGoalDate) {
continue;
}
Expand Down Expand Up @@ -102,15 +102,15 @@ async function syncBadges(fixGoals = false, debug = false) {
const goalCheck = await checkCurrentWeekGoal(goal, weekConsos);

if (goal.status === "InProgress" && goalCheck.status === null && !goalCheck.weekIsFilledWithConsos) {
if (debug) console.log("GOAL IN PROGRESS", user.id, startOfWeek);
// if (debug) console.log("GOAL IN PROGRESS", user.id, startOfWeek);
continue;
}
if (goal.status === "Failure" && goalCheck.status === "Failure") {
if (debug) console.log("GOAL FAILURE", user.id, startOfWeek);
// if (debug) console.log("GOAL FAILURE", user.id, startOfWeek);
continue;
}
if (goal.status === "Success" && goalCheck.status === "Success") {
if (debug) console.log("GOAL SUCCESS", user.id, startOfWeek);
// if (debug) console.log("GOAL SUCCESS", user.id, startOfWeek);
continue;
}
usersWithFuckedUpGoals[user.matomo_id] = true;
Expand Down Expand Up @@ -241,4 +241,4 @@ async function syncBadges(fixGoals = false, debug = false) {
});
}

syncBadges(true, true);
syncBadges(false, true);
4 changes: 2 additions & 2 deletions api/src/utils/drinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dayjs.extend(utc);
dayjs.locale("fr");
dayjs.extend(weekday);

async function syncBadgesWithConsos(matomoId) {
async function syncDrinkBadgesWithConsos(matomoId) {
if (!matomoId) return null;
const user = await prisma.user.findUnique({ where: { matomo_id: matomoId } });
if (!user) return null;
Expand Down Expand Up @@ -234,7 +234,7 @@ function countMaxConsecutiveDays(allConsos) {
}

module.exports = {
syncBadgesWithConsos,
syncDrinkBadgesWithConsos,
getBadgeCorrespondingToConsecutiveDays,
getStarsCorrespondingToConsecutiveDays,
countConsecutiveDays,
Expand Down
Loading

0 comments on commit 53c1732

Please sign in to comment.