(
request: FastifyRequest<{
Body: DeprecatedPostEventPayload;
}>,
reply: FastifyReply
)
| 14 | import { getDeviceId } from '@/utils/ids'; |
| 15 | |
| 16 | export async function postEvent( |
| 17 | request: FastifyRequest<{ |
| 18 | Body: DeprecatedPostEventPayload; |
| 19 | }>, |
| 20 | reply: FastifyReply |
| 21 | ) { |
| 22 | const { timestamp, isTimestampFromThePast } = getTimestamp( |
| 23 | request.timestamp, |
| 24 | request.body |
| 25 | ); |
| 26 | const ip = request.clientIp; |
| 27 | const ua = request.headers['user-agent'] ?? 'unknown/1.0'; |
| 28 | const projectId = request.client?.projectId; |
| 29 | const headers = getStringHeaders(request.headers); |
| 30 | |
| 31 | if (!projectId) { |
| 32 | reply.status(400).send('missing origin'); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | const [salts, geo] = await Promise.all([getSalts(), getGeoLocation(ip)]); |
| 37 | const { deviceId, sessionId } = await getDeviceId({ |
| 38 | projectId, |
| 39 | ip, |
| 40 | ua, |
| 41 | salts, |
| 42 | }); |
| 43 | |
| 44 | const uaInfo = parseUserAgent(ua, request.body?.properties); |
| 45 | const groupId = uaInfo.isServer |
| 46 | ? `${projectId}:${request.body?.profileId ?? generateId()}` |
| 47 | : deviceId; |
| 48 | const queueData: EventsQueuePayloadIncomingEvent['payload'] = { |
| 49 | projectId, |
| 50 | headers, |
| 51 | event: { |
| 52 | ...request.body, |
| 53 | timestamp, |
| 54 | isTimestampFromThePast, |
| 55 | }, |
| 56 | uaInfo, |
| 57 | geo, |
| 58 | deviceId, |
| 59 | sessionId: sessionId ?? '', |
| 60 | }; |
| 61 | |
| 62 | if (shouldUseKafka()) { |
| 63 | await produceIncomingEvent(queueData, groupId); |
| 64 | } else { |
| 65 | await getEventsGroupQueueShard(groupId).add({ |
| 66 | orderMs: new Date(timestamp).getTime(), |
| 67 | data: queueData, |
| 68 | groupId, |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | reply.status(202).send('ok'); |
| 73 | } |
nothing calls this directly
no test coverage detected