1+ import { IdGenerator } from "../services/IdGenerator" ;
2+ import { HashManager } from "../services/HashManager" ;
3+ import { UserDatabase } from "../data/UserDatabase" ;
4+ import { Authenticator } from "../services/Authenticator" ;
5+ import { UserRelationDatabase } from "../data/UserRelationDatabase" ;
6+ import { FeedDatabase } from "../data/FeedDatabase" ;
7+
8+ export class UserBusiness {
9+
10+ public async signUp ( name : string , email : string , password : string ) : Promise < string > {
11+
12+ if ( ! name || ! email || ! password ) {
13+ throw new Error ( 'Insira todas as informações necessárias para o cadastro' ) ;
14+ }
15+
16+ if ( password . length < 6 ) {
17+ throw new Error ( 'A senha deve conter no mínimo seis caracteres' ) ;
18+ }
19+
20+ const idGenerator = new IdGenerator ( ) ;
21+ const id = idGenerator . generateId ( ) ;
22+
23+ const hashManager = new HashManager ( ) ;
24+ const hashPassword = await hashManager . hash ( password ) ;
25+
26+ const userDataBase = new UserDatabase ( ) ;
27+ await userDataBase . registerUser (
28+ id ,
29+ name ,
30+ email ,
31+ hashPassword
32+ ) ;
33+
34+ const authenticator = new Authenticator ( ) ;
35+ const token = authenticator . generateToken ( { id } ) ;
36+
37+ return token ;
38+ }
39+
40+
41+ public async makeFriend ( token : string , makeFriendshipUserId : string ) : Promise < void > {
42+
43+ const authenticator = new Authenticator ;
44+ const authenticationData = authenticator . verify ( token )
45+ const userId = authenticationData . id
46+
47+ if ( ! makeFriendshipUserId ) {
48+ throw new Error ( "Insira um ID válido" )
49+ }
50+
51+ const userDatabase = new UserDatabase ( ) ;
52+ const user = await userDatabase . getUserById ( makeFriendshipUserId )
53+
54+ if ( ! user ) {
55+ throw new Error ( "Usuário não existe" )
56+ }
57+
58+ const userRelationDatabase = new UserRelationDatabase ( ) ;
59+ await userRelationDatabase . makeFriend ( userId , makeFriendshipUserId )
60+ await userRelationDatabase . makeFriend ( makeFriendshipUserId , userId )
61+
62+ }
63+
64+ public async undoFriend ( token : string , undoFriendshipUserId : string ) : Promise < void > {
65+
66+ const authenticator = new Authenticator ;
67+ const authenticationData = authenticator . verify ( token )
68+ const userId = authenticationData . id
69+
70+ if ( ! undoFriendshipUserId ) {
71+ throw new Error ( "Insira um ID válido" )
72+ }
73+
74+ const userDatabase = new UserDatabase ( ) ;
75+ const user = await userDatabase . getUserById ( undoFriendshipUserId )
76+
77+ if ( ! user ) {
78+ throw new Error ( "Usuário não existe" )
79+ }
80+
81+ const userRelationDatabase = new UserRelationDatabase ( ) ;
82+ await userRelationDatabase . undoFriend ( userId , undoFriendshipUserId )
83+ await userRelationDatabase . undoFriend ( undoFriendshipUserId , userId )
84+ }
85+ async getUserByEmail ( user :any ) {
86+
87+ const userDatabase = new UserDatabase ( ) ;
88+ const userFromDB = await userDatabase . getUserByEmail ( user . email ) ;
89+
90+ if ( userFromDB === undefined ) {
91+ throw new Error ( 'Email ou senha incorretos' ) ;
92+ }
93+
94+ const hashManager = new HashManager ( ) ;
95+ const hashCompare = await hashManager . compare ( user . password , userFromDB . password ) ;
96+
97+ const authenticator = new Authenticator ( ) ;
98+ const accessToken = authenticator . generateToken ( { id : userFromDB . id } ) ;
99+
100+
101+ if ( ! hashCompare ) {
102+ throw new Error ( "Email ou senha incorretos" ) ;
103+ }
104+
105+ return accessToken ;
106+
107+ }
108+
109+ async get ( token :string ) {
110+ const authenticator = new Authenticator ( ) ;
111+ const authenticationData = authenticator . verify ( token ) ;
112+ const userId = authenticationData . id ;
113+
114+ const feedDatabase = new FeedDatabase ( ) ;
115+ const feed = await feedDatabase . getFeed ( userId ) ;
116+ const mappedFeed = feed . map ( ( item :any ) => ( {
117+ title : item . title ,
118+ photoPost : item . photoPost ,
119+ description : item . description
120+ } ) )
121+
122+ return mappedFeed
123+ }
124+ }
0 commit comments