You are on page 1of 3

import { Injectable } from '@angular/core';

import { HttpClient, HttpHeaders } from '@angular/common/http';


import * as io from 'socket.io-client'
import { Observable } from 'rxjs/Observable';

@Injectable()
export class UserService {
imageprofile: string;
token;
socket: any;
constructor(private http: HttpClient) {

this.socket = io('http://localhost:3000')
this.setSession();
}
setSession() {
if (localStorage.getItem('token'))
this.token = localStorage.getItem('token');
else if (sessionStorage.getItem('token'))
this.token = sessionStorage.getItem('token');
}
newNotification() {
let observable = new Observable<{ userid: string, notification: {} }
>(observer => {
this.socket.on('sendnotification', (data) => {
observer.next(data);
});
return () => this.socket.disconnect()

})
return observable;
}
getUserByToken() {
// console.log(this.token);
const headers = new HttpHeaders().set('Authorization', this.token);
return this.http.get('http://localhost:3000/user/bytoken', {
headers: headers,
});
}

getUser(id: string) {
return this.http.get(`http://localhost:3000/user/${id}`);
}

userLogin(username, password) {
return this.http.post('http://localhost:3000/user/login', {
username: username,
password: password,
});
}

getUsers() {
return this.http.get('http://localhost:3000/user');
}

addUser(info) {
return this.http.post('http://localhost:3000/user', info);
}
updateProfileImage(fd: FormData) {
const headers = new HttpHeaders().set('Authorization', this.token);
return this.http.patch(
'http://localhost:3000/user/userimage',
fd,
{
headers: headers,
}
);
}
deleteNotification(id: string) {
const headers = new HttpHeaders().set('Authorization', this.token);
return this.http.delete(
'http://localhost:3000/user/notifications/id_' + id,
{
headers: headers,
}
);
}
deleteNotifications() {
const headers = new HttpHeaders().set('Authorization', this.token);
return this.http.delete(
'http://localhost:3000/user/notifications',
{
headers: headers,
}
);
}

updateUser(propName: string, value: string) {


const headers = new HttpHeaders().set('Authorization', this.token);

return this.http.patch(
'http://localhost:3000/user',
[{ propName: propName, value: value }],
{ headers: headers }
);
}
getManagers() {
return this.http.get('http://localhost:3000/user/managers');
}

getManagerInfo() {
const headers = new HttpHeaders().set('Authorization', this.token);

return this.http.get('http://localhost:3000/user/informations', {
headers: headers,
});
}
confirmAccount(token: string) {
return this.http.post(
'http://localhost:3000/user/confirmation',
{ token },
{
observe: 'response',
}
);
}
sendEmail(email: string) {
return this.http.post(
'http://localhost:3000/user/sendconfirmation',
{ email },
{
observe: 'response',
}
);
}
//updatePassword(oldPass, NewPass)patch{}
resetPassword(email) {
return this.http.post(
'http://localhost:3000/user/reset-password-mail',
{ email },
{
observe: 'response',
}
);
}
resetPasswordAuth(token: string) {
return this.http.get(
`http://localhost:3000/user/reset-password-auhorization/${token}`,
{
observe: 'response',
}
);
}
updatePassword(newpassword: string, token: string) {
return this.http.patch(
`http://localhost:3000/user/reset-userpassword/${token}`,
{ newpassword },
{
observe: 'response',
}
);
}
getClients() {
const headers = new HttpHeaders().set('Authorization', this.token);

return this.http.get('http://localhost:3000/user/clients', {
headers: headers,
});
}
}

You might also like