chore(server): core IoC #66 - markUserAsVerifiedFactory
This commit is contained in:
@@ -78,6 +78,8 @@ export type UpdateUserServerRole = (params: {
|
||||
role: ServerRoles
|
||||
}) => Promise<boolean>
|
||||
|
||||
export type MarkUserAsVerified = (email: string) => Promise<boolean>
|
||||
|
||||
export type LegacyGetUserByEmail = (params: {
|
||||
email: string
|
||||
}) => Promise<Nullable<Omit<User, 'passwordDigest'>>>
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
LegacyGetUser,
|
||||
LegacyGetUserByEmail,
|
||||
ListPaginatedUsersPage,
|
||||
MarkUserAsVerified,
|
||||
SearchLimitedUsers,
|
||||
StoreUser,
|
||||
StoreUserAcl,
|
||||
@@ -194,21 +195,24 @@ export const getUserByEmailFactory =
|
||||
/**
|
||||
* Mark a user as verified by e-mail address, and return true on success
|
||||
*/
|
||||
export async function markUserAsVerified(email: string) {
|
||||
const UserCols = Users.with({ withoutTablePrefix: true }).col
|
||||
export const markUserAsVerifiedFactory =
|
||||
(deps: { db: Knex }): MarkUserAsVerified =>
|
||||
async (email: string) => {
|
||||
const UserCols = Users.with({ withoutTablePrefix: true }).col
|
||||
|
||||
const usersUpdate = await Users.knex()
|
||||
.whereRaw('lower(email) = lower(?)', [email])
|
||||
.update({
|
||||
[UserCols.verified]: true
|
||||
})
|
||||
const usersUpdate = await tables
|
||||
.users(deps.db)
|
||||
.whereRaw('lower(email) = lower(?)', [email])
|
||||
.update({
|
||||
[UserCols.verified]: true
|
||||
})
|
||||
|
||||
const userEmailsUpdate = await markUserEmailAsVerifiedFactory({
|
||||
updateUserEmail: updateUserEmailFactory({ db })
|
||||
})({ email: email.toLowerCase().trim() })
|
||||
const userEmailsUpdate = await markUserEmailAsVerifiedFactory({
|
||||
updateUserEmail: updateUserEmailFactory({ db: deps.db })
|
||||
})({ email: email.toLowerCase().trim() })
|
||||
|
||||
return !!(usersUpdate || userEmailsUpdate)
|
||||
}
|
||||
return !!(usersUpdate || userEmailsUpdate)
|
||||
}
|
||||
|
||||
export async function markOnboardingComplete(userId: string) {
|
||||
if (!userId) return false
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
legacyGetPaginatedUsersFactory,
|
||||
legacyGetUserByEmailFactory,
|
||||
listUsersFactory,
|
||||
markUserAsVerified,
|
||||
markUserAsVerifiedFactory,
|
||||
storeUserAclFactory,
|
||||
storeUserFactory
|
||||
} from '@/modules/core/repositories/users'
|
||||
@@ -84,6 +84,7 @@ const createUser = createUserFactory({
|
||||
const getUserByEmail = getUserByEmailFactory({ db })
|
||||
const legacyGetUserByEmail = legacyGetUserByEmailFactory({ db })
|
||||
const listUsers = listUsersFactory({ db })
|
||||
const markUserAsVerified = markUserAsVerifiedFactory({ db })
|
||||
|
||||
describe('Core @user-emails', () => {
|
||||
before(async () => {
|
||||
|
||||
@@ -7,15 +7,15 @@ import {
|
||||
deleteVerificationsFactory,
|
||||
getPendingTokenFactory
|
||||
} from '@/modules/emails/repositories'
|
||||
import { markUserAsVerified } from '@/modules/core/repositories/users'
|
||||
import { db } from '@/db/knex'
|
||||
import { markUserAsVerifiedFactory } from '@/modules/core/repositories/users'
|
||||
|
||||
export = (app: Express) => {
|
||||
app.get('/auth/verifyemail', async (req, res) => {
|
||||
try {
|
||||
const finalizeEmailVerification = finalizeEmailVerificationFactory({
|
||||
getPendingToken: getPendingTokenFactory({ db }),
|
||||
markUserAsVerified,
|
||||
markUserAsVerified: markUserAsVerifiedFactory({ db }),
|
||||
deleteVerifications: deleteVerificationsFactory({ db })
|
||||
})
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Optional } from '@speckle/shared'
|
||||
import { markUserAsVerified } from '@/modules/core/repositories/users'
|
||||
import { EmailVerificationFinalizationError } from '@/modules/emails/errors'
|
||||
import {
|
||||
DeleteVerifications,
|
||||
GetPendingToken
|
||||
} from '@/modules/emails/domain/operations'
|
||||
import { MarkUserAsVerified } from '@/modules/core/domain/users/operations'
|
||||
|
||||
type InitializeStateDeps = {
|
||||
getPendingToken: GetPendingToken
|
||||
@@ -27,7 +27,7 @@ const initializeState =
|
||||
type FinalizationState = Awaited<ReturnType<ReturnType<typeof initializeState>>>
|
||||
|
||||
type FinalizeVerificationDeps = {
|
||||
markUserAsVerified: typeof markUserAsVerified
|
||||
markUserAsVerified: MarkUserAsVerified
|
||||
deleteVerifications: DeleteVerifications
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user