Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made RTD endpoint optional. Read the floors from local storage if the dynamic endpoint is not provided. #23

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions modules/pubxaiRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ajax } from '../src/ajax.js';
import { config } from '../src/config.js';
import { submodule } from '../src/hook.js';
import { deepAccess } from '../src/utils.js';
import { getStorageManager } from '../src/storageManager.js';
/**
* This RTD module has a dependency on the priceFloors module.
* We utilize the createFloorsDataForAuction function from the priceFloors module to incorporate price floors data into the current auction.
Expand All @@ -16,6 +17,7 @@ export const FloorsApiStatus = Object.freeze({
SUCCESS: 'SUCCESS',
ERROR: 'ERROR',
});
const storage = getStorageManager({ moduleType: MODULE_TYPE_ANALYTICS, moduleName: adapterCode });
export const FLOORS_EVENT_HANDLE = 'floorsApi';
export const FLOORS_END_POINT = 'https://floor.pbxai.com/';
export const FLOOR_PROVIDER = 'PubxFloorProvider';
Expand Down Expand Up @@ -80,31 +82,48 @@ export const setFloorsApiStatus = (status) => {

export const getUrl = (provider) => {
const { pubxId, endpoint } = deepAccess(provider, 'params');
return `${endpoint || FLOORS_END_POINT}?pubxId=${pubxId}&page=${
window.location.href
}`;
if (!endpoint) {
return null; // Indicate that no endpoint is provided
}
return `${endpoint || FLOORS_END_POINT}?pubxId=${pubxId}&page=${window.location.href}`;
};

export const fetchFloorRules = async (provider) => {
return new Promise((resolve, reject) => {
setFloorsApiStatus(FloorsApiStatus.IN_PROGRESS);
ajax(getUrl(provider), {
success: (responseText, response) => {
try {
if (response && response.response) {
const floorsResponse = JSON.parse(response.response);
resolve(floorsResponse);
} else {
resolve(null);
const url = getUrl(provider);
if (url) {
// Fetch from remote endpoint
ajax(url, {
success: (responseText, response) => {
try {
if (response && response.response) {
const floorsResponse = JSON.parse(response.response);
resolve(floorsResponse);
} else {
resolve(null);
}
} catch (error) {
reject(error);
}
} catch (error) {
reject(error);
},
error: (responseText, response) => {
reject(response);
},
});
} else {
// Fetch from local storage
try {
const localData = storage.getDataFromLocalStorage('pubx:dynamicFloors') || window.__pubxDynamicFloors__;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This data is written to localStorage by RTD module or will be written by the external floors loading script?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

if (localData) {
resolve(JSON.parse(localData));
} else {
resolve(null);
}
},
error: (responseText, response) => {
reject(response);
},
});
} catch (error) {
reject(error);
}
}
});
};

Expand Down
38 changes: 36 additions & 2 deletions test/spec/modules/pubxaiRtdProvider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { config } from '../../../src/config';
import * as hook from '../../../src/hook.js';
import { server } from '../../mocks/xhr.js';
import { getStorageManager } from '../../../src/storageManager.js';

const getConfig = () => ({
params: {
Expand Down Expand Up @@ -45,6 +46,7 @@ const resetGlobals = () => {
window.__pubxFloorsConfig__ = undefined;
window.__pubxFloorsApiStatus__ = undefined;
window.__pubxFloorRulesPromise__ = null;
localStorage.removeItem('pubx:dynamicFloors');
};

const fakeServer = (
Expand Down Expand Up @@ -119,7 +121,7 @@ describe('pubxaiRtdProvider', () => {
stub.restore();
});
it('createFloorsDataForAuction called once before and once after __pubxFloorRulesPromise__. Also getBidRequestData executed only once', async () => {
pubxaiSubmodule.getBidRequestData(reqBidsConfigObj, () => {});
pubxaiSubmodule.getBidRequestData(reqBidsConfigObj, () => { });
assert(priceFloors.createFloorsDataForAuction.calledOnce);
await window.__pubxFloorRulesPromise__;
assert(priceFloors.createFloorsDataForAuction.calledTwice);
Expand All @@ -129,14 +131,24 @@ describe('pubxaiRtdProvider', () => {
reqBidsConfigObj.auctionId
)
);
pubxaiSubmodule.getBidRequestData(reqBidsConfigObj, () => {});
pubxaiSubmodule.getBidRequestData(reqBidsConfigObj, () => { });
await window.__pubxFloorRulesPromise__;
assert(priceFloors.createFloorsDataForAuction.calledTwice);
});
});
describe('fetchFloorRules', () => {
const providerConfig = getConfig();
const floorsResponse = getFloorsResponse();
let storageStub;

beforeEach(() => {
storageStub = sinon.stub(getStorageManager({ moduleType: 'rtd', moduleName: 'pubxai' }), 'getDataFromLocalStorage');
});

afterEach(() => {
storageStub.restore();
});

it('success with floors response', (done) => {
const promise = fetchFloorRules(providerConfig);
fakeServer(floorsResponse);
Expand All @@ -145,6 +157,7 @@ describe('pubxaiRtdProvider', () => {
done();
});
});

it('success with no floors response', (done) => {
const promise = fetchFloorRules(providerConfig);
fakeServer(undefined);
Expand All @@ -153,6 +166,7 @@ describe('pubxaiRtdProvider', () => {
done();
});
});

it('API call error', (done) => {
const promise = fetchFloorRules(providerConfig);
fakeServer(undefined, undefined, 404);
Expand All @@ -167,6 +181,7 @@ describe('pubxaiRtdProvider', () => {
done();
});
});

it('Wrong API response', (done) => {
const promise = fetchFloorRules(providerConfig);
fakeServer('floorsResponse');
Expand All @@ -181,6 +196,25 @@ describe('pubxaiRtdProvider', () => {
done();
});
});

it('success with local data response', (done) => {
const localFloorsResponse = getFloorsResponse();
storageStub.withArgs('pubx:dynamicFloors').returns(JSON.stringify(localFloorsResponse));
const promise = fetchFloorRules({ params: {} });
promise.then((res) => {
expect(res).to.deep.equal(localFloorsResponse);
done();
});
});

it('no local data response', (done) => {
storageStub.withArgs('pubx:dynamicFloors').returns(null);
const promise = fetchFloorRules({ params: {} });
promise.then((res) => {
expect(res).to.deep.equal(null);
done();
});
});
});
describe('setPriceFloors', () => {
const providerConfig = getConfig();
Expand Down