Skip to content

Commit

Permalink
feat(core): Add setOrderBillingAddress mutation to Shop API
Browse files Browse the repository at this point in the history
Relates to #372
  • Loading branch information
michaelbromley committed Jun 30, 2020
1 parent cfb3c03 commit 83347b2
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,10 @@ export type Mutation = {
/** Removes the given coupon code from the active Order */
removeCouponCode?: Maybe<Order>;
transitionOrderToState?: Maybe<Order>;
/** Sets the shipping address for this order */
setOrderShippingAddress?: Maybe<Order>;
/** Sets the billing address for this order */
setOrderBillingAddress?: Maybe<Order>;
setOrderShippingMethod?: Maybe<Order>;
addPaymentToOrder?: Maybe<Order>;
setCustomerForOrder?: Maybe<Order>;
Expand Down Expand Up @@ -1412,6 +1415,10 @@ export type MutationSetOrderShippingAddressArgs = {
input: CreateAddressInput;
};

export type MutationSetOrderBillingAddressArgs = {
input: CreateAddressInput;
};

export type MutationSetOrderShippingMethodArgs = {
shippingMethodId: Scalars['ID'];
};
Expand Down
41 changes: 41 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,10 @@ export type Mutation = {
/** Removes the given coupon code from the active Order */
removeCouponCode?: Maybe<Order>;
transitionOrderToState?: Maybe<Order>;
/** Sets the shipping address for this order */
setOrderShippingAddress?: Maybe<Order>;
/** Sets the billing address for this order */
setOrderBillingAddress?: Maybe<Order>;
setOrderShippingMethod?: Maybe<Order>;
addPaymentToOrder?: Maybe<Order>;
setCustomerForOrder?: Maybe<Order>;
Expand Down Expand Up @@ -1412,6 +1415,10 @@ export type MutationSetOrderShippingAddressArgs = {
input: CreateAddressInput;
};

export type MutationSetOrderBillingAddressArgs = {
input: CreateAddressInput;
};

export type MutationSetOrderShippingMethodArgs = {
shippingMethodId: Scalars['ID'];
};
Expand Down Expand Up @@ -2587,6 +2594,31 @@ export type SetShippingAddressMutation = { __typename?: 'Mutation' } & {
>;
};

export type SetBillingAddressMutationVariables = {
input: CreateAddressInput;
};

export type SetBillingAddressMutation = { __typename?: 'Mutation' } & {
setOrderBillingAddress?: Maybe<
{ __typename?: 'Order' } & {
billingAddress?: Maybe<
{ __typename?: 'OrderAddress' } & Pick<
OrderAddress,
| 'fullName'
| 'company'
| 'streetLine1'
| 'streetLine2'
| 'city'
| 'province'
| 'postalCode'
| 'country'
| 'phoneNumber'
>
>;
}
>;
};

export type AddPaymentToOrderMutationVariables = {
input: PaymentInput;
};
Expand Down Expand Up @@ -2885,6 +2917,15 @@ export namespace SetShippingAddress {
>;
}

export namespace SetBillingAddress {
export type Variables = SetBillingAddressMutationVariables;
export type Mutation = SetBillingAddressMutation;
export type SetOrderBillingAddress = NonNullable<SetBillingAddressMutation['setOrderBillingAddress']>;
export type BillingAddress = NonNullable<
NonNullable<SetBillingAddressMutation['setOrderBillingAddress']>['billingAddress']
>;
}

export namespace AddPaymentToOrder {
export type Variables = AddPaymentToOrderMutationVariables;
export type Mutation = AddPaymentToOrderMutation;
Expand Down
18 changes: 18 additions & 0 deletions packages/core/e2e/graphql/shop-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,24 @@ export const SET_SHIPPING_ADDRESS = gql`
}
`;

export const SET_BILLING_ADDRESS = gql`
mutation SetBillingAddress($input: CreateAddressInput!) {
setOrderBillingAddress(input: $input) {
billingAddress {
fullName
company
streetLine1
streetLine2
city
province
postalCode
country
phoneNumber
}
}
}
`;

export const ADD_PAYMENT = gql`
mutation AddPaymentToOrder($input: PaymentInput!) {
addPaymentToOrder(input: $input) {
Expand Down
34 changes: 34 additions & 0 deletions packages/core/e2e/shop-order.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
GetOrderByCode,
GetShippingMethods,
RemoveItemFromOrder,
SetBillingAddress,
SetCustomerForOrder,
SetShippingAddress,
SetShippingMethod,
Expand All @@ -57,6 +58,7 @@ import {
GET_NEXT_STATES,
GET_ORDER_BY_CODE,
REMOVE_ITEM_FROM_ORDER,
SET_BILLING_ADDRESS,
SET_CUSTOMER,
SET_SHIPPING_ADDRESS,
SET_SHIPPING_METHOD,
Expand Down Expand Up @@ -382,6 +384,38 @@ describe('Shop orders', () => {
});
});

it('setOrderBillingAddress sets billing address', async () => {
const address: CreateAddressInput = {
fullName: 'name',
company: 'company',
streetLine1: '12 the street',
streetLine2: null,
city: 'foo',
province: 'bar',
postalCode: '123456',
countryCode: 'US',
phoneNumber: '4444444',
};
const { setOrderBillingAddress } = await shopClient.query<
SetBillingAddress.Mutation,
SetBillingAddress.Variables
>(SET_BILLING_ADDRESS, {
input: address,
});

expect(setOrderBillingAddress!.billingAddress).toEqual({
fullName: 'name',
company: 'company',
streetLine1: '12 the street',
streetLine2: null,
city: 'foo',
province: 'bar',
postalCode: '123456',
country: 'United States of America',
phoneNumber: '4444444',
});
});

it('customer default Addresses are not updated before payment', async () => {
const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
const { customer } = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(
Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/api/resolvers/shop/shop-order.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MutationApplyCouponCodeArgs,
MutationRemoveOrderLineArgs,
MutationSetCustomerForOrderArgs,
MutationSetOrderBillingAddressArgs,
MutationSetOrderShippingAddressArgs,
MutationSetOrderShippingMethodArgs,
MutationTransitionOrderToStateArgs,
Expand Down Expand Up @@ -55,7 +56,7 @@ export class ShopOrderResolver {
skip: 0,
take: 99999,
})
.then(data => data.items);
.then((data) => data.items);
}

@Query()
Expand Down Expand Up @@ -136,6 +137,22 @@ export class ShopOrderResolver {
}
}

@Mutation()
@Allow(Permission.Owner)
async setOrderBillingAddress(
@Ctx() ctx: RequestContext,
@Args() args: MutationSetOrderBillingAddressArgs,
): Promise<Order | undefined> {
if (ctx.authorizedAsOwnerOnly) {
const sessionOrder = await this.getOrderFromContext(ctx);
if (sessionOrder) {
return this.orderService.setBillingAddress(ctx, sessionOrder.id, args.input);
} else {
return;
}
}
}

@Query()
@Allow(Permission.Owner)
async eligibleShippingMethods(@Ctx() ctx: RequestContext): Promise<ShippingMethodQuote[]> {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/api/schema/shop-api/shop.api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ type Mutation {
"Removes the given coupon code from the active Order"
removeCouponCode(couponCode: String!): Order
transitionOrderToState(state: String!): Order
"Sets the shipping address for this order"
setOrderShippingAddress(input: CreateAddressInput!): Order
"Sets the billing address for this order"
setOrderBillingAddress(input: CreateAddressInput!): Order
"Sets the shipping method by id, which can be obtained with the `eligibleShippingMethods` query"
setOrderShippingMethod(shippingMethodId: ID!): Order
addPaymentToOrder(input: PaymentInput!): Order
setCustomerForOrder(input: CreateCustomerInput!): Order
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ export class OrderService {
return this.connection.getRepository(Order).save(order);
}

async setBillingAddress(ctx: RequestContext, orderId: ID, input: CreateAddressInput): Promise<Order> {
const order = await this.getOrderOrThrow(ctx, orderId);
const country = await this.countryService.findOneByCode(ctx, input.countryCode);
order.billingAddress = { ...input, countryCode: input.countryCode, country: country.name };
return this.connection.getRepository(Order).save(order);
}

async getEligibleShippingMethods(ctx: RequestContext, orderId: ID): Promise<ShippingMethodQuote[]> {
const order = await this.getOrderOrThrow(ctx, orderId);
const eligibleMethods = await this.shippingCalculator.getEligibleShippingMethods(ctx, order);
Expand Down
2 changes: 1 addition & 1 deletion schema-shop.json

Large diffs are not rendered by default.

0 comments on commit 83347b2

Please sign in to comment.