refactor: use capture

This commit is contained in:
karishmas6
2024-10-29 03:41:36 +05:30
parent 2e8bbe1665
commit ea0f5ba2d8

View File

@@ -6,7 +6,7 @@ import { hashPassword, comparePassword } from '../utils/auth';
import { requireSignIn } from '../middlewares/auth'; import { requireSignIn } from '../middlewares/auth';
import { genAPIKey } from '../utils/api'; import { genAPIKey } from '../utils/api';
import { google } from 'googleapis'; import { google } from 'googleapis';
import captureServerAnalytics from "../utils/analytics" import { capture } from "../utils/analytics"
export const router = Router(); export const router = Router();
interface AuthenticatedRequest extends Request { interface AuthenticatedRequest extends Request {
@@ -32,15 +32,14 @@ router.post('/register', async (req, res) => {
res.cookie('token', token, { res.cookie('token', token, {
httpOnly: true httpOnly: true
}) })
captureServerAnalytics.capture({ capture(
distinctId: user.id.toString(), 'maxun-oss-user-registered',
event: 'maxun-oss-user-registered', {
properties: {
email: user.email, email: user.email,
userId: user.id, userId: user.id,
registeredAt: new Date().toISOString() registeredAt: new Date().toISOString()
} }
}) )
res.json(user) res.json(user)
} catch (error: any) { } catch (error: any) {
res.status(500).send(`Could not register user - ${error.message}`) res.status(500).send(`Could not register user - ${error.message}`)
@@ -144,14 +143,13 @@ router.post('/generate-api-key', requireSignIn, async (req: AuthenticatedRequest
await user.update({ api_key: apiKey }); await user.update({ api_key: apiKey });
captureServerAnalytics.capture({ capture(
distinctId: user.id.toString(), 'maxun-oss-api-key-created',
event: 'maxun-oss-api-key-created', {
properties: {
user_id: user.id, user_id: user.id,
created_at: new Date().toISOString() created_at: new Date().toISOString()
} }
}) )
return res.status(200).json({ return res.status(200).json({
message: 'API key generated successfully', message: 'API key generated successfully',
@@ -205,14 +203,13 @@ router.delete('/delete-api-key', requireSignIn, async (req: AuthenticatedRequest
await User.update({ api_key: null }, { where: { id: req.user.id } }); await User.update({ api_key: null }, { where: { id: req.user.id } });
captureServerAnalytics.capture({ capture(
distinctId: user.id.toString(), 'maxun-oss-api-key-deleted',
event: 'maxun-oss-api-key-deleted', {
properties: {
user_id: user.id, user_id: user.id,
deleted_at: new Date().toISOString() deleted_at: new Date().toISOString()
} }
}) )
return res.status(200).json({ message: 'API Key deleted successfully' }); return res.status(200).json({ message: 'API Key deleted successfully' });
} catch (error: any) { } catch (error: any) {
@@ -293,16 +290,14 @@ router.get('/google/callback', requireSignIn, async (req: AuthenticatedRequest,
google_access_token: tokens.access_token, google_access_token: tokens.access_token,
google_refresh_token: tokens.refresh_token, google_refresh_token: tokens.refresh_token,
}); });
capture(
captureServerAnalytics.capture({ 'maxun-oss-google-sheet-integration-created',
distinctId: user.id.toString(), {
event: 'maxun-oss-google-sheet-integration-created',
properties: {
user_id: user.id, user_id: user.id,
robot_id: robot.recording_meta.id, robot_id: robot.recording_meta.id,
created_at: new Date().toISOString() created_at: new Date().toISOString()
} }
}) )
// List user's Google Sheets from their Google Drive // List user's Google Sheets from their Google Drive
const drive = google.drive({ version: 'v3', auth: oauth2Client }); const drive = google.drive({ version: 'v3', auth: oauth2Client });
@@ -451,15 +446,14 @@ router.post('/gsheets/remove', requireSignIn, async (req: AuthenticatedRequest,
google_refresh_token: null google_refresh_token: null
}); });
captureServerAnalytics.capture({ capture(
distinctId: req.user.id.toString(), 'maxun-oss-google-sheet-integration-removed',
event: 'maxun-oss-google-sheet-integration-removed', {
properties: {
user_id: req.user.id, user_id: req.user.id,
robot_id: robotId, robot_id: robotId,
deleted_at: new Date().toISOString() deleted_at: new Date().toISOString()
} }
}) )
res.json({ message: 'Google Sheets integration removed successfully' }); res.json({ message: 'Google Sheets integration removed successfully' });
} catch (error: any) { } catch (error: any) {