Skip to content

Commit

Permalink
fix: unsaved back changes
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanRos committed Sep 12, 2024
1 parent bd0679c commit 16101af
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 197 deletions.
32 changes: 7 additions & 25 deletions api/src/controllers/appMilestone.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@ const router = express.Router();
const prisma = require("../prisma");
const dayjs = require("dayjs");
const { superUser30DaysInAppModal, superUser90DaysInAppModal } = require("../utils/inAppModals");
const { authenticateToken } = require("../middlewares/tokenAuth");

router.post(
"/",
authenticateToken,
catchErrors(async (req, res) => {
const { matomoId, appMilestone } = req.body || {};
const { appMilestone } = req.body || {};

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,
created_from: "AppMilestonePost",
email: "[email protected]",
password: "password12@Abc",
},
update: {},
});
const user = req.user;

await prisma.appMilestone.upsert({
where: { id: `${user.id}_${appMilestone}` },
Expand All @@ -42,19 +33,10 @@ router.post(

router.post(
"/init",
authenticateToken,
catchErrors(async (req, res) => {
const { matomoId, isRegistered } = req.body || {};
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,
created_from: "AppMilestoneInit",
email: "[email protected]",
password: "password12@Abc",
},
update: {},
});
const { isRegistered } = req.body || {};
const user = req.user;

const allowNotification = await prisma.appMilestone.findUnique({
where: { id: `${user.id}_@AllowNotification` },
Expand Down
17 changes: 4 additions & 13 deletions api/src/controllers/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@ 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(
"/",
authenticateToken,
catchErrors(async (req, res) => {
const { matomoId, articleTitle } = req.body || {};
const { articleTitle } = req.body || {};

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,
created_from: "Articles",
email: "[email protected]",
password: "password12@Abc",
},
update: {},
});
const user = req.user;

await prisma.article.upsert({
where: { id: `${user.id}_${articleTitle.replaceAll(" ", "")}` },
Expand Down
27 changes: 7 additions & 20 deletions api/src/controllers/badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { catchErrors } = require("../middlewares/errors");
const router = express.Router();
const prisma = require("../prisma");
const { getBadgeCatalog, grabBadgeFromCatalog, missedGoal } = require("../utils/badges");
const { authenticateToken } = require("../middlewares/tokenAuth");

router.get(
"/test",
Expand All @@ -16,21 +17,17 @@ router.get(

router.get(
"/:matomoId",
authenticateToken,

catchErrors(async (req, res) => {
const { matomoId } = req.params;
// Don't show badges that are not available in the app version

if (!matomoId) return res.status(200).send({ ok: true, data: [] });

const user = req.user;

// find badges of matomoId
const user = await prisma.user.upsert({
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
created_from: "GetBadges",
},
update: {},
});

const badges = await prisma.badge.findMany({
where: {
Expand All @@ -57,20 +54,10 @@ router.get(

router.post(
"/shares",
authenticateToken,
catchErrors(async (req, res) => {
const { matomoId } = req.body || {};
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: "GetBadges",
},
const user = req.user;

update: {},
});
const share_badges = await prisma.badge.findMany({
where: { userId: user.id, category: "share" },
});
Expand Down
2 changes: 1 addition & 1 deletion api/src/controllers/consommation.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ router.post(
});
}

await syncGoalsWithConsos(user.id, date);
await syncGoalsWithConsos(user, date);

const drinksBadgeToShow = await syncDrinkBadgesWithConsos(matomoId);

Expand Down
31 changes: 9 additions & 22 deletions api/src/controllers/drinksContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ const express = require("express");
const { catchErrors } = require("../middlewares/errors");
const router = express.Router();
const prisma = require("../prisma");
const { authenticateToken } = require("../middlewares/tokenAuth");

router.post(
"/request",
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 context = req.body.context;
const category = req.body.category;
// find user with matomoId
let user = await prisma.user.findUnique({ where: { matomo_id: matomoId } });

if (!user) return res.status(400).json({ ok: false, error: "no user" });
await prisma.drinksContextRequest.create({
data: {
userId: user.id,
matomo_id: matomoId,
context: context,
category: category?.toLowerCase(),
},
Expand All @@ -29,16 +28,14 @@ router.post(

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" });
const user = req.user;
const id = req.body.id;
const context = req.body.context;
const date = req.body.date;
const emotion = req.body.emotion;

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

await prisma.drinksContext.upsert({
where: { id: id },
update: {
Expand All @@ -61,23 +58,13 @@ router.post(

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

const { drinksContexts } = req.body;

if (!drinksContexts.length) return res.status(200).json({ ok: true });

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

const drinksContextsToSave = drinksContexts.map((drinksContext) => {
return {
Expand All @@ -89,7 +76,7 @@ router.post(
};
});

await prisma.DrinksContext.createMany({
await prisma.drinksContext.createMany({
data: drinksContextsToSave,
skipDuplicates: true,
});
Expand Down
35 changes: 9 additions & 26 deletions api/src/controllers/goal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,23 @@ const prisma = require("../prisma");
const { grabBadgeFromCatalog, getBadgeCatalog } = require("../utils/badges");
const { GoalStatus } = require("@prisma/client");
const router = express.Router();
const { authenticateToken } = require("../middlewares/tokenAuth");

router.post(
["/", "/sync"],
authenticateToken,
catchErrors(async (req, res) => {
const { matomoId, daysWithGoalNoDrink, dosesByDrinkingDay, dosesPerWeek, noDisplayBadge, forceDate } = req.body || {};
if (!matomoId) return res.status(400).json({ ok: false, error: "no matomo id" });
const { daysWithGoalNoDrink, dosesByDrinkingDay, dosesPerWeek, noDisplayBadge, forceDate } = req.body || {};
const user = req.user;

/* 1. update user settings */
/* 2. update current goal if any */
/* 3. send badge if not yet sent */

/* 1. update user settings */
let user = await prisma.user.upsert({
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
created_from: "Goal",
goal_isSetup: true,
goal_daysWithGoalNoDrink: daysWithGoalNoDrink,
goal_dosesByDrinkingDay: dosesByDrinkingDay,
goal_dosesPerWeek: dosesPerWeek,
},
update: {
await prisma.user.update({
where: { id: user.id },
data: {
goal_isSetup: true,
goal_daysWithGoalNoDrink: daysWithGoalNoDrink,
goal_dosesByDrinkingDay: dosesByDrinkingDay,
Expand Down Expand Up @@ -107,18 +99,9 @@ router.post(

router.get(
"/list",
authenticateToken,
catchErrors(async (req, res) => {
const { matomoId } = req.query;
const user = await prisma.user.upsert({
where: { matomo_id: matomoId },
create: {
matomo_id: matomoId,
email: "[email protected]",
password: "password12@Abc",
created_from: "GetGoal",
},
update: {},
});
const user = req.query;

if (user.goal_isSetup) {
let currentGoal = {
Expand Down
5 changes: 2 additions & 3 deletions api/src/utils/drinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ dayjs.extend(utc);
dayjs.locale("fr");
dayjs.extend(weekday);

async function syncDrinkBadgesWithConsos(matomoId) {
if (!matomoId) return null;
const user = await prisma.user.findUnique({ where: { matomo_id: matomoId } });
async function syncDrinkBadgesWithConsos(userId) {
const user = await prisma.user.findUnique({ where: { id: userId } });
if (!user) return null;

const latestDrinksBadge = await prisma.badge.findFirst({
Expand Down
12 changes: 5 additions & 7 deletions api/src/utils/goals.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ dayjs.extend(isBetween);
dayjs.locale("fr");
dayjs.extend(weekday);

const syncGoalsWithConsos = async (matomoId, date) => {
if (!matomoId) return null;
const user = await prisma.user.findUnique({ where: { matomo_id: matomoId } });
const syncGoalsWithConsos = async (user, date) => {
if (!user) return null;
if (!user.goal_isSetup) return null;

Expand Down Expand Up @@ -182,10 +180,10 @@ function getStarsCorrespondingToGoalsSuccess(goalsSuccessCount) {
return 1;
}

async function syncAllGoalsWithConsos(matomoId, fixGoals = false, debug = false) {
async function syncAllGoalsWithConsos(userId, fixGoals = false, debug = false) {
const user = await prisma.user.findUnique({
where: {
matomo_id: matomoId,
id: userId,
},
include: {
consommations: {
Expand Down Expand Up @@ -355,10 +353,10 @@ async function syncAllGoalsWithConsos(matomoId, fixGoals = false, debug = false)
}
}

async function syncBadgesWithGoals(matomoId, fixBadges = false) {
async function syncBadgesWithGoals(userId, fixBadges = false) {
const user = await prisma.user.findUnique({
where: {
matomo_id: matomoId,
id: userId,
},
include: {
badges: {
Expand Down
28 changes: 1 addition & 27 deletions expo/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "react-native-get-random-values";
import React, { useEffect, useState } from "react";
import React, { useEffect} from "react";
import * as Sentry from "@sentry/react-native";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { RecoilRoot } from "recoil";
Expand All @@ -16,17 +16,6 @@ import "./src/styles/theme";

import { getBundleId } from "react-native-device-info";
import { initMatomo } from "./src/services/logEventsWithMatomo";
import {
cleanConsosAndCatalog,
hasCleanConsoAndCatalog,
hasMigrateFromDailyGoalToWeekly,
hasMigrateMissingDrinkKey,
hasSentPreviousDrinksToDB,
migrateFromDailyGoalToWeekly,
migrateMissingDrinkKey,
sendPreviousDrinksToDB,
} from "./src/migrations";
import { reconciliateDrinksToDB, reconciliateGoalToDB } from "./src/reconciliations";

dayjs.locale("fr");
dayjs.extend(isSameOrAfter);
Expand All @@ -44,25 +33,10 @@ Sentry.init({
});

const App = () => {
// sync everytime we open the app
const [reconciliatedDrinksToDB, setReconciliatedDrinksToDB] = useState(false);
const [reconciliatedGoalsToDB, setReconciliatedGoalsToDB] = useState(false);

// migrate only once if not yet done
// TODO: clean migrations when it's time
const [_hasSentPreviousDrinksToDB, setHasSentPreviousDrinksToDB] =
useState(hasSentPreviousDrinksToDB);
const [_hasCleanConsoAndCatalog, setHasCleanConsoAndCatalog] = useState(hasCleanConsoAndCatalog);
const [_hasMigrateMissingDrinkKey, sethasMigrateMissingDrinkKey] =
useState(hasMigrateMissingDrinkKey);
const [_hasMigrateFromDailyGoalToWeekly, sethasMigrateFromDailyGoalToWeekly] = useState(
hasMigrateFromDailyGoalToWeekly
);

useEffect(() => {
initMatomo()

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);


Expand Down
Loading

0 comments on commit 16101af

Please sign in to comment.