diff --git a/packages/api-angular/src/settings.js b/packages/api-angular/src/settings.js index 0984e1cd8..ee8a36eba 100644 --- a/packages/api-angular/src/settings.js +++ b/packages/api-angular/src/settings.js @@ -2,10 +2,11 @@ import angular from 'angular'; import { AvSettings } from '@availity/api-core'; -export default ($http, $q, avApiOptions) => +export default ($http, $q, avUsersApi, avApiOptions) => new AvSettings({ http: $http, promise: $q, merge: angular.merge, + avUsers: avUsersApi, config: angular.copy(avApiOptions), }); diff --git a/packages/api-axios/src/settings.js b/packages/api-axios/src/settings.js index 7ea1e4916..a909c5a2e 100644 --- a/packages/api-axios/src/settings.js +++ b/packages/api-axios/src/settings.js @@ -1,9 +1,11 @@ import axios from 'axios'; import merge from 'deep-assign'; import { AvSettings } from '@availity/api-core'; +import userApi from './user'; export default new AvSettings({ http: axios, promise: Promise, merge, + avUsers: userApi, }); diff --git a/packages/api-core/src/resources/settings.js b/packages/api-core/src/resources/settings.js index 2ff4ce0bf..0054c1ae4 100644 --- a/packages/api-core/src/resources/settings.js +++ b/packages/api-core/src/resources/settings.js @@ -1,11 +1,13 @@ import AvApi from '../api'; export default class AvSettings extends AvApi { - constructor({ http, promise, merge, config }) { + constructor({ http, promise, merge, avUsers, config }) { const options = Object.assign( { path: 'api/utils', name: 'settings', + sessionBust: false, + pageBust: true, }, config ); @@ -15,5 +17,41 @@ export default class AvSettings extends AvApi { merge, config: options, }); + this.avUsers = avUsers; + } + + getApplication(applicationId, config) { + if (!applicationId) { + throw new Error('must define applicationId'); + } + return this.avUsers.me().then(user => { + const queryConfig = this.addParams( + { applicationId, userId: user.id }, + config + ); + return this.query(queryConfig); + }); + } + + setApplication(applicaitonId, data, config) { + if ( + typeof applicaitonId !== 'string' && + typeof applicaitonId !== 'number' + ) { + config = data; + data = applicaitonId; + applicaitonId = ''; + } + + if (!applicaitonId && (!data.scope || !data.scope.applicationId)) { + throw new Error('must set applicationId in settings call'); + } + + return this.avUsers.me().then(user => { + data.scope = data.scope || {}; + data.scope.applicationId = applicaitonId; + data.scope.userId = user.id; + return this.update(data, config); + }); } } diff --git a/packages/api-core/src/resources/tests/settings.test.js b/packages/api-core/src/resources/tests/settings.test.js index 73e8c8f72..9164beb31 100644 --- a/packages/api-core/src/resources/tests/settings.test.js +++ b/packages/api-core/src/resources/tests/settings.test.js @@ -3,81 +3,88 @@ import AvSettings from '../settings'; const mockHttp = jest.fn(() => Promise.resolve({})); const mockMerge = jest.fn((...args) => Object.assign(...args)); -const mockConfig = { - scope: { - applicationId: '123', - userId: 'myUser', - }, +const testAppId = 'testApplicationId'; + +const mockUser = { + id: 'mockUserId', +}; +const mockAvUsers = { + me: jest.fn(() => Promise.resolve(mockUser)), }; describe('AvSettings', () => { let api; - test('should be defined', () => { + beforeEach(() => { api = new AvSettings({ http: mockHttp, promise: Promise, merge: mockMerge, + avUsers: mockAvUsers, config: {}, }); - expect(api).toBeDefined(); }); - test('should handle no config passed in', () => { - api = new AvSettings({ - http: mockHttp, - promise: Promise, - merge: mockMerge, - }); + test('should be defined', () => { expect(api).toBeDefined(); }); - test('url should be correct', () => { - api = new AvSettings({ - http: mockHttp, - promise: Promise, - merge: mockMerge, + describe('getApplication', () => { + beforeEach(() => { + api.query = jest.fn(); + }); + test('should call avUsers.me and use in query', async () => { + const expectedQuery = { + params: { + applicationId: testAppId, + userId: mockUser.id, + }, + }; + await api.getApplication(testAppId); + expect(mockAvUsers.me).toHaveBeenCalled(); + expect(api.query).toHaveBeenCalledWith(expectedQuery); + }); + + test('should throw error if no applicationId passed in', () => { + expect(() => api.getApplication()).toThrow(); }); - expect(api.getUrl(api.config(mockConfig))).toBe('/api/utils/v1/settings'); }); - test('query() should be called with params', () => { - api = new AvSettings({ - http: mockHttp, - promise: Promise, - merge: mockMerge, - config: {}, + describe('setApplication', () => { + beforeEach(() => { + api.update = jest.fn(); }); - const data = { - params: { - applicationId: '123', - userId: 'myUser', - }, - }; + test('should add applicationId and user.me to scope', async () => { + const testData = { key: 'value' }; + const testConfig = {}; + const expectedUpdate = Object.assign( + { + scope: { + applicationId: testAppId, + userId: mockUser.id, + }, + }, + testData + ); - api.query = jest.fn(); - api.query(data); - expect(api.query).toHaveBeenLastCalledWith(data); - }); + await api.setApplication(testAppId, testData, testConfig); + expect(mockAvUsers.me).toHaveBeenCalled(); + expect(api.update).toHaveBeenCalledWith(expectedUpdate, testConfig); + }); - test('update() should be called with scope', async () => { - api = new AvSettings({ - http: mockHttp, - promise: Promise, - merge: mockMerge, - config: {}, + test('should not throw error if application id passed in as arugment', () => { + expect(() => api.setApplication(testAppId, {})).not.toThrow(); + }); + + test('should not throw error if applicationId in scope', () => { + expect(() => + api.setApplication({ scope: { applicationId: testAppId } }) + ).not.toThrow(); }); - const data = { - scope: { - applicationId: '123', - userId: 'myUser', - }, - key: 'value', - }; - api.update = jest.fn(); - api.update(data); - expect(api.update).toHaveBeenLastCalledWith(data); + test('should throw error if no applicationId in argument or data', () => { + expect(() => api.setApplication()).toThrow(); + }); }); });