@vrplatform/log 1.0.12 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/build/main/index.d.ts +2 -22
  2. package/build/main/index.js +2 -74
  3. package/build/main/index.js.map +1 -1
  4. package/build/main/log/baselog.js.map +1 -0
  5. package/build/main/log/color.js.map +1 -0
  6. package/build/main/{common.js → log/common.js} +3 -1
  7. package/build/main/log/common.js.map +1 -0
  8. package/build/main/log/index.d.ts +22 -0
  9. package/build/main/log/index.js +91 -0
  10. package/build/main/log/index.js.map +1 -0
  11. package/build/main/log/type.js.map +1 -0
  12. package/build/main/tracking/index.d.ts +39 -0
  13. package/build/main/tracking/index.js +91 -0
  14. package/build/main/tracking/index.js.map +1 -0
  15. package/build/main/tracking/intercom.d.ts +112 -0
  16. package/build/main/tracking/intercom.js +124 -0
  17. package/build/main/tracking/intercom.js.map +1 -0
  18. package/build/module/index.d.ts +2 -22
  19. package/build/module/index.js +2 -73
  20. package/build/module/index.js.map +1 -1
  21. package/build/module/log/baselog.js.map +1 -0
  22. package/build/module/log/color.js.map +1 -0
  23. package/build/module/{common.js → log/common.js} +3 -1
  24. package/build/module/log/common.js.map +1 -0
  25. package/build/module/log/index.d.ts +22 -0
  26. package/build/module/log/index.js +74 -0
  27. package/build/module/log/index.js.map +1 -0
  28. package/build/module/log/type.js.map +1 -0
  29. package/build/module/tracking/index.d.ts +39 -0
  30. package/build/module/tracking/index.js +87 -0
  31. package/build/module/tracking/index.js.map +1 -0
  32. package/build/module/tracking/intercom.d.ts +112 -0
  33. package/build/module/tracking/intercom.js +121 -0
  34. package/build/module/tracking/intercom.js.map +1 -0
  35. package/package.json +33 -13
  36. package/src/index.ts +2 -127
  37. package/src/{common.ts → log/common.ts} +4 -1
  38. package/src/log/index.ts +127 -0
  39. package/src/tracking/index.ts +131 -0
  40. package/src/tracking/intercom.ts +250 -0
  41. package/build/main/baselog.js.map +0 -1
  42. package/build/main/color.js.map +0 -1
  43. package/build/main/common.js.map +0 -1
  44. package/build/main/index.spec.d.ts +0 -0
  45. package/build/main/index.spec.js +0 -2
  46. package/build/main/index.spec.js.map +0 -1
  47. package/build/main/type.js.map +0 -1
  48. package/build/module/baselog.js.map +0 -1
  49. package/build/module/color.js.map +0 -1
  50. package/build/module/common.js.map +0 -1
  51. package/build/module/index.spec.d.ts +0 -0
  52. package/build/module/index.spec.js +0 -2
  53. package/build/module/index.spec.js.map +0 -1
  54. package/build/module/type.js.map +0 -1
  55. /package/build/main/{baselog.d.ts → log/baselog.d.ts} +0 -0
  56. /package/build/main/{baselog.js → log/baselog.js} +0 -0
  57. /package/build/main/{color.d.ts → log/color.d.ts} +0 -0
  58. /package/build/main/{color.js → log/color.js} +0 -0
  59. /package/build/main/{common.d.ts → log/common.d.ts} +0 -0
  60. /package/build/main/{type.d.ts → log/type.d.ts} +0 -0
  61. /package/build/main/{type.js → log/type.js} +0 -0
  62. /package/build/module/{baselog.d.ts → log/baselog.d.ts} +0 -0
  63. /package/build/module/{baselog.js → log/baselog.js} +0 -0
  64. /package/build/module/{color.d.ts → log/color.d.ts} +0 -0
  65. /package/build/module/{color.js → log/color.js} +0 -0
  66. /package/build/module/{common.d.ts → log/common.d.ts} +0 -0
  67. /package/build/module/{type.d.ts → log/type.d.ts} +0 -0
  68. /package/build/module/{type.js → log/type.js} +0 -0
  69. /package/src/{baselog.ts → log/baselog.ts} +0 -0
  70. /package/src/{color.ts → log/color.ts} +0 -0
  71. /package/src/{type.ts → log/type.ts} +0 -0
@@ -0,0 +1,121 @@
1
+ export class IntercomAPI {
2
+ constructor(token, log) {
3
+ this.headers = {
4
+ 'Content-Type': 'application/json',
5
+ Accept: 'application/json',
6
+ 'Intercom-Version': '2.11',
7
+ Authorization: `Bearer ${token}`,
8
+ };
9
+ this.log = log;
10
+ }
11
+ headers;
12
+ log;
13
+ async fetch(path, options) {
14
+ if (options?.data) {
15
+ options.body = JSON.stringify(options.data);
16
+ options.data = undefined;
17
+ }
18
+ try {
19
+ const result = await fetch(`https://api.intercom.io${path}`, {
20
+ method: 'GET',
21
+ ...options,
22
+ headers: this.headers,
23
+ });
24
+ if (!result.ok) {
25
+ this.log.info('Failed to fetch from Intercom', {
26
+ result: {
27
+ status: result.status,
28
+ statusText: result.statusText,
29
+ body: await result.text(),
30
+ },
31
+ });
32
+ return undefined;
33
+ }
34
+ return result.status === 202 ? {} : (await result.json());
35
+ }
36
+ catch (error) {
37
+ this.log.error(error);
38
+ return undefined;
39
+ }
40
+ }
41
+ async updateUser({ data, contactId, }) {
42
+ return await this.fetch(`/contacts/${contactId}`, {
43
+ method: 'PUT',
44
+ data,
45
+ });
46
+ }
47
+ async updateCompany({ data, companyId, }) {
48
+ return await this.fetch(`/companies/${companyId}`, {
49
+ method: 'PUT',
50
+ data,
51
+ });
52
+ }
53
+ async createUser({ data, userId, }) {
54
+ return await this.fetch('/contacts', {
55
+ method: 'POST',
56
+ data: { ...data, external_id: userId },
57
+ });
58
+ }
59
+ async createCompany({ data, tenantId, }) {
60
+ return await this.fetch('/companies', {
61
+ method: 'POST',
62
+ data: {
63
+ ...data,
64
+ company_id: tenantId,
65
+ },
66
+ });
67
+ }
68
+ async createCompanyUser(data) {
69
+ return await this.fetch(`/contacts/${data.intercom_contact_id}/companies`, {
70
+ method: 'POST',
71
+ data: {
72
+ id: data.intercom_company_id,
73
+ },
74
+ });
75
+ }
76
+ async getCompanyByTenantId(tenantId) {
77
+ return await this.fetch(`/companies?company_id=${tenantId}`, {
78
+ method: 'GET',
79
+ });
80
+ }
81
+ async getUserByUserId(userId) {
82
+ return await this.fetch('/contacts/search', {
83
+ method: 'POST',
84
+ data: {
85
+ query: {
86
+ field: 'external_id',
87
+ operator: '=',
88
+ value: userId,
89
+ },
90
+ },
91
+ });
92
+ }
93
+ async deleteUser(intercom_contact_id) {
94
+ return await this.fetch(`/contacts/${intercom_contact_id}`, {
95
+ method: 'DELETE',
96
+ });
97
+ }
98
+ async deleteCompany(intercom_company_id) {
99
+ return await this.fetch(`/companies/${intercom_company_id}`, {
100
+ method: 'DELETE',
101
+ });
102
+ }
103
+ async deleteCompanyUser({ intercom_company_id, intercom_contact_id, }) {
104
+ return await this.fetch(`/contacts/${intercom_contact_id}/companies/${intercom_company_id}`, {
105
+ method: 'DELETE',
106
+ });
107
+ }
108
+ async trackEvent(data) {
109
+ return await this.fetch('/events', {
110
+ method: 'POST',
111
+ data: {
112
+ created_at: Math.floor(new Date().getTime() / 1000), // to seconds
113
+ ...data,
114
+ },
115
+ });
116
+ }
117
+ }
118
+ export function useIntercom(token, log) {
119
+ return new IntercomAPI(token, log);
120
+ }
121
+ //# sourceMappingURL=intercom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"intercom.js","sourceRoot":"src/","sources":["tracking/intercom.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAW;IACtB,YAAY,KAAa,EAAE,GAAQ;QACjC,IAAI,CAAC,OAAO,GAAG;YACb,cAAc,EAAE,kBAAkB;YAClC,MAAM,EAAE,kBAAkB;YAC1B,kBAAkB,EAAE,MAAM;YAC1B,aAAa,EAAE,UAAU,KAAK,EAAE;SACjC,CAAC;QAEF,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAEO,OAAO,CAAyB;IAChC,GAAG,CAAM;IAET,KAAK,CAAC,KAAK,CACjB,IAAY,EACZ,OAAsC;QAEtC,IAAI,OAAO,EAAE,IAAI,EAAE,CAAC;YAClB,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YAC5C,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC;QAC3B,CAAC;QAED,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,0BAA0B,IAAI,EAAE,EAAE;gBAC3D,MAAM,EAAE,KAAK;gBACb,GAAG,OAAO;gBACV,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CA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AmB,EACnB,mBAAmB,GAIpB;QACC,OAAO,MAAM,IAAI,CAAC,KAAK,CAEpB,aAAa,mBAAmB,cAAc,mBAAmB,EAAE,EAAE;YACtE,MAAM,EAAE,QAAQ;SACjB,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,IAIhB;QACC,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE;YACjC,MAAM,EAAE,MAAM;YACd,IAAI,EAAE;gBACJ,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,EAAE,aAAa;gBAClE,GAAG,IAAI;aACR;SACF,CAAC,CAAC;IACL,CAAC;CACF;AACD,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,GAAQ;IACjD,OAAO,IAAI,WAAW,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AACrC,CAAC"}
package/package.json CHANGED
@@ -1,14 +1,30 @@
1
1
  {
2
2
  "name": "@vrplatform/log",
3
- "version": "1.0.12",
4
- "main": "build/main/index.js",
3
+ "version": "1.0.15",
4
+ "main": "src/index.ts",
5
5
  "publishConfig": {
6
- "access": "public"
6
+ "access": "public",
7
+ "main": "build/main/index.js",
8
+ "typings": "build/main/index.d.ts",
9
+ "module": "build/module/index.js"
7
10
  },
8
11
  "description": "Finalytic cf",
9
12
  "repository": "https://github.com/finalytic/ecosystem",
10
13
  "license": "UNLICENSED",
11
14
  "keywords": [],
15
+ "scripts": {
16
+ "pub": "bun run build && npm version patch --no-commit-hooks --no-git-tag-version && npm publish --no-git-checks",
17
+ "build": "bun build:main & bun build:module",
18
+ "build:main": "tsc -b tsconfig.main.json",
19
+ "build:module": "tsc -b tsconfig.esm.json",
20
+ "typecheck": "tsc --noEmit",
21
+ "lint": "biome check .",
22
+ "lint:fix": "biome check --apply-unsafe --skip-errors .",
23
+ "pre-commit": "echo precommit",
24
+ "pre-push": "bun typecheck && bun lint",
25
+ "post-merge": "bun i",
26
+ "prepare": "husky"
27
+ },
12
28
  "files": [
13
29
  "build/main",
14
30
  "build/module",
@@ -17,15 +33,19 @@
17
33
  "LICENSE"
18
34
  ],
19
35
  "dependencies": {
20
- "@axiomhq/js": "^1.0.0"
36
+ "@axiomhq/js": "1.0.0-rc.3",
37
+ "@june-so/analytics-node": "^8.0.0",
38
+ "posthog-node": "^4.0.1",
39
+ "serialize-error": "11.0.3"
21
40
  },
22
- "scripts": {
23
- "pub": "bun run build && pnpm publish --no-git-checks",
24
- "build": "bun build:main & bun build:module",
25
- "build:main": "tsc -b tsconfig.main.json",
26
- "build:module": "tsc -b tsconfig.esm.json",
27
- "typecheck": "tsc --noEmit"
41
+ "devDependencies": {
42
+ "@biomejs/biome": "1.8.3",
43
+ "@cloudflare/workers-types": "4.20240620.0",
44
+ "@types/node": "^20.14.9",
45
+ "husky": "^9.0.11",
46
+ "typescript": "5.5.3"
28
47
  },
29
- "typings": "build/main/index.d.ts",
30
- "module": "build/module/index.js"
31
- }
48
+ "trustedDependencies": [
49
+ "@biomejs/biome"
50
+ ]
51
+ }
package/src/index.ts CHANGED
@@ -1,127 +1,2 @@
1
- import { createBaseLog } from './baselog';
2
- import {
3
- AXIOM_AUTH_TOKEN_ENV,
4
- AXIOM_DATASET_ENV,
5
- AXIOM_ORG_ID_ENV,
6
- LOG_ENV,
7
- TASK_QUEUE_HEADER,
8
- WORKFLOW_ID_HEADER,
9
- getCorrelationId,
10
- getEnvironment,
11
- requestToContext,
12
- } from './common';
13
- import type { RequestLike, WorkerLog } from './type';
14
-
15
- export * from './common';
16
- export * from './baselog';
17
- export * from './type';
18
-
19
- export function useLog({
20
- environment: _environment,
21
- name,
22
- request,
23
- body,
24
- env,
25
- correlationId: cid,
26
- version,
27
- executionContext,
28
- logRequest,
29
- dataset: _dataset,
30
- context: initialContext = {},
31
- }: {
32
- environment?: string;
33
- name: string;
34
- request?: RequestLike & { cf?: any };
35
- logRequest?: boolean | ((raw: ReturnType<typeof requestToContext>) => any);
36
- body?: any;
37
- correlationId?: string;
38
- version?: string;
39
- dataset?: string;
40
- env?: Record<string, any>;
41
- context?: Record<string, any>;
42
- executionContext?: {
43
- waitUntil(promise: Promise<any>): void;
44
- };
45
- }): WorkerLog {
46
- const environment = _environment || getEnvironment(env);
47
- const colorLogs = env?.[LOG_ENV] === '*' || environment === 'development';
48
- const correlationId = cid || getCorrelationId(request); //const logger = nodeColorLog;
49
-
50
- const axiomToken = env?.[AXIOM_AUTH_TOKEN_ENV];
51
-
52
- const axiom = createBaseLog(
53
- {
54
- token: axiomToken,
55
- orgId: env?.[AXIOM_ORG_ID_ENV],
56
- dataset: _dataset || env?.[AXIOM_DATASET_ENV] || 'default',
57
- consoleLog: colorLogs ? 'color' : false,
58
- },
59
- {
60
- environment,
61
- type: 'worker',
62
- executionContext,
63
- workerId: env?.MACHINE_NAME || name || 'default',
64
- app: name || 'default',
65
- version: version || 'default',
66
- correlationId,
67
- context: {
68
- ...initialContext,
69
- rootCorrelationId: correlationId,
70
- correlationId,
71
- taskQueue: request?.headers?.get(TASK_QUEUE_HEADER) || undefined,
72
- workflowId: request?.headers?.get(WORKFLOW_ID_HEADER) || undefined,
73
- child: undefined as any as string,
74
- },
75
- }
76
- ) as WorkerLog;
77
-
78
- if (logRequest !== false) {
79
- const { message, ...raw } =
80
- typeof logRequest === 'function'
81
- ? logRequest(requestToContext(request, body))
82
- : requestToContext(request, body);
83
- axiom.info(message || 'Request', raw);
84
- }
85
-
86
- axiom.respond = async (res) => {
87
- const temp = await (typeof res === 'function' ? res() : res);
88
-
89
- const response = (() => {
90
- if (temp instanceof Response) {
91
- axiom?.info(`Response ${temp.status}`, {
92
- response: {
93
- body: temp
94
- .clone()
95
- .json()
96
- .catch(() => undefined),
97
- response: temp,
98
- status: temp.status,
99
- statusText: temp.statusText,
100
- headers: temp.headers,
101
- },
102
- });
103
-
104
- return temp;
105
- } else {
106
- // We have no response status code YET
107
- axiom?.info('Response 200', {
108
- response: temp,
109
- });
110
-
111
- return new Response(
112
- typeof temp.body === 'object' ? JSON.stringify(temp.body) : temp.body,
113
- {
114
- status: temp.status,
115
- statusText: temp.statusText,
116
- headers: temp.headers,
117
- }
118
- );
119
- }
120
- })();
121
-
122
- await axiom?.flush();
123
-
124
- return response!;
125
- };
126
- return axiom;
127
- }
1
+ export * from './log';
2
+ export * from './tracking';
@@ -83,7 +83,10 @@ export function requestToContext(
83
83
  headers: getHeaderMap(request?.headers),
84
84
  method: request?.method,
85
85
  cloudflare: cf as any,
86
- body: body || undefined,
86
+ body:
87
+ body && typeof body === 'object'
88
+ ? JSON.stringify(body)
89
+ : body || undefined,
87
90
  };
88
91
  if (request?.url) {
89
92
  const url = new URL(request.url);
@@ -0,0 +1,127 @@
1
+ import { createBaseLog } from './baselog';
2
+ import {
3
+ AXIOM_AUTH_TOKEN_ENV,
4
+ AXIOM_DATASET_ENV,
5
+ AXIOM_ORG_ID_ENV,
6
+ LOG_ENV,
7
+ TASK_QUEUE_HEADER,
8
+ WORKFLOW_ID_HEADER,
9
+ getCorrelationId,
10
+ getEnvironment,
11
+ requestToContext,
12
+ } from './common';
13
+ import type { RequestLike, WorkerLog } from './type';
14
+
15
+ export * from './common';
16
+ export * from './baselog';
17
+ export * from './type';
18
+
19
+ export function useLog({
20
+ environment: _environment,
21
+ name,
22
+ request,
23
+ body,
24
+ env,
25
+ correlationId: cid,
26
+ version,
27
+ executionContext,
28
+ logRequest,
29
+ dataset: _dataset,
30
+ context: initialContext = {},
31
+ }: {
32
+ environment?: string;
33
+ name: string;
34
+ request?: RequestLike & { cf?: any };
35
+ logRequest?: boolean | ((raw: ReturnType<typeof requestToContext>) => any);
36
+ body?: any;
37
+ correlationId?: string;
38
+ version?: string;
39
+ dataset?: string;
40
+ env?: Record<string, any>;
41
+ context?: Record<string, any>;
42
+ executionContext?: {
43
+ waitUntil(promise: Promise<any>): void;
44
+ };
45
+ }): WorkerLog {
46
+ const environment = _environment || getEnvironment(env);
47
+ const colorLogs = env?.[LOG_ENV] === '*' || environment === 'development';
48
+ const correlationId = cid || getCorrelationId(request); //const logger = nodeColorLog;
49
+
50
+ const axiomToken = env?.[AXIOM_AUTH_TOKEN_ENV];
51
+
52
+ const axiom = createBaseLog(
53
+ {
54
+ token: axiomToken,
55
+ orgId: env?.[AXIOM_ORG_ID_ENV],
56
+ dataset: _dataset || env?.[AXIOM_DATASET_ENV] || 'default',
57
+ consoleLog: colorLogs ? 'color' : false,
58
+ },
59
+ {
60
+ environment,
61
+ type: 'worker',
62
+ executionContext,
63
+ workerId: env?.MACHINE_NAME || name || 'default',
64
+ app: name || 'default',
65
+ version: version || 'default',
66
+ correlationId,
67
+ context: {
68
+ ...initialContext,
69
+ rootCorrelationId: correlationId,
70
+ correlationId,
71
+ taskQueue: request?.headers?.get(TASK_QUEUE_HEADER) || undefined,
72
+ workflowId: request?.headers?.get(WORKFLOW_ID_HEADER) || undefined,
73
+ child: undefined as any as string,
74
+ },
75
+ }
76
+ ) as WorkerLog;
77
+
78
+ if (logRequest !== false) {
79
+ const { message, ...raw } =
80
+ typeof logRequest === 'function'
81
+ ? logRequest(requestToContext(request, body))
82
+ : requestToContext(request, body);
83
+ axiom.info(message || 'Request', raw);
84
+ }
85
+
86
+ axiom.respond = async (res) => {
87
+ const temp = await (typeof res === 'function' ? res() : res);
88
+
89
+ const response = (() => {
90
+ if (temp instanceof Response) {
91
+ axiom?.info(`Response ${temp.status}`, {
92
+ response: {
93
+ body: temp
94
+ .clone()
95
+ .json()
96
+ .catch(() => undefined),
97
+ response: temp,
98
+ status: temp.status,
99
+ statusText: temp.statusText,
100
+ headers: temp.headers,
101
+ },
102
+ });
103
+
104
+ return temp;
105
+ } else {
106
+ // We have no response status code YET
107
+ axiom?.info('Response 200', {
108
+ response: temp,
109
+ });
110
+
111
+ return new Response(
112
+ typeof temp.body === 'object' ? JSON.stringify(temp.body) : temp.body,
113
+ {
114
+ status: temp.status,
115
+ statusText: temp.statusText,
116
+ headers: temp.headers,
117
+ }
118
+ );
119
+ }
120
+ })();
121
+
122
+ await axiom?.flush();
123
+
124
+ return response!;
125
+ };
126
+ return axiom;
127
+ }
@@ -0,0 +1,131 @@
1
+ import { Analytics } from '@june-so/analytics-node';
2
+ import { PostHog } from 'posthog-node';
3
+ import { useLog } from '../log';
4
+ import { type IntercomAPI, useIntercom } from './intercom';
5
+
6
+ type Props = {
7
+ name: string;
8
+ dataset: string;
9
+ env: {
10
+ INTERCOM_TOKEN: string;
11
+ JUNESO_ANALYTICS_WRITE_KEY: string;
12
+ POSTHOG_API_KEY: string;
13
+ };
14
+ };
15
+
16
+ export type Tracking = ReturnType<typeof useTracking>;
17
+
18
+ export const useTracking = ({ dataset, env, name }: Props, isDev: boolean) => {
19
+ const log = useLog({ name, dataset, env });
20
+ const intercom = useIntercom(env.INTERCOM_TOKEN, log);
21
+ const juneso = new Analytics(env.JUNESO_ANALYTICS_WRITE_KEY);
22
+ const posthog = new PostHog(env.POSTHOG_API_KEY, {
23
+ host: 'https://app.posthog.com',
24
+ });
25
+
26
+ return {
27
+ track: async ({
28
+ userId,
29
+ event,
30
+ tenantId,
31
+ properties,
32
+ }: {
33
+ userId: string;
34
+ event: string;
35
+ tenantId: string;
36
+ properties: Record<string, any>;
37
+ }) => {
38
+ if (isDev) return;
39
+
40
+ posthog.capture({
41
+ distinctId: userId,
42
+ event,
43
+ groups: {
44
+ tenant: tenantId,
45
+ },
46
+ properties,
47
+ });
48
+ juneso.track({
49
+ userId,
50
+ event,
51
+ properties,
52
+ context: { groupId: tenantId },
53
+ });
54
+
55
+ try {
56
+ await intercom.trackEvent({
57
+ user_id: userId,
58
+ event_name: event,
59
+ metadata: properties,
60
+ });
61
+ } catch (error: any) {
62
+ console.error(error);
63
+ }
64
+ },
65
+ identify: ({
66
+ properties,
67
+ userId,
68
+ }: { userId: string; properties: Record<string, any> }) => {
69
+ posthog.identify({
70
+ distinctId: userId,
71
+ properties,
72
+ });
73
+ juneso.identify({
74
+ userId,
75
+ traits: properties,
76
+ });
77
+ },
78
+ group: ({
79
+ properties,
80
+ tenantId,
81
+ userId,
82
+ }: {
83
+ tenantId: string;
84
+ userId: string;
85
+ properties: Record<string, any>;
86
+ }) => {
87
+ posthog.groupIdentify({
88
+ groupKey: tenantId,
89
+ groupType: 'tenant',
90
+ properties,
91
+ distinctId: userId,
92
+ });
93
+ juneso.group({
94
+ userId,
95
+ groupId: tenantId,
96
+ traits: properties,
97
+ });
98
+ },
99
+ alias: ({ distinctId, alias }: { distinctId: string; alias: string }) =>
100
+ posthog.alias({ distinctId, alias }),
101
+ shutdown: async () => {
102
+ await posthog.shutdown();
103
+ await juneso.closeAndFlush();
104
+ },
105
+ setIntercomCompanyAttributes: async ({
106
+ tenantId,
107
+ customerId,
108
+ data,
109
+ }: {
110
+ tenantId: string;
111
+ customerId: string;
112
+ data: Parameters<IntercomAPI['updateCompany']>[0]['data'];
113
+ }) => {
114
+ const intercomCompanies = await intercom.getCompanyByTenantId(tenantId);
115
+ const intercomCompany = intercomCompanies?.data?.[0];
116
+
117
+ if (!intercomCompany) return;
118
+
119
+ await intercom.updateCompany({
120
+ companyId: intercomCompany.id,
121
+ data: {
122
+ ...data,
123
+ custom_attributes: {
124
+ ...data.custom_attributes,
125
+ billingPortalUrl: `https://billing.vrplatform.app/portal/${customerId}`,
126
+ },
127
+ },
128
+ });
129
+ },
130
+ };
131
+ };