diff --git a/src/Controllers/UserController.ts b/src/Controllers/UserController.ts index 9b3ab78c12e385f6a7761c0f55821a2cc8a1786d..aad73c5b22bc085e30946fa09f8bec0adff1425a 100644 --- a/src/Controllers/UserController.ts +++ b/src/Controllers/UserController.ts @@ -20,7 +20,8 @@ const create: Handler = async (req: Request, res: Response) => { const listUsers: Handler = async (req: Request, res: Response) => { try { - const userList: any = await UserAPI.listUsers(req.body); + const token: any = req.headers.authorization; + const userList: any = await UserAPI.listUsers(req.query.type, token); return res.status(200).send(userList); } catch (error) { const e = error as Error; @@ -40,7 +41,8 @@ const login: Handler = async (req: Request, res: Response) => { const update: Handler = async (req: Request, res: Response) => { try { - const user: any = await UserAPI.update(req.body, req.params.id); + const token: any = req.headers.authorization; + const user: any = await UserAPI.update(req.body, req.params.id, token); return res.status(200).send(user); } catch (error) { const e = error as Error; @@ -50,7 +52,8 @@ const update: Handler = async (req: Request, res: Response) => { const password: Handler = async (req: Request, res: Response) => { try { - const user: any = await UserAPI.password(req.body, req.params.id); + const token: any = req.headers.authorization; + const user: any = await UserAPI.password(req.body, req.params.id, token); return res.status(200).send(user); } catch (error) { const e = error as Error; @@ -60,7 +63,8 @@ const password: Handler = async (req: Request, res: Response) => { const approve: Handler = async (req: Request, res: Response) => { try { - const user: any = await UserAPI.approve(req.params.id); + const token: any = req.headers.authorization; + const user: any = await UserAPI.approve(req.params.id, token); return res.status(200).send(user); } catch (error) { const e = error as Error; @@ -70,7 +74,8 @@ const approve: Handler = async (req: Request, res: Response) => { const cancel: Handler = async (req: Request, res: Response) => { try { - const user: any = await UserAPI.cancel(req.params.id); + const token: any = req.headers.authorization; + const user: any = await UserAPI.cancel(req.params.id, token); return res.status(200).send(user); } catch (error) { const e = error as Error; @@ -80,7 +85,8 @@ const cancel: Handler = async (req: Request, res: Response) => { const giveAdminPermission: Handler = async (req: Request, res: Response) => { try { - const user: any = await UserAPI.giveAdminPermission(req.params.id); + const token: any = req.headers.authorization; + const user: any = await UserAPI.giveAdminPermission(req.params.id, token); return res.status(200).send(user); } catch (error) { const e = error as Error; @@ -90,7 +96,8 @@ const giveAdminPermission: Handler = async (req: Request, res: Response) => { const removeAdminPermission: Handler = async (req: Request, res: Response) => { try { - const user: any = await UserAPI.removeAdminPermission(req.params.id); + const token: any = req.headers.authorization; + const user: any = await UserAPI.removeAdminPermission(req.params.id, token); return res.status(200).send(user); } catch (error) { const e = error as Error; diff --git a/src/Services/UserAPI.ts b/src/Services/UserAPI.ts index 25ca43962e90ca6413136338b48b78374999211f..bbe92b26cac22a71b7f84987145477428065eac2 100644 --- a/src/Services/UserAPI.ts +++ b/src/Services/UserAPI.ts @@ -30,69 +30,50 @@ const create = (user: any): any => { }); }; -const login = (user: any): any => { - instance.post('/login', user) - .then((res) => res) - .catch((err) => { - throw (err); - }); +const login = async (user: any) => { + const res = await instance.post('/login', user); + return res.data; }; -const listUsers = (requirements: any): any => { - instance.get('/', requirements) - .then((res) => res) - .catch((err) => { - throw (err); - }); +const listUsers = async (userType: any, token: string) => { + const res = await instance.get('/', { headers: { authorization: token }, params: { type: userType } }); + return res.data; }; -const update = (user: any, idUser: string) => { +const update = async (user: any, idUser: string, token: string) => { const url = `/${idUser}`; - instance.put(url, user) - .then((res) => res) - .catch((err) => { - throw (err); - }); + const res = await instance.put(url, user, { headers: { authorization: token } }); + return res.data; }; -const password = (user: any, idUser: string) => { - instance.put('/password', user, { params: { id: idUser } }) - .then((res) => res) - .catch((err) => { - throw (err); - }); +const password = async (user: any, idUser: string, token: string) => { + const url = `/${idUser}/password`; + const res = await instance.put(url, user, { headers: { authorization: token } }); + return res.data; }; -const approve = (idUser: string) => { - instance.put('/approve', { params: { id: idUser } }) - .then((res) => res) - .catch((err) => { - throw (err); - }); +const approve = async (idUser: string, token: string) => { + const url = `/${idUser}/approve`; + const res = await instance.put(url, {}, { headers: { authorization: token } }); + return res.data; }; -const cancel = (idUser: string) => { - instance.put('/cancel', { params: { id: idUser } }) - .then((res) => res) - .catch((err) => { - throw (err); - }); +const cancel = async (idUser: string, token: string) => { + const url = `/${idUser}/cancel`; + const res = await instance.put(url, {}, { headers: { authorization: token } }); + return res.data; }; -const giveAdminPermission = (idUser: string) => { - instance.put('/admin', { params: { id: idUser } }) - .then((res) => res) - .catch((err) => { - throw (err); - }); +const giveAdminPermission = async (idUser: string, token: string) => { + const url = `/${idUser}/admin`; + const res = await instance.put(url, {}, { headers: { authorization: token } }); + return res.data; }; -const removeAdminPermission = (idUser: string) => { - instance.put('/client', { params: { id: idUser } }) - .then((res) => res) - .catch((err) => { - throw (err); - }); +const removeAdminPermission = async (idUser: string, token: string) => { + const url = `/${idUser}/client`; + const res = await instance.put(url, {}, { headers: { authorization: token } }); + return res.data; }; export default {