diff --git a/packages/db/src/buffers/profile-buffer-redis.ts b/packages/db/src/buffers/profile-buffer-redis.ts index 50ca9d35..7adafdcb 100644 --- a/packages/db/src/buffers/profile-buffer-redis.ts +++ b/packages/db/src/buffers/profile-buffer-redis.ts @@ -53,7 +53,7 @@ export class ProfileBuffer extends BaseBuffer { return `${this.redisProfilePrefix}${projectId}:${profileId}`; } - async add(profile: IClickhouseProfile) { + async add(profile: IClickhouseProfile, isFromEvent = false) { const logger = this.logger.child({ projectId: profile.project_id, profileId: profile.id, @@ -64,6 +64,11 @@ export class ProfileBuffer extends BaseBuffer { const existingProfile = await this.fetchFromCache(profile, logger); + if (isFromEvent && existingProfile) { + logger.debug('Profile already created, skipping'); + return; + } + const mergedProfile: IClickhouseProfile = existingProfile ? deepMergeObjects(existingProfile, profile) : profile; diff --git a/packages/db/src/services/event.service.ts b/packages/db/src/services/event.service.ts index 17bd0879..9a96eb3b 100644 --- a/packages/db/src/services/event.service.ts +++ b/packages/db/src/services/event.service.ts @@ -303,7 +303,7 @@ export async function createEvent(payload: IServiceCreateEventPayload) { profile.isExternal || (profile.isExternal === false && payload.name === 'session_start') ) { - await upsertProfile(profile); + await upsertProfile(profile, true); } } diff --git a/packages/db/src/services/profile.service.ts b/packages/db/src/services/profile.service.ts index fbfb4012..8bc73f87 100644 --- a/packages/db/src/services/profile.service.ts +++ b/packages/db/src/services/profile.service.ts @@ -212,27 +212,33 @@ export async function createProfileAlias({ }); } -export async function upsertProfile({ - id, - firstName, - lastName, - email, - avatar, - properties, - projectId, - isExternal, -}: IServiceUpsertProfile) { - return profileBuffer.add({ +export async function upsertProfile( + { id, - first_name: firstName!, - last_name: lastName!, - email: email!, - avatar: avatar!, - properties: properties as Record, - project_id: projectId, - created_at: formatClickhouseDate(new Date()), - is_external: isExternal, - }); + firstName, + lastName, + email, + avatar, + properties, + projectId, + isExternal, + }: IServiceUpsertProfile, + isFromEvent = false, +) { + return profileBuffer.add( + { + id, + first_name: firstName!, + last_name: lastName!, + email: email!, + avatar: avatar!, + properties: properties as Record, + project_id: projectId, + created_at: formatClickhouseDate(new Date()), + is_external: isExternal, + }, + isFromEvent, + ); } export async function getProfileId({