Breaking down mega-package. Hello auth-db 1.0.0!
This commit is contained in:
45
lib/schema/log.ts
Normal file
45
lib/schema/log.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
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>;
|
||||
Reference in New Issue
Block a user