39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { eq } from "drizzle-orm";
|
|
import { passwordResets } from "@app/models";
|
|
import { db } from "@app/db";
|
|
|
|
export type PasswordResetRow = typeof passwordResets.$inferSelect;
|
|
|
|
export const passwordResetDao = {
|
|
async getByToken(token: string): Promise<PasswordResetRow | null> {
|
|
const [row] = await db
|
|
.select()
|
|
.from(passwordResets)
|
|
.where(eq(passwordResets.token, token))
|
|
.limit(1);
|
|
return row ?? null;
|
|
},
|
|
|
|
async create(data: {
|
|
userId: string;
|
|
token: string;
|
|
expiresAt: Date;
|
|
}): Promise<PasswordResetRow> {
|
|
const rows = await db.insert(passwordResets).values(data).returning();
|
|
const row = rows[0];
|
|
if (!row) throw new Error("Failed to create password reset");
|
|
return row;
|
|
},
|
|
|
|
async markUsed(id: string): Promise<void> {
|
|
await db
|
|
.update(passwordResets)
|
|
.set({ usedAt: new Date() })
|
|
.where(eq(passwordResets.id, id));
|
|
},
|
|
|
|
async deleteByUserId(userId: string): Promise<void> {
|
|
await db.delete(passwordResets).where(eq(passwordResets.userId, userId));
|
|
},
|
|
};
|