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

AJ-954 - Add capability to edit a cell in Data table view in Azure #4588

Merged
merged 25 commits into from
Feb 6, 2024
Merged
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
14 changes: 14 additions & 0 deletions src/libs/ajax/WorkspaceDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash/fp';
import { authOpts, fetchWDS, jsonBody } from 'src/libs/ajax/ajax-common';
import {
RecordQueryResponse,
RecordResponseBody,
RecordTypeSchema,
SearchRequest,
TsvUploadResponse,
Expand Down Expand Up @@ -134,6 +135,19 @@ export const WorkspaceData = (signal) => ({
);
return res.json();
},
updateRecord: async (
root: string,
instanceId: string,
recordType: string,
recordId: string,
record: { [attribute: string]: any }
): Promise<RecordResponseBody> => {
const res = await fetchWDS(root)(
`${instanceId}/records/v0.2/${recordType}/${recordId}`,
_.mergeAll([authOpts(), jsonBody(record), { signal, method: 'PATCH' }])
);
return res.json();
},
getVersion: async (root: string): Promise<WDSVersionResponse> => {
const res = await fetchWDS(root)('version', _.merge(authOpts(), { signal }));
return res.json();
Expand Down
9 changes: 9 additions & 0 deletions src/libs/ajax/data-table-providers/DataTableProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// define metadata structures
export interface EntityTypeMetadata {
attributeNames: string[];
attributes: { name: string; datatype: string }[];
count: number;
idName: string;
}
Expand Down Expand Up @@ -61,6 +62,13 @@ export type UploadParameters = {
recordType: string;
};

export type RecordEditParameters = {
instance: string;
recordName: string;
recordId: string;
record: { [attribute: string]: any };
};

export type InvalidTsvOptions = {
fileImportModeMatches: boolean;
filePresent: boolean;
Expand Down Expand Up @@ -125,6 +133,7 @@ export interface DataTableFeatures {
supportsTypeRenaming: boolean;
supportsEntityRenaming: boolean;
supportsEntityUpdating: boolean;
supportsEntityUpdatingTypes?: string[];
supportsAttributeRenaming: boolean;
supportsAttributeDeleting: boolean;
supportsAttributeClearing: boolean;
Expand Down
140 changes: 140 additions & 0 deletions src/libs/ajax/data-table-providers/WdsDataTableProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,26 @@ describe('WdsDataTableProvider', () => {
return Promise.resolve({ message: 'Upload Succeeded', recordsModified: 1 });
};

const updateRecordMockImpl: WorkspaceDataContract['updateRecord'] = (
_root: string,
_instanceId: string,
_recordType: string,
_recordId: string,
_record: { [attribute: string]: any }
) => {
const expected: RecordAttributes = {
something: 123,
testAttr: 'string_value',
};
return Promise.resolve({ id: 'record1', type: 'test', attributes: expected });
};

const listAppsV2MockImpl = (_workspaceId: string): Promise<ListAppItem[]> => {
return Promise.resolve(testProxyUrlResponse);
};

let getRecords: jest.MockedFunction<WorkspaceDataContract['getRecords']>;
let updateRecord: jest.MockedFunction<WorkspaceDataContract['updateRecord']>;
let getCapabilities: jest.MockedFunction<WorkspaceDataContract['getCapabilities']>;
let deleteTable: jest.MockedFunction<WorkspaceDataContract['deleteTable']>;
let downloadTsv: jest.MockedFunction<WorkspaceDataContract['downloadTsv']>;
Expand All @@ -185,6 +200,7 @@ describe('WdsDataTableProvider', () => {

beforeEach(() => {
getRecords = jest.fn().mockImplementation(getRecordsMockImpl);
updateRecord = jest.fn().mockImplementation(updateRecordMockImpl);
getCapabilities = jest.fn().mockImplementation(getCapabilitiesMockImpl);
deleteTable = jest.fn().mockImplementation(deleteTableMockImpl);
downloadTsv = jest.fn().mockImplementation(downloadTsvMockImpl);
Expand All @@ -196,6 +212,7 @@ describe('WdsDataTableProvider', () => {
({
WorkspaceData: {
getRecords,
updateRecord,
getCapabilities,
deleteTable,
downloadTsv,
Expand Down Expand Up @@ -298,6 +315,24 @@ describe('WdsDataTableProvider', () => {
item: {
count: 7,
attributeNames: ['booleanAttr', 'numericAttr', 'stringAttr', 'timestamp'],
attributes: [
{
name: 'booleanAttr',
datatype: 'BOOLEAN',
},
{
name: 'stringAttr',
datatype: 'STRING',
},
{
name: 'numericAttr',
datatype: 'NUMBER',
},
{
name: 'timestamp',
datatype: 'STRING',
},
],
idName: 'stringAttr',
},
};
Expand Down Expand Up @@ -374,6 +409,16 @@ describe('WdsDataTableProvider', () => {
item: {
count: 7,
attributeNames: ['arrayOfNums', 'stringAttr'],
attributes: [
{
name: 'arrayOfNums',
datatype: 'ARRAY_OF_NUMBER',
},
{
name: 'stringAttr',
datatype: 'STRING',
},
],
idName: 'stringAttr',
},
};
Expand Down Expand Up @@ -444,6 +489,24 @@ describe('WdsDataTableProvider', () => {
item: {
count: 7,
attributeNames: ['numAttr', 'stringAttr', 'relationScalar', 'relationArray'],
attributes: [
{
name: 'relationScalar',
datatype: 'RELATION',
},
{
name: 'stringAttr',
datatype: 'STRING',
},
{
name: 'numericAttr',
datatype: 'NUMBER',
},
{
name: 'relationArray',
datatype: 'ARRAY_OF_RELATION',
},
],
idName: 'stringAttr',
},
};
Expand Down Expand Up @@ -518,6 +581,16 @@ describe('WdsDataTableProvider', () => {
item: {
count: 7,
attributeNames: ['mixedArrayRelationFirst', 'mixedArrayRelationLast'],
attributes: [
{
name: 'mixedArrayRelationFirst',
datatype: 'ARRAY_OF_RELATION',
},
{
name: 'mixedArrayRelationLast',
datatype: 'ARRAY_OF_RELATION',
},
],
idName: 'sys_name',
},
};
Expand Down Expand Up @@ -571,6 +644,16 @@ describe('WdsDataTableProvider', () => {
item: {
count: 7,
attributeNames: ['mixedArrayRelationFirst', 'mixedArrayRelationLast'],
attributes: [
{
name: 'mixedArrayRelationFirst',
datatype: 'ARRAY_OF_RELATION',
},
{
name: 'mixedArrayRelationLast',
datatype: 'ARRAY_OF_RELATION',
},
],
idName: 'sys_name',
},
};
Expand Down Expand Up @@ -691,6 +774,29 @@ describe('WdsDataTableProvider', () => {
});
});

describe('updateRecord', () => {
it('restructures a WDS response', async () => {
// Arrange
const provider = new TestableWdsProvider();

const expected: RecordAttributes = {
something: 123,
testAttr: 'string_value',
};

// Act
const actual = await provider.updateRecord({
instance: uuid,
recordName: 'test',
recordId: 'record1',
record: expected,
});

// Assert
expect(actual.attributes).toStrictEqual(expected);
});
});

describe('transformMetadata', () => {
it('restructures a WDS response', () => {
// Arrange
Expand Down Expand Up @@ -763,16 +869,50 @@ describe('transformMetadata', () => {
item: {
count: 7,
attributeNames: ['booleanAttr', 'stringAttr'],
attributes: [
{
name: 'booleanAttr',
datatype: 'BOOLEAN',
},
{
name: 'stringAttr',
datatype: 'STRING',
},
],
idName: 'item_id',
},
thing: {
count: 4,
attributeNames: ['numericAttr', 'stringAttr', 'timestamp'],
attributes: [
{
name: 'numericAttr',
datatype: 'NUMBER',
},
{
name: 'stringAttr',
datatype: 'STRING',
},
{
name: 'timestamp',
datatype: 'STRING',
},
],
idName: 'thing_id',
},
system: {
count: 12345,
attributeNames: ['one', 'two'],
attributes: [
{
name: 'one',
datatype: 'NUMBER',
},
{
name: 'two',
datatype: 'STRING',
},
],
idName: 'sys_name',
},
};
Expand Down
29 changes: 27 additions & 2 deletions src/libs/ajax/data-table-providers/WdsDataTableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EntityMetadata,
EntityQueryOptions,
EntityQueryResponse,
RecordEditParameters,
TSVFeatures,
TsvUploadButtonDisabledOptions,
TsvUploadButtonTooltipOptions,
Expand Down Expand Up @@ -59,13 +60,24 @@ export interface TsvUploadResponse {
recordsModified: number;
}

export interface RecordResponseBody {
id: string;
type: string;
attributes: { [attributeName: string]: any };
}

export const wdsToEntityServiceMetadata = (wdsSchema: RecordTypeSchema[]): EntityMetadata => {
const keyedSchema: Record<string, RecordTypeSchema> = _.keyBy((x) => x.name, wdsSchema);
return _.mapValues((typeDef) => {
// exclude the primary-key attribute from the list of attributes. The data table reads
// the primary-key attribute from the "idName" property.
const attrs = _.filter((attr) => attr.name !== typeDef.primaryKey, typeDef.attributes);
return { count: typeDef.count, attributeNames: _.map((attr) => attr.name, attrs), idName: typeDef.primaryKey };
return {
count: typeDef.count,
attributeNames: _.map((attr) => attr.name, attrs),
attributes: attrs,
idName: typeDef.primaryKey,
};
}, keyedSchema);
};

Expand Down Expand Up @@ -157,7 +169,8 @@ export class WdsDataTableProvider implements DataTableProvider {
supportsTypeDeletion: true,
supportsTypeRenaming: false,
supportsEntityRenaming: false,
supportsEntityUpdating: false, // TODO: enable as part of AJ-594
supportsEntityUpdating: true,
yuliadub marked this conversation as resolved.
Show resolved Hide resolved
supportsEntityUpdatingTypes: ['string', 'number', 'boolean', 'json'], // remove this as part of AJ-<need to create ticket> for other types
supportsAttributeRenaming: this.isCapabilityEnabled('edit.renameAttribute'),
supportsAttributeDeleting: this.isCapabilityEnabled('edit.deleteAttribute'),
supportsAttributeClearing: false,
Expand Down Expand Up @@ -325,6 +338,18 @@ export class WdsDataTableProvider implements DataTableProvider {
);
};

updateRecord = (recordEditParams: RecordEditParameters): Promise<RecordResponseBody> => {
if (!this.proxyUrl) return Promise.reject('Proxy Url not loaded');

return Ajax().WorkspaceData.updateRecord(
this.proxyUrl,
recordEditParams.instance,
recordEditParams.recordName,
recordEditParams.recordId,
recordEditParams.record
);
};

updateAttribute = (params: UpdateAttributeParameters): Promise<Blob> => {
if (!this.proxyUrl) return Promise.reject('Proxy Url not loaded');
return Ajax().WorkspaceData.updateAttribute(
Expand Down
Loading
Loading