Skip to content

Commit

Permalink
fix(admin-ui): Fix saving relation custom fields on ProductVariants
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Jul 14, 2021
1 parent 5d483f6 commit fb38c68
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,66 @@ describe('transformRelationCustomFieldInput()', () => {
},
} as any);
});

it('transforms input object', () => {
const config: CustomFieldConfig[] = [
{ name: 'weight', type: 'int', list: false },
{ name: 'avatar', type: 'relation', list: false, entity: 'Asset' },
];
const entity = {
id: 1,
name: 'test',
customFields: {
weight: 500,
avatar: {
id: 123,
preview: '...',
},
},
};

const result = transformRelationCustomFieldInputs({ input: entity }, config);
expect(result).toEqual({
input: {
id: 1,
name: 'test',
customFields: {
weight: 500,
avatarId: 123,
},
},
} as any);
});

it('transforms input array (as in UpdateProductVariantsInput)', () => {
const config: CustomFieldConfig[] = [
{ name: 'weight', type: 'int', list: false },
{ name: 'avatar', type: 'relation', list: false, entity: 'Asset' },
];
const entity = {
id: 1,
name: 'test',
customFields: {
weight: 500,
avatar: {
id: 123,
preview: '...',
},
},
};

const result = transformRelationCustomFieldInputs({ input: [entity] }, config);
expect(result).toEqual({
input: [
{
id: 1,
name: 'test',
customFields: {
weight: 500,
avatarId: 123,
},
},
],
} as any);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import { CustomFieldConfig } from '../../common/generated-types';
* Transforms any custom field "relation" type inputs into the corresponding `<name>Id` format,
* as expected by the server.
*/
export function transformRelationCustomFieldInputs<T extends { input?: any } & Record<string, any> = any>(
variables: T,
customFieldConfig: CustomFieldConfig[],
): T {
export function transformRelationCustomFieldInputs<
T extends { input?: Record<string, any> | Array<Record<string, any>> } & Record<string, any> = any
>(variables: T, customFieldConfig: CustomFieldConfig[]): T {
if (variables.input) {
transformRelations(variables.input, customFieldConfig);
if (Array.isArray(variables.input)) {
for (const item of variables.input) {
transformRelations(item, customFieldConfig);
}
} else {
transformRelations(variables.input, customFieldConfig);
}
}
return transformRelations(variables, customFieldConfig);
}
Expand All @@ -22,7 +27,7 @@ export function transformRelationCustomFieldInputs<T extends { input?: any } & R
* When persisting custom fields, we need to send just the IDs of the relations,
* rather than the objects themselves.
*/
function transformRelations(input: any, customFieldConfig: CustomFieldConfig[]) {
function transformRelations<T>(input: T, customFieldConfig: CustomFieldConfig[]) {
for (const field of customFieldConfig) {
if (field.type === 'relation') {
if (hasCustomFields(input)) {
Expand Down

0 comments on commit fb38c68

Please sign in to comment.