remove fake names from profile
This commit is contained in:
@@ -3,7 +3,7 @@ import { escape } from 'sqlstring';
|
||||
import superjson from 'superjson';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
import { randomSplitName, toDots } from '@openpanel/common';
|
||||
import { toDots } from '@openpanel/common';
|
||||
import { redis, redisPub } from '@openpanel/redis';
|
||||
import type { IChartEventFilter } from '@openpanel/validation';
|
||||
|
||||
@@ -215,14 +215,14 @@ export async function createEvent(
|
||||
`create event ${payload.name} for deviceId: ${payload.deviceId} profileId ${payload.profileId}`
|
||||
);
|
||||
|
||||
const exists = await getProfileById(payload.profileId);
|
||||
const exists = await getProfileById(payload.profileId, payload.projectId);
|
||||
if (!exists && payload.profileId !== '') {
|
||||
const { firstName, lastName } = randomSplitName();
|
||||
await upsertProfile({
|
||||
id: payload.profileId,
|
||||
isExternal: false,
|
||||
projectId: payload.projectId,
|
||||
firstName,
|
||||
lastName,
|
||||
firstName: '',
|
||||
lastName: '',
|
||||
properties: {
|
||||
path: payload.path,
|
||||
country: payload.country,
|
||||
|
||||
@@ -7,13 +7,13 @@ import { ch, chQuery } from '../clickhouse-client';
|
||||
import { createSqlBuilder } from '../sql-builder';
|
||||
import { getEventFiltersWhereClause } from './chart.service';
|
||||
|
||||
export async function getProfileById(id: string) {
|
||||
if (id === '') {
|
||||
export async function getProfileById(id: string, projectId: string) {
|
||||
if (id === '' || projectId === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [profile] = await chQuery<IClickhouseProfile>(
|
||||
`SELECT *, created_at as max_created_at FROM profiles WHERE id = ${escape(id)} ORDER BY created_at DESC LIMIT 1`
|
||||
`SELECT *, created_at as max_created_at FROM profiles WHERE id = ${escape(id)} AND project_id = ${escape(projectId)} ORDER BY created_at DESC LIMIT 1`
|
||||
);
|
||||
|
||||
if (!profile) {
|
||||
@@ -39,6 +39,7 @@ function getProfileSelectFields() {
|
||||
'argMax(avatar, created_at) as avatar',
|
||||
'argMax(properties, created_at) as properties',
|
||||
'argMax(project_id, created_at) as project_id',
|
||||
'argMax(is_external, created_at) as is_external',
|
||||
'max(created_at) as max_created_at',
|
||||
].join(', ');
|
||||
}
|
||||
@@ -109,32 +110,14 @@ export async function getProfileListCount({
|
||||
return data?.count ?? 0;
|
||||
}
|
||||
|
||||
export async function getProfilesByExternalId(
|
||||
externalId: string | null,
|
||||
projectId: string
|
||||
) {
|
||||
if (externalId === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const data = await chQuery<IClickhouseProfile>(
|
||||
`SELECT
|
||||
${getProfileSelectFields()}
|
||||
FROM profiles
|
||||
GROUP BY id
|
||||
HAVING project_id = ${escape(projectId)} AND external_id = ${escape(externalId)}`
|
||||
);
|
||||
|
||||
return data.map(transformProfile);
|
||||
}
|
||||
|
||||
export type IServiceProfile = Omit<
|
||||
IClickhouseProfile,
|
||||
'max_created_at' | 'properties' | 'first_name' | 'last_name'
|
||||
'max_created_at' | 'properties' | 'first_name' | 'last_name' | 'is_external'
|
||||
> & {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
createdAt: Date;
|
||||
isExternal: boolean;
|
||||
properties: Record<string, unknown> & {
|
||||
country?: string;
|
||||
city?: string;
|
||||
@@ -155,6 +138,7 @@ export interface IClickhouseProfile {
|
||||
avatar: string;
|
||||
properties: Record<string, string | undefined>;
|
||||
project_id: string;
|
||||
is_external: boolean;
|
||||
max_created_at: string;
|
||||
}
|
||||
|
||||
@@ -166,6 +150,7 @@ export interface IServiceUpsertProfile {
|
||||
email?: string;
|
||||
avatar?: string;
|
||||
properties?: Record<string, unknown>;
|
||||
isExternal: boolean;
|
||||
}
|
||||
|
||||
export function transformProfile({
|
||||
@@ -178,6 +163,7 @@ export function transformProfile({
|
||||
...profile,
|
||||
firstName: first_name,
|
||||
lastName: last_name,
|
||||
isExternal: profile.is_external,
|
||||
properties: toObject(profile.properties),
|
||||
createdAt: new Date(max_created_at),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user