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 - also remember to get CLi fixed and working
*/ */
//done
const NostrServiceProvider = require('../services/nostrService'); const NostrServiceProvider = require('../services/nostrService');
const nostrService = new NostrServiceProvider({ const nostrService = new NostrServiceProvider({
models: models, models: models,
constants: constants, constants: constants,
errors: errors, errors: errors,
}).provide(); }).provide();
const InvitesServiceProvider = require('../services/invitesService'); const InvitesServiceProvider = require('../services/invitesService');
const invitesService = new InvitesServiceProvider({ const invitesService = new InvitesServiceProvider({
nostrService, models: models,
errors: errors,
nostrService: nostrService,
}).provide(); }).provide();
const LoginServiceProvider = require('../services/loginService'); const LoginServiceProvider = require('../services/loginService');

View file

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