Skip to content

Commit

Permalink
fix(cart): Magento driver crashes while setting cart address by ID (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Aug 2, 2023
1 parent b4b63f3 commit 9bbde26
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 32 deletions.
44 changes: 44 additions & 0 deletions libs/cart/driver/magento/src/cart-address.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@ describe('@daffodil/cart/driver/magento | DaffMagentoCartAddressService', () =>
mockDaffCartAddress.street = street;
});

describe('and when the cart address ID is set', () => {
let id: string;

beforeEach(() => {
id = '5';
mockDaffCartAddress.id = id;
});

it('should set the addressId variable', done => {
service.update(cartId, mockDaffCartAddress).subscribe(result => {
done();
});

const op = controller.expectOne(addTypenameToDocument(updateAddressWithEmail([])));
expect(op.operation.variables.addressId).toEqual(Number(id));

op.flush({
data: mockUpdateAddressWithEmailResponse,
});
});
});

it('should return the correct value', done => {
service.update(cartId, mockDaffCartAddress).subscribe(result => {
expect(result.billing_address.street).toEqual(street);
Expand Down Expand Up @@ -198,6 +220,28 @@ describe('@daffodil/cart/driver/magento | DaffMagentoCartAddressService', () =>
mockDaffCartAddress.email = null;
});

describe('and when the cart address ID is set', () => {
let id: string;

beforeEach(() => {
id = '5';
mockDaffCartAddress.id = id;
});

it('should set the addressId variable', done => {
service.update(cartId, mockDaffCartAddress).subscribe(result => {
done();
});

const op = controller.expectOne(addTypenameToDocument(updateAddress([])));
expect(op.operation.variables.addressId).toEqual(Number(id));

op.flush({
data: mockUpdateAddressWithEmailResponse,
});
});
});

it('should return the correct value', done => {
service.update(cartId, mockDaffCartAddress).subscribe(result => {
expect(result.billing_address.street).toEqual(street);
Expand Down
26 changes: 18 additions & 8 deletions libs/cart/driver/magento/src/cart-address.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ import { DaffQueuedApollo } from '@daffodil/core/graphql';
import { transformCartMagentoError } from './errors/transform';
import { DAFF_MAGENTO_CART_MUTATION_QUEUE } from './injection-tokens/cart-mutation-queue.token';
import { DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS } from './injection-tokens/public_api';
import { MagentoCartAddressInput } from './models/public_api';
import {
updateAddress,
updateAddressWithEmail,
} from './queries/public_api';
import {
MagentoUpdateAddressResponse,
MagentoUpdateAddressWithEmailResponse,
} from './queries/responses/public_api';
import { DaffMagentoCartAddressInputTransformer } from './transforms/inputs/cart-address.service';
import { DaffMagentoCartTransformer } from './transforms/outputs/cart.service';

Expand All @@ -53,12 +50,25 @@ export class DaffMagentoCartAddressService implements DaffCartAddressServiceInte
return address.email ? this.updateAddressWithEmail(cartId, address) : this.updateAddress(cartId, address);
}

private getAddressInput(address: Partial<DaffCartAddress>): {
address?: MagentoCartAddressInput;
addressId?: number;
} {
return address.id
? {
addressId: Number(address.id),
}
: {
address: this.cartAddressInputTransformer.transform(address),
};
}

private updateAddress(cartId: DaffCart['id'], address: Partial<DaffCartAddress>): Observable<Partial<DaffCart>> {
return this.mutationQueue.mutate<MagentoUpdateAddressResponse>({
return this.mutationQueue.mutate({
mutation: updateAddress(this.extraCartFragments),
variables: {
...this.getAddressInput(address),
cartId,
address: this.cartAddressInputTransformer.transform(address),
},
fetchPolicy: 'network-only',
}).pipe(
Expand All @@ -68,12 +78,12 @@ export class DaffMagentoCartAddressService implements DaffCartAddressServiceInte
}

private updateAddressWithEmail(cartId: DaffCart['id'], address: Partial<DaffCartAddress>): Observable<Partial<DaffCart>> {
return this.mutationQueue.mutate<MagentoUpdateAddressWithEmailResponse>({
return this.mutationQueue.mutate({
mutation: updateAddressWithEmail(this.extraCartFragments),
variables: {
...this.getAddressInput(address),
cartId,
email: address.email,
address: this.cartAddressInputTransformer.transform(address),
},
fetchPolicy: 'network-only',
}).pipe(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { MagentoCartAddressInput } from './cart-address';
import { MagentoCartAddressInputBase } from './cart-address-base';

export interface MagentoBillingAddressInput {
address: MagentoCartAddressInput;
customer_address_id?: number;
export interface MagentoBillingAddressInput extends MagentoCartAddressInputBase {
same_as_shipping?: boolean;
use_for_shipping?: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MagentoCartAddressInput } from './cart-address';

export interface MagentoCartAddressInputBase {
address?: MagentoCartAddressInput;
customer_address_id?: number;
}
1 change: 1 addition & 0 deletions libs/cart/driver/magento/src/models/requests/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { MagentoBillingAddressInput } from './billing-address';
export { MagentoCartAddressInput } from './cart-address';
export { MagentoCartAddressInputBase } from './cart-address-base';
export * from './cart-item';
export { MagentoCartItemUpdateInput } from './cart-item-update';
export { MagentoPaymentMethodInput } from './payment-method';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { MagentoCartAddressInput } from './cart-address';
import { MagentoCartAddressInputBase } from './cart-address-base';

export interface MagentoShippingAddressInput {
address: MagentoCartAddressInput;
customer_address_id?: number;
export interface MagentoShippingAddressInput extends MagentoCartAddressInputBase {
customer_notes?: string;
}
4 changes: 2 additions & 2 deletions libs/cart/driver/magento/src/queries/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export { updateBillingAddressWithEmail } from './update-billing-address-with-ema
export { getShippingAddress } from './get-shipping-address';
export { updateShippingAddress } from './update-shipping-address';
export { updateShippingAddressWithEmail } from './update-shipping-address-with-email';
export { updateAddress } from './update-address';
export { updateAddressWithEmail } from './update-address-with-email';
export * from './update-address/public_api';
export * from './update-address-with-email/public_api';
export { getCart } from './get-cart';
export { createCart } from './create-cart';
export { placeOrder } from './place-order';
Expand Down
2 changes: 0 additions & 2 deletions libs/cart/driver/magento/src/queries/responses/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export { MagentoUpdateBillingAddressWithEmailResponse } from './update-billing-a
export { MagentoGetShippingAddressResponse } from './get-shipping-address';
export { MagentoUpdateShippingAddressResponse } from './update-shipping-address';
export { MagentoUpdateShippingAddressWithEmailResponse } from './update-shipping-address-with-email';
export { MagentoUpdateAddressResponse } from './update-address';
export { MagentoUpdateAddressWithEmailResponse } from './update-address-with-email';
export { MagentoGetCartResponse } from './get-cart';
export { MagentoCreateCartResponse } from './create-cart';
export { MagentoSetGuestEmailResponse } from './set-guest-email';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './query';
export * from './variables.type';
export * from './response.type';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
daffBuildFragmentDefinition,
} from '@daffodil/core/graphql';

import { cartFragment } from './fragments/public_api';
import { cartFragment } from '../fragments/public_api';
import { MagentoUpdateAddressWithEmailResponse } from './response.type';
import { MagentoCartUpdateAddressWithEmailQueryVariables } from './variables.type';

/**
* Update the shipping and billing address of the cart.
Expand All @@ -15,12 +17,13 @@ import { cartFragment } from './fragments/public_api';
* This helps us keep query complexity down and save some server CPU cycles.
* Driver behavior is not guaranteed if Magento no longer processes compound queries in the order they are defined.
*/
export const updateAddressWithEmail = (extraCartFragments: DocumentNode[] = []) => gql`
mutation MagentoUpdateAddressWithEmail($cartId: String!, $address: CartAddressInput!, $email: String!) {
export const updateAddressWithEmail = (extraCartFragments: DocumentNode[] = []) => gql<MagentoUpdateAddressWithEmailResponse, MagentoCartUpdateAddressWithEmailQueryVariables>`
mutation MagentoUpdateAddressWithEmail($cartId: String!, $email: String!, $address: CartAddressInput, $addressId: Int) {
setBillingAddressOnCart(input: {
cart_id: $cartId
billing_address: {
address: $address
address: $address,
customer_address_id: $addressId
}
}) {
cart {
Expand All @@ -30,7 +33,8 @@ export const updateAddressWithEmail = (extraCartFragments: DocumentNode[] = [])
setShippingAddressesOnCart(input: {
cart_id: $cartId
shipping_addresses: [{
address: $address
address: $address,
customer_address_id: $addressId
}]
}) {
cart {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MagentoGetCartResponse } from './get-cart';
import { MagentoGetCartResponse } from '../responses/get-cart';

export interface MagentoUpdateAddressWithEmailResponse {
setBillingAddressOnCart: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { MagentoCartAddressInput } from '../../models/public_api';

export interface MagentoCartUpdateAddressWithEmailQueryVariables {
cartId: string;
email: string;
address?: MagentoCartAddressInput;
addressId?: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './query';
export * from './variables.type';
export * from './response.type';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
daffBuildFragmentDefinition,
} from '@daffodil/core/graphql';

import { cartFragment } from './fragments/public_api';
import { cartFragment } from '../fragments/public_api';
import { MagentoUpdateAddressResponse } from './response.type';
import { MagentoCartUpdateAddressQueryVariables } from './variables.type';

/**
* Update the shipping and billing address of the cart.
Expand All @@ -15,12 +17,13 @@ import { cartFragment } from './fragments/public_api';
* This helps us keep query complexity down and save some server CPU cycles.
* Driver behavior is not guaranteed if Magento no longer processes compound queries in the order they are defined.
*/
export const updateAddress = (extraCartFragments: DocumentNode[] = []) => gql`
mutation MagentoUpdateAddress($cartId: String!, $address: CartAddressInput!) {
export const updateAddress = (extraCartFragments: DocumentNode[] = []) => gql<MagentoUpdateAddressResponse, MagentoCartUpdateAddressQueryVariables>`
mutation MagentoUpdateAddress($cartId: String!, $address: CartAddressInput, $addressId: Int) {
setBillingAddressOnCart(input: {
cart_id: $cartId
billing_address: {
address: $address
address: $address,
customer_address_id: $addressId
}
}) {
cart {
Expand All @@ -30,7 +33,8 @@ export const updateAddress = (extraCartFragments: DocumentNode[] = []) => gql`
setShippingAddressesOnCart(input: {
cart_id: $cartId
shipping_addresses: [{
address: $address
address: $address,
customer_address_id: $addressId
}]
}) {
cart {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MagentoGetCartResponse } from './get-cart';
import { MagentoGetCartResponse } from '../responses/get-cart';

export interface MagentoUpdateAddressResponse {
setBillingAddressOnCart: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { MagentoCartAddressInput } from '../../models/public_api';

export interface MagentoCartUpdateAddressQueryVariables {
cartId: string;
address?: MagentoCartAddressInput;
addressId?: number;
}

0 comments on commit 9bbde26

Please sign in to comment.