Skip to content

Commit

Permalink
feat: add back
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanRos committed Sep 12, 2024
1 parent 426e004 commit bd0679c
Show file tree
Hide file tree
Showing 20 changed files with 484 additions and 547 deletions.
11 changes: 11 additions & 0 deletions api/prisma/migrations/20240906143112_no_matomoid/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- You are about to drop the column `matomo_id` on the `User` table. All the data in the column will be lost.
*/
-- DropIndex
DROP INDEX "User_matomo_id_key";

-- AlterTable
ALTER TABLE "User" DROP COLUMN "matomo_id";
1 change: 0 additions & 1 deletion api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ datasource db {

model User {
id String @id @default(uuid())
matomo_id String @unique
email String @unique
password String
push_notif_token String @default("")
Expand Down
137 changes: 19 additions & 118 deletions api/src/controllers/consommation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const prisma = require("../prisma");
const { getBadgeCatalog } = require("../utils/badges");
const { syncGoalsWithConsos, syncAllGoalsWithConsos } = require("../utils/goals");
const { checksConsecutiveDays, syncDrinkBadgesWithConsos } = require("../utils/drinks");
const { authenticateToken } = require("../middlewares/tokenAuth");

router.post(
"/init",
authenticateToken, // Add authentication middleware
catchErrors(async (req, res) => {
// kept for retrocompatilibity
return res.status(200).send({ ok: true });
Expand All @@ -17,32 +19,17 @@ router.post(

router.post(
"/sync",
authenticateToken, // Add authentication middleware
catchErrors(async (req, res) => {
const matomoId = req.body?.matomoId;
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });
console.log("syncing consos", matomoId);
const { drinks, drinksCatalog } = req.body;
const user = req.user;

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

await syncAllGoalsWithConsos(matomoId, true);

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

const user = await prisma.user.upsert({
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
},
update: {},
});

for (const drink of drinks) {
if (drink.drinkKey === "no-conso") {
const drinkToSave = {
Expand Down Expand Up @@ -83,29 +70,21 @@ router.post(
});
}

await syncDrinkBadgesWithConsos(matomoId);

await syncAllGoalsWithConsos(matomoId, true);

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

return res.status(200).send({ ok: true });
})
);

router.post(
"/",
authenticateToken,
catchErrors(async (req, res) => {
const matomoId = req.body?.matomoId;
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });

/* 1. save conso in DB */

let user = await prisma.user.findUnique({ where: { matomo_id: matomoId } });
let user = req.user;

const date = req.body.date;
const conso_id = req.body.id; // setup in frontend
const conso_id = req.body.id;
const conso = {
name: req.body.name,
drinkKey: req.body.drinkKey,
Expand All @@ -129,14 +108,9 @@ router.post(
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 syncGoalsWithConsos(matomoId, date);
await syncGoalsWithConsos(user.id, date);

const drinksBadgeToShow = await syncDrinkBadgesWithConsos(matomoId);

Expand All @@ -159,12 +133,10 @@ router.post(

router.post(
"/fix-missing-key",
authenticateToken,
catchErrors(async (req, res) => {
const matomoId = req.body?.matomoId;
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });
const drinkKey = req.body.drinkKey;
const conso_id = req.body.id;
// find user with matomoId

await prisma.consommation.update({
where: { id: conso_id },
Expand All @@ -178,10 +150,9 @@ router.post(

router.delete(
"/",
authenticateToken,
catchErrors(async (req, res) => {
const matomoId = req.body?.matomoId;
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });
const user = await prisma.user.findFirst({ where: { matomo_id: matomoId } });
const user = req.user;
const conso_id = req.body.id;
const consommation = await prisma.consommation.findFirst({
where: { id: conso_id },
Expand All @@ -196,85 +167,18 @@ router.delete(
})
);

const NPSInAppMessage = {
id: "@NPSDone",
title: "5 sec pour nous aider à améliorer l'application\u00A0?",
content: `Nous construisons l'application ensemble et __votre avis sera pris en compte dans les prochaines mises à jour.__ Merci d'avance\u00A0!`,
CTATitle: "Je donne mon avis sur Oz",
CTANavigation: ["NPS_SCREEN", { triggeredFrom: "5 seconds for NPS" }],
};

const checkNPSAvailability = async (user) => {
const npsDone = await prisma.appMilestone.findUnique({ where: { id: `${user.id}_@NPSDone` } });
if (npsDone) return null;
const npsAsked3 = await prisma.appMilestone.findUnique({ where: { id: `${user.id}_@NPSAsked3` } });
if (npsAsked3) return null;
const allConsos = await prisma.consommation.findMany({
where: {
userId: user.id,
},
orderBy: { date: "desc" },
});
const enoughConsecutiveDays = checksConsecutiveDays(4, allConsos);
if (!enoughConsecutiveDays) return null;

const npsAsked2 = await prisma.appMilestone.findUnique({ where: { id: `${user.id}_@NPSAsked2` } });

const now = dayjs();

if (npsAsked2) {
if (dayjs(npsAsked2.date).diff(now, "day") < 7) {
return null;
}
await prisma.appMilestone.create({
data: {
id: `${user.id}_@NPSAsked3`,
date: now.format("YYYY-MM-DD"),
userId: user.id,
},
});
return NPSInAppMessage;
}

const npsAsked1 = await prisma.appMilestone.findUnique({ where: { id: `${user.id}_@NPSAsked1` } });
if (npsAsked1) {
if (dayjs(npsAsked1.date).diff(now, "day") < 7) {
return null;
}
await prisma.appMilestone.create({
data: {
id: `${user.id}_@NPSAsked2`,
date: now.format("YYYY-MM-DD"),
userId: user.id,
},
});
return NPSInAppMessage;
}
await prisma.appMilestone.create({
data: {
id: `${user.id}_@NPSAsked1`,
date: now.format("YYYY-MM-DD"),
userId: user.id,
},
});
return NPSInAppMessage;
};

router.post(
"/update-own-conso",
authenticateToken,
catchErrors(async (req, res) => {
const matomoId = req.body?.matomoId;
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });
const user = req.user;
const oldDrinkKey = req.body?.oldDrinkKey;
const drinkKey = req.body?.drinkKey;
const doses = req.body?.doses;
const kcal = req.body?.kcal;
const price = req.body?.price;
const volume = req.body?.volume;

// find user with matomoId
let user = await prisma.user.findUnique({ where: { matomo_id: matomoId } });

await prisma.consommation.updateMany({
where: { userId: user.id, drinkKey: oldDrinkKey },
data: {
Expand All @@ -292,12 +196,9 @@ router.post(

router.get(
"/get-all-consos",
authenticateToken, // Add authentication middleware
catchErrors(async (req, res) => {
const { matomoId } = req.query;
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });

// find user with matomoId
let user = await prisma.user.findUnique({ where: { matomo_id: matomoId } });
const user = req.user;

const consos = await prisma.consommation.findMany({
where: { userId: user.id },
Expand Down
23 changes: 6 additions & 17 deletions api/src/controllers/defis.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,23 @@ const { catchErrors } = require("../middlewares/errors");
const router = express.Router();
const prisma = require("../prisma");
const { getBadgeCatalog, grabBadgeFromCatalog } = require("../utils/badges");
const { authenticateToken } = require("../middlewares/tokenAuth");

router.post(
"/init",
authenticateToken,
catchErrors(async (req, res) => {
return res.status(200).send({ ok: true });
})
);

router.post(
"/",
authenticateToken,
catchErrors(async (req, res) => {
const matomoId = req.body?.matomoId;
const user = req.user;
const completedDays = Number(req.body?.daysValidated);
const autoEvaluationDone = req.body?.autoEvaluationDone;
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });

const user = await prisma.user.upsert({
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
created_from: "Defis",
},
update: {},
});

const defis_badges = await prisma.badge.findMany({
where: { userId: user.id, category: "defis" },
Expand Down Expand Up @@ -102,11 +93,9 @@ router.post(

router.post(
"/display",
authenticateToken,
catchErrors(async (req, res) => {
const { matomoId } = req.body || {};
const user = await prisma.user.findUnique({
where: { matomo_id: matomoId },
});
const user = req.user;
const badge_defis_to_show = await prisma.badge.findFirst({
where: { userId: user.id, category: "defis", shown: false },
});
Expand Down
12 changes: 5 additions & 7 deletions api/src/controllers/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ const { catchErrors } = require("../middlewares/errors");
const router = express.Router();
const { capture } = require("../third-parties/sentry");
const prisma = require("../prisma");
const { authenticateToken } = require("../middlewares/tokenAuth");

router.post(
"/",
authenticateToken,
catchErrors(async (req, res) => {
let { matomoId, to, replyTo, replyToName, subject, text, html, attachments } = req.body || {};
if (!matomoId && req.headers.appversion > 225) {
return res.status(200).json({ ok: true });
}
let { to, replyTo, replyToName, subject, text, html, attachments } = req.body || {};
const user = req.user;

if (!subject || (!text && !html)) return res.status(400).json({ ok: false, error: "wrong parameters" });

if (subject === "Context suggestion") {
Expand All @@ -23,15 +24,12 @@ router.post(
.split("\n")
.filter(Boolean)
.map((line) => line.split(":"));
const matomoId = textAndValues[0][1].trim();
const requestContext = textAndValues[4][1].trim().toLowerCase();
const requestCategory = textAndValues[4][0].split(" ").at(-1).replace(":", "").trim();
const user = await prisma.user.findUnique({ where: { matomo_id: matomoId } });
if (!user) return res.status(400).json({ ok: false, error: "no matomo id" });
await prisma.drinksContextRequest.create({
data: {
userId: user.id,
matomo_id: matomoId,
context: requestContext,
category: requestCategory,
},
Expand Down
Loading

0 comments on commit bd0679c

Please sign in to comment.