mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2026-01-08 05:02:13 +00:00
resend email in progress
This commit is contained in:
8
src/modules/users/entities/user.entity.ts
Normal file
8
src/modules/users/entities/user.entity.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Prisma } from '@prisma/client'
|
||||
|
||||
export class User implements Prisma.UserUncheckedCreateInput {
|
||||
id: string
|
||||
email: string
|
||||
password: string
|
||||
name: string
|
||||
}
|
||||
@@ -10,7 +10,8 @@ import { addHours } from 'date-fns'
|
||||
import { IUsersRepository } from '../services/users.service'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { PrismaService } from '../../../prisma.service'
|
||||
|
||||
import { pick } from 'remeda'
|
||||
import { Prisma } from '@prisma/client'
|
||||
@Injectable()
|
||||
export class UsersRepository implements IUsersRepository {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
@@ -38,14 +39,8 @@ export class UsersRepository implements IUsersRepository {
|
||||
}),
|
||||
])
|
||||
|
||||
console.log(users, 'usersFromBase')
|
||||
const totalPages = Math.ceil(totalItems / itemsPerPage)
|
||||
const usersView = users.map(u => ({
|
||||
id: u.id,
|
||||
name: u.name,
|
||||
email: u.email,
|
||||
}))
|
||||
console.log(usersView, 'users---')
|
||||
const usersView = users.map(u => pick(u, ['id', 'name', 'email', 'isEmailVerified']))
|
||||
return {
|
||||
totalPages,
|
||||
currentPage,
|
||||
@@ -56,6 +51,7 @@ export class UsersRepository implements IUsersRepository {
|
||||
}
|
||||
|
||||
async createUser(newUser: CreateUserInput): Promise<User | null> {
|
||||
console.log(newUser)
|
||||
return await this.prisma.user.create({
|
||||
data: {
|
||||
email: newUser.email,
|
||||
@@ -88,13 +84,16 @@ export class UsersRepository implements IUsersRepository {
|
||||
return result.count > 0
|
||||
}
|
||||
|
||||
async findUserById(id: string): Promise<User | null> {
|
||||
const user = await this.prisma.user.findUnique({ where: { id } })
|
||||
async findUserById(id: string, include?: Prisma.UserInclude) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
include,
|
||||
})
|
||||
if (!user) {
|
||||
return null
|
||||
}
|
||||
|
||||
return user
|
||||
return user as Prisma.UserGetPayload<{ include: typeof include }>
|
||||
}
|
||||
|
||||
async findUserByEmail(email: string): Promise<User | null> {
|
||||
|
||||
@@ -21,11 +21,12 @@ export class UsersService {
|
||||
|
||||
async createUser(name: string, password: string, email: string): Promise<UserViewType | null> {
|
||||
const passwordHash = await this._generateHash(password)
|
||||
const verificationToken = uuidv4()
|
||||
const newUser: CreateUserInput = {
|
||||
name: name || email.split('@')[0],
|
||||
email: email,
|
||||
password: passwordHash,
|
||||
verificationToken: uuidv4(),
|
||||
verificationToken,
|
||||
verificationTokenExpiry: addHours(new Date(), 24),
|
||||
isEmailVerified: false,
|
||||
}
|
||||
@@ -33,23 +34,32 @@ export class UsersService {
|
||||
if (!createdUser) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
await this.emailService.sendMail({
|
||||
from: 'andrii <andrii@andrii.es>',
|
||||
to: createdUser.email,
|
||||
text: 'hello and welcome',
|
||||
subject: 'E-mail confirmation ',
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
await this.sendConfirmationEmail({
|
||||
email: createdUser.email,
|
||||
name: createdUser.name,
|
||||
verificationToken: verificationToken,
|
||||
})
|
||||
return {
|
||||
id: createdUser.id,
|
||||
name: createdUser.name,
|
||||
email: createdUser.email,
|
||||
}
|
||||
}
|
||||
|
||||
async resendConfirmationEmail(userId: string) {
|
||||
const user = await this.usersRepository.findUserById(userId, { verification: true })
|
||||
if (!user) {
|
||||
return null
|
||||
}
|
||||
if (user.isEmailVerified) {
|
||||
return null
|
||||
}
|
||||
await this.sendConfirmationEmail({
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
verificationToken: user.verification.verificationToken,
|
||||
})
|
||||
return true
|
||||
}
|
||||
async deleteUserById(id: string): Promise<boolean> {
|
||||
return await this.usersRepository.deleteUserById(id)
|
||||
}
|
||||
@@ -70,7 +80,27 @@ export class UsersService {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private async sendConfirmationEmail({
|
||||
email,
|
||||
name,
|
||||
verificationToken,
|
||||
}: {
|
||||
email: string
|
||||
name: string
|
||||
verificationToken: string
|
||||
}) {
|
||||
try {
|
||||
await this.emailService.sendMail({
|
||||
from: 'andrii <andrii@andrii.es>',
|
||||
to: email,
|
||||
text: 'hello and welcome, token is: ' + verificationToken,
|
||||
html: `<b>Hello ${name}!</b><br/>Please confirm your email by clicking on the link below:<br/><a href="http://localhost:3000/confirm-email/${verificationToken}">Confirm email</a>`,
|
||||
subject: 'E-mail confirmation ',
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
private async _generateHash(password: string) {
|
||||
return await bcrypt.hash(password, 10)
|
||||
}
|
||||
@@ -88,7 +118,7 @@ export interface IUsersRepository {
|
||||
|
||||
deleteUserById(id: string): Promise<boolean>
|
||||
|
||||
findUserById(id: string): Promise<User | null>
|
||||
// findUserById(id: string): Promise<User | null>
|
||||
|
||||
revokeToken(id: string, token: string): Promise<User | null>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user