Skip to content

Commit

Permalink
fix: date format for drinks
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanRos committed Sep 4, 2024
1 parent 3f75c77 commit 973d1f1
Showing 1 changed file with 36 additions and 50 deletions.
86 changes: 36 additions & 50 deletions api/src/controllers/consommation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,63 @@ router.post(
router.post(
"/sync",
catchErrors(async (req, res) => {
const matomoId = req.body?.matomoId;
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });
try {
const matomoId = req.body?.matomoId;
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });

const { drinks, drinksCatalog } = req.body;
const { drinks, drinksCatalog } = req.body;

if (!drinks.length) {
await syncDrinkBadgesWithConsos(matomoId);
if (!drinks.length) {
await syncDrinkBadgesWithConsos(matomoId);

await syncAllGoalsWithConsos(matomoId, true);
await syncAllGoalsWithConsos(matomoId, true);

// TODO: uncomment this line when the notifications for goals sync is sent
// await syncBadgesWithGoals(matomoId, true);
return res.status(200).json({ ok: true });
}
// TODO: uncomment this line when the notifications for goals sync is sent
// await syncBadgesWithGoals(matomoId, true);
return res.status(200).json({ ok: true });
}

const user = await prisma.user.upsert({
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
},
update: {},
});
const user = await prisma.user.upsert({
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
},
update: {},
});

for (const drink of drinks) {
if (drink.drinkKey === "no-conso") {
for (const drink of drinks) {
const drinkToSave = {
id: drink.id,
drinkKey: drink.drinkKey,
name: drink.drinkKey,
name: drink.drinkKey === "no-conso" ? drink.drinkKey : drinksCatalog.find((dc) => dc.drinkKey === drink.drinkKey)?.displayDrinkModal,
quantity: Number(drink.quantity),
date: dayjs(drink.timestamp).format(),
date: dayjs(drink.timestamp).toISOString(),
userId: user.id,
doses: 0,
kcal: 0,
price: 0,
volume: "",
doses: drink.drinkKey === "no-conso" ? 0 : Number(drinksCatalog.find((dc) => dc.drinkKey === drink.drinkKey)?.doses),
kcal: drink.drinkKey === "no-conso" ? 0 : Number(drinksCatalog.find((dc) => dc.drinkKey === drink.drinkKey)?.kcal),
price: drink.drinkKey === "no-conso" ? 0 : Number(drinksCatalog.find((dc) => dc.drinkKey === drink.drinkKey)?.price),
volume: drink.drinkKey === "no-conso" ? "" : drinksCatalog.find((dc) => dc.drinkKey === drink.drinkKey)?.volume,
};
return await prisma.consommation.upsert({

await prisma.consommation.upsert({
where: { id: drink.id },
update: drinkToSave,
create: drinkToSave,
});
}
const drinkFromCatalog = drinksCatalog.find((drinkCatalog) => drinkCatalog.drinkKey === drink.drinkKey);
const drinkToSave = {
id: drink.id,
drinkKey: drink.drinkKey,
name: drinkFromCatalog.displayDrinkModal,
quantity: drink.quantity,
date: dayjs(drink.timestamp).format(),
userId: user.id,
doses: Number(drinkFromCatalog.doses),
kcal: Number(drinkFromCatalog.kcal),
price: Number(drinkFromCatalog.price),
volume: drinkFromCatalog.volume,
};
await prisma.consommation.upsert({
where: { id: drink.id },
update: drinkToSave,
create: drinkToSave,
});
}

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

await syncAllGoalsWithConsos(matomoId, true);
await syncAllGoalsWithConsos(matomoId, true);

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

return res.status(200).send({ ok: true });
return res.status(200).send({ ok: true });
} catch (error) {
console.error("An error occurred during syncing:", error);
return res.status(500).json({ ok: false, error: "An internal server error occurred." });
}
})
);

Expand Down

0 comments on commit 973d1f1

Please sign in to comment.