Skip to content

Commit

Permalink
feat(admin-ui): Set default shipping and billing address for draft or…
Browse files Browse the repository at this point in the history
…ders (#3196)

Closes #2342
  • Loading branch information
oliverstreissi authored Nov 8, 2024
1 parent 5547128 commit dec72e7
Show file tree
Hide file tree
Showing 14 changed files with 982 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ cd packages/dev-server
If you are making changes to the admin ui, you need to start the admin ui independent from the dev-server:
1. `cd packages/admin-ui`
2. `npm run dev`
2. `npm run start`
3. Go to http://localhost:4200 and log in with "superadmin", "superadmin"
This will auto restart when you make changes to the admin ui. You don't need this step when you just use the admin ui just
Expand Down
30 changes: 30 additions & 0 deletions packages/admin-ui/src/lib/core/src/common/generated-types.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,24 @@ export const SET_BILLING_ADDRESS_FOR_DRAFT_ORDER = gql`
${ORDER_DETAIL_FRAGMENT}
`;

export const UNSET_SHIPPING_ADDRESS_FOR_DRAFT_ORDER = gql`
mutation UnsetDraftOrderShippingAddress($orderId: ID!) {
unsetDraftOrderShippingAddress(orderId: $orderId) {
...OrderDetail
}
}
${ORDER_DETAIL_FRAGMENT}
`;

export const UNSET_BILLING_ADDRESS_FOR_DRAFT_ORDER = gql`
mutation UnsetDraftOrderBillingAddress($orderId: ID!) {
unsetDraftOrderBillingAddress(orderId: $orderId) {
...OrderDetail
}
}
${ORDER_DETAIL_FRAGMENT}
`;

export const APPLY_COUPON_CODE_TO_DRAFT_ORDER = gql`
mutation ApplyCouponCodeToDraftOrder($orderId: ID!, $couponCode: String!) {
applyCouponCodeToDraftOrder(orderId: $orderId, couponCode: $couponCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
TRANSITION_FULFILLMENT_TO_STATE,
TRANSITION_ORDER_TO_STATE,
TRANSITION_PAYMENT_TO_STATE,
UNSET_BILLING_ADDRESS_FOR_DRAFT_ORDER,
UNSET_SHIPPING_ADDRESS_FOR_DRAFT_ORDER,
UPDATE_ORDER_CUSTOM_FIELDS,
UPDATE_ORDER_NOTE,
} from '../definitions/order-definitions';
Expand Down Expand Up @@ -254,6 +256,20 @@ export class OrderDataService {
>(SET_BILLING_ADDRESS_FOR_DRAFT_ORDER, { orderId, input });
}

unsetDraftOrderShippingAddress(orderId: string) {
return this.baseDataService.mutate<
Codegen.UnsetDraftOrderShippingAddressMutation,
Codegen.UnsetDraftOrderShippingAddressMutationVariables
>(UNSET_SHIPPING_ADDRESS_FOR_DRAFT_ORDER, { orderId });
}

unsetDraftOrderBillingAddress(orderId: string) {
return this.baseDataService.mutate<
Codegen.UnsetDraftOrderBillingAddressMutation,
Codegen.UnsetDraftOrderBillingAddressMutationVariables
>(UNSET_BILLING_ADDRESS_FOR_DRAFT_ORDER, { orderId });
}

applyCouponCodeToDraftOrder(orderId: string, couponCode: string) {
return this.baseDataService.mutate<
Codegen.ApplyCouponCodeToDraftOrderMutation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnIni
import { UntypedFormGroup } from '@angular/forms';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import {
AddressFragment,
CreateAddressInput,
DataService,
DeletionResult,
DraftOrderEligibleShippingMethodsQuery,
GetCustomerAddressesDocument,
ModalService,
NotificationService,
Order,
OrderDetailFragment,
OrderDetailQueryDocument,
TypedBaseDetailComponent,
} from '@vendure/admin-ui/core';
import { combineLatest, Observable, Subject } from 'rxjs';
import { combineLatest, forkJoin, Observable, of, Subject } from 'rxjs';
import { switchMap, take } from 'rxjs/operators';

import { OrderTransitionService } from '../../providers/order-transition.service';
Expand Down Expand Up @@ -108,6 +111,36 @@ export class DraftOrderDetailComponent
if (this.hasId(result)) {
this.dataService.order
.setCustomerForDraftOrder(this.id, { customerId: result.id })
.pipe(
switchMap(() => {
return this.dataService.query(GetCustomerAddressesDocument, {
customerId: result.id,
}).single$;
}),
switchMap(({ customer }) => {
const defaultShippingAddress = customer?.addresses?.find(
addr => addr.defaultShippingAddress,
);
const defaultBillingAddress = customer?.addresses?.find(
addr => addr.defaultBillingAddress,
);

return forkJoin([
defaultShippingAddress
? this.dataService.order.setDraftOrderShippingAddress(
this.id,
this.mapToAddressInput(defaultShippingAddress),
)
: this.dataService.order.unsetDraftOrderShippingAddress(this.id),
defaultBillingAddress
? this.dataService.order.setDraftOrderBillingAddress(
this.id,
this.mapToAddressInput(defaultBillingAddress),
)
: this.dataService.order.unsetDraftOrderBillingAddress(this.id),
]);
}),
)
.subscribe();
} else if (result) {
const { note, ...input } = result;
Expand All @@ -116,6 +149,22 @@ export class DraftOrderDetailComponent
});
}

private mapToAddressInput(address: AddressFragment): CreateAddressInput {
return {
fullName: address.fullName,
company: address.company,
streetLine1: address.streetLine1,
streetLine2: address.streetLine2,
city: address.city,
province: address.province,
postalCode: address.postalCode,
countryCode: address.country.code,
phoneNumber: address.phoneNumber,
defaultShippingAddress: address.defaultShippingAddress,
defaultBillingAddress: address.defaultBillingAddress,
};
}

setShippingAddress() {
this.entity$
.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2873,6 +2873,10 @@ export type Mutation = {
transitionFulfillmentToState: TransitionFulfillmentToStateResult;
transitionOrderToState?: Maybe<TransitionOrderToStateResult>;
transitionPaymentToState: TransitionPaymentToStateResult;
/** Unsets the billing address for a draft Order */
unsetDraftOrderBillingAddress: Order;
/** Unsets the sthipping address for a draft Order */
unsetDraftOrderShippingAddress: Order;
/** Update the active (currently logged-in) Administrator */
updateActiveAdministrator: Administrator;
/** Update an existing Administrator */
Expand Down Expand Up @@ -3475,6 +3479,14 @@ export type MutationTransitionPaymentToStateArgs = {
state: Scalars['String']['input'];
};

export type MutationUnsetDraftOrderBillingAddressArgs = {
orderId: Scalars['ID']['input'];
};

export type MutationUnsetDraftOrderShippingAddressArgs = {
orderId: Scalars['ID']['input'];
};

export type MutationUpdateActiveAdministratorArgs = {
input: UpdateActiveAdministratorInput;
};
Expand Down
14 changes: 14 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2921,6 +2921,10 @@ export type Mutation = {
transitionFulfillmentToState: TransitionFulfillmentToStateResult;
transitionOrderToState?: Maybe<TransitionOrderToStateResult>;
transitionPaymentToState: TransitionPaymentToStateResult;
/** Unsets the billing address for a draft Order */
unsetDraftOrderBillingAddress: Order;
/** Unsets the sthipping address for a draft Order */
unsetDraftOrderShippingAddress: Order;
/** Update the active (currently logged-in) Administrator */
updateActiveAdministrator: Administrator;
/** Update an existing Administrator */
Expand Down Expand Up @@ -3652,6 +3656,16 @@ export type MutationTransitionPaymentToStateArgs = {
};


export type MutationUnsetDraftOrderBillingAddressArgs = {
orderId: Scalars['ID']['input'];
};


export type MutationUnsetDraftOrderShippingAddressArgs = {
orderId: Scalars['ID']['input'];
};


export type MutationUpdateActiveAdministratorArgs = {
input: UpdateActiveAdministratorInput;
};
Expand Down
66 changes: 66 additions & 0 deletions packages/core/e2e/draft-order.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,48 @@ describe('Draft Orders resolver', () => {
});
});

it('unsetDraftOrderShippingAddress', async () => {
const { unsetDraftOrderShippingAddress } = await adminClient.query<
Codegen.UnsetDraftOrderShippingAddressMutation,
Codegen.UnsetDraftOrderShippingAddressMutationVariables
>(UNSET_SHIPPING_ADDRESS_FOR_DRAFT_ORDER, {
orderId: draftOrder.id,
});

expect(unsetDraftOrderShippingAddress.shippingAddress).toEqual({
company: null,
fullName: null,
phoneNumber: null,
streetLine2: null,
province: null,
city: null,
country: null,
postalCode: null,
streetLine1: null,
});
});

it('unsetDraftOrderBillingAddress', async () => {
const { unsetDraftOrderBillingAddress } = await adminClient.query<
Codegen.UnsetDraftOrderBillingAddressMutation,
Codegen.UnsetDraftOrderBillingAddressMutationVariables
>(UNSET_BILLING_ADDRESS_FOR_DRAFT_ORDER, {
orderId: draftOrder.id,
});

expect(unsetDraftOrderBillingAddress.billingAddress).toEqual({
company: null,
fullName: null,
phoneNumber: null,
streetLine2: null,
province: null,
city: null,
country: null,
postalCode: null,
streetLine1: null,
});
});

it('applyCouponCodeToDraftOrder', async () => {
const { addItemToDraftOrder } = await adminClient.query<
Codegen.AddItemToDraftOrderMutation,
Expand Down Expand Up @@ -475,6 +517,30 @@ export const SET_BILLING_ADDRESS_FOR_DRAFT_ORDER = gql`
${ORDER_WITH_LINES_FRAGMENT}
`;

export const UNSET_SHIPPING_ADDRESS_FOR_DRAFT_ORDER = gql`
mutation UnsetDraftOrderShippingAddress($orderId: ID!) {
unsetDraftOrderShippingAddress(orderId: $orderId) {
...OrderWithLines
shippingAddress {
...ShippingAddress
}
}
}
${ORDER_WITH_LINES_FRAGMENT}
`;

export const UNSET_BILLING_ADDRESS_FOR_DRAFT_ORDER = gql`
mutation UnsetDraftOrderBillingAddress($orderId: ID!) {
unsetDraftOrderBillingAddress(orderId: $orderId) {
...OrderWithLines
billingAddress {
...ShippingAddress
}
}
}
${ORDER_WITH_LINES_FRAGMENT}
`;

export const APPLY_COUPON_CODE_TO_DRAFT_ORDER = gql`
mutation ApplyCouponCodeToDraftOrder($orderId: ID!, $couponCode: String!) {
applyCouponCodeToDraftOrder(orderId: $orderId, couponCode: $couponCode) {
Expand Down
Loading

0 comments on commit dec72e7

Please sign in to comment.