fix(variable): should remove through collection field (#4590)

This commit is contained in:
Zeke Zhang 2024-06-07 14:05:39 +08:00 committed by GitHub
parent 34108f1fcb
commit 7d4ae69f08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 142 additions and 3 deletions

View File

@ -124,7 +124,7 @@ const VariablesProvider = ({ children }) => {
}
return item?.[key];
});
current = _.flatten(await Promise.all(result));
current = removeThroughCollectionFields(_.flatten(await Promise.all(result)), associationField);
} else if (shouldToRequest(current[key]) && current.id != null && associationField?.target) {
const url = `/${collectionName}/${current.id}/${key}:${getAction(associationField.type)}`;
let data = null;
@ -149,9 +149,9 @@ const VariablesProvider = ({ children }) => {
raw(current)[key] = data.data.data;
}
current = getValuesByPath(current, key);
current = removeThroughCollectionFields(getValuesByPath(current, key), associationField);
} else {
current = getValuesByPath(current, key);
current = removeThroughCollectionFields(getValuesByPath(current, key), associationField);
}
if (associationField?.target) {
@ -344,3 +344,26 @@ function mergeVariableToCollectionNameWithLocalVariables(
return variablesStore;
}
/**
*
*
* @param value
* @param associationField
* @returns
*/
export function removeThroughCollectionFields(
value: Record<string, any> | Record<string, any>[],
associationField: CollectionFieldOptions_deprecated,
) {
if (!associationField?.through || !value) {
return value;
}
if (Array.isArray(value)) {
return value.map((item) => {
return _.omit(item, associationField.through);
});
}
return _.omit(value, associationField.through);
}

View File

@ -0,0 +1,80 @@
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { removeThroughCollectionFields } from '../VariablesProvider';
describe('removeThroughCollectionFields', () => {
it('should remove through collection fields from a single record', () => {
const value = {
id: 1,
name: 'John Doe',
throughField: 'Some value',
};
const associationField = {
through: 'throughField',
};
const result = removeThroughCollectionFields(value, associationField);
expect(result).toEqual({
id: 1,
name: 'John Doe',
});
});
it('should remove through collection fields from an array of records', () => {
const value = [
{
id: 1,
name: 'John Doe',
throughField: 'Some value',
},
{
id: 2,
name: 'Jane Smith',
throughField: 'Another value',
},
];
const associationField = {
through: 'throughField',
};
const result = removeThroughCollectionFields(value, associationField);
expect(result).toEqual([
{
id: 1,
name: 'John Doe',
},
{
id: 2,
name: 'Jane Smith',
},
]);
});
it('should return the original value if associationField.through is not defined', () => {
const value = {
id: 1,
name: 'John Doe',
};
const associationField = {};
const result = removeThroughCollectionFields(value, associationField);
expect(result).toEqual(value);
});
it('should return the original value if value is null or undefined', () => {
const associationField = {
through: 'throughField',
};
let value = null;
let result = removeThroughCollectionFields(value, associationField);
expect(result).toBeNull();
value = undefined;
result = removeThroughCollectionFields(value, associationField);
expect(result).toBeUndefined();
});
});

View File

@ -52,6 +52,13 @@ vi.mock('../../collection-manager', async () => {
target: 'test',
};
}
if (path === 'users.belongsToManyField') {
return {
type: 'belongsToMany',
target: 'test',
through: 'throughCollectionName',
};
}
if (path === 'local.belongsToField') {
return {
type: 'belongsTo',
@ -115,6 +122,20 @@ mockRequest.onGet('/users/0/hasManyField:list?pageSize=9999').reply(() => {
},
];
});
mockRequest.onGet('/users/0/belongsToManyField:list?pageSize=9999').reply(() => {
return [
200,
{
data: [
{
id: 0,
name: '$user.belongsToManyField',
throughCollectionName: 'throughCollectionName',
},
],
},
];
});
mockRequest.onGet('/test/0/hasManyField:list?pageSize=9999').reply(() => {
return [
200,
@ -370,6 +391,21 @@ describe('useVariables', () => {
});
});
it('should remove through collection field', async () => {
const { result } = renderHook(() => useVariables(), {
wrapper: Providers,
});
await waitFor(async () => {
expect(await result.current.parseVariable('{{ $user.belongsToManyField }}')).toEqual([
{
id: 0,
name: '$user.belongsToManyField',
},
]);
});
});
it('register variable', async () => {
const { result } = renderHook(() => useVariables(), {
wrapper: Providers,