Skip to content

Commit

Permalink
[Google Sheets] Add SyncMode support (#2214)
Browse files Browse the repository at this point in the history
Co-authored-by: Maryam Sharif <[email protected]>
  • Loading branch information
marinhero and maryamsharif authored Aug 13, 2024
1 parent a01e339 commit 7d29315
Show file tree
Hide file tree
Showing 5 changed files with 500 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { ExecuteInput } from '@segment/actions-core'
import { Settings } from '../generated-types'
import { Payload } from '../postSheet2/generated-types'
import PostSheet from '../postSheet2/index'
import { GoogleSheets, GetResponse } from '../googleapis/index'
import { CONSTANTS } from '../constants'

jest.mock('../constants', () => ({
CONSTANTS: {
MAX_CELLS: 300000
}
}))

const mockGoogleSheets = {
get: jest.fn(),
batchUpdate: jest.fn(),
append: jest.fn()
}

jest.mock('../googleapis/index', () => {
const original = jest.requireActual('../googleapis/index')
return {
...original,
GoogleSheets: jest.fn().mockImplementation(() => {
return mockGoogleSheets
})
}
})

describe('Google Sheets', () => {
describe('postSheet2', () => {
beforeEach(() => {
mockGoogleSheets.get.mockClear()
mockGoogleSheets.batchUpdate.mockClear()
mockGoogleSheets.append.mockClear()
})

const add_data: Partial<ExecuteInput<Settings, Payload[]>> = {
payload: [
{
record_identifier: 'record_id',
spreadsheet_id: 'spreadsheet_id',
spreadsheet_name: 'spreadsheet_name',
data_format: 'data_format',
fields: { column1: 'value1', column2: 'value2' }
}
],
syncMode: 'add'
}

it('should call append if the new data is not found in get response', async () => {
const getResponse: Partial<GetResponse> = {
values: [['unknown_id']]
}

mockGoogleSheets.get.mockResolvedValue({
data: getResponse
})

await PostSheet.performBatch?.(jest.fn(), add_data as ExecuteInput<Settings, Payload[], string | undefined>)

expect(GoogleSheets).toHaveBeenCalled()
expect(mockGoogleSheets.get).toHaveBeenCalled()
expect(mockGoogleSheets.append).toHaveBeenCalled()
expect(mockGoogleSheets.batchUpdate).toHaveBeenCalled() // batchUpdate always gets called to write columns
})

it('should call update (and not append) if the new data is found in get response', async () => {
// Make sure the spreadsheet contains the event from the payload
const getResponse: Partial<GetResponse> = {
values: [[add_data.payload?.[0].record_identifier as string]]
}

add_data.syncMode = 'update'

mockGoogleSheets.get.mockResolvedValue({
data: getResponse
})

await PostSheet.performBatch?.(jest.fn(), add_data as ExecuteInput<Settings, Payload[]>)

expect(GoogleSheets).toHaveBeenCalled()
expect(mockGoogleSheets.get).toHaveBeenCalled()
expect(mockGoogleSheets.append).not.toHaveBeenCalled()
expect(mockGoogleSheets.batchUpdate).toHaveBeenCalled()
})

it('should fail because number of cells limit is reached', async () => {
// Make sure the spreadsheet contains the event from the payload
CONSTANTS.MAX_CELLS = 1
const getResponse: Partial<GetResponse> = {
values: [['id'], ['1234'], ['12345']]
}

mockGoogleSheets.get.mockResolvedValue({
data: getResponse
})

await expect(
PostSheet.performBatch?.(jest.fn(), add_data as ExecuteInput<Settings, Payload[]>)
).rejects.toThrowError('Sheet has reached maximum limit')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { DestinationDefinition } from '@segment/actions-core'
import type { Settings } from './generated-types'

import postSheet from './postSheet'
import postSheet2 from './postSheet2'
interface RefreshTokenResponse {
access_token: string
scope: string
Expand Down Expand Up @@ -51,7 +52,8 @@ const destination: DestinationDefinition<Settings> = {
// },

actions: {
postSheet
postSheet,
postSheet2
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { ActionDefinition, IntegrationError } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'

import { processData } from './operations2'

const action: ActionDefinition<Settings, Payload> = {
title: 'Post Sheet (Simplified)',
description: 'Write values to a Google Sheets spreadsheet.',
defaultSubscription: 'event = "updated" or event = "new"',
syncMode: {
description: 'Define how the records from your destination will be synced.',
label: 'How to sync records',
default: 'upsert',
choices: [
{
label:
'If a record with the specified identifier is found, it will be updated. If not, a new row will be created.',
value: 'upsert'
},
{
label: "Add a new record when the specified identifier doesn't exist. If it does, it will be skipped.",
value: 'add'
},
{
label:
"Update a record if a match with the specified identifier is found. Do nothing if the row doesn't exist.",
value: 'update'
}
]
},
// TODO: Hide record_identifier and operation_type
fields: {
record_identifier: {
label: 'Record Identifier',
description: 'Property which uniquely identifies each row in the spreadsheet.',
type: 'string',
required: true,
default: { '@path': '$.__segment_id' }
},
spreadsheet_id: {
label: 'Spreadsheet ID',
description:
'The identifier of the spreadsheet. You can find this value in the URL of the spreadsheet. e.g. https://docs.google.com/spreadsheets/d/{SPREADSHEET_ID}/edit',
type: 'string',
required: true,
default: ''
},
spreadsheet_name: {
label: 'Spreadsheet Name',
description:
'The name of the spreadsheet. You can find this value on the tab at the bottom of the spreadsheet. Please provide a valid name of a sheet that already exists.',
type: 'string',
required: true,
default: 'Sheet1'
},
data_format: {
label: 'Data Format',
description:
'The way Google will interpret values. If you select raw, values will not be parsed and will be stored as-is. If you select user entered, values will be parsed as if you typed them into the UI. Numbers will stay as numbers, but strings may be converted to numbers, dates, etc. following the same rules that are applied when entering text into a cell via the Google Sheets UI.',
type: 'string',
required: true,
default: 'RAW',
choices: [
{ label: 'Raw', value: 'RAW' },
{ label: 'User Entered', value: 'USER_ENTERED' }
]
},
fields: {
label: 'Fields',
description: `
The fields to write to the spreadsheet.
On the left-hand side, input the name of the field as it will appear in the Google Sheet.
On the right-hand side, select the field from your data model that maps to the given field in your sheet.
---
`,
type: 'object',
required: true,
defaultObjectUI: 'keyvalue:only'
},
enable_batching: {
type: 'boolean',
label: 'Batch Data to Google Sheets',
description: 'Set as true to ensure Segment sends data to Google Sheets in batches. Please do not set to false.',
default: true
}
},
perform: (request, { payload, syncMode }) => {
if (!syncMode) {
throw new IntegrationError('Sync mode is required for this action.', 'INVALID_REQUEST_DATA', 400)
}
return processData(request, [payload], syncMode)
},
performBatch: (request, { payload, syncMode }) => {
if (!syncMode) {
throw new IntegrationError('Sync mode is required for this action.', 'INVALID_REQUEST_DATA', 400)
}
return processData(request, payload, syncMode)
}
}

export default action
Loading

0 comments on commit 7d29315

Please sign in to comment.