Restructuring the folders
Some checks failed
continuous-integration/drone/pr Build is failing

This commit is contained in:
2023-05-24 10:24:15 -04:00
parent d3210b73c9
commit 7619899edc
38 changed files with 42 additions and 7 deletions

45
src/schema/log.ts Normal file
View File

@@ -0,0 +1,45 @@
import { InferSchemaType, Model, Schema, StringSchemaDefinition, Types } from 'mongoose';
import { Payload } from '@mifi/services-common/lib/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>;