Skip to content

Commit

Permalink
Working version of PUT gristlabs#601
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent authored and Florent F committed Aug 2, 2023
1 parent 661ac64 commit 54330f9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 29 deletions.
42 changes: 20 additions & 22 deletions app/server/lib/DocApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,29 +693,27 @@ export class DocWorkerApi {
throw new ApiError(`Table not found "${tableId}"`, 404);
}
const body = req.body as Types.ColumnsPut;
// interface TriagedColumnsToOperate {
// columnsToAdd: Types.Record[];
// columnsToUpdate: Types.Record[];
// }
// const { columnsToAdd, columnsToUpdate } = body.columns.reduce<TriagedColumnsToOperate>((acc, col) => {
// const id = columnsTable.findMatchingRowId({parentId: tableRef, colId: col.id});
// const columnDesc = {...col, id};
// return {
// columnsToAdd: [...acc.columnsToAdd, ...(id ? [] : [columnDesc])],
// columnsToUpdate: [...acc.columnsToUpdate, ...(id ? [columnDesc] : [])]
// }
// }, { columnsToAdd: [], columnsToUpdate: [] });
const ops = getTableOperations(req, activeDoc, "_grist_Tables_column");
const records: Types.AddOrUpdateRecord[] = body.columns.map(col => {
interface TriagedColumnsToOperate {
columnsToAdd: Types.RecordWithStringId[];
columnsToUpdate: Types.Record[];
}
const { columnsToAdd, columnsToUpdate } = body.columns.reduce<TriagedColumnsToOperate>((acc, col) => {
const id = columnsTable.findMatchingRowId({parentId: tableRef, colId: col.id});
return { require: { id }, fields: col.fields };
});
const options = {
add: !isAffirmative(req.query.noadd),
update: !isAffirmative(req.query.noupdate),
allowEmptyRequire: isAffirmative(req.query.allow_empty_require),
};
await ops.upsert(records, options);
return {
columnsToAdd: [...acc.columnsToAdd, ...(id ? [] : [ col ])],
columnsToUpdate: [...acc.columnsToUpdate, ...(id ? [{ ...col, id }] : [])]
};
}, { columnsToAdd: [], columnsToUpdate: [] });
const addActions = columnsToAdd.map(
({id, fields}) => ['AddVisibleColumn', tableId, id, fields || {}]
);
const updateActions = columnsToUpdate.map(
({id: colId, fields}) => ['UpdateRecord', '_grist_Tables_column', colId, fields || {}]
);
await handleSandboxError(tableId, [],
activeDoc.applyUserActions(docSessionFromRequest(req), [...updateActions, ...addActions])
);
res.json(null);
})
);

Expand Down
79 changes: 72 additions & 7 deletions test/server/lib/DocApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {arrayRepeat} from 'app/common/gutil';
import {WebhookSummary} from 'app/common/Triggers';
import {DocAPI, DocState, UserAPIImpl} from 'app/common/UserAPI';
import {testDailyApiLimitFeatures} from 'app/gen-server/entity/Product';
import {AddOrUpdateRecord, ColumnsPut, Record as ApiRecord} from 'app/plugin/DocApiTypes';
import {AddOrUpdateRecord, Record as ApiRecord, ColumnsPut} from 'app/plugin/DocApiTypes';
import {CellValue, GristObjCode} from 'app/plugin/GristData';
import {
applyQueryParameters,
Expand Down Expand Up @@ -856,20 +856,85 @@ function testDocApi() {
id: "Foo",
fields: {
type: "Text",
colId: "Foo",
label: "Foo"
label: "FooLabel"
}
}]
}
};
// when
const resp = await axios.put(url, body, chimpy);

// then
console.log("resp.statusText = ", resp.statusText);
console.log("url = ", url);
console.log("resp.data = ", resp.data);
assert.equal(resp.status, 200);
}

{
// when
const resp = await axios.get(url, chimpy);

// then
assert.equal(resp.status, 200);
const fooColumn = resp.data.columns.find((col: {id: string}) => col.id === "Foo");
assert.exists(fooColumn, 'Expecting to have a column with id "Foo"');
assert.deepInclude(fooColumn.fields, { type: "Text", label: "FooLabel" });
}

{
const NEW_COLUMN_ID = "Bar";
const EXISTING_COLUMN_ID = "Foo";
const NEW_EXISTING_COLUMN_ID = "FooNew";
const body: ColumnsPut = {
columns: [{
id: NEW_COLUMN_ID,
fields: {
type: "Text",
label: "Bar",
}
}, {
id: EXISTING_COLUMN_ID,
fields: {
type: "Numeric",
colId: NEW_EXISTING_COLUMN_ID
}
}]
};

// when
const resp = await axios.put(url, body, chimpy);

// then
assert.equal(resp.status, 200);
}

{
// when
const resp = await axios.get(url, chimpy);

// then
assert.equal(resp.status, 200);
const fieldsByColId = new Map(
resp.data.columns.map(
({id, fields}: {id: string, fields: object}) => [id, fields]
)
);

const fooNewFields = fieldsByColId.get("FooNew");
assert.exists(fooNewFields, 'Expecting to have a column with id "FooNew"');
assert.deepInclude(
fooNewFields,
{ type: "Numeric", label: "FooLabel" },
"Expecting to have type changed to Numeric and label unchanged"
);

assert.isFalse(
fieldsByColId.has("Foo"),
'Expecting to not have a column with id "Foo" anymore as the ID has been renamed'
);

const barFields = fieldsByColId.get('Bar');
assert.exists(barFields, 'Bar column should have been added');
assert.deepInclude(barFields, { type: "Text", label: "Bar" });

}
});
});

Expand Down

0 comments on commit 54330f9

Please sign in to comment.