Files
Checkmate/server/src/db/mongo/modules/inviteModule.js
T

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "inviteModule";
2025-07-29 19:53:11 -07:00
class InviteModule {
constructor({ InviteToken, crypto, stringService }) {
this.InviteToken = InviteToken;
this.crypto = crypto;
this.stringService = stringService;
2025-04-20 11:29:53 -07:00
}
2025-07-29 19:53:11 -07:00
requestInviteToken = async (userData) => {
try {
await this.InviteToken.deleteMany({ email: userData.email });
userData.token = this.crypto.randomBytes(32).toString("hex");
let inviteToken = new this.InviteToken(userData);
await inviteToken.save();
return inviteToken;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "requestInviteToken";
throw error;
2025-04-20 11:29:53 -07:00
}
2025-07-29 19:53:11 -07:00
};
2025-04-20 11:29:53 -07:00
2025-07-29 19:53:11 -07:00
getInviteToken = async (token) => {
try {
const invite = await this.InviteToken.findOne({
token,
});
if (invite === null) {
throw new Error(this.stringService.authInviteNotFound);
}
return invite;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getInviteToken";
throw error;
2025-04-20 11:29:53 -07:00
}
2025-07-29 19:53:11 -07:00
};
getInviteTokenAndDelete = async (token) => {
try {
const invite = await this.InviteToken.findOneAndDelete({
token,
});
if (invite === null) {
throw new Error(this.stringService.authInviteNotFound);
}
return invite;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getInviteTokenAndDelete";
throw error;
}
};
}
2025-04-20 11:29:53 -07:00
2025-07-29 19:53:11 -07:00
export default InviteModule;