Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLU-309: [TILES-ATOMIC-INCREMENT-4] backend implementation for increment/decrement row value #835

Open
wants to merge 1 commit into
base: fix/tiles/update-operator
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions packages/backend/src/apps/tiles/actions/update-row/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { IRawAction } from '@plumber/types'

import StepError from '@/errors/step'
import { stripInvalidKeys } from '@/models/dynamodb/helpers'
import { patchTableRow } from '@/models/dynamodb/table-row'
import {
autoMarshallNumberStrings,
stripInvalidKeys,
} from '@/models/dynamodb/helpers'
import { PatchRowInput, patchTableRow } from '@/models/dynamodb/table-row'
import TableCollaborator from '@/models/table-collaborators'
import TableColumnMetadata from '@/models/table-column-metadata'

Expand Down Expand Up @@ -117,7 +120,7 @@ const action: IRawAction = {
const { tableId, rowId, rowData } = $.step.parameters as {
tableId: string
rowId: string
rowData: { columnId: string; cellValue: string }[]
rowData: { columnId: string; cellValue: string; operator?: string }[]
}

if (!tableId) {
Expand Down Expand Up @@ -150,20 +153,48 @@ const action: IRawAction = {
return
}

function assertNumber(value: string): void {
if (typeof autoMarshallNumberStrings(value) !== 'number') {
throw new StepError(
'Add/subtract value must be a number',
'The value to add or subtract by must be a number.',
$.step.position,
$.app.name,
)
}
}

const patchData = {
...rowData.reduce((acc, { columnId, cellValue }) => {
// Check that the column still exists
if (columnIds.includes(columnId)) {
acc[columnId] = cellValue
}
return acc
}, {} as Record<string, string>),
...rowData.reduce(
(acc, { columnId, cellValue, operator }) => {
// Check that the column still exists
if (columnIds.includes(columnId)) {
switch (operator) {
case 'add':
assertNumber(cellValue)
acc.add[columnId] = cellValue
break
case 'subtract':
assertNumber(cellValue)
acc.subtract[columnId] = cellValue
break
// we default to set, since old operators will be undefined
default:
acc.set[columnId] = cellValue
break
}
}
return acc
},
{ set: {}, add: {}, subtract: {} } as PatchRowInput['patchData'],
),
}

try {
const updatedRow = await patchTableRow({
tableId,
rowId,
data: patchData,
patchData,
})

const updatedRowData = stripInvalidKeys({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export async function generateColumnNameMetadata(

const columns = await TableColumnMetadata.query()
.findByIds(columnIds)
.select('id', 'name')
.select('id', 'name', 'position')

columns.forEach((column) => {
columnMetadata[column.id] = {
label: column.name,
order: column.position,
}
})
return { [parentKey]: columnMetadata }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,124 @@ describe('dynamodb table row functions', () => {
const updatedRow = await patchTableRow({
tableId: dummyTable.id,
rowId: row.rowId,
data: newData,
patchData: {
set: newData,
},
})

expect(updatedRow.data).toEqual({ ...data, ...newData })
})

it('should set, add or subtract values for the row', async () => {
const data = {
[dummyColumnIds[0]]: 10,
[dummyColumnIds[1]]: 20,
[dummyColumnIds[2]]: 30,
}
const row = await createTableRow({ tableId: dummyTable.id, data })
const updatedRow = await patchTableRow({
tableId: dummyTable.id,
rowId: row.rowId,
patchData: {
set: {
[dummyColumnIds[0]]: '1',
},
add: {
[dummyColumnIds[1]]: '2',
},
subtract: {
[dummyColumnIds[2]]: '3',
},
},
})

const expectedData = {
[dummyColumnIds[0]]: 1,
[dummyColumnIds[1]]: 22,
[dummyColumnIds[2]]: 27,
}

expect(updatedRow.data).toEqual(expectedData)
})

it('should throw a step error if operand is not a number', async () => {
const data = {
[dummyColumnIds[0]]: 10,
[dummyColumnIds[1]]: 20,
[dummyColumnIds[2]]: 30,
}
const row = await createTableRow({ tableId: dummyTable.id, data })

await expect(
patchTableRow({
tableId: dummyTable.id,
rowId: row.rowId,
patchData: {
set: {
[dummyColumnIds[0]]: '1',
},
add: {
[dummyColumnIds[1]]: 'add',
},
subtract: {
[dummyColumnIds[2]]: '3',
},
},
}),
).rejects.toThrow(Error)
})

it('should throw a generic error if original value is not a number', async () => {
const data = {
[dummyColumnIds[0]]: 10,
[dummyColumnIds[1]]: 20,
[dummyColumnIds[2]]: 'string',
}
const row = await createTableRow({ tableId: dummyTable.id, data })

await expect(
patchTableRow({
tableId: dummyTable.id,
rowId: row.rowId,
patchData: {
set: {
[dummyColumnIds[0]]: '1',
},
add: {
[dummyColumnIds[1]]: '2re',
},
subtract: {
[dummyColumnIds[2]]: '3',
},
},
}),
).rejects.toThrow(Error)
})

it('should work fine if only add/subtract is provided', async () => {
const data = {
[dummyColumnIds[0]]: 10,
[dummyColumnIds[1]]: 20,
[dummyColumnIds[2]]: 30,
}
const row = await createTableRow({ tableId: dummyTable.id, data })

const updatedRow = await patchTableRow({
tableId: dummyTable.id,
rowId: row.rowId,
patchData: {
subtract: {
[dummyColumnIds[2]]: '3',
},
},
})
expect(updatedRow.data).toEqual({
[dummyColumnIds[0]]: 10,
[dummyColumnIds[1]]: 20,
[dummyColumnIds[2]]: 27,
})
})

it('should not change if no columns are provided', async () => {
const data = generateMockTableRowData({
columnIds: dummyColumnIds,
Expand All @@ -361,7 +473,7 @@ describe('dynamodb table row functions', () => {
const updatedRow = await patchTableRow({
tableId: dummyTable.id,
rowId: row.rowId,
data: {},
patchData: {},
})

expect(updatedRow.data).toEqual(data)
Expand All @@ -375,7 +487,7 @@ describe('dynamodb table row functions', () => {
const updatedRow = await patchTableRow({
tableId: dummyTable.id,
rowId: row.rowId,
data: { [dummyColumnIds[0]]: '123' },
patchData: { set: { [dummyColumnIds[0]]: '123' } },
})
expect(updatedRow.data).toEqual({ ...data, [dummyColumnIds[0]]: 123 })
})
Expand All @@ -388,7 +500,7 @@ describe('dynamodb table row functions', () => {
const updatedRow = await patchTableRow({
tableId: dummyTable.id,
rowId: row.rowId,
data: { [dummyColumnIds[0]]: '123' },
patchData: { [dummyColumnIds[0]]: '123' },
})
expect(updatedRow.updatedAt).toBeGreaterThan(row.updatedAt)
})
Expand Down
18 changes: 14 additions & 4 deletions packages/backend/src/models/dynamodb/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export function handleDynamoDBError(error: unknown): never {
delayType: 'step',
})
}
if (
message.includes(
'An operand in the update expression has an incorrect data type',
) ||
message.includes('Incorrect operand type for operator or function')
) {
throw new Error(
'You can only add or subtract numbers, please check your values',
)
}
throw new Error('DynamoDB Internal Error: ' + message)
}
if (error.code < 6000) {
Expand Down Expand Up @@ -89,11 +99,11 @@ export function stripInvalidKeys({
columnIds: string[]
data: Record<string, string | number>
}) {
const validKeys = new Set(columnIds)
const strippedData: Record<string, string | number> = {}
for (const [key, value] of Object.entries(data)) {
if (validKeys.has(key)) {
strippedData[key] = value
// Iterate through columnIds to maintain order
for (const columnId of columnIds) {
if (columnId in data) {
strippedData[columnId] = data[columnId]
}
}
return strippedData
Expand Down
47 changes: 31 additions & 16 deletions packages/backend/src/models/dynamodb/table-row/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CreateRowInput,
CreateRowsInput,
DeleteRowsInput,
PatchRowInput,
TableRowFilter,
TableRowFilterOperator,
TableRowItem,
Expand Down Expand Up @@ -227,26 +228,40 @@ export const updateTableRow = async ({
export const patchTableRow = async ({
rowId,
tableId,
data: patchData,
}: UpdateRowInput): Promise<TableRowItem> => {
patchData,
}: PatchRowInput): Promise<TableRowItem> => {
try {
const res = await TableRow.patch({
const patchOperation = TableRow.patch({
tableId,
rowId,
}).data(({ data }, { set, add, subtract }) => {
// Handle set operations
Object.entries(patchData.set || {}).forEach(
([key, value]: [string, string]) => {
set(data[key], value ? autoMarshallNumberStrings(value) : '')
},
)

// Handle add operations
Object.entries(patchData.add || {}).forEach(
([key, value]: [string, string]) => {
add(data[key], autoMarshallNumberStrings(value))
},
)

// Handle subtract operations
Object.entries(patchData.subtract || {}).forEach(
([key, value]: [string, string]) => {
subtract(data[key], autoMarshallNumberStrings(value))
},
)
})
.data(({ data }, { set }) => {
for (const key in patchData) {
set(
data[key],
patchData[key] ? autoMarshallNumberStrings(patchData[key]) : '',
)
}
})
.go({
ignoreOwnership: true,
// Return the new row data
response: 'all_new',
})

const res = await patchOperation.go({
ignoreOwnership: true,
response: 'all_new',
})

return res.data
} catch (e: unknown) {
handleDynamoDBError(e)
Expand Down
10 changes: 9 additions & 1 deletion packages/backend/src/models/dynamodb/table-row/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ export type CreateRowsInput = {
tableId: string
dataArray: Array<TableRowItem['data']>
}
export type UpdateRowInput = Pick<TableRowItem, 'tableId' | 'data' | 'rowId'>
export type UpdateRowInput = Pick<TableRowItem, 'tableId' | 'rowId' | 'data'>

export type PatchRowInput = Pick<TableRowItem, 'tableId' | 'rowId'> & {
patchData: {
set?: TableRowItem['data']
add?: TableRowItem['data']
subtract?: TableRowItem['data']
}
}
export interface DeleteRowsInput {
tableId: string
rowIds: string[]
Expand Down
8 changes: 5 additions & 3 deletions packages/backend/src/models/table-column-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ class TableColumnMetadata extends Base {
})

static getColumns = async (tableId: string, $?: IGlobalVariable) => {
const columns = await TableColumnMetadata.query().where({
table_id: tableId,
})
const columns = await TableColumnMetadata.query()
.where({
table_id: tableId,
})
.orderBy('position')

if (columns.length === 0) {
throw new StepError(
Expand Down
6 changes: 3 additions & 3 deletions packages/frontend/src/helpers/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ function sortVariables(variables: Variable[]): void {
variables.sort((a, b) => {
// Put vars with null order last, but preserve ordering (via `sort`'s
// stability) if both are null.
if (!a.order && !b.order) {
if (a.order == null && !b.order == null) {
return 0
}
if (!a.order) {
if (a.order == null) {
return 1
}
if (!b.order) {
if (b.order == null) {
return -1
}
return a.order - b.order
Expand Down
Loading