Skip to content

Commit

Permalink
Use streak id to synchronize blurb
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Oct 3, 2023
1 parent 7fd0a3f commit 2962913
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions worker/streak.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function checkStreak ({ models }) {
console.log('checking streak', id)

// if user is actively streaking skip
const streak = await models.streak.findFirst({
let streak = await models.streak.findFirst({
where: {
userId: Number(id),
endedAt: null
Expand All @@ -84,7 +84,7 @@ export function checkStreak ({ models }) {
return
}

const affected = await models.$executeRaw`
[streak] = await models.$queryRaw`
WITH streak_started (id) AS (
SELECT "userId"
FROM
Expand All @@ -106,14 +106,15 @@ export function checkStreak ({ models }) {
)
INSERT INTO "Streak" ("userId", "startedAt", created_at, updated_at)
SELECT id, (now() AT TIME ZONE 'America/Chicago')::date, now_utc(), now_utc()
FROM streak_started`
FROM streak_started
RETURNING "Streak".id`

console.log('done checking streak', id)

if (!affected) return
if (!streak) return

// new streak started for user
const index = Math.floor(Math.random() * FOUND_BLURBS.length)
const index = streak.id % FOUND_BLURBS.length
const blurb = FOUND_BLURBS[index]
sendUserNotification(id, {
title: 'you found a cowboy hat',
Expand Down

0 comments on commit 2962913

Please sign in to comment.