chore(server): emails IoC 3 - deleteOldAndInsertNewVerificationFactory

This commit is contained in:
Kristaps Fabians Geikins
2024-09-10 10:07:56 +03:00
parent a270abbdd5
commit eafe0fa565
3 changed files with 27 additions and 19 deletions
@@ -6,3 +6,5 @@ export type GetPendingToken = (params: {
}) => Promise<EmailVerificationRecord | undefined>
export type DeleteVerifications = (email: string) => Promise<void>
export type DeleteOldAndInsertNewVerification = (email: string) => Promise<string>
@@ -1,6 +1,6 @@
import { db } from '@/db/knex'
import { EmailVerifications } from '@/modules/core/dbSchema'
import {
DeleteOldAndInsertNewVerification,
DeleteVerifications,
GetPendingToken
} from '@/modules/emails/domain/operations'
@@ -57,22 +57,24 @@ export const deleteVerificationsFactory =
/**
* Delete all previous verification entries and create a new one
*/
export async function deleteOldAndInsertNewVerification(email: string) {
if (!email) throw new InvalidArgumentError('E-mail address is empty')
export const deleteOldAndInsertNewVerificationFactory =
(deps: { db: Knex }): DeleteOldAndInsertNewVerification =>
async (email) => {
if (!email) throw new InvalidArgumentError('E-mail address is empty')
// delete all existing verifications first
await deleteVerificationsFactory({ db })(email)
// delete all existing verifications first
await deleteVerificationsFactory({ db: deps.db })(email)
// insert new one
const EmailVerificationCols = EmailVerifications.with({
withoutTablePrefix: true
}).col
// insert new one
const EmailVerificationCols = EmailVerifications.with({
withoutTablePrefix: true
}).col
const newId = cryptoRandomString({ length: 20 })
await EmailVerifications.knex().insert({
[EmailVerificationCols.id]: newId,
[EmailVerificationCols.email]: email
})
const newId = cryptoRandomString({ length: 20 })
await EmailVerifications.knex(deps.db).insert({
[EmailVerificationCols.id]: newId,
[EmailVerificationCols.email]: email
})
return newId
}
return newId
}
@@ -10,7 +10,7 @@ import {
import { getUser } from '@/modules/core/repositories/users'
import { getServerInfo } from '@/modules/core/services/generic'
import { EmailVerificationRequestError } from '@/modules/emails/errors'
import { deleteOldAndInsertNewVerification } from '@/modules/emails/repositories'
import { deleteOldAndInsertNewVerificationFactory } from '@/modules/emails/repositories'
import {
EmailTemplateParams,
renderEmail
@@ -43,7 +43,9 @@ async function createNewVerification(
if (user.verified)
throw new EmailVerificationRequestError("User's email is already verified")
const verificationId = await deleteOldAndInsertNewVerification(user.email)
const verificationId = await deleteOldAndInsertNewVerificationFactory({ db })(
user.email
)
return {
user,
@@ -80,7 +82,9 @@ const createNewEmailVerificationFactory =
'Unable to resolve verification target user'
)
const verificationId = await deleteOldAndInsertNewVerification(emailRecord.email)
const verificationId = await deleteOldAndInsertNewVerificationFactory({ db })(
emailRecord.email
)
return {
user,
email: emailRecord,