refactor model imports in all services

This commit is contained in:
counterweight 2025-03-05 16:49:03 +01:00
parent 011a255d9e
commit d34e62070a
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
6 changed files with 69 additions and 69 deletions

View file

@ -1,14 +1,11 @@
const uuid = require('uuid'); const uuid = require('uuid');
const nostrService = require('./nostrService'); const nostrService = require('./nostrService');
const AppInviteCreated = require('../models/AppInviteCreated'); const models = require('../models');
const SignUpChallengeCreated = require('../models/SignUpChallengeCreated');
const SignUpChallengeCompleted = require('../models/SignUpChallengeCompleted');
const errors = require('../errors'); const errors = require('../errors');
async function appInviteExists(inviteUuid) { async function appInviteExists(inviteUuid) {
const invite = await AppInviteCreated.findOne({ const invite = await models.AppInviteCreated.findOne({
where: { uuid: inviteUuid }, where: { uuid: inviteUuid },
}); });
if (invite) { if (invite) {
@ -18,18 +15,19 @@ async function appInviteExists(inviteUuid) {
} }
async function getAppInvite(inviteUuid) { async function getAppInvite(inviteUuid) {
const invite = await AppInviteCreated.findOne({ const invite = await models.AppInviteCreated.findOne({
where: { uuid: inviteUuid }, where: { uuid: inviteUuid },
}); });
return invite; return invite;
} }
async function isAppInviteSpent(appInviteUuid) { async function isAppInviteSpent(appInviteUuid) {
const signUpChallengeCompleted = await SignUpChallengeCompleted.findOne({ const signUpChallengeCompleted =
where: { await models.SignUpChallengeCompleted.findOne({
app_invite_uuid: appInviteUuid, where: {
}, app_invite_uuid: appInviteUuid,
}); },
});
if (signUpChallengeCompleted) { if (signUpChallengeCompleted) {
return true; return true;
@ -38,7 +36,7 @@ async function isAppInviteSpent(appInviteUuid) {
} }
async function createAppInvite(inviterPubKey) { async function createAppInvite(inviterPubKey) {
return await AppInviteCreated.create({ return await 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(),
@ -56,7 +54,7 @@ async function createSignUpChallenge(appInviteUuid) {
const nostrChallenge = await nostrService.createNostrChallenge(); const nostrChallenge = await nostrService.createNostrChallenge();
return await SignUpChallengeCreated.create({ return await 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,
@ -70,7 +68,7 @@ async function verifySignUpChallenge(signedEvent) {
const nostrChallenge = await nostrService.getNostrChallenge(null, challenge); const nostrChallenge = await nostrService.getNostrChallenge(null, challenge);
const signUpChallenge = await SignUpChallengeCreated.findOne({ const signUpChallenge = await models.SignUpChallengeCreated.findOne({
where: { where: {
nostr_challenge_uuid: nostrChallenge.uuid, nostr_challenge_uuid: nostrChallenge.uuid,
}, },
@ -83,23 +81,26 @@ async function verifySignUpChallenge(signedEvent) {
const completedNostrChallenge = const completedNostrChallenge =
await nostrService.verifyNostrChallenge(signedEvent); await nostrService.verifyNostrChallenge(signedEvent);
const completedSignUpChallenge = await SignUpChallengeCompleted.create({ const completedSignUpChallenge = await models.SignUpChallengeCompleted.create(
uuid: uuid.v7(), {
nostr_challenge_completed_uuid: completedNostrChallenge.uuid, uuid: uuid.v7(),
app_invite_uuid: signUpChallenge.app_invite_uuid, nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
public_key: completedNostrChallenge.public_key, app_invite_uuid: signUpChallenge.app_invite_uuid,
created_at: new Date().toISOString(), public_key: completedNostrChallenge.public_key,
}); created_at: new Date().toISOString(),
}
);
return completedSignUpChallenge; return completedSignUpChallenge;
} }
async function isPublicKeySignedUp(publicKey) { async function isPublicKeySignedUp(publicKey) {
const signUpChallengeCompleted = await SignUpChallengeCompleted.findOne({ const signUpChallengeCompleted =
where: { await models.SignUpChallengeCompleted.findOne({
public_key: publicKey, where: {
}, public_key: publicKey,
}); },
});
if (signUpChallengeCompleted) { if (signUpChallengeCompleted) {
return true; return true;

View file

@ -2,15 +2,14 @@ const uuid = require('uuid');
const nostrService = require('./nostrService'); const nostrService = require('./nostrService');
const invitesService = require('./invitesService'); const invitesService = require('./invitesService');
const LoginChallengeCreated = require('../models/LoginChallengeCreated'); const models = require('../models');
const LoginChallengeCompleted = require('../models/LoginChallengeCompleted');
const errors = require('../errors'); const errors = require('../errors');
async function createLoginChallenge() { async function createLoginChallenge() {
const nostrChallenge = await nostrService.createNostrChallenge(); const nostrChallenge = await nostrService.createNostrChallenge();
return await LoginChallengeCreated.create({ return await models.LoginChallengeCreated.create({
uuid: uuid.v7(), uuid: uuid.v7(),
nostr_challenge_uuid: nostrChallenge.uuid, nostr_challenge_uuid: nostrChallenge.uuid,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
@ -39,7 +38,7 @@ async function verifyLoginChallenge(signedEvent) {
); );
} }
const completedLoginChallenge = await LoginChallengeCompleted.create({ const completedLoginChallenge = await models.LoginChallengeCompleted.create({
uuid: uuid.v7(), uuid: uuid.v7(),
nostr_challenge_completed_uuid: completedNostrChallenge.uuid, nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
public_key: completedNostrChallenge.public_key, public_key: completedNostrChallenge.public_key,

View file

@ -3,8 +3,7 @@ const crypto = require('crypto');
const { Op, TimeoutError } = require('sequelize'); const { Op, TimeoutError } = require('sequelize');
const { verifyEvent } = require('nostr-tools'); const { verifyEvent } = require('nostr-tools');
const NostrChallengeCreated = require('../models/NostrChallengeCreated'); const models = require('../models');
const NostrChallengeCompleted = require('../models/NostrChallengeCompleted');
const constants = require('../constants'); const constants = require('../constants');
const errors = require('../errors'); const errors = require('../errors');
@ -17,7 +16,7 @@ async function createNostrChallenge() {
constants.DEFAULT_NOSTR_CHALLENGE_DURATION_SECONDS constants.DEFAULT_NOSTR_CHALLENGE_DURATION_SECONDS
); );
const nostrChallenge = await NostrChallengeCreated.create({ const nostrChallenge = await models.NostrChallengeCreated.create({
uuid: uuid.v7(), uuid: uuid.v7(),
challenge: crypto.randomBytes(32).toString('hex'), challenge: crypto.randomBytes(32).toString('hex'),
expires_at: expiryTimestamp.toISOString(), expires_at: expiryTimestamp.toISOString(),
@ -29,7 +28,7 @@ async function createNostrChallenge() {
async function getNostrChallenge(nostrChallengeUuid = null, challenge = null) { async function getNostrChallenge(nostrChallengeUuid = null, challenge = null) {
if (nostrChallengeUuid) { if (nostrChallengeUuid) {
return await NostrChallengeCreated.findOne({ return await models.NostrChallengeCreated.findOne({
where: { where: {
uuid: nostrChallengeUuid, uuid: nostrChallengeUuid,
}, },
@ -37,7 +36,7 @@ async function getNostrChallenge(nostrChallengeUuid = null, challenge = null) {
} }
if (challenge) { if (challenge) {
return await NostrChallengeCreated.findOne({ return await models.NostrChallengeCreated.findOne({
where: { where: {
challenge, challenge,
}, },
@ -66,7 +65,7 @@ async function verifyNostrChallenge(signedEvent) {
throw new errors.InvalidSignatureError('Signature is not valid.'); throw new errors.InvalidSignatureError('Signature is not valid.');
} }
return await NostrChallengeCompleted.create({ return await models.NostrChallengeCompleted.create({
uuid: uuid.v7(), uuid: uuid.v7(),
challenge: challenge, challenge: challenge,
signed_event: signedEvent, signed_event: signedEvent,
@ -76,7 +75,7 @@ async function verifyNostrChallenge(signedEvent) {
} }
async function isNostrChallengeFresh(challengeString) { async function isNostrChallengeFresh(challengeString) {
const nostrChallenge = await NostrChallengeCreated.findOne({ const nostrChallenge = await models.NostrChallengeCreated.findOne({
where: { where: {
challenge: challengeString, challenge: challengeString,
expires_at: { expires_at: {
@ -92,7 +91,7 @@ async function isNostrChallengeFresh(challengeString) {
} }
async function hasNostrChallengeBeenCompleted(challengeString) { async function hasNostrChallengeBeenCompleted(challengeString) {
const completedNostrChallenge = await NostrChallengeCompleted.findOne({ const completedNostrChallenge = await models.NostrChallengeCompleted.findOne({
where: { where: {
challenge: challengeString, challenge: challengeString,
}, },

View file

@ -1,19 +1,17 @@
const uuid = require('uuid'); const uuid = require('uuid');
const OfferCreated = require('../models/OfferCreated'); const models = require('../models');
const OfferDetailsSet = require('../models/OfferDetailsSet');
const OfferDeleted = require('../models/OfferDeleted');
const errors = require('../errors'); const errors = require('../errors');
async function createOffer(publicKey, offerDetails) { async function createOffer(publicKey, offerDetails) {
const offerCreated = await OfferCreated.create({ const offerCreated = await models.OfferCreated.create({
uuid: uuid.v7(), uuid: uuid.v7(),
public_key: publicKey, public_key: publicKey,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
}); });
await OfferDetailsSet.create({ await models.OfferDetailsSet.create({
uuid: uuid.v7(), uuid: uuid.v7(),
offer_uuid: offerCreated.uuid, offer_uuid: offerCreated.uuid,
wants: offerDetails.wants, wants: offerDetails.wants,
@ -33,17 +31,17 @@ async function createOffer(publicKey, offerDetails) {
async function deleteOffer(offerUuid) { async function deleteOffer(offerUuid) {
const offerExists = Boolean( const offerExists = Boolean(
await OfferCreated.findOne({ where: { uuid: offerUuid } }) await models.OfferCreated.findOne({ where: { uuid: offerUuid } })
); );
const offerHasBeenDeleted = Boolean( const offerHasBeenDeleted = Boolean(
await OfferDeleted.findOne({ where: { offer_uuid: offerUuid } }) await models.OfferDeleted.findOne({ where: { offer_uuid: offerUuid } })
); );
if (!offerExists || offerHasBeenDeleted) { if (!offerExists || offerHasBeenDeleted) {
throw new errors.NotFoundError(`Could not find the offer.`); throw new errors.NotFoundError(`Could not find the offer.`);
} }
return OfferDeleted.create({ return models.OfferDeleted.create({
uuid: uuid.v7(), uuid: uuid.v7(),
offer_uuid: offerUuid, offer_uuid: offerUuid,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
@ -51,7 +49,7 @@ async function deleteOffer(offerUuid) {
} }
async function getOffersByPublicKey(publicKey) { async function getOffersByPublicKey(publicKey) {
const offers = await OfferCreated.findAll({ const offers = await models.OfferCreated.findAll({
where: { where: {
public_key: publicKey, public_key: publicKey,
}, },
@ -66,7 +64,7 @@ async function getOffersByPublicKey(publicKey) {
const offersToReturn = []; const offersToReturn = [];
if (offers) { if (offers) {
for (const someOffer of offers) { for (const someOffer of offers) {
const offerDetails = await OfferDetailsSet.findOne({ const offerDetails = await models.OfferDetailsSet.findOne({
where: { where: {
offer_uuid: someOffer.uuid, offer_uuid: someOffer.uuid,
}, },

View file

@ -1,9 +1,9 @@
const uuid = require('uuid'); const uuid = require('uuid');
const ContactDetailsSet = require('../models/ContactDetailsSet');
const NymSet = require('../models/NymSet'); const models = require('../models');
async function setContactDetails(publicKey, encryptedContactDetails) { async function setContactDetails(publicKey, encryptedContactDetails) {
return await ContactDetailsSet.create({ return await models.ContactDetailsSet.create({
uuid: uuid.v7(), uuid: uuid.v7(),
public_key: publicKey, public_key: publicKey,
encrypted_contact_details: encryptedContactDetails, encrypted_contact_details: encryptedContactDetails,
@ -12,7 +12,7 @@ async function setContactDetails(publicKey, encryptedContactDetails) {
} }
async function setNym(publicKey, nym) { async function setNym(publicKey, nym) {
return await NymSet.create({ return await models.NymSet.create({
uuid: uuid.v7(), uuid: uuid.v7(),
public_key: publicKey, public_key: publicKey,
nym: nym, nym: nym,
@ -21,8 +21,10 @@ async function setNym(publicKey, nym) {
} }
async function areProfileDetailsComplete(publicKey) { async function areProfileDetailsComplete(publicKey) {
const isNymSet = await NymSet.findOne({ where: { public_key: publicKey } }); const isNymSet = await models.NymSet.findOne({
const areContactDetailsSet = await ContactDetailsSet.findOne({ where: { public_key: publicKey },
});
const areContactDetailsSet = await models.ContactDetailsSet.findOne({
where: { where: {
public_key: publicKey, public_key: publicKey,
}, },

View file

@ -1,7 +1,6 @@
const uuid = require('uuid'); const uuid = require('uuid');
const SessionCreated = require('../models/SessionCreated'); const models = require('../models');
const SessionRelatedToPublickey = require('../models/SessionRelatedToPublickey');
const invitesService = require('./invitesService'); const invitesService = require('./invitesService');
const constants = require('../constants'); const constants = require('../constants');
@ -13,7 +12,7 @@ async function createSession(sessionUuid) {
expiryTimestamp.getSeconds() + constants.DEFAULT_SESSION_DURATION_SECONDS expiryTimestamp.getSeconds() + constants.DEFAULT_SESSION_DURATION_SECONDS
); );
return await SessionCreated.create({ return await models.SessionCreated.create({
uuid: sessionUuid, uuid: sessionUuid,
created_at: currentTimestamp.toISOString(), created_at: currentTimestamp.toISOString(),
expires_at: expiryTimestamp.toISOString(), expires_at: expiryTimestamp.toISOString(),
@ -21,7 +20,7 @@ async function createSession(sessionUuid) {
} }
async function isSessionValid(sessionUuid) { async function isSessionValid(sessionUuid) {
const currentSession = await SessionCreated.findOne({ const currentSession = await models.SessionCreated.findOne({
where: { where: {
uuid: sessionUuid, uuid: sessionUuid,
}, },
@ -47,7 +46,7 @@ async function relateSessionToPublicKey(sessionUuid, publicKey) {
throw Error('Public key is not signed up.'); throw Error('Public key is not signed up.');
} }
return SessionRelatedToPublickey.create({ return models.SessionRelatedToPublickey.create({
uuid: uuid.v7(), uuid: uuid.v7(),
session_uuid: sessionUuid, session_uuid: sessionUuid,
public_key: publicKey, public_key: publicKey,
@ -56,11 +55,12 @@ async function relateSessionToPublicKey(sessionUuid, publicKey) {
} }
async function isSessionAuthorized(sessionUuid) { async function isSessionAuthorized(sessionUuid) {
const isSessionRelatedToPublicKey = await SessionRelatedToPublickey.findOne({ const isSessionRelatedToPublicKey =
where: { await models.SessionRelatedToPublickey.findOne({
session_uuid: sessionUuid, where: {
}, session_uuid: sessionUuid,
}); },
});
if (isSessionRelatedToPublicKey) { if (isSessionRelatedToPublicKey) {
return true; return true;
@ -70,11 +70,12 @@ async function isSessionAuthorized(sessionUuid) {
} }
async function getPublicKeyRelatedToSession(sessionUuid) { async function getPublicKeyRelatedToSession(sessionUuid) {
const sessionRelatedToPublickey = await SessionRelatedToPublickey.findOne({ const sessionRelatedToPublickey =
where: { await models.SessionRelatedToPublickey.findOne({
session_uuid: sessionUuid, where: {
}, session_uuid: sessionUuid,
}); },
});
if (sessionRelatedToPublickey) { if (sessionRelatedToPublickey) {
return sessionRelatedToPublickey.public_key; return sessionRelatedToPublickey.public_key;