Skip to content

Commit e5f14c8

Browse files
committed
Better handle duplicate ads with .onConflictDoNothing()
1 parent fde9d74 commit e5f14c8

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

web/src/app/api/v1/ads/_post.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,12 @@ export async function postAds(params: {
242242
return NextResponse.json({ ad: null, variant }, { status: 200 })
243243
}
244244

245-
// Store all returned ads in the database
245+
// Store all returned ads in the database (skip duplicates via imp_url unique constraint)
246246
for (const ad of ads) {
247247
const payout = ad.payout || DEFAULT_PAYOUT
248-
try {
249-
await db.insert(schema.adImpression).values({
248+
await db
249+
.insert(schema.adImpression)
250+
.values({
250251
user_id: userId,
251252
ad_text: ad.adText,
252253
title: ad.title,
@@ -258,20 +259,7 @@ export async function postAds(params: {
258259
payout: String(payout),
259260
credits_granted: 0,
260261
})
261-
} catch (error) {
262-
logger.warn(
263-
{
264-
userId,
265-
impUrl: ad.impUrl,
266-
status: response.status,
267-
error:
268-
error instanceof Error
269-
? { name: error.name, message: error.message }
270-
: error,
271-
},
272-
'[ads] Failed to create ad_impression record (likely duplicate)',
273-
)
274-
}
262+
.onConflictDoNothing()
275263
}
276264

277265
// Strip payout from all ads before returning to client

0 commit comments

Comments
 (0)