From eb9b59ba8586c4e9f62a45441df66b7b1df1fe23 Mon Sep 17 00:00:00 2001 From: Arnaud Ambroselli <31724752+arnaudambro@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:33:59 +0200 Subject: [PATCH] fix: in app rating asked from backend (#516) --- .../migration.sql | 2 ++ api-node/prisma/schema.prisma | 6 +++-- api-node/src/controllers/event.ts | 3 +-- api-node/src/utils/user.ts | 25 +++++++++++++------ 4 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 api-node/prisma/migrations/20240408183137_notification_index_by_user_id/migration.sql diff --git a/api-node/prisma/migrations/20240408183137_notification_index_by_user_id/migration.sql b/api-node/prisma/migrations/20240408183137_notification_index_by_user_id/migration.sql new file mode 100644 index 00000000..9a05cf0d --- /dev/null +++ b/api-node/prisma/migrations/20240408183137_notification_index_by_user_id/migration.sql @@ -0,0 +1,2 @@ +-- CreateIndex +CREATE INDEX "notification_user_id" ON "Notification"("user_id"); diff --git a/api-node/prisma/schema.prisma b/api-node/prisma/schema.prisma index 2b00f466..1538f7e4 100644 --- a/api-node/prisma/schema.prisma +++ b/api-node/prisma/schema.prisma @@ -37,8 +37,8 @@ model User { municipality_zip_code String? udi String? // code UDI for drinking water push_notif_token String? - notifications_sent Int? @default(0) // cache the number of notifications sent - asked_for_review Int? @default(0) + notifications_sent Int? @default(0) // cache the number of notifications sent TODO FIXME: not used + asked_for_review Int? @default(0) // TODO FIXME: should be rename to `triggered_manually_from_app` asked_for_review_latest_at DateTime? created_at DateTime @default(now()) updated_at DateTime @default(now()) @updatedAt @@ -375,6 +375,8 @@ model Notification { created_at DateTime @default(now()) updated_at DateTime @default(now()) @updatedAt status NotificationEnum? + + @@index([user_id], name: "notification_user_id") } model Feedback { diff --git a/api-node/src/controllers/event.ts b/api-node/src/controllers/event.ts index 6e26c6d7..a82f5679 100644 --- a/api-node/src/controllers/event.ts +++ b/api-node/src/controllers/event.ts @@ -24,7 +24,7 @@ router.post( matomo_id: req.body.userId, }, data: { - asked_for_review: { increment: 1 }, + asked_for_review: { increment: 1 }, // TODO FIXME: `asked_for_review` should be rename to `triggered_manually_from_app` asked_for_review_latest_at: new Date(), }, }); @@ -44,7 +44,6 @@ router.post( matomo_id: req.body.userId, }, data: { - asked_for_review: { increment: 1 }, asked_for_review_latest_at: new Date(), }, }); diff --git a/api-node/src/utils/user.ts b/api-node/src/utils/user.ts index c6a2605b..46b01117 100644 --- a/api-node/src/utils/user.ts +++ b/api-node/src/utils/user.ts @@ -1,21 +1,30 @@ import dayjs from 'dayjs'; import type { User } from '@prisma/client'; +import prisma from '~/prisma'; -export function canAskReviewForUser(user: User | null) { +export async function canAskReviewForUser(user: User | null) { if (!user) return false; // no user if (Number(user.appbuild) < 24) { console.log('store review unavailable before'); - return false; // store review unavailable before + return false; } - if (!user?.notifications_sent) { + if (user?.asked_for_review) { + console.log('already done manually'); + return false; + } + const notificationsSent = await prisma.notification.count({ + where: { + user_id: user.id, + }, + }); + if (!notificationsSent) { console.log('not enough experience with the app'); - return false; // not enough experience with the app + return false; } - if (user?.asked_for_review) { - console.log('already done'); - return false; // already done + if (!user?.asked_for_review_latest_at) { + console.log('no date, meaning never asked before so we can ask'); + return true; } - if (!user?.asked_for_review_latest_at) return true; // no date if (dayjs().diff(dayjs(user?.asked_for_review_latest_at), 'months') < 3) { console.log('too recent'); return false; // too recent