From b9485da34af80255cc9e4a7f5eae1590a09cfd45 Mon Sep 17 00:00:00 2001 From: Arnaud AMBROSELLI Date: Mon, 29 Jul 2024 15:18:48 +0200 Subject: [PATCH] fix: test notif api --- api/package.json | 1 + api/src/controllers/test.js | 36 ++++++++++++++++++++++++++ api/src/scripts/test-notification.js | 38 ++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 api/src/scripts/test-notification.js diff --git a/api/package.json b/api/package.json index e0ebf1d7a..74177c43e 100644 --- a/api/package.json +++ b/api/package.json @@ -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", diff --git a/api/src/controllers/test.js b/api/src/controllers/test.js index f4e3068ae..338200f59 100644 --- a/api/src/controllers/test.js +++ b/api/src/controllers/test.js @@ -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( "/", @@ -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) => { diff --git a/api/src/scripts/test-notification.js b/api/src/scripts/test-notification.js new file mode 100644 index 000000000..39edde247 --- /dev/null +++ b/api/src/scripts/test-notification.js @@ -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)); +})();