diff --git a/src/services/index.js b/src/services/index.js index 321fa54..04d47a3 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -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'); diff --git a/src/services/invitesService.js b/src/services/invitesService.js index 6b3fde0..df357e3 100644 --- a/src/services/invitesService.js +++ b/src/services/invitesService.js @@ -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,