This commit is contained in:
@@ -1,45 +1,51 @@
|
||||
import { InferSchemaType, Model, Schema, StringSchemaDefinition, Types } from 'mongoose';
|
||||
import {
|
||||
InferSchemaType,
|
||||
Model,
|
||||
Schema,
|
||||
StringSchemaDefinition,
|
||||
Types,
|
||||
} from "mongoose";
|
||||
|
||||
import { Payload } from '@mifi/services-common/lib/types/Payload';
|
||||
import { Payload } from "@mifi/services-common/lib/types/Payload";
|
||||
|
||||
import { Action } from '../constants/action';
|
||||
import { Action } from "../constants/action";
|
||||
|
||||
export interface Log {
|
||||
action: Action;
|
||||
auth: StringSchemaDefinition;
|
||||
payload?: Payload;
|
||||
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>;
|
||||
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,
|
||||
},
|
||||
{
|
||||
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();
|
||||
},
|
||||
add(id, action, payload) {
|
||||
this.create({ action, auth: id, payload }).catch();
|
||||
},
|
||||
|
||||
async historyForUser(id, action) {
|
||||
return this.find({ auth: id, action });
|
||||
},
|
||||
async historyForUser(id, action) {
|
||||
return this.find({ auth: id, action });
|
||||
},
|
||||
|
||||
async loginsForUser(id) {
|
||||
return this.find({ auth: id, action: Action.AUTHENTICATE });
|
||||
},
|
||||
async loginsForUser(id) {
|
||||
return this.find({ auth: id, action: Action.AUTHENTICATE });
|
||||
},
|
||||
};
|
||||
|
||||
export type LogSchema = InferSchemaType<typeof LogSchema>;
|
||||
|
||||
Reference in New Issue
Block a user