Files
sign/packages/lib/server-only/telemetry/register-instance.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-06-03 19:38:52 +00:00
import { nanoid } from 'nanoid';
import { getSiteSettings } from '../site-settings/get-site-settings';
import {
SITE_SETTINGS_TELEMETRY_ID,
ZSiteSettingsTelemetrySchema,
} from '../site-settings/schemas/telemetry';
import { upsertSiteSetting } from '../site-settings/upsert-site-setting';
import { sendInstance } from './send-instance';
2024-06-06 23:02:06 +00:00
type RegisterInstanceOptions = {
version: string;
};
2024-06-06 23:02:06 +00:00
export const registerInstance = async ({ version }: RegisterInstanceOptions) => {
2024-06-03 19:38:52 +00:00
const instanceResponse = await getSiteSettings().then((settings) =>
settings.find((setting) => setting.id === SITE_SETTINGS_TELEMETRY_ID),
);
const instance = ZSiteSettingsTelemetrySchema.parse(instanceResponse);
if (!instance) {
const upsert = await upsertSiteSetting({
data: {
instanceId: nanoid(),
},
enabled: true,
id: SITE_SETTINGS_TELEMETRY_ID,
userId: null,
});
const instance = ZSiteSettingsTelemetrySchema.parse(upsert);
return await sendInstance({
uniqueId: instance.data?.instanceId,
timestamp: new Date(),
version,
2024-06-03 19:38:52 +00:00
});
}
return await sendInstance({
uniqueId: instance.data.instanceId,
timestamp: new Date(),
version,
2024-06-03 19:38:52 +00:00
});
};