Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: in app rating asked from backend #516

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading