convert to ts
This commit is contained in:
1 parent
288ac08cd6
commit
3e8d4cb49b
4 files changed
+81
-2
No files matched your search
@@ -2,7 +2,9 @@ import type { Invite } from "@/types/index.js";
|
||||
|
||||
export interface IInvitesRepository {
|
||||
// create
|
||||
create(invite: Partial<Invite>): Promise<Invite>;
|
||||
// fetch
|
||||
findByToken(token: string): Promise<Invite>;
|
||||
findByTokenAndDelete(token: string): Promise<Invite>;
|
||||
// update
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { Invite } from "@/types/index.js";
|
||||
import { type InviteDocument, InviteModel } from "@/db/models/index.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
import mongoose from "mongoose";
|
||||
import crypto from "crypto";
|
||||
|
||||
class MongoInvitesRepository implements IInvitesRepository {
|
||||
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
|
||||
@@ -32,6 +33,23 @@ class MongoInvitesRepository implements IInvitesRepository {
|
||||
};
|
||||
};
|
||||
|
||||
create = async (invite: Partial<Invite>) => {
|
||||
await InviteModel.deleteMany({ email: invite.email });
|
||||
invite.token = crypto.randomBytes(32).toString("hex");
|
||||
let inviteToken = await InviteModel.create(invite);
|
||||
return this.toEntity(inviteToken);
|
||||
};
|
||||
|
||||
findByToken = async (token: string) => {
|
||||
const invite = await InviteModel.findOne({
|
||||
token,
|
||||
});
|
||||
if (invite === null) {
|
||||
throw new AppError({ message: "Invite not found", status: 404 });
|
||||
}
|
||||
return this.toEntity(invite);
|
||||
};
|
||||
|
||||
findByTokenAndDelete = async (token: string) => {
|
||||
const invite = await InviteModel.findOneAndDelete({
|
||||
token,
|
||||
|
||||
Reference in new issue
Block a user