78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
|
|
import * as plugins from '../plugins.js';
|
||
|
|
import { ActivityLog } from './classes.activitylog.js';
|
||
|
|
import { Reception } from './classes.reception.js';
|
||
|
|
|
||
|
|
export class ActivityLogManager {
|
||
|
|
// refs
|
||
|
|
public receptionRef: Reception;
|
||
|
|
public get db() {
|
||
|
|
return this.receptionRef.db.smartdataDb;
|
||
|
|
}
|
||
|
|
|
||
|
|
public CActivityLog = plugins.smartdata.setDefaultManagerForDoc(this, ActivityLog);
|
||
|
|
|
||
|
|
public typedRouter = new plugins.typedrequest.TypedRouter();
|
||
|
|
|
||
|
|
constructor(receptionRefArg: Reception) {
|
||
|
|
this.receptionRef = receptionRefArg;
|
||
|
|
this.receptionRef.typedrouter.addTypedRouter(this.typedRouter);
|
||
|
|
|
||
|
|
// Get user activity handler
|
||
|
|
this.typedRouter.addTypedHandler(
|
||
|
|
new plugins.typedrequest.TypedHandler<plugins.idpInterfaces.request.IReq_GetUserActivity>(
|
||
|
|
'getUserActivity',
|
||
|
|
async (requestArg) => {
|
||
|
|
const jwt = await this.receptionRef.jwtManager.verifyJWTAndGetData(requestArg.jwt);
|
||
|
|
if (!jwt) {
|
||
|
|
throw new plugins.typedrequest.TypedResponseError('Invalid JWT');
|
||
|
|
}
|
||
|
|
|
||
|
|
const limit = requestArg.limit || 20;
|
||
|
|
const offset = requestArg.offset || 0;
|
||
|
|
|
||
|
|
// Get activities for this user
|
||
|
|
const activities = await this.CActivityLog.getInstances({
|
||
|
|
'data.userId': jwt.data.userId,
|
||
|
|
});
|
||
|
|
|
||
|
|
// Sort by timestamp descending
|
||
|
|
const sortedActivities = activities
|
||
|
|
.sort((a, b) => b.data.timestamp - a.data.timestamp)
|
||
|
|
.slice(offset, offset + limit);
|
||
|
|
|
||
|
|
return {
|
||
|
|
activities: sortedActivities.map((a) => ({
|
||
|
|
id: a.id,
|
||
|
|
data: a.data,
|
||
|
|
})),
|
||
|
|
total: activities.length,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Log a user activity
|
||
|
|
*/
|
||
|
|
public async logActivity(
|
||
|
|
userId: string,
|
||
|
|
action: plugins.idpInterfaces.data.TActivityAction,
|
||
|
|
description: string,
|
||
|
|
metadata?: {
|
||
|
|
ip?: string;
|
||
|
|
userAgent?: string;
|
||
|
|
targetId?: string;
|
||
|
|
targetType?: string;
|
||
|
|
}
|
||
|
|
) {
|
||
|
|
return await ActivityLog.createActivityLog(
|
||
|
|
this,
|
||
|
|
userId,
|
||
|
|
action,
|
||
|
|
description,
|
||
|
|
metadata
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|