Skip to content

Commit

Permalink
more improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
humandad committed Mar 14, 2024
1 parent bf066b5 commit f1042b5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
19 changes: 13 additions & 6 deletions apps/smartsheet/bundle/bots/load/smartsheet_load/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,17 @@ export default function smartsheet_load(bot: LoadBotApi) {
})
)

bot.log.info("fvc", fieldsByColumn)
const set = (
obj: Record<string, unknown>,
path: string[],
value: unknown
) => {
path.reduce((acc, key, i) => {
if (acc[key] === undefined) acc[key] = {}
if (i === path.length - 1) acc[key] = value
return acc[key]
}, obj)
}

const maxRecordForPagination = doPagination
? (queryParams.pageSize as number) - 1
Expand Down Expand Up @@ -163,11 +173,8 @@ export default function smartsheet_load(bot: LoadBotApi) {
record[fieldKey] = {}
existing = record[fieldKey] as Record<string, unknown>
}
const mapValue = {
...existing,
[fieldInfo.path]: cell.value,
}
record[fieldKey] = mapValue
set(existing, fieldInfo.path.split("->"), cell.value)
record[fieldKey] = existing
} else {
record[fieldKey] = cell.value
}
Expand Down
10 changes: 8 additions & 2 deletions apps/smartsheet/bundle/bots/save/smartsheet_save/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export default function smartsheet_save(bot: SaveBotApi) {

const fieldMappings = mappingResult["uesio/smartsheet.fields"]

const get = (obj: Record<string, unknown>, path: string[]) => {
// If path is not defined or it has false value
if (!path) return undefined
return path.reduce((prevObj, key) => prevObj && prevObj[key], obj)
}

const getCellChanges = (changeData: Record<string, FieldValue>) =>
Object.keys(changeData).flatMap((fieldkey) => {
const fieldMetadata = fieldsMetadata[fieldkey]
Expand All @@ -94,8 +100,8 @@ export default function smartsheet_save(bot: SaveBotApi) {
if (!mapValue) {
return []
}
const valueToSet = mapValue[path]
if (!valueToSet) {
const valueToSet = get(mapValue, path.split("->"))
if (valueToSet === undefined) {
return []
}
return [
Expand Down
73 changes: 73 additions & 0 deletions apps/smartsheet/tests/smartsheet_load.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ const getSampleCollectionMetadata = () => ({
name: "status",
namespace: "uesio/smartsheet",
},
"uesio/smartsheet.extra": {
name: "extra",
namespace: "uesio/smartsheet",
type: "MAP",
},
})),
})
const uesioRow1 = {
Expand Down Expand Up @@ -119,6 +124,74 @@ describe("Smartsheet Load", () => {
expect(bot.addRecord).toHaveBeenCalledWith(uesioRow1)
expect(bot.addRecord).toHaveBeenCalledWith(uesioRow2)
})
it("should load data from Smartsheet with a map mapping", () => {
const bot = mockBot([row1, row2])
bot.load.mockReturnValue([
{
"uesio/smartsheet.sheet": {
"uesio/core.id": sheetId,
},
"uesio/smartsheet.fields": {
["uesio/smartsheet.extra"]: {
name: nameColumnId,
status: statusColumnId,
},
},
},
])
smartsheet_load(bot as unknown as LoadBotApi)
expect(bot.http.request).toHaveBeenCalledWith({
method: "GET",
url: `${smartSheetBaseUrl}/${sheetId}?includeAll=true`,
})
expect(bot.addRecord).toHaveBeenCalledWith({
"uesio/core.id": "1",
"uesio/smartsheet.extra": {
name: "Test",
status: "in_progress",
},
})
expect(bot.addRecord).toHaveBeenCalledWith({
"uesio/core.id": "2",
"uesio/smartsheet.extra": {
name: "Another test",
status: "completed",
},
})
})
it("should load data from Smartsheet with a multi-level map mapping", () => {
const bot = mockBot([row1, row2])
bot.load.mockReturnValue([
{
"uesio/smartsheet.sheet": {
"uesio/core.id": sheetId,
},
"uesio/smartsheet.fields": {
["uesio/smartsheet.extra"]: {
"info->name": nameColumnId,
"info->status": statusColumnId,
},
},
},
])
smartsheet_load(bot as unknown as LoadBotApi)
expect(bot.http.request).toHaveBeenCalledWith({
method: "GET",
url: `${smartSheetBaseUrl}/${sheetId}?includeAll=true`,
})
expect(bot.addRecord).toHaveBeenCalledWith({
"uesio/core.id": "1",
"uesio/smartsheet.extra": {
info: { name: "Test", status: "in_progress" },
},
})
expect(bot.addRecord).toHaveBeenCalledWith({
"uesio/core.id": "2",
"uesio/smartsheet.extra": {
info: { name: "Another test", status: "completed" },
},
})
})
it("should not bomb if no rows or cells are returned", () => {
let bot = mockBot(undefined)
bot.load.mockReturnValue(sampleMapping)
Expand Down

0 comments on commit f1042b5

Please sign in to comment.