|
| 1 | +import axios from 'axios' |
| 2 | +import { CROWD_ANALYTICS_CONFIG } from '@/conf' |
| 3 | +import UserRepository from '../database/repositories/userRepository' |
| 4 | +import TenantRepository from '../database/repositories/tenantRepository' |
| 5 | +import SequelizeRepository from '../database/repositories/sequelizeRepository' |
| 6 | + |
| 7 | +const IS_CROWD_ANALYTICS_ENABLED = CROWD_ANALYTICS_CONFIG.isEnabled === 'true' |
| 8 | +const CROWD_ANALYTICS_TENANT_ID = CROWD_ANALYTICS_CONFIG.tenantId |
| 9 | +const CROWD_ANALYTICS_BASE_URL = CROWD_ANALYTICS_CONFIG.baseUrl |
| 10 | +const CROWD_ANALYTICS_TOKEN = CROWD_ANALYTICS_CONFIG.apiToken |
| 11 | + |
| 12 | +// createdAnAccount (boolean) |
| 13 | + |
| 14 | +// email |
| 15 | + |
| 16 | +// firstName |
| 17 | + |
| 18 | +// lastName |
| 19 | + |
| 20 | +// plan |
| 21 | + |
| 22 | +// is_trial |
| 23 | + |
| 24 | +// trialEndsDate |
| 25 | + |
| 26 | +// workspace <> organization |
| 27 | + |
| 28 | +// Workspace → organization attributes |
| 29 | + |
| 30 | +// Name |
| 31 | + |
| 32 | +interface CrowdAnalyticsData { |
| 33 | + userId: string |
| 34 | + tenantId: string |
| 35 | + event: string |
| 36 | + timestamp: string |
| 37 | + properties: any |
| 38 | +} |
| 39 | + |
| 40 | +export default async function addProductData(data: CrowdAnalyticsData) { |
| 41 | + if (!IS_CROWD_ANALYTICS_ENABLED) { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + if (!CROWD_ANALYTICS_TENANT_ID) { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + if (!CROWD_ANALYTICS_BASE_URL) { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if (!CROWD_ANALYTICS_TOKEN) { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + const repositoryOptions = await SequelizeRepository.getDefaultIRepositoryOptions() |
| 59 | + |
| 60 | + const user = await UserRepository.findById(data.userId, repositoryOptions) |
| 61 | + |
| 62 | + const tenant = await TenantRepository.getTenantInfo(data.tenantId, repositoryOptions) |
| 63 | + |
| 64 | + const obj = { |
| 65 | + member: { |
| 66 | + username: { |
| 67 | + 'crowd.dev': user.email, |
| 68 | + }, |
| 69 | + emails: [user.email], |
| 70 | + displayName: user.fullName, |
| 71 | + attributes: { |
| 72 | + email: user.email, |
| 73 | + createdAnAccount: true, |
| 74 | + firstName: user.firstName, |
| 75 | + lastName: user.lastName, |
| 76 | + plan: tenant.plan, |
| 77 | + isTrialPlan: tenant.isTrialPlan, |
| 78 | + trialEndsAt: tenant.trialEndsAt, |
| 79 | + }, |
| 80 | + }, |
| 81 | + type: data.event, |
| 82 | + timestamp: data.timestamp, |
| 83 | + platform: 'crowd.dev', |
| 84 | + sourceId: `${data.userId}-${data.timestamp}-${data.event}`, |
| 85 | + } |
| 86 | + const endpoint = `${CROWD_ANALYTICS_BASE_URL}/api/tenant/${CROWD_ANALYTICS_TENANT_ID}/activity/with-member` |
| 87 | + await axios.post(endpoint, obj, { |
| 88 | + headers: { |
| 89 | + Authorization: `Bearer ${CROWD_ANALYTICS_TOKEN}`, |
| 90 | + }, |
| 91 | + }) |
| 92 | + } catch (error) { |
| 93 | + // nothing |
| 94 | + } |
| 95 | +} |
0 commit comments