Skip to content

Commit

Permalink
Record all views for logged-out users (#2508)
Browse files Browse the repository at this point in the history
  • Loading branch information
mqp committed Mar 5, 2024
1 parent c349330 commit b9ad0ee
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions backend/api/src/record-contract-view.ts
Expand Up @@ -16,14 +16,15 @@ export const recordContractView: APIHandler<'record-contract-view'> = async (
throw new APIError(401, 'Can only insert views for own user ID.')
}
const pg = createSupabaseDirectClient()
// when we see an existing row, we need to bump the count by 1 and flip the timestamp to now
const [ts_column, count_column] = VIEW_COLUMNS[kind]
// when we see an existing row, we need to bump the count by 1 and flip the timestamp to now.
// for authed users, count at most one view per minute; for logged out users, count all
await pg.none(
`insert into user_contract_views as ucv (user_id, contract_id, $3:name, $4:name)
values ($1, $2, now(), 1)
on conflict (user_id, contract_id) do update set
$3:name = excluded.$3:name, $4:name = ucv.$4:name + excluded.$4:name
where ucv.$3:name is null or ucv.$3:name < now() - interval '1 minute'`,
where $1 is null or ucv.$3:name is null or ucv.$3:name < now() - interval '1 minute'`,
[userId ?? null, contractId, ts_column, count_column]
)
return { status: 'success' }
Expand Down

0 comments on commit b9ad0ee

Please sign in to comment.