(params: {
req: NextRequest
getUserInfoFromApiKey: GetUserInfoFromApiKeyFn
logger: Logger
loggerWithContext: LoggerWithContextFn
trackEvent: TrackEventFn
})
| 21 | }) |
| 22 | |
| 23 | export async function postAdClick(params: { |
| 24 | req: NextRequest |
| 25 | getUserInfoFromApiKey: GetUserInfoFromApiKeyFn |
| 26 | logger: Logger |
| 27 | loggerWithContext: LoggerWithContextFn |
| 28 | trackEvent: TrackEventFn |
| 29 | }) { |
| 30 | const { req, getUserInfoFromApiKey, loggerWithContext, trackEvent } = params |
| 31 | const baseLogger = params.logger |
| 32 | |
| 33 | let impUrl: string |
| 34 | let surface: z.infer<typeof bodySchema>['surface'] |
| 35 | try { |
| 36 | const json = await req.json() |
| 37 | const parsed = bodySchema.safeParse(json) |
| 38 | if (!parsed.success) { |
| 39 | return NextResponse.json( |
| 40 | { error: 'Invalid request body', details: parsed.error.format() }, |
| 41 | { status: 400 }, |
| 42 | ) |
| 43 | } |
| 44 | impUrl = parsed.data.impUrl |
| 45 | surface = parsed.data.surface |
| 46 | } catch { |
| 47 | return NextResponse.json( |
| 48 | { error: 'Invalid JSON in request body' }, |
| 49 | { status: 400 }, |
| 50 | ) |
| 51 | } |
| 52 | |
| 53 | const authed = await requireUserFromApiKey({ |
| 54 | req, |
| 55 | getUserInfoFromApiKey, |
| 56 | logger: baseLogger, |
| 57 | loggerWithContext, |
| 58 | trackEvent, |
| 59 | authErrorEvent: AnalyticsEvent.ADS_API_AUTH_ERROR, |
| 60 | }) |
| 61 | if (!authed.ok) return authed.response |
| 62 | |
| 63 | const { userId, logger } = authed.data |
| 64 | |
| 65 | const adRecord = await db.query.adImpression.findFirst({ |
| 66 | where: eq(schema.adImpression.imp_url, impUrl), |
| 67 | }) |
| 68 | |
| 69 | if (!adRecord || adRecord.user_id !== userId) { |
| 70 | logger.warn( |
| 71 | { |
| 72 | userId, |
| 73 | adUserId: adRecord?.user_id, |
| 74 | impUrl, |
| 75 | }, |
| 76 | '[ads] Ad click not found for user', |
| 77 | ) |
| 78 | return NextResponse.json( |
| 79 | { success: false, error: 'Ad not found' }, |
| 80 | { status: 404 }, |
no test coverage detected