invite service

This commit is contained in:
counterweight 2025-03-07 15:05:43 +01:00
parent 0da67c6104
commit 544a134eb3
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
2 changed files with 29 additions and 26 deletions

View file

@ -12,15 +12,19 @@ class ServicesProvider {
- also remember to get CLi fixed and working
*/
//done
const NostrServiceProvider = require('../services/nostrService');
const nostrService = new NostrServiceProvider({
models: models,
constants: constants,
errors: errors,
}).provide();
const InvitesServiceProvider = require('../services/invitesService');
const invitesService = new InvitesServiceProvider({
nostrService,
models: models,
errors: errors,
nostrService: nostrService,
}).provide();
const LoginServiceProvider = require('../services/loginService');

View file

@ -1,34 +1,33 @@
const uuid = require('uuid');
const models = require('../models');
const errors = require('../errors');
class InvitesServiceProvider {
constructor({ nostrService }) {
constructor({ models, errors, nostrService }) {
this.models = models;
this.errors = errors;
this.nostrService = nostrService;
}
provide() {
async function appInviteExists(inviteUuid) {
const invite = await models.AppInviteCreated.findOne({
const appInviteExists = async (inviteUuid) => {
const invite = await this.models.AppInviteCreated.findOne({
where: { uuid: inviteUuid },
});
if (invite) {
return true;
}
return false;
}
};
async function getAppInvite(inviteUuid) {
const invite = await models.AppInviteCreated.findOne({
const getAppInvite = async (inviteUuid) => {
const invite = await this.models.AppInviteCreated.findOne({
where: { uuid: inviteUuid },
});
return invite;
}
};
async function isAppInviteSpent(appInviteUuid) {
const isAppInviteSpent = async (appInviteUuid) => {
const signUpChallengeCompleted =
await models.SignUpChallengeCompleted.findOne({
await this.models.SignUpChallengeCompleted.findOne({
where: {
app_invite_uuid: appInviteUuid,
},
@ -38,28 +37,28 @@ class InvitesServiceProvider {
return true;
}
return false;
}
};
async function createAppInvite(inviterPubKey) {
return await models.AppInviteCreated.create({
const createAppInvite = async (inviterPubKey) => {
return await this.models.AppInviteCreated.create({
uuid: uuid.v7(),
inviter_pub_key: inviterPubKey,
created_at: new Date().toISOString(),
});
}
};
const createSignUpChallenge = async (appInviteUuid) => {
if (!(await appInviteExists(appInviteUuid))) {
throw new errors.NotFoundError("Invite doesn't exist.");
throw new this.errors.NotFoundError("Invite doesn't exist.");
}
if (await isAppInviteSpent(appInviteUuid)) {
throw new errors.AlreadyUsedError('Invite has already been used.');
throw new this.errors.AlreadyUsedError('Invite has already been used.');
}
const nostrChallenge = await this.nostrService.createNostrChallenge();
return await models.SignUpChallengeCreated.create({
return await this.models.SignUpChallengeCreated.create({
uuid: uuid.v7(),
nostr_challenge_uuid: nostrChallenge.uuid,
app_invite_uuid: appInviteUuid,
@ -78,14 +77,14 @@ class InvitesServiceProvider {
challenge
);
const signUpChallenge = await models.SignUpChallengeCreated.findOne({
const signUpChallenge = await this.models.SignUpChallengeCreated.findOne({
where: {
nostr_challenge_uuid: nostrChallenge.uuid,
},
});
if (await this.nostrService.hasNostrChallengeBeenCompleted(challenge)) {
throw new errors.AlreadyUsedError(
throw new this.errors.AlreadyUsedError(
'This challenge has already been used.'
);
}
@ -94,7 +93,7 @@ class InvitesServiceProvider {
await this.nostrService.verifyNostrChallenge(signedEvent);
const completedSignUpChallenge =
await models.SignUpChallengeCompleted.create({
await this.models.SignUpChallengeCompleted.create({
uuid: uuid.v7(),
nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
app_invite_uuid: signUpChallenge.app_invite_uuid,
@ -105,9 +104,9 @@ class InvitesServiceProvider {
return completedSignUpChallenge;
};
async function isPublicKeySignedUp(publicKey) {
const isPublicKeySignedUp = async (publicKey) => {
const signUpChallengeCompleted =
await models.SignUpChallengeCompleted.findOne({
await this.models.SignUpChallengeCompleted.findOne({
where: {
public_key: publicKey,
},
@ -118,7 +117,7 @@ class InvitesServiceProvider {
}
return false;
}
};
return {
appInviteExists,