import { HttpException, Inject, Injectable } from '@nestjs/common'; import { HttpService } from '@nestjs/axios'; import { ConfigModuleOptions, MODULE_OPTIONS_TOKEN } from './auth.module-definition'; @Injectable() export class FxProviderService { constructor(private httpService: HttpService, @Inject(MODULE_OPTIONS_TOKEN) private options: ConfigModuleOptions) { } async provide(token) { try { const { providerUrl } = this.options const data = (await this.httpService.get(`${providerUrl}/auth/user`, { headers: { 'Authorization': token } }).toPromise()).data return data } catch (error) { console.log('axios', error) } } async getAccessToken(code: string) { const { providerUrl, clientSecret } = this.options if (!providerUrl) { throw new Error("Auth url is null or undefined."); } if (!clientSecret) { throw new Error("Client secret is null or undefined."); } // const clientSecret = process.env.AUTH_CLIENT_SECRET return await this.httpService.get(`${providerUrl}/auth/access-token?code=${code}&secret=${clientSecret}`).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async switch_login(customerId: any, unitId: any, roleId: any, employeeId: any, token: any) { const { providerUrl } = this.options return this.httpService.post(providerUrl + '/auth/login/switch', { customerId, unitId, roleId, employeeId }, { headers: { 'Authorization': token } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async switch_space(space: any, token: any) { const { providerUrl } = this.options return this.httpService.post(providerUrl + '/auth/switch_space', { space }, { headers: { 'Authorization': token } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async switch_shop(shop: any, token: any) { const { providerUrl } = this.options return this.httpService.post(providerUrl + '/auth/switch_shop', { shop }, { headers: { 'Authorization': token } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async getUserEmployees(token: any) { const { providerUrl, clientId } = this.options return await this.httpService.get(providerUrl + '/auth/user/employee', { headers: { 'Authorization': token }, params: { clientId: clientId } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async getUserEmployee(employeeId, token: any) { const { providerUrl, clientId } = this.options return await this.httpService.get(providerUrl + '/auth/user/employee/' + employeeId, { headers: { 'Authorization': token }, params: { clientId: clientId } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async getUserCustomerUnit(customerId: any, unitId: any, token: any) { const { providerUrl } = this.options return this.httpService.get(`${providerUrl}/auth/user/orgs/` + customerId + '/' + unitId, { headers: { 'Authorization': token } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async updatePassword(token, { oldPassword, newPassword }) { const { providerUrl } = this.options return await this.httpService.put(providerUrl + '/user/password', { oldPassword, newPassword }, { headers: { 'Authorization': token } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async updateUser(token, { lastName, firstName, email, phone, regNum, _id, image }) { const { providerUrl } = this.options return await this.httpService.put(providerUrl + '/user/info', { _id, lastName, firstName, email, phone, regNum, image }, { headers: { 'Authorization': token } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async getClientSystems(token) { const { providerUrl } = this.options return this.httpService.get(`${providerUrl}/client-system/list`, { headers: { 'Authorization': token } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async getUserByUnit(token: any, customerId, unitId) { const { providerUrl } = this.options return await this.httpService.get(providerUrl + '/user/findAll', { headers: { 'Authorization': token }, params: { customerId, unitId } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } async getUnitEmployees(token: any, customerId, unitId) { const { providerUrl, clientId } = this.options return await this.httpService.get(providerUrl + '/user/unitEmployee', { headers: { 'Authorization': token }, params: { customerId, unitId, clientId } }).toPromise().then(res => res.data).catch(error=>this.handleError(error)) } handleError(error) { console.log(error, 'errr') throw new HttpException("message", 400, { cause: new Error("Some Error") }) } }