Skip to content

Commit

Permalink
fix(core): Hide private OrderLine customFields in addItemToOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Mar 18, 2021
1 parent ef30618 commit c2c7f1d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
18 changes: 17 additions & 1 deletion packages/core/e2e/shop-order.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ describe('Shop orders', () => {
},
customFields: {
Order: [{ name: 'giftWrap', type: 'boolean', defaultValue: false }],
OrderLine: [{ name: 'notes', type: 'string' }],
OrderLine: [
{ name: 'notes', type: 'string' },
{ name: 'privateField', type: 'string', public: false },
],
},
orderOptions: {
orderItemsLimit: 99,
Expand Down Expand Up @@ -227,6 +230,19 @@ describe('Shop orders', () => {
expect(addItemToOrder!.lines[0].quantity).toBe(3);
});

it(
'addItemToOrder with private customFields errors',
assertThrowsWithMessage(async () => {
await shopClient.query<AddItemToOrder.Mutation>(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
productVariantId: 'T_2',
quantity: 1,
customFields: {
privateField: 'oh no!',
},
});
}, 'Variable "$customFields" got invalid value { privateField: "oh no!" }; Field "privateField" is not defined by type "OrderLineCustomFieldsInput".'),
);

it('addItemToOrder with equal customFields adds quantity to the existing OrderLine', async () => {
const { addItemToOrder: add1 } = await shopClient.query<AddItemToOrder.Mutation>(
ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/api/config/graphql-custom-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,10 @@ export function addOrderLineCustomFieldsInput(
if (!mutationType) {
return schema;
}
const publicCustomFields = orderLineCustomFields.filter(f => f.public !== false);
const input = new GraphQLInputObjectType({
name: 'OrderLineCustomFieldsInput',
fields: orderLineCustomFields.reduce((fields, field) => {
fields: publicCustomFields.reduce((fields, field) => {
const name = getGraphQlInputName(field);
// tslint:disable-next-line:no-non-null-assertion
const primitiveType = schema.getType(getGraphQlInputType(field))!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,14 @@ export class OrderModifier {
// of every property of an existing customFields object being null.
return Object.values(existingCustomFields).every(v => v === null);
}
return JSON.stringify(inputCustomFields) === JSON.stringify(existingCustomFields);
for (const [key, value] of Object.entries(existingCustomFields || {})) {
const valuesMatch = JSON.stringify(inputCustomFields?.[key]) === JSON.stringify(value);
const undefinedMatchesNull = value === null && inputCustomFields?.[key] === undefined;
if (!valuesMatch && !undefinedMatchesNull) {
return false;
}
}
return true;
}

private async getProductVariantOrThrow(
Expand Down

0 comments on commit c2c7f1d

Please sign in to comment.