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

Record all views for logged-out users #2508

Merged
merged 1 commit into from Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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