46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { InferSchemaType, Model, Schema, StringSchemaDefinition, Types } from 'mongoose';
|
|
|
|
import { Payload } from '@mifi/services-common/types/Payload';
|
|
|
|
import { Action } from '../constants/action';
|
|
|
|
export interface Log {
|
|
action: Action;
|
|
auth: StringSchemaDefinition;
|
|
payload?: Payload;
|
|
}
|
|
|
|
export interface LogModel extends Model<Log> {
|
|
add(id: StringSchemaDefinition, action: Action, payload?: Payload): void;
|
|
historyForUser(id: StringSchemaDefinition, action?: Action): Array<Log>;
|
|
loginsForUser(id: StringSchemaDefinition): Array<Log>;
|
|
}
|
|
|
|
export const LogSchema = new Schema<Log, LogModel>(
|
|
{
|
|
action: { type: String, enum: Action, required: true },
|
|
auth: { type: Types.ObjectId, index: true, required: true },
|
|
payload: { type: Object },
|
|
},
|
|
{
|
|
minimize: true,
|
|
timestamps: true,
|
|
},
|
|
);
|
|
|
|
LogSchema.statics = {
|
|
add(id, action, payload) {
|
|
this.create({ action, auth: id, payload }).catch();
|
|
},
|
|
|
|
async historyForUser(id, action) {
|
|
return this.find({ auth: id, action });
|
|
},
|
|
|
|
async loginsForUser(id) {
|
|
return this.find({ auth: id, action: Action.AUTHENTICATE });
|
|
},
|
|
};
|
|
|
|
export type LogSchema = InferSchemaType<typeof LogSchema>;
|