Skip to content

Commit

Permalink
Acoustic PR (#1105)
Browse files Browse the repository at this point in the history
* Refreshed Folder

Reset to clean up old,

* refresh - reset

* refresh -

* Reset Auth, refactor accesstoken

* Revert "Reset Auth, refactor accesstoken"

This reverts commit 24525e8.

* Refactor Auth, AcccessToken

Signed-off-by: kwlandry-acoustic <[email protected]>

* Refersh, all,

Signed-off-by: kwlandry-acoustic <[email protected]>

* Refresh, all,

Signed-off-by: kwlandry-acoustic <[email protected]>

* Refresh tests

Signed-off-by: kwlandry-acoustic <[email protected]>

* Rest auth for cred interface

Signed-off-by: kwlandry-acoustic <[email protected]>

* Update tests

Signed-off-by: kwlandry-acoustic <[email protected]>

* rename files, dirs,

Signed-off-by: kwlandry-acoustic <[email protected]>

* Update tests, renames

Signed-off-by: kwlandry-acoustic <[email protected]>

* Update packages/destination-actions/src/destinations/acoustic/index.ts

Co-authored-by: Joe Ayoub <[email protected]>

* Update packages/destination-actions/src/destinations/acoustic/index.ts

Co-authored-by: Joe Ayoub <[email protected]>

* Update packages/destination-actions/src/destinations/acoustic/index.ts

Co-authored-by: Joe Ayoub <[email protected]>

* Reset names,

Signed-off-by: kwlandry-acoustic <[email protected]>

* refresh test, refactor table id

Signed-off-by: kwlandry-acoustic <[email protected]>

* Firm up chkTableExist

Signed-off-by: kwlandry-acoustic <[email protected]>

* Add safety on parse Sections depth

Signed-off-by: kwlandry-acoustic <[email protected]>

* remove creds

Signed-off-by: kwlandry-acoustic <[email protected]>

* Snapshot clean up

Signed-off-by: kwlandry-acoustic <[email protected]>

* file rename alignment

Signed-off-by: kwlandry-acoustic <[email protected]>

* file rename alignment

Signed-off-by: kwlandry-acoustic <[email protected]>

* Rename EventProcessing.test.ts to eventprocessing.test.ts

---------

Signed-off-by: kwlandry-acoustic <[email protected]>
Co-authored-by: Joe Ayoub <[email protected]>
  • Loading branch information
KWLandry-acoustic and joe-ayoub-segment authored Mar 20, 2023
1 parent 2e39219 commit 5af062b
Show file tree
Hide file tree
Showing 11 changed files with 1,759 additions and 0 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`checkRTExist checkRTExist should return expected output 1`] = `[Function]`;

exports[`createSegmentEventsTable createSegmentEventsTable should return expected output 1`] = `[Function]`;

exports[`preChecksAndMaint preChecksAndMaint should return expected output 1`] = `[Function]`;

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// import { IntegrationError } from '@segment/actions-core'
// import { RequestClient } from '@segment/actions-core'
// import get from 'lodash/get'
// import { Settings } from '../../generated-types'
import nock from 'nock'
import { preChecksAndMaint, createSegmentEventsTable, checkRTExist } from '../tablemaintutilities'

jest.mock('@segment/actions-core')
jest.mock('@segment/actions-core')
jest.mock('lodash/get')
jest.mock('../../generated-types')

const ap = nock('https://api-campaign-us-2.goacoustic.com').post('/XMLAPI').reply(200)
ap.isDone

describe('preChecksAndMaint', () => {
it('should be present', () => {
expect(preChecksAndMaint).toBeDefined()
})

it('preChecksAndMaint should return expected output', async () => {
// const retValue = await preChecksAndMaint(request,settings);
expect(preChecksAndMaint).toMatchSnapshot()
})
})

describe('createSegmentEventsTable', () => {
it('should expose a function', () => {
expect(createSegmentEventsTable).toBeDefined()
})

it('createSegmentEventsTable should return expected output', async () => {
// const retValue = await createSegmentEventsTable(request,settings,auth);
expect(createSegmentEventsTable).toMatchSnapshot()
})
})

describe('checkRTExist', () => {
afterAll(nock.restore)
afterEach(nock.cleanAll)

it('should expose a function', () => {
expect(checkRTExist).toBeDefined()
})

it('checkRTExist should return expected output', async () => {
const settings = {
a_pod: '2',
a_region: 'US'
}

const accessToken = ''

// nock.recorder.rec()
nock.back.setMode('record')

const checkResult = nock(`https://api-campaign-${settings.a_region}-${settings.a_pod}.goacoustic.com`, {
reqheaders: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'text/xml',
'user-agent': 'Segment (checkforRT)',
Connection: 'keep-alive',
'Accept-Encoding': 'gzip, deflate, br',
Accept: '*/*'
}
})
.persist()
.post(
'/XMLAPI',
`<Envelope>
<Body>
<GetLists>
<VISIBILITY>1 </VISIBILITY>
<LIST_TYPE> 15 </LIST_TYPE>
<INCLUDE_ALL_LISTS> True </INCLUDE_ALL_LISTS>
</GetLists>
</Body>
</Envelope>`
) //, body => body.username && body.password
.reply(200, {})

checkResult.isDone

expect(checkRTExist).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { IntegrationError } from '@segment/actions-core'
import { RequestClient } from '@segment/actions-core'
import get from 'lodash/get'
import { Settings } from '../generated-types'
import { Payload } from '../receiveEvents/generated-types'
import { accToken, eventTableListId } from './tablemaintutilities'

export function parseSections(section: { [key: string]: string }, nestDepth: number) {
const parseResults: { [key: string]: string } = {}
//if (nestDepth > 5) return parseResults
if (nestDepth > 5)
throw new IntegrationError(
'Event data exceeds nesting depth. Restate event data to avoid nesting attributes more than 5 levels deep'
)

for (const key of Object.keys(section)) {
if (typeof section[key] === 'object') {
nestDepth++
const nested: { [key: string]: string } = parseSections(
section[key] as {} as { [key: string]: string },
nestDepth
)
for (const nestedKey of Object.keys(nested)) {
parseResults[`${key}.${nestedKey}`] = nested[nestedKey]
}
} else {
parseResults[key] = section[key]
}
}
return parseResults
}

export function addUpdateEvents(payload: Payload, email: string) {
let eventName = ''
let eventValue = ''
let xmlRows = ''

//Event Source
const eventSource = get(payload, 'type', 'Null') + ' Event'

//Timestamp
// "timestamp": "2023-02-07T02:19:23.469Z"`
const timestamp = get(payload, 'timestamp', 'Null')

//Audience
if (get(payload, 'context.personas.computation_class', 'Null') === 'audience') {
const ak = get(payload, 'context.personas.computation_key', 'Null')
const av = `properties.${ak}`
const audiStatus = get(payload, av, 'Null')
if (audiStatus) eventValue = 'Audience Entered'
if (!audiStatus) eventValue = 'Audience Exited'
eventName = ak

xmlRows += `
<ROW>
<COLUMN name="EMAIL"> <![CDATA[${email}]]></COLUMN>
<COLUMN name="EventSource"> <![CDATA[${eventSource}]]></COLUMN>
<COLUMN name="EventName"> <![CDATA[${eventName}]]></COLUMN>
<COLUMN name="EventValue"> <![CDATA[${eventValue}]]></COLUMN>
<COLUMN name="Event Timestamp"> <![CDATA[${timestamp}]]></COLUMN>
</ROW>`
}

let propertiesTraitsKV: { [key: string]: string } = {}

if (payload.traits)
propertiesTraitsKV = {
...propertiesTraitsKV,
...parseSections(payload.traits as { [key: string]: string }, 0)
}
if (payload.properties)
propertiesTraitsKV = {
...propertiesTraitsKV,
...parseSections(payload.properties as { [key: string]: string }, 0)
}
if (payload.context)
propertiesTraitsKV = {
...propertiesTraitsKV,
...parseSections(payload.context as { [key: string]: string }, 0)
}

console.log()

//Wrap Properties and Traits into XML
for (const e in propertiesTraitsKV) {
const eventName = e
const eventValue = propertiesTraitsKV[e]

xmlRows += `
<ROW>
<COLUMN name="Email"> <![CDATA[${email}]]></COLUMN>
<COLUMN name="EventSource"> <![CDATA[${eventSource}]]></COLUMN>
<COLUMN name="EventName"> <![CDATA[${eventName}]]></COLUMN>
<COLUMN name="EventValue"> <![CDATA[${eventValue}]]></COLUMN>
<COLUMN name="Event Timestamp"> <![CDATA[${timestamp}]]></COLUMN>
</ROW>`
}
return xmlRows
}

export const postUpdates = async (
request: RequestClient,
settings: Settings,
xmlRows: string,
i: number
): Promise<Response> => {
const pup = await request(`https://api-campaign-${settings.a_region}-${settings.a_pod}.goacoustic.com/XMLAPI`, {
method: 'POST',
headers: {
'user-agent': `Segment Event Table processing ${i}`,
Authorization: `Bearer ${accToken}`
},
body: `<Envelope>
<Body>
<InsertUpdateRelationalTable>
<TABLE_ID>${eventTableListId} </TABLE_ID>
<ROWS>
${xmlRows}
</ROWS>
</InsertUpdateRelationalTable>
</Body>
</Envelope>`
})

return pup
}
Loading

0 comments on commit 5af062b

Please sign in to comment.