From b9ad0eee6aedf92d72b07602d695eb0888363f4c Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Mon, 4 Mar 2024 16:51:14 -0800 Subject: [PATCH] Record all views for logged-out users (#2508) --- backend/api/src/record-contract-view.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/api/src/record-contract-view.ts b/backend/api/src/record-contract-view.ts index 1d3acac2a8..58ddd569a4 100644 --- a/backend/api/src/record-contract-view.ts +++ b/backend/api/src/record-contract-view.ts @@ -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' }