Skip to content

Commit

Permalink
Merged revert-2129-revert-2100-STRAT-3832 to staging
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav Kochar committed Jul 29, 2024
2 parents cc83743 + 66bf9e9 commit a83991d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/browser-destinations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"size-limit": [
{
"path": "dist/web/*/*.js",
"limit": "169 KB"
"limit": "190 KB"
}
]
}
9 changes: 8 additions & 1 deletion packages/core/src/destination-kit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { AuthTokens, getAuthData, getOAuth2Data, updateOAuthSettings } from './p
import { InputData, Features } from '../mapping-kit'
import { retry } from '../retry'
import { HTTPError } from '..'
import isEmpty from 'lodash/isEmpty'

export type {
BaseActionDefinition,
Expand Down Expand Up @@ -94,12 +95,15 @@ export type AudienceResult = {
}

export type AudienceMode = { type: 'realtime' } | { type: 'synced'; full_audience_sync: boolean }
export type Personas = { computation_id: 'string'; computation_key: 'string' } & { [key: string]: any }

export type CreateAudienceInput<Settings = unknown, AudienceSettings = unknown> = {
settings: Settings

audienceSettings?: AudienceSettings

personas?: Personas

audienceName: string

statsContext?: StatsContext
Expand Down Expand Up @@ -426,6 +430,10 @@ export class Destination<Settings = JSONObject, AudienceSettings = JSONObject> {
if (!instanceOfAudienceDestinationSettingsWithCreateGet(audienceDefinition.audienceConfig)) {
throw new Error('Unexpected call to createAudience')
}
//validate audienceField Input
if (!isEmpty(createAudienceInput.audienceSettings)) {
validateSchema(createAudienceInput.audienceSettings, fieldsToJsonSchema(audienceDefinition.audienceFields))
}
const destinationSettings = this.getDestinationSettings(createAudienceInput.settings as unknown as JSONObject)
const auth = getAuthData(createAudienceInput.settings as unknown as JSONObject)
const context: ExecuteInput<Settings, any, AudienceSettings> = {
Expand All @@ -436,7 +444,6 @@ export class Destination<Settings = JSONObject, AudienceSettings = JSONObject> {
}
const options = this.extendRequest?.(context) ?? {}
const requestClient = createRequestClient({ ...options, statsContext: context.statsContext })

return audienceDefinition.audienceConfig?.createAudience(requestClient, createAudienceInput)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ describe('Amazon-Ads (actions)', () => {

await expect(testDestination.createAudience(createAudienceInputTemp)).rejects.toThrowError('Bad Request')
})
it('Should throw an error when invalid cpmCent is provided', async () => {
const createAudienceInput = {
settings,
audienceName: 'Test Audience',
audienceSettings: {
...audienceSettings,
ttl: '12345678',
currency: 'USD',
cpmCents: 'invalid cpm cents'
}
}

await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(
'CPM Cents must be a number but it was a string.'
)
})

it('creates an audience', async () => {
const endpoint = AUTHORIZATION_URL[`${settings.region}`]
Expand All @@ -151,9 +167,9 @@ describe('Amazon-Ads (actions)', () => {
audienceName: 'Test Audience',
audienceSettings: {
...audienceSettings,
ttl: 12345678,
ttl: '12345678',
currency: 'USD',
cpmCents: 1234
cpmCents: '1234'
}
}

Expand Down
12 changes: 2 additions & 10 deletions packages/destination-actions/src/destinations/amazon-amc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,14 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
}

if (ttl) {
const timeToLive = Number(ttl)
if (!timeToLive) {
throw new IntegrationError('TTL:-String can not be converted to Number', 'INVALID_TTL_VALUE', 400)
}
payload.metadata.ttl = timeToLive
payload.metadata.ttl = ttl
}

if (cpm_cents && currency) {
const cpmCents = Number(cpm_cents)
if (!cpmCents) {
throw new IntegrationError('CPM_CENTS:-String can not be converted to Number', 'INVALID_CPMCENTS_VALUE', 400)
}
payload.metadata.audienceFees = []
payload.metadata?.audienceFees.push({
currency,
cpmCents: cpmCents
cpmCents: cpm_cents
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import nock from 'nock'
import { createTestIntegration, IntegrationError } from '@segment/actions-core'
import Destination, { FACEBOOK_API_VERSION } from '../index'

const adAccountId = 1500000000000000
const adAccountId = '1500000000000000'
const audienceId = '1506489116128966'
const testDestination = createTestIntegration(Destination)
const getAudienceUrl = `https://graph.facebook.com/${FACEBOOK_API_VERSION}/`
Expand All @@ -29,7 +29,7 @@ describe('Facebook Custom Audiences', () => {

it('should fail if no ad account ID is set', async () => {
createAudienceInput.audienceName = 'The Void'
createAudienceInput.audienceSettings.adAccountId = 0
createAudienceInput.audienceSettings.adAccountId = ''
await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(IntegrationError)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const getAudienceInput = {
audienceSettings: {}
}



describe('Taboola (actions)', () => {
describe('testAuthentication', () => {
it('should validate authentication inputs', async () => {
Expand All @@ -33,7 +31,6 @@ describe('Taboola (actions)', () => {

describe('createAudience', () => {
it('should fail if no audience name is set', async () => {

const createAudienceInput1 = {
settings: {
client_id: 'test_client_id',
Expand All @@ -43,7 +40,7 @@ describe('Taboola (actions)', () => {
audienceSettings: {
ttl_in_hours: 1024,
exclude_from_campaigns: false,
account_id:accountId
account_id: accountId
}
}

Expand All @@ -53,7 +50,6 @@ describe('Taboola (actions)', () => {
})

it('should fail if no account ID is set', async () => {

const createAudienceInput2 = {
settings: {
client_id: 'test_client_id',
Expand All @@ -63,7 +59,7 @@ describe('Taboola (actions)', () => {
audienceSettings: {
ttl_in_hours: 1024,
exclude_from_campaigns: false,
account_id:''
account_id: ''
}
}

Expand All @@ -85,7 +81,7 @@ describe('Taboola (actions)', () => {
audienceSettings: {
ttl_in_hours: 1024,
exclude_from_campaigns: false,
account_id:accountId
account_id: accountId
}
}
const r = await testDestination.createAudience(createAudienceInput3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ const createAudienceInput = {
customer_desc: CUST_DESC
},
audienceName: '',
audienceSettings: {
personas: {
computation_key: AUDIENCE_KEY,
computation_id: AUDIENCE_ID
}
audienceSettings: {},
personas: {
computation_key: AUDIENCE_KEY,
computation_id: AUDIENCE_ID
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
},

async createAudience(request, createAudienceInput) {
const audienceSettings = createAudienceInput.audienceSettings
// @ts-ignore type is not defined, and we will define it later
const personas = audienceSettings.personas as PersonasSettings
const personas = createAudienceInput.personas as PersonasSettings
if (!personas) {
throw new IntegrationError('Missing computation parameters: Id and Key', 'MISSING_REQUIRED_FIELD', 400)
}
Expand Down

0 comments on commit a83991d

Please sign in to comment.