Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
feat: add api analytics (#320)
Browse files Browse the repository at this point in the history
* add api analytics w/moesif

* feat: make api analytics work

* only run db migration when prisma folder changes
  • Loading branch information
jatinsandilya committed Oct 19, 2023
1 parent 1fba486 commit a8a5faf
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy-db-changes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ on:
push:
branches:
- main
# paths:
# - packages/prisma/**
paths:
- packages/prisma/**

jobs:
deploy-prisma:
Expand Down
Binary file modified .yarn/install-state.gz
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ SVIX_ENDPOINT_SECRET=
FERN_TOKEN=
PIPEDRIVE_CLIENT_ID=
PIPEDRIVE_CLIENT_SECRET=
AES_ENCRYPTION_SECRET=
AES_ENCRYPTION_SECRET=
MOESIF_APPLICATION_ID=
1 change: 1 addition & 0 deletions packages/backend/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const config = {
svix: process.env.SVIX_AUTH_TOKEN ? new Svix(process.env.SVIX_AUTH_TOKEN!) : undefined,
WHITE_LISTED_DOMAINS: process.env.WHITE_LISTED_DOMAINS?.split(','),
AES_ENCRYPTION_SECRET: process.env.AES_ENCRYPTION_SECRET!,
MOESIF_APPLICATION_ID: process.env.MOESIF_APPLICATION_ID,
};

export default config;
17 changes: 17 additions & 0 deletions packages/backend/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { Express, Request, Response } from 'express';
// Note: Sentry should be initialized as early in your app as possible.
import * as Sentry from '@sentry/node';
import moesif from 'moesif-nodejs';
import config from './config';
import indexRouter from './routes/index';
import cors from 'cors';
Expand Down Expand Up @@ -96,6 +97,22 @@ const testv2Router = (_req: Request, res: Response) => {
res.send({ data: 'v2 hit' });
};

// 2. Set the options, the only required field is applicationId
const moesifMiddleware = moesif({
applicationId: config.MOESIF_APPLICATION_ID!,
debug: process.env.NODE_ENV !== 'production', // enable debug mode.
logBody: true,
// Optional hook to link API calls to users
identifyUser: function (req: any, _: any) {
return req.headers['x-revert-t-id'] ? req.headers['x-revert-t-id'] : undefined;
},
identifyCompany: function (_: any, res: any) {
return res.locals?.account?.id;
},
});

app.use(moesifMiddleware);

app.use(
'/',
manageRouterVersioning({
Expand Down
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"express": "^4.18.1",
"express-rate-limit": "^6.7.0",
"lodash": "^4.17.21",
"moesif-nodejs": "^3.5.3",
"morgan": "^1.10.0",
"node-cron": "^3.0.2",
"prisma": "^4.16.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/services/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const metadataService = new MetadataService({

try {
const apps = await prisma.apps.findMany({
select: { scope: true, app_client_id: true, tp_id: true },
select: { scope: true, app_client_id: true, tp_id: true, env: { include: { accounts: true } } },
where: {
env: {
public_token: token as string,
Expand All @@ -28,6 +28,7 @@ const metadataService = new MetadataService({
error: 'Api token unauthorized',
});
}
res.locals.account = apps?.[0].env.accounts;
const getScope = (apps: Partial<apps>[], integration: TP_ID) => {
const app = apps.find((app) => app.tp_id === integration);
const scopes = app?.is_revert_app ? [] : app?.scope;
Expand Down
Loading

1 comment on commit a8a5faf

@vercel
Copy link

@vercel vercel bot commented on a8a5faf Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

revert-client – ./

revert-client-git-main-revertdev.vercel.app
app.revert.dev
revert-client-revertdev.vercel.app

Please sign in to comment.