(
request: FastifyRequest<{
Body: ITrackHandlerPayload;
}>,
validatedBody: ITrackHandlerPayload
)
| 148 | } |
| 149 | |
| 150 | async function buildContext( |
| 151 | request: FastifyRequest<{ |
| 152 | Body: ITrackHandlerPayload; |
| 153 | }>, |
| 154 | validatedBody: ITrackHandlerPayload |
| 155 | ): Promise<TrackContext> { |
| 156 | const projectId = request.client?.projectId; |
| 157 | if (!projectId) { |
| 158 | throw new HttpError('Missing projectId', { status: 400 }); |
| 159 | } |
| 160 | |
| 161 | const timestamp = getTimestamp(request.timestamp, validatedBody.payload); |
| 162 | const ip = |
| 163 | validatedBody.type === 'track' && validatedBody.payload.properties?.__ip |
| 164 | ? (validatedBody.payload.properties.__ip as string) |
| 165 | : request.clientIp; |
| 166 | const ua = request.headers['user-agent'] ?? 'unknown/1.0'; |
| 167 | |
| 168 | const headers = getStringHeaders(request.headers); |
| 169 | const identity = getIdentity(validatedBody); |
| 170 | const profileId = identity?.profileId; |
| 171 | |
| 172 | if (profileId && validatedBody.type === 'track') { |
| 173 | validatedBody.payload.profileId = profileId; |
| 174 | } |
| 175 | |
| 176 | const overrideDeviceId = getOverrideDeviceId(validatedBody); |
| 177 | |
| 178 | // Get geo location (needed for track and identify) |
| 179 | const [geo, salts] = await Promise.all([getGeoLocation(ip), getSalts()]); |
| 180 | |
| 181 | const deviceIdResult = await getDeviceId({ |
| 182 | projectId, |
| 183 | ip, |
| 184 | ua, |
| 185 | salts, |
| 186 | overrideDeviceId, |
| 187 | eventTimeMs: timestamp.timestamp, |
| 188 | }); |
| 189 | |
| 190 | return { |
| 191 | projectId, |
| 192 | ip, |
| 193 | ua, |
| 194 | headers, |
| 195 | timestamp: { |
| 196 | value: timestamp.timestamp, |
| 197 | isFromPast: timestamp.isTimestampFromThePast, |
| 198 | }, |
| 199 | identity, |
| 200 | deviceId: deviceIdResult.deviceId, |
| 201 | sessionId: deviceIdResult.sessionId, |
| 202 | geo, |
| 203 | }; |
| 204 | } |
| 205 | |
| 206 | async function handleTrack( |
| 207 | payload: ITrackPayload, |
no test coverage detected