Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inicio #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.15",
"@types/jsonwebtoken": "^9.0.2",
"@types/knex": "^0.16.1",
"@types/node": "^18.11.18",
"@types/uuid": "^9.0.2",
"ts-node-dev": "^2.0.0",
"typescript": "^4.9.4"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.1",
"knex": "^2.4.2",
"sqlite3": "^5.1.4",
"uuid": "^9.0.0",
"zod": "^3.21.4"
}
}
24 changes: 16 additions & 8 deletions src/business/UserBusiness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { SignupInputDTO, SignupOutputDTO } from "../dtos/signup.dto"
import { BadRequestError } from "../errors/BadRequestError"
import { NotFoundError } from "../errors/NotFoundError"
import { USER_ROLES, User } from "../models/User"
import { IdGenerator } from "../services/IdGenerator"
import { TokenManager, TokenPayload } from "../services/TokenManager"

export class UserBusiness {
constructor(
private userDatabase: UserDatabase
private userDatabase: UserDatabase,
private idGenerator: IdGenerator,
private tokenManager: TokenManager
) { }

public getUsers = async (
Expand Down Expand Up @@ -39,13 +43,9 @@ export class UserBusiness {
public signup = async (
input: SignupInputDTO
): Promise<SignupOutputDTO> => {
const { id, name, email, password } = input
const { name, email, password } = input

const userDBExists = await this.userDatabase.findUserById(id)

if (userDBExists) {
throw new BadRequestError("'id' já existe")
}
const id = this.idGenerator.generate()

const newUser = new User(
id,
Expand All @@ -59,9 +59,17 @@ export class UserBusiness {
const newUserDB = newUser.toDBModel()
await this.userDatabase.insertUser(newUserDB)

const tokenPayload:TokenPayload = {
id: newUser.getId(),
name: newUser.getName(),
role: newUser.getRole()
}

const token = this.tokenManager.createToken(tokenPayload)

const output: SignupOutputDTO = {
message: "Cadastro realizado com sucesso",
token: "token"
token: token
}

return output
Expand Down
1 change: 0 additions & 1 deletion src/controller/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class UserController {
public signup = async (req: Request, res: Response) => {
try {
const input = SignupSchema.parse({
id: req.body.id,
name: req.body.name,
email: req.body.email,
password: req.body.password
Expand Down
Loading