Skip to content

Commit

Permalink
feat: Ability to track user activity navigation logs (#1167)
Browse files Browse the repository at this point in the history
AB#105244

Co-authored-by: Tai Kamilla <taikamilla@gmail.com>
Co-authored-by: unai-reliefapp <123092672+unai-reliefapp@users.noreply.github.com>
Co-authored-by: MwanPygmay <59645813+MwanPygmay@users.noreply.github.com>
Co-authored-by: Yafar Valverde <15035750+TaiKamilla@users.noreply.github.com>
  • Loading branch information
5 people authored Dec 13, 2024
1 parent d10dfd7 commit 5233f6c
Show file tree
Hide file tree
Showing 13 changed files with 1,235 additions and 27 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"graphql-type-json": "^0.3.2",
"graphql-ws": "^5.14.0",
"http": "0.0.1-security",
"http-status-codes": "^2.3.0",
"i18next": "^21.6.13",
"i18next-http-middleware": "^3.2.0",
"i18next-node-fs-backend": "^2.1.3",
Expand Down
43 changes: 43 additions & 0 deletions src/abstractions/api-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ReasonPhrases, StatusCodes } from 'http-status-codes';

/** Error interface */
export interface Error {
status: number;
fields: {
name: {
message: string;
};
};
message: string;
name: string;
}

/** Api Error class */
class ApiError extends Error implements Error {
public status = 500;

public success = false;

public fields: { name: { message: string } } = { name: { message: '' } };

/**
* Api Error class
*
* @param msg Message
* @param statusCode Http status code number
* @param name Error name
*/
constructor(
msg: string,
statusCode: number,
name: string = ReasonPhrases.INTERNAL_SERVER_ERROR
) {
super();
this.message = msg;
this.status = statusCode;
this.name =
statusCode === StatusCodes.NOT_FOUND ? ReasonPhrases.NOT_FOUND : name;
}
}

export default ApiError;
22 changes: 22 additions & 0 deletions src/abstractions/base.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Response } from 'express';
import { StatusCodes } from 'http-status-codes';
import { RouteDefinition } from '../types/route-definition';

/**
* Provides services common to all API methods
*/
export default abstract class BaseController {
public abstract routes(): RouteDefinition[];

/**
* Global method to send API response.
*
* @param res Express response
* @param statusCode http status code
*/
public send(res: Response, statusCode: number = StatusCodes.OK): void {
let obj = {};
obj = res.locals.data;
res.status(statusCode).send(obj);
}
}
36 changes: 36 additions & 0 deletions src/models/activityLog.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { AccessibleRecordModel, accessibleRecordsPlugin } from '@casl/mongoose';
import mongoose, { Schema, Document } from 'mongoose';

/** Mongoose activity log schema declaration */
export interface ActivityLog extends Document {
kind: 'ActivityLog';
userId: mongoose.Types.ObjectId;
eventType: string;
metadata: any;
username: string;
attributes: any;
createdAt: Date;
}

/** Activity log documents interface declaration */
const schema = new Schema<ActivityLog>(
{
userId: Schema.Types.ObjectId,
eventType: String,
username: String,
metadata: Schema.Types.Mixed,
attributes: Schema.Types.Mixed,
},
{
timestamps: { createdAt: 'createdAt' },
}
);

schema.plugin(accessibleRecordsPlugin);

/** Mongoose activity log model */
// eslint-disable-next-line @typescript-eslint/no-redeclare
export const ActivityLog = mongoose.model<
ActivityLog,
AccessibleRecordModel<ActivityLog>
>('ActivityLog', schema);
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export * from './customNotification.model';
export * from './layer.model';
export * from './draftRecord.model';
export * from './emailNotification.model';
export * from './activityLog.model';
export * from './emailDistributionList.model';
Loading

0 comments on commit 5233f6c

Please sign in to comment.