Skip to content

Commit

Permalink
fix: test notif api
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Jul 29, 2024
1 parent 46b1e96 commit b9485da
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"sync-drinks-badges": "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 ",
"test-notification": "cross-env NODE_ENV=development node ./src/scripts/test-notification.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
36 changes: 36 additions & 0 deletions api/src/controllers/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const prisma = require("../prisma");
const dayjs = require("dayjs");
const { superUser90DaysInAppModal, superUser30DaysInAppModal, cravingInAppModal } = require("../utils/inAppModals");
const { scheduleNotificationPlan } = require("../utils/notifications");
const { sendPushNotification } = require("../services/push-notifications");

router.get(
"/",
Expand Down Expand Up @@ -89,6 +90,41 @@ router.post(
}
})
);

router.get(
"/test-notif",
catchErrors(async (req, res) => {
const { userId } = req.query || {};
if (!userId) {
return res.status(400).json({ ok: false, error: "no user id" });
}

const user = await prisma.user.findFirst({
where: {
id: userId,
},
});

if (!user) {
console.error(`User with id ${userId} not found`);
process.exit(1);
}

const results = await sendPushNotification({
userId,
matomoId: user.matomo_id,
pushNotifToken: user.push_notif_token,
title: "La notif elle est là",
body: "This is a test notification",
data: {
type: "test",
},
});

return res.status(200).json({ ok: true, results });
})
);

router.post(
"/launch-notification-plan",
catchErrors(async (req, res) => {
Expand Down
38 changes: 38 additions & 0 deletions api/src/scripts/test-notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require("dotenv").config({ path: "./.env" });
const prisma = require("../prisma");
const { sendPushNotification } = require("../services/push-notifications");

const userId = process.argv[2];

if (!userId) {
console.error("Please provide a userId as an argument");
process.exit(1);
}

(async () => {
const user = await prisma.user.findFirst({
where: {
id: userId,
},
});

if (!user) {
console.error(`User with id ${userId} not found`);
process.exit(1);
}

console.log({ user });

const results = await sendPushNotification({
userId,
matomoId: user.matomo_id,
pushNotifToken: user.push_notif_token,
title: "La notif elle est là",
body: "This is a test notification",
data: {
type: "test",
},
});

console.log(JSON.stringify(results, null, 2));
})();

0 comments on commit b9485da

Please sign in to comment.