Skip to content

Commit

Permalink
moving dynamic field code to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-ayoub-segment committed Oct 3, 2024
1 parent b9c4b7e commit 5022f87
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,104 @@ import { HubSpotError } from '../../errors'
import { HUBSPOT_BASE_URL } from '../../properties'
import { SUPPORTED_HUBSPOT_OBJECT_TYPES } from '../constants'
import { DynamicFieldResponse } from '@segment/actions-core'
import { Payload } from '../generated-types'
import { DynamicFieldContext } from '@segment/actions-core/destination-kittypes'

enum AssociationCategory {
HUBSPOT_DEFINED = 'HUBSPOT_DEFINED',
USER_DEFINED = 'USER_DEFINED',
INTEGRATOR_DEFINED = 'INTEGRATOR_DEFINED'
}

export async function dynamicReadIdFields(request: RequestClient, objectType: string): Promise<DynamicFieldResponse> {
export const dynamicFields = {
object_details: {
object_type: async (request: RequestClient) => {
return await dynamicReadObjectTypes(request)
},
id_field_name: async (request: RequestClient, { payload }: {payload: Payload}) => {
const fromObjectType = payload?.object_details?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the 'Object Type' field")
}

return await dynamicReadIdFields(request, fromObjectType)
},
property_group: async (request: RequestClient, { payload }: {payload: Payload}) => {
const fromObjectType = payload?.object_details?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the 'Object Type' field")
}

return await dynamicReadPropertyGroups(request, fromObjectType)
}
},
properties: {
__keys__: async (request: RequestClient, { payload }: {payload: Payload}) => {
const fromObjectType = payload?.object_details?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the 'Object Type' field")
}

return await dynamicReadProperties(request, fromObjectType, false)
}
},
sensitive_properties: {
__keys__: async (request: RequestClient, { payload }: {payload: Payload}) => {
const fromObjectType = payload?.object_details?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the 'Object Type' field")
}

return await dynamicReadProperties(request, fromObjectType, true)
}
},
associations: {
object_type: async (request: RequestClient) => {
return await dynamicReadObjectTypes(request)
},
association_label: async (request: RequestClient, { dynamicFieldContext, payload }: {dynamicFieldContext?: DynamicFieldContext, payload: Payload}) => {
const selectedIndex = dynamicFieldContext?.selectedArrayIndex

if (selectedIndex === undefined) {
throw new Error('Selected array index is missing')
}

const fromObjectType = payload?.object_details?.object_type
const toObjectType = payload?.associations?.[selectedIndex]?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the from 'Object Type' field")
}

if (!toObjectType) {
throw new Error("Select a value from the 'To Object Type' field")
}

return await dynamicReadAssociationLabels(request, fromObjectType, toObjectType)
},
id_field_name: async (request: RequestClient, { dynamicFieldContext, payload }: {dynamicFieldContext?: DynamicFieldContext, payload: Payload}) => {
const selectedIndex = dynamicFieldContext?.selectedArrayIndex

if (selectedIndex === undefined) {
throw new Error('Selected array index is missing')
}

const toObjectType = payload?.associations?.[selectedIndex]?.object_type

if (!toObjectType) {
throw new Error("Select a value from the 'To Object Type' field")
}

return await dynamicReadIdFields(request, toObjectType)
}
}
}

async function dynamicReadIdFields(request: RequestClient, objectType: string): Promise<DynamicFieldResponse> {
interface ResultItem {
label: string
name: string
Expand Down Expand Up @@ -78,7 +168,7 @@ export async function dynamicReadIdFields(request: RequestClient, objectType: st
}
}

export async function dynamicReadPropertyGroups(
async function dynamicReadPropertyGroups(
request: RequestClient,
objectType: string
): Promise<DynamicFieldResponse> {
Expand Down Expand Up @@ -133,7 +223,7 @@ export async function dynamicReadPropertyGroups(
}
}

export async function dynamicReadAssociationLabels(
async function dynamicReadAssociationLabels(
request: RequestClient,
fromObjectType: string,
toObjectType: string
Expand Down Expand Up @@ -191,7 +281,7 @@ export async function dynamicReadAssociationLabels(
}
}

export async function dynamicReadObjectTypes(request: RequestClient): Promise<DynamicFieldResponse> {
async function dynamicReadObjectTypes(request: RequestClient): Promise<DynamicFieldResponse> {
interface ResultItem {
labels: { singular: string; plural: string }
fullyQualifiedName: string
Expand Down Expand Up @@ -238,7 +328,7 @@ export async function dynamicReadObjectTypes(request: RequestClient): Promise<Dy
}
}

export async function dynamicReadProperties(
async function dynamicReadProperties(
request: RequestClient,
objectType: string,
sensitive: boolean
Expand Down Expand Up @@ -300,4 +390,4 @@ export async function dynamicReadProperties(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Payload } from './generated-types'
import { commonFields } from './common-fields'
import { Client } from './client'
import { AssociationSyncMode, SyncMode, SchemaMatch } from './types'
import { dynamicReadAssociationLabels, dynamicReadIdFields, dynamicReadObjectTypes, dynamicReadPropertyGroups, dynamicReadProperties } from './functions/dynamic-field-functions'
import { dynamicFields } from './functions/dynamic-field-functions'
import { compareToCache, saveSchemaToCache,} from './functions/cache-functions'
import { validate } from './functions/validation-functions'
import { objectSchema } from './functions/schema-functions'
Expand All @@ -29,93 +29,7 @@ const action: ActionDefinition<Settings, Payload> = {
fields: {
...commonFields
},
dynamicFields: {
object_details: {
object_type: async (request) => {
return await dynamicReadObjectTypes(request)
},
id_field_name: async (request, { payload }) => {
const fromObjectType = payload?.object_details?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the 'Object Type' field")
}

return await dynamicReadIdFields(request, fromObjectType)
},
property_group: async (request, { payload }) => {
const fromObjectType = payload?.object_details?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the 'Object Type' field")
}

return await dynamicReadPropertyGroups(request, fromObjectType)
}
},
properties: {
__keys__: async (request, { payload }) => {
const fromObjectType = payload?.object_details?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the 'Object Type' field")
}

return await dynamicReadProperties(request, fromObjectType, false)
}
},
sensitive_properties: {
__keys__: async (request, { payload }) => {
const fromObjectType = payload?.object_details?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the 'Object Type' field")
}

return await dynamicReadProperties(request, fromObjectType, true)
}
},
associations: {
object_type: async (request) => {
return await dynamicReadObjectTypes(request)
},
association_label: async (request, { dynamicFieldContext, payload }) => {
const selectedIndex = dynamicFieldContext?.selectedArrayIndex

if (selectedIndex === undefined) {
throw new Error('Selected array index is missing')
}

const fromObjectType = payload?.object_details?.object_type
const toObjectType = payload?.associations?.[selectedIndex]?.object_type

if (!fromObjectType) {
throw new Error("Select a value from the from 'Object Type' field")
}

if (!toObjectType) {
throw new Error("Select a value from the 'To Object Type' field")
}

return await dynamicReadAssociationLabels(request, fromObjectType, toObjectType)
},
id_field_name: async (request, { dynamicFieldContext, payload }) => {
const selectedIndex = dynamicFieldContext?.selectedArrayIndex

if (selectedIndex === undefined) {
throw new Error('Selected array index is missing')
}

const toObjectType = payload?.associations?.[selectedIndex]?.object_type

if (!toObjectType) {
throw new Error("Select a value from the 'To Object Type' field")
}

return await dynamicReadIdFields(request, toObjectType)
}
}
},
dynamicFields,
perform: async (request, { payload, syncMode }) => {
return await send(request, [payload], syncMode as SyncMode)
},
Expand Down

0 comments on commit 5022f87

Please sign in to comment.