From c902d6ccf58b8979bc7502a0095df1438136745c Mon Sep 17 00:00:00 2001 From: Love Sharma Date: Thu, 9 May 2024 10:23:21 -0400 Subject: [PATCH] chore: code cleanup [PB-2828] (#11468) Co-authored-by: Love Sharma --- modules/ixBidAdapter.js | 153 +--------------------- test/spec/modules/ixBidAdapter_spec.js | 174 +------------------------ 2 files changed, 8 insertions(+), 319 deletions(-) diff --git a/modules/ixBidAdapter.js b/modules/ixBidAdapter.js index 248dbea0046..f56e2790ad6 100644 --- a/modules/ixBidAdapter.js +++ b/modules/ixBidAdapter.js @@ -18,15 +18,12 @@ import { } from '../src/utils.js'; import { BANNER, VIDEO, NATIVE } from '../src/mediaTypes.js'; import { config } from '../src/config.js'; -import { EVENTS } from '../src/constants.js'; import { getStorageManager } from '../src/storageManager.js'; -import * as events from '../src/events.js'; import { find } from '../src/polyfill.js'; import { registerBidder } from '../src/adapters/bidderFactory.js'; import { INSTREAM, OUTSTREAM } from '../src/video.js'; import { Renderer } from '../src/Renderer.js'; import {getGptSlotInfoForAdUnitCode} from '../libraries/gptUtils/gptUtils.js'; -import {convertTypes} from '../libraries/transformParamsUtils/convertTypes.js'; const BIDDER_CODE = 'ix'; const ALIAS_BIDDER_CODE = 'roundel'; @@ -49,17 +46,6 @@ const PRICE_TO_DOLLAR_FACTOR = { const IFRAME_USER_SYNC_URL = 'https://js-sec.indexww.com/um/ixmatch.html'; const FLOOR_SOURCE = { PBJS: 'p', IX: 'x' }; const IMG_USER_SYNC_URL = 'https://dsum.casalemedia.com/pbusermatch?origin=prebid'; -export const ERROR_CODES = { - BID_SIZE_INVALID_FORMAT: 1, - BID_SIZE_NOT_INCLUDED: 2, - PROPERTY_NOT_INCLUDED: 3, - SITE_ID_INVALID_VALUE: 4, - BID_FLOOR_INVALID_FORMAT: 5, - IX_FPD_EXCEEDS_MAX_SIZE: 6, - EXCEEDS_MAX_SIZE: 7, - PB_FPD_EXCEEDS_MAX_SIZE: 8, - VIDEO_DURATION_INVALID: 9 -}; const FIRST_PARTY_DATA = { SITE: [ 'id', 'name', 'domain', 'cat', 'sectioncat', 'pagecat', 'page', 'ref', 'search', 'mobile', @@ -110,7 +96,6 @@ const VIDEO_PARAMS_ALLOW_LIST = [ ]; const LOCAL_STORAGE_KEY = 'ixdiag'; export const LOCAL_STORAGE_FEATURE_TOGGLES_KEY = `${BIDDER_CODE}_features`; -let hasRegisteredHandler = false; export const storage = getStorageManager({ bidderCode: BIDDER_CODE }); export const FEATURE_TOGGLES = { // Update with list of CFTs to be requested from Exchange @@ -262,8 +247,7 @@ export function bidToVideoImp(bid) { if (imp.video.minduration > imp.video.maxduration) { logError( - `IX Bid Adapter: video minduration [${imp.video.minduration}] cannot be greater than video maxduration [${imp.video.maxduration}]`, - { bidder: BIDDER_CODE, code: ERROR_CODES.VIDEO_DURATION_INVALID } + `IX Bid Adapter: video minduration [${imp.video.minduration}] cannot be greater than video maxduration [${imp.video.maxduration}]` ); return {}; } @@ -883,13 +867,6 @@ function enrichRequest(r, bidderRequest, impressions, validBidRequests, userEids r.ext.ixdiag.syncsPerBidder = config.getConfig('userSync').syncsPerBidder; } - // Get cached errors stored in LocalStorage - const cachedErrors = getCachedErrors(); - - if (!isEmpty(cachedErrors)) { - r.ext.ixdiag.err = cachedErrors; - } - // Add number of available imps to ixDiag. r.ext.ixdiag.imps = Object.keys(impressions).length; @@ -1546,104 +1523,6 @@ function createMissingBannerImp(bid, imp, newSize) { return newImp; } -/** - * @typedef {Array[message: string, err: Object]} ErrorData - * @property {string} message - The error message. - * @property {object} err - The error object. - * @property {string} err.bidder - The bidder of the error. - * @property {string} err.code - The error code. - */ - -/** - * Error Event handler that receives type and arguments in a data object. - * - * @param {ErrorData} data - */ -function storeErrorEventData(data) { - if (!storage.localStorageIsEnabled()) { - return; - } - - let currentStorage; - - try { - currentStorage = JSON.parse(storage.getDataFromLocalStorage(LOCAL_STORAGE_KEY) || '{}'); - } catch (e) { - logWarn('ix can not read ixdiag from localStorage.'); - } - - const todayDate = new Date(); - - Object.keys(currentStorage).map((errorDate) => { - const date = new Date(errorDate); - - if (date.setDate(date.getDate() + 7) - todayDate < 0) { - delete currentStorage[errorDate]; - } - }); - - if (data.type === 'ERROR' && data.arguments && data.arguments[1] && data.arguments[1].bidder === BIDDER_CODE) { - const todayString = todayDate.toISOString().slice(0, 10); - - const errorCode = data.arguments[1].code; - - if (errorCode) { - currentStorage[todayString] = currentStorage[todayString] || {}; - - if (!Number(currentStorage[todayString][errorCode])) { - currentStorage[todayString][errorCode] = 0; - } - - currentStorage[todayString][errorCode]++; - }; - } - - storage.setDataInLocalStorage(LOCAL_STORAGE_KEY, JSON.stringify(currentStorage)); -} - -/** - * Event handler for storing data into local storage. It will only store data if - * local storage premissions are avaliable - */ -function localStorageHandler(data) { - if (data.type === 'ERROR' && data.arguments && data.arguments[1] && data.arguments[1].bidder === BIDDER_CODE) { - storeErrorEventData(data); - } -} - -/** - * Get ixdiag stored in LocalStorage and format to be added to request payload - * - * @returns {Object} Object with error codes and counts - */ -function getCachedErrors() { - if (!storage.localStorageIsEnabled()) { - return; - } - - const errors = {}; - let currentStorage; - - try { - currentStorage = JSON.parse(storage.getDataFromLocalStorage(LOCAL_STORAGE_KEY) || '{}'); - } catch (e) { - logError('ix can not read ixdiag from localStorage.'); - return null; - } - - Object.keys(currentStorage).forEach((date) => { - Object.keys(currentStorage[date]).forEach((code) => { - if (typeof currentStorage[date][code] === 'number') { - errors[code] = errors[code] - ? errors[code] + currentStorage[date][code] - : currentStorage[date][code]; - } - }); - }); - - return errors; -} - /** * * Initialize IX Outstream Renderer @@ -1738,12 +1617,6 @@ export const spec = { * @return {boolean} True if this is a valid bid, and false otherwise. */ isBidRequestValid: function (bid) { - if (!hasRegisteredHandler) { - events.on(EVENTS.AUCTION_DEBUG, localStorageHandler); - events.on(EVENTS.AD_RENDER_FAILED, localStorageHandler); - hasRegisteredHandler = true; - } - const paramsVideoRef = deepAccess(bid, 'params.video'); const paramsSize = deepAccess(bid, 'params.size'); const mediaTypeBannerSizes = deepAccess(bid, 'mediaTypes.banner.sizes'); @@ -1765,14 +1638,14 @@ export const spec = { // since there is an ix bidder level size, make sure its valid const ixSize = getFirstSize(paramsSize); if (!ixSize) { - logError('IX Bid Adapter: size has invalid format.', { bidder: BIDDER_CODE, code: ERROR_CODES.BID_SIZE_INVALID_FORMAT }); + logError('IX Bid Adapter: size has invalid format.'); return false; } // check if the ix bidder level size, is present in ad unit level if (!includesSize(bid.sizes, ixSize) && !(includesSize(mediaTypeVideoPlayerSize, ixSize)) && !(includesSize(mediaTypeBannerSizes, ixSize))) { - logError('IX Bid Adapter: bid size is not included in ad unit sizes or player size.', { bidder: BIDDER_CODE, code: ERROR_CODES.BID_SIZE_NOT_INCLUDED }); + logError('IX Bid Adapter: bid size is not included in ad unit sizes or player size.'); return false; } } @@ -1784,19 +1657,19 @@ export const spec = { if (bid.params.siteId !== undefined) { if (typeof bid.params.siteId !== 'string' && typeof bid.params.siteId !== 'number') { - logError('IX Bid Adapter: siteId must be string or number type.', { bidder: BIDDER_CODE, code: ERROR_CODES.SITE_ID_INVALID_VALUE }); + logError('IX Bid Adapter: siteId must be string or number type.'); return false; } if (typeof bid.params.siteId !== 'string' && isNaN(Number(bid.params.siteId))) { - logError('IX Bid Adapter: siteId must valid value', { bidder: BIDDER_CODE, code: ERROR_CODES.SITE_ID_INVALID_VALUE }); + logError('IX Bid Adapter: siteId must valid value'); return false; } } if (hasBidFloor || hasBidFloorCur) { if (!(hasBidFloor && hasBidFloorCur && isValidBidFloorParams(bid.params.bidFloor, bid.params.bidFloorCur))) { - logError('IX Bid Adapter: bidFloor / bidFloorCur parameter has invalid format.', { bidder: BIDDER_CODE, code: ERROR_CODES.BID_FLOOR_INVALID_FORMAT }); + logError('IX Bid Adapter: bidFloor / bidFloorCur parameter has invalid format.'); return false; } } @@ -1815,7 +1688,7 @@ export const spec = { if (errorList.length) { errorList.forEach((err) => { - logError(err, { bidder: BIDDER_CODE, code: ERROR_CODES.PROPERTY_NOT_INCLUDED }); + logError(err); }); return false; } @@ -1997,18 +1870,6 @@ export const spec = { } }, - /** - * Covert bid param types for S2S - * @param {Object} params bid params - * @param {Boolean} isOpenRtb boolean to check openrtb2 protocol - * @return {Object} params bid params - */ - transformBidParams: function (params, isOpenRtb) { - return convertTypes({ - 'siteID': 'number' - }, params); - }, - /** * Determine which user syncs should occur * @param {object} syncOptions diff --git a/test/spec/modules/ixBidAdapter_spec.js b/test/spec/modules/ixBidAdapter_spec.js index 7655868ffc3..88cfb93850f 100644 --- a/test/spec/modules/ixBidAdapter_spec.js +++ b/test/spec/modules/ixBidAdapter_spec.js @@ -2,7 +2,7 @@ import * as utils from 'src/utils.js'; import { config } from 'src/config.js'; import { expect } from 'chai'; import { newBidder } from 'src/adapters/bidderFactory.js'; -import { spec, storage, ERROR_CODES, FEATURE_TOGGLES, LOCAL_STORAGE_FEATURE_TOGGLES_KEY, REQUESTED_FEATURE_TOGGLES, combineImps, bidToVideoImp, bidToNativeImp, deduplicateImpExtFields, removeSiteIDs, addDeviceInfo } from '../../../modules/ixBidAdapter.js'; +import { spec, storage, FEATURE_TOGGLES, LOCAL_STORAGE_FEATURE_TOGGLES_KEY, REQUESTED_FEATURE_TOGGLES, combineImps, bidToVideoImp, bidToNativeImp, deduplicateImpExtFields, removeSiteIDs, addDeviceInfo } from '../../../modules/ixBidAdapter.js'; import { deepAccess, deepClone } from '../../../src/utils.js'; describe('IndexexchangeAdapter', function () { @@ -4867,178 +4867,6 @@ describe('IndexexchangeAdapter', function () { }); }); - describe('LocalStorage error codes', () => { - let TODAY = new Date().toISOString().slice(0, 10); - const key = 'ixdiag'; - - let sandbox; - let setDataInLocalStorageStub; - let getDataFromLocalStorageStub; - let removeDataFromLocalStorageStub; - let localStorageValues = {}; - - beforeEach(() => { - sandbox = sinon.sandbox.create(); - setDataInLocalStorageStub = sandbox.stub(storage, 'setDataInLocalStorage').callsFake((key, value) => localStorageValues[key] = value) - getDataFromLocalStorageStub = sandbox.stub(storage, 'getDataFromLocalStorage').callsFake((key) => localStorageValues[key]) - removeDataFromLocalStorageStub = sandbox.stub(storage, 'removeDataFromLocalStorage').callsFake((key) => delete localStorageValues[key]) - sandbox.stub(storage, 'localStorageIsEnabled').returns(true); - }); - - afterEach(() => { - setDataInLocalStorageStub.restore(); - getDataFromLocalStorageStub.restore(); - removeDataFromLocalStorageStub.restore(); - localStorageValues = {}; - sandbox.restore(); - - config.setConfig({ - ortb2: {}, - ix: {}, - }) - }); - - it('should not log error in LocalStorage when there is no logError called.', () => { - const bid = DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]; - expect(spec.isBidRequestValid(bid)).to.be.true; - expect(localStorageValues[key]).to.be.undefined; - }); - - it('should log ERROR_CODES.BID_SIZE_INVALID_FORMAT in LocalStorage when there is logError called.', () => { - const bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.size = ['400', 100]; - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { [ERROR_CODES.BID_SIZE_INVALID_FORMAT]: 1 } }); - }); - - it('should log ERROR_CODES.BID_SIZE_NOT_INCLUDED in LocalStorage when there is logError called.', () => { - const bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.size = [407, 100]; - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { [ERROR_CODES.BID_SIZE_NOT_INCLUDED]: 1 } }); - }); - - it('should log ERROR_CODES.PROPERTY_NOT_INCLUDED in LocalStorage when there is logError called.', () => { - const bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.video = {}; - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { [ERROR_CODES.PROPERTY_NOT_INCLUDED]: 4 } }); - }); - - it('should log ERROR_CODES.SITE_ID_INVALID_VALUE in LocalStorage when there is logError called.', () => { - const bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.siteId = false; - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { [ERROR_CODES.SITE_ID_INVALID_VALUE]: 1 } }); - }); - - it('should log ERROR_CODES.BID_FLOOR_INVALID_FORMAT in LocalStorage when there is logError called.', () => { - const bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.bidFloor = true; - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { [ERROR_CODES.BID_FLOOR_INVALID_FORMAT]: 1 } }); - }); - - it('should log ERROR_CODES.VIDEO_DURATION_INVALID in LocalStorage when there is logError called.', () => { - const bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.video.minduration = 1; - bid.params.video.maxduration = 0; - - expect(spec.isBidRequestValid(bid)).to.be.true; - spec.buildRequests([bid]); - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { [ERROR_CODES.VIDEO_DURATION_INVALID]: 2 } }); - }); - - it('should increment errors for errorCode', () => { - const bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.video = {}; - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { [ERROR_CODES.PROPERTY_NOT_INCLUDED]: 4 } }); - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { [ERROR_CODES.PROPERTY_NOT_INCLUDED]: 8 } }); - }); - - it('should add new errorCode to ixdiag.', () => { - let bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.size = ['400', 100]; - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { [ERROR_CODES.BID_SIZE_INVALID_FORMAT]: 1 } }); - - bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.siteId = false; - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ - [TODAY]: { - [ERROR_CODES.BID_SIZE_INVALID_FORMAT]: 1, - [ERROR_CODES.SITE_ID_INVALID_VALUE]: 1 - } - }); - }); - - it('should clear errors with successful response', () => { - const ixdiag = { [TODAY]: { '1': 1, '3': 8, '4': 1 } }; - setDataInLocalStorageStub(key, JSON.stringify(ixdiag)); - - expect(JSON.parse(localStorageValues[key])).to.deep.equal(ixdiag); - - const request = DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]; - expect(spec.isBidRequestValid(request)).to.be.true; - - const data = { - id: '345', - imp: [ - { - id: '1a2b3c4e', - } - ], - ext: { - ixdiag: { - err: { - '4': 8 - } - } - } - }; - - const validBidRequest = DEFAULT_MULTIFORMAT_BANNER_VALID_BID[0]; - - spec.interpretResponse({ body: DEFAULT_BANNER_BID_RESPONSE }, { data, validBidRequest }); - - expect(localStorageValues[key]).to.be.undefined; - }); - - it('should clear errors after 7 day expiry errorCode', () => { - const EXPIRED_DATE = '2019-12-12'; - - const ixdiag = { [EXPIRED_DATE]: { '1': 1, '3': 8, '4': 1 }, [TODAY]: { '3': 8, '4': 1 } }; - setDataInLocalStorageStub(key, JSON.stringify(ixdiag)); - - const bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.size = ['400', 100]; - - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(JSON.parse(localStorageValues[key])[EXPIRED_DATE]).to.be.undefined; - expect(JSON.parse(localStorageValues[key])).to.deep.equal({ [TODAY]: { '1': 1, '3': 8, '4': 1 } }) - }); - - it('should not save error data into localstorage if consent is not given', () => { - config.setConfig({ deviceAccess: false }); - storage.localStorageIsEnabled.restore(); // let core manage device access - const bid = utils.deepClone(DEFAULT_MULTIFORMAT_VIDEO_VALID_BID[0]); - bid.params.size = ['400', 100]; - expect(spec.isBidRequestValid(bid)).to.be.false; - expect(localStorageValues[key]).to.be.undefined; - }); - }); describe('combine imps test', function () { it('base test', function () { const imps = [