feat(user-emails): mark user email as verified
This commit is contained in:
Родитель
dab5b7b9de
Коммит
bb964cd457
|
@ -8,6 +8,7 @@ import { Knex } from 'knex'
|
|||
import { Roles, ServerRoles } from '@speckle/shared'
|
||||
import { updateUserEmailFactory } from '@/modules/user-emails/repositories/userEmails'
|
||||
import knexInstance from '@/db/knex'
|
||||
import { markUserEmailAsVerifiedFactory } from '@/modules/user-emails/services/verification'
|
||||
|
||||
export type UserWithOptionalRole<User extends LimitedUserRecord = UserRecord> = User & {
|
||||
/**
|
||||
|
@ -163,6 +164,10 @@ export async function markUserAsVerified(email: string) {
|
|||
[UserCols.verified]: true
|
||||
})
|
||||
|
||||
await markUserEmailAsVerifiedFactory({
|
||||
updateUserEmail: updateUserEmailFactory({ db: knexInstance })
|
||||
})(email.toLowerCase().trim())
|
||||
|
||||
return !!(await q)
|
||||
}
|
||||
|
||||
|
|
|
@ -2,10 +2,13 @@ import { before } from 'mocha'
|
|||
import { createUser } from '@/modules/core/services/users'
|
||||
import { beforeEachContext } from '@/test/hooks'
|
||||
import { expect } from 'chai'
|
||||
import { getUserByEmail } from '@/modules/core/repositories/users'
|
||||
import { getUserByEmail, markUserAsVerified } from '@/modules/core/repositories/users'
|
||||
import { deleteUserEmailFactory } from '@/modules/user-emails/repositories/userEmails'
|
||||
import knexInstance from '@/db/knex'
|
||||
import { createRandomEmail, createRandomPassword } from '../../helpers/test-helpers'
|
||||
import { USER_EMAILS_TABLE_NAME } from '@/modules/user-emails/constants'
|
||||
|
||||
const userEmailTable = knexInstance(USER_EMAILS_TABLE_NAME)
|
||||
|
||||
describe('Core @user-emails', () => {
|
||||
before(async () => {
|
||||
|
@ -34,4 +37,20 @@ describe('Core @user-emails', () => {
|
|||
expect(user.email).to.eq(email)
|
||||
})
|
||||
})
|
||||
|
||||
describe('markUserEmailAsVerified', () => {
|
||||
it('should mark user email as verified', async () => {
|
||||
const email = createRandomEmail()
|
||||
await createUser({
|
||||
name: 'John Doe',
|
||||
email,
|
||||
password: createRandomPassword()
|
||||
})
|
||||
|
||||
await markUserAsVerified(email)
|
||||
|
||||
const userEmail = await userEmailTable.where({ email }).first()
|
||||
expect(userEmail.verified).to.be.true
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -6,9 +6,9 @@ export type CreateUserEmail = (
|
|||
|
||||
export type UpdateUserEmail = (
|
||||
query:
|
||||
| (Pick<UserEmail, 'email' | 'userId'> & { primary?: boolean })
|
||||
| (Pick<UserEmail, 'email'> & { primary?: boolean })
|
||||
| (Pick<UserEmail, 'userId'> & { primary: true }),
|
||||
update: Pick<Partial<UserEmail>, 'email' | 'primary'>
|
||||
update: Pick<Partial<UserEmail>, 'email' | 'primary' | 'verified'>
|
||||
) => Promise<UserEmail>
|
||||
|
||||
export type DeleteUserEmail = ({
|
||||
|
@ -18,3 +18,5 @@ export type DeleteUserEmail = ({
|
|||
userId: string
|
||||
email: string
|
||||
}) => Promise<boolean>
|
||||
|
||||
export type MarkUserEmailAsVerified = (email: string) => Promise<UserEmail>
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
import { MarkUserEmailAsVerified, UpdateUserEmail } from '../domain/operations'
|
||||
|
||||
export const markUserEmailAsVerifiedFactory =
|
||||
({
|
||||
updateUserEmail
|
||||
}: {
|
||||
updateUserEmail: UpdateUserEmail
|
||||
}): MarkUserEmailAsVerified =>
|
||||
async (email) =>
|
||||
updateUserEmail({ email }, { verified: true })
|
|
@ -0,0 +1,32 @@
|
|||
import { describe } from 'mocha'
|
||||
import { updateUserEmailFactory } from '../../repositories/userEmails'
|
||||
import knexInstance from '@/db/knex'
|
||||
import { createUser } from '@/modules/core/services/users'
|
||||
import {
|
||||
createRandomEmail,
|
||||
createRandomPassword
|
||||
} from '@/modules/core/helpers/test-helpers'
|
||||
import { USER_EMAILS_TABLE_NAME } from '../../constants'
|
||||
import { markUserEmailAsVerifiedFactory } from '../../services/verification'
|
||||
import { expect } from 'chai'
|
||||
|
||||
const userEmailTable = knexInstance(USER_EMAILS_TABLE_NAME)
|
||||
|
||||
describe('Verification @user-emails', () => {
|
||||
it('should mark user email as verified', async () => {
|
||||
const email = createRandomEmail()
|
||||
|
||||
await createUser({
|
||||
name: 'John',
|
||||
email,
|
||||
password: createRandomPassword()
|
||||
})
|
||||
|
||||
await markUserEmailAsVerifiedFactory({
|
||||
updateUserEmail: updateUserEmailFactory({ db: knexInstance })
|
||||
})(email)
|
||||
|
||||
const userEmail = await userEmailTable.where({ email }).first()
|
||||
expect(userEmail.verified).to.be.true
|
||||
})
|
||||
})
|
Загрузка…
Ссылка в новой задаче