Skip to content

Commit

Permalink
fix: in app rating asked from backend (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudambro authored Apr 8, 2024
1 parent 302a00c commit eb9b59b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX "notification_user_id" ON "Notification"("user_id");
6 changes: 4 additions & 2 deletions api-node/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions api-node/src/controllers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
});
Expand All @@ -44,7 +44,6 @@ router.post(
matomo_id: req.body.userId,
},
data: {
asked_for_review: { increment: 1 },
asked_for_review_latest_at: new Date(),
},
});
Expand Down
25 changes: 17 additions & 8 deletions api-node/src/utils/user.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit eb9b59b

Please sign in to comment.