Breaking down mega-package. Hello auth-db 1.0.0!

This commit is contained in:
2023-05-23 14:15:34 -04:00
commit 2d341e5a9a
33 changed files with 641 additions and 0 deletions

45
lib/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/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>;