-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Google Sheets] Add SyncMode support (#2214)
Co-authored-by: Maryam Sharif <[email protected]>
- Loading branch information
1 parent
a01e339
commit 7d29315
Showing
5 changed files
with
500 additions
and
1 deletion.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...estination-actions/src/destinations/google-sheets/__tests__/postSheet2.operations.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/destination-actions/src/destinations/google-sheets/postSheet2/generated-types.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
packages/destination-actions/src/destinations/google-sheets/postSheet2/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.