Skip to content

Commit

Permalink
Initial schema ideas for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dangtony98 committed Dec 17, 2022
1 parent c12eeac commit 2e84b7e
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
4 changes: 3 additions & 1 deletion backend/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as stripeController from './stripeController';
import * as userActionController from './userActionController';
import * as userController from './userController';
import * as workspaceController from './workspaceController';
import * as logController from './logController';

export {
authController,
Expand All @@ -29,5 +30,6 @@ export {
stripeController,
userActionController,
userController,
workspaceController
workspaceController,
logController
};
30 changes: 30 additions & 0 deletions backend/src/controllers/logController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Request, Response } from 'express';
import * as Sentry from '@sentry/node';
import {
Log
} from '../models';


export const getLogs = async (req: Request, res: Response) => {
// get logs

console.log('getLogs');
let logs;
try {
const { workspaceId } = req.params;

logs = await Log.find({
workspace: workspaceId
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'Failed to get audit logs'
});
}

return res.status(200).send({
logs
});
}
4 changes: 3 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import {
password as passwordRouter,
stripe as stripeRouter,
integration as integrationRouter,
integrationAuth as integrationAuthRouter
integrationAuth as integrationAuthRouter,
log as logRouter
} from './routes';

const connectWithRetry = () => {
Expand Down Expand Up @@ -92,6 +93,7 @@ app.use('/api/v1/password', passwordRouter);
app.use('/api/v1/stripe', stripeRouter);
app.use('/api/v1/integration', integrationRouter);
app.use('/api/v1/integration-auth', integrationAuthRouter);
app.use('/api/v1/log', logRouter);

const server = http.createServer(app);

Expand Down
5 changes: 4 additions & 1 deletion backend/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Token, { IToken } from './token';
import User, { IUser } from './user';
import UserAction, { IUserAction } from './userAction';
import Workspace, { IWorkspace } from './workspace';
import Log, { ILog } from './log';

export {
BackupPrivateKey,
Expand Down Expand Up @@ -41,5 +42,7 @@ export {
UserAction,
IUserAction,
Workspace,
IWorkspace
IWorkspace,
Log,
ILog
};
46 changes: 46 additions & 0 deletions backend/src/models/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Schema, model, Types } from 'mongoose';

export interface ILog {
_id: Types.ObjectId;
user: Types.ObjectId;
workspace: Types.ObjectId;
event: string;
source: string;
ipAddress: string;
}

// TODO: need a way to store payload info for each
// log

// which secret is being ref etc.

const logSchema = new Schema<ILog>(
{
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
workspace: {
type: Schema.Types.ObjectId,
ref: 'Workspace'
},
event: {
type: String,
required: true
},
source: { // should this just be a payload attr?
type: String,
required: true
},
ipAddress: { // store in bytes?
type: String,
required: true
}
}, {
timestamps: true
}
);

const Log = model<ILog>('Log', logSchema);

export default Log;
4 changes: 3 additions & 1 deletion backend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import password from './password';
import stripe from './stripe';
import integration from './integration';
import integrationAuth from './integrationAuth';
import log from './log';

export {
signup,
Expand All @@ -31,5 +32,6 @@ export {
password,
stripe,
integration,
integrationAuth
integrationAuth,
log
};
17 changes: 17 additions & 0 deletions backend/src/routes/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import express from 'express';
const router = express.Router();
import {
requireAuth,
validateRequest
} from '../middleware';
import { logController } from '../controllers';

// TODO: workspaceId validation
router.get(
'/:workspaceId',
requireAuth,
validateRequest,
logController.getLogs
);

export default router;

0 comments on commit 2e84b7e

Please sign in to comment.