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

View file

@ -2,15 +2,14 @@ const uuid = require('uuid');
const nostrService = require('./nostrService');
const invitesService = require('./invitesService');
const LoginChallengeCreated = require('../models/LoginChallengeCreated');
const LoginChallengeCompleted = require('../models/LoginChallengeCompleted');
const models = require('../models');
const errors = require('../errors');
async function createLoginChallenge() {
const nostrChallenge = await nostrService.createNostrChallenge();
return await LoginChallengeCreated.create({
return await models.LoginChallengeCreated.create({
uuid: uuid.v7(),
nostr_challenge_uuid: nostrChallenge.uuid,
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(),
nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
public_key: completedNostrChallenge.public_key,

View file

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

View file

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

View file

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

View file

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