MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / buildContext

Function buildContext

apps/api/src/controllers/track.controller.ts:150–204  ·  view source on GitHub ↗
(
  request: FastifyRequest<{
    Body: ITrackHandlerPayload;
  }>,
  validatedBody: ITrackHandlerPayload
)

Source from the content-addressed store, hash-verified

148}
149
150async 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
206async function handleTrack(
207 payload: ITrackPayload,

Callers 1

handlerFunction · 0.85

Calls 6

getGeoLocationFunction · 0.90
getDeviceIdFunction · 0.90
getTimestampFunction · 0.85
getStringHeadersFunction · 0.85
getIdentityFunction · 0.85
getOverrideDeviceIdFunction · 0.85

Tested by

no test coverage detected