reorg to domain and api

This commit is contained in:
Alex Holliday committed 2026-06-09 12:34:15 -07:00
1 parent 0fb210e9e1
commit d022f7d849
190 files changed
+643 -830

No files matched your search

@@ -0,0 +1,27 @@
import { Schema, model, type Types } from "mongoose";
import type { RecoveryToken as RecoveryTokenEntity } from "@/domain/recovery-tokens/recovery-token.type.js";
type RecoveryTokenDocumentBase = Omit<RecoveryTokenEntity, "id" | "createdAt" | "updatedAt" | "expiry"> & {
expiry: Date;
};
interface RecoveryTokenDocument extends RecoveryTokenDocumentBase {
_id: Types.ObjectId;
createdAt: Date;
updatedAt: Date;
}
const RecoveryTokenSchema = new Schema<RecoveryTokenDocument>(
{
email: { type: String, required: true, unique: true },
token: { type: String, required: true },
expiry: { type: Date, default: Date.now, expires: 600 },
},
{ timestamps: true }
);
const RecoveryTokenModel = model<RecoveryTokenDocument>("RecoveryToken", RecoveryTokenSchema);
export type { RecoveryTokenDocument };
export { RecoveryTokenModel };
export default RecoveryTokenModel;
@@ -0,0 +1,12 @@
import type { RecoveryToken } from "@/domain/recovery-tokens/recovery-token.type.js";
export interface IRecoveryTokensRepository {
// create
create(email: string): Promise<RecoveryToken>;
// fetch
findByToken(token: string): Promise<RecoveryToken>;
// update
// delete
deleteManyByEmail(email: string): Promise<number>;
// other
}
@@ -0,0 +1,61 @@
import type { RecoveryToken } from "@/domain/recovery-tokens/recovery-token.type.js";
import type { IRecoveryTokensRepository } from "./recovery-token.repository.interface.js";
import type { RecoveryTokenDocument } from "@/domain/recovery-tokens/recovery-token.model.js";
import { RecoveryTokenModel } from "@/domain/recovery-tokens/recovery-token.model.js";
import mongoose from "mongoose";
import crypto from "crypto";
import { AppError } from "@/utils/AppError.js";
const SERVICE_NAME = "MongoRecoveryTokensRepository";
class MongoRecoveryTokensRepository implements IRecoveryTokensRepository {
static SERVICE_NAME = SERVICE_NAME;
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
if (!value) {
return "";
}
return value instanceof mongoose.Types.ObjectId ? value.toString() : String(value);
};
private toDateString = (value?: Date | string | null): string => {
if (!value) {
return new Date(0).toISOString();
}
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
};
protected toEntity = (doc: RecoveryTokenDocument): RecoveryToken => {
return {
id: this.toStringId(doc._id),
email: doc.email,
token: doc.token,
expiry: this.toDateString(doc.expiry),
createdAt: this.toDateString(doc.createdAt),
updatedAt: this.toDateString(doc.updatedAt),
};
};
create = async (email: string): Promise<RecoveryToken> => {
const token = await RecoveryTokenModel.create({
email,
token: crypto.randomBytes(32).toString("hex"),
});
return this.toEntity(token);
};
findByToken = async (token: string): Promise<RecoveryToken> => {
const recoveryToken = await RecoveryTokenModel.findOne({ token });
if (!recoveryToken) {
throw new AppError({ message: "Recovery token not found", service: SERVICE_NAME, status: 404 });
}
return this.toEntity(recoveryToken);
};
deleteManyByEmail = async (email: string) => {
const result = await RecoveryTokenModel.deleteMany({
email,
});
return Promise.resolve(result.deletedCount || 0);
};
}
export default MongoRecoveryTokensRepository;
@@ -0,0 +1,8 @@
export interface RecoveryToken {
id: string;
email: string;
token: string;
expiry: string;
createdAt: string;
updatedAt: string;
}