Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat: place guest order with proper params and load sessionContext (#590
Browse files Browse the repository at this point in the history
)

* feat: changed createGuestOrder method to accept proper params

* feat: load sessionContext
  • Loading branch information
patzick authored Apr 7, 2020
1 parent 34c8ff0 commit e241104
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 30 deletions.
9 changes: 7 additions & 2 deletions api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Cart } from '@shopware-pwa/commons/interfaces/models/checkout/cart/Cart';
import { Category } from '@shopware-pwa/commons/interfaces/models/content/category/Category';
import { CmsPage } from '@shopware-pwa/commons/interfaces/models/content/cms/CmsPage';
import { ContextTokenResponse } from '@shopware-pwa/commons/interfaces/response/ContextTokenResponse';
import { ContextTokenResponse } from '@shopware-pwa/commons/interfaces/response/SessionContext';
import { Country } from '@shopware-pwa/commons/interfaces/models/system/country/Country';
import { Currency } from '@shopware-pwa/commons/interfaces/models/system/currency/Currency';
import { Customer } from '@shopware-pwa/commons/interfaces/models/checkout/customer/Customer';
Expand All @@ -16,6 +16,7 @@ import { CustomerRegistrationParams } from '@shopware-pwa/commons/interfaces/req
import { EqualsAnyFilter } from '@shopware-pwa/commons/interfaces/search/SearchFilter';
import { EqualsFilter } from '@shopware-pwa/commons/interfaces/search/SearchFilter';
import { Grouping } from '@shopware-pwa/commons/interfaces/search/Grouping';
import { GuestOrderParams } from '@shopware-pwa/commons/interfaces/request/GuestOrderParams';
import { Language } from '@shopware-pwa/commons/interfaces/models/framework/language/Language';
import { MultiFilter } from '@shopware-pwa/commons/interfaces/search/SearchFilter';
import { NavigationResponse } from '@shopware-pwa/commons/interfaces/models/content/navigation/Navigation';
Expand All @@ -27,6 +28,7 @@ import { RangeFilter } from '@shopware-pwa/commons/interfaces/search/SearchFilte
import { Salutation } from '@shopware-pwa/commons/interfaces/models/system/salutation/Salutation';
import { SearchCriteria } from '@shopware-pwa/commons/interfaces/search/SearchCriteria';
import { SearchResult } from '@shopware-pwa/commons/interfaces/response/SearchResult';
import { SessionContext } from '@shopware-pwa/commons/interfaces/response/SessionContext';
import { ShippingMethod } from '@shopware-pwa/commons/interfaces/models/checkout/shipping/ShippingMethod';
import { ShopwareAssociation } from '@shopware-pwa/commons/interfaces/search/Association';

Expand Down Expand Up @@ -60,7 +62,7 @@ export interface ConfigChangedArgs {
export function createCustomerAddress(params: CustomerAddressParam): Promise<string>;

// @alpha
export function createGuestOrder(email: string): Promise<Order>;
export function createGuestOrder(params: GuestOrderParams): Promise<Order>;

// @alpha
export function createOrder(): Promise<Order>;
Expand Down Expand Up @@ -175,6 +177,9 @@ export const getProducts: (searchCriteria?: SearchCriteria | undefined) => Promi
// @alpha
export const getProductsIds: () => Promise<SearchResult<string[]>>;

// @alpha
export function getSessionContext(): Promise<SessionContext>;

// @alpha (undocumented)
export function getUserCountry(countryId: string): Promise<Country>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface BillingAddress {
export interface BillingAddress {
countryId: string;
salutationId: string;
street: string;
Expand All @@ -10,18 +10,18 @@ interface BillingAddress {
phoneNumber?: string;
}

interface ShippingAddress extends BillingAddress {
export interface ShippingAddress extends BillingAddress {
firstName: string;
lastName: string;
}

export interface CreateGuestOrderParams {
export interface GuestOrderParams {
email: string;
salutationId: string;
firstName: string;
lastName: string;
billingAddress: BillingAddress;
shippingAddress: ShippingAddress;
shippingAddress?: ShippingAddress;
affiliateCode?: string;
campaignCode?: string;
phoneNumber?: string;
Expand Down
3 changes: 0 additions & 3 deletions packages/commons/interfaces/response/ContextTokenResponse.ts

This file was deleted.

38 changes: 38 additions & 0 deletions packages/commons/interfaces/response/SessionContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { PaymentMethod } from "../models/checkout/payment/PaymentMethod";
import { ShippingMethod } from "../models/checkout/shipping/ShippingMethod";
import { Country } from "../models/system/country/Country";
import { User } from "../models/system/user/User";

export interface ContextTokenResponse {
contextToken: string;
}

export interface SessionContext {
token: string;
currentCustomerGroup: {
id: string;
name: string;
};
fallbackCustomerGroup: {
id: string;
name: string;
};
currency: {
id: string;
name: string;
};
salesChannel: {
id: string;
name: string;
};
taxRules: {
id: string;
name: string;
}[];
customer?: User;
paymentMethod: PaymentMethod;
shippingMethod: ShippingMethod;
shippingLocation: {
country: Country;
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createOrder, createGuestOrder } from "@shopware-pwa/shopware-6-client";
import { apiService } from "../../../src/apiService";
import { GuestOrderParams } from "@shopware-pwa/commons/interfaces/request/GuestOrderParams";

jest.mock("../../../src/apiService");
const mockedAxios = apiService as jest.Mocked<typeof apiService>;
Expand Down Expand Up @@ -33,14 +34,28 @@ describe("CheckoutService createOrder", () => {
});
});
describe("createGuestOrder", () => {
const createGuestOrderData: GuestOrderParams = {
email: "some@email.com",
salutationId: "2bbb89dfa4664bc581e80b37eaa80fa7",
firstName: "Joe",
lastName: "Doe",
billingAddress: {
countryId: "0bbb89dfa4664bc581e80b37eaa80fb7",
salutationId: "2bbb89dfa4664bc581e80b37eaa80fa7",
street: "Shopstreet",
zipcode: "51-123",
city: "Wroclaw",
},
};
it("should return undefined when there is no data property in the response", async () => {
mockedAxios.post.mockResolvedValueOnce({});

const result = await createGuestOrder("some@email.com");
const result = await createGuestOrder(createGuestOrderData);
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith("/checkout/guest-order", {
email: "some@email.com",
});
expect(mockedAxios.post).toBeCalledWith(
"/checkout/guest-order",
createGuestOrderData
);
expect(result).toBeUndefined();
});
it("should return newly added order object", async () => {
Expand All @@ -52,11 +67,12 @@ describe("CheckoutService createOrder", () => {
},
});

const result = await createGuestOrder("dummy@email.com");
const result = await createGuestOrder(createGuestOrderData);
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith("/checkout/guest-order", {
email: "dummy@email.com",
});
expect(mockedAxios.post).toBeCalledWith(
"/checkout/guest-order",
createGuestOrderData
);
expect(result).toHaveProperty("id");
});

Expand All @@ -65,7 +81,7 @@ describe("CheckoutService createOrder", () => {
await createGuestOrder(undefined as any);
} catch (e) {
expect(e.message).toBe(
"createGuestOrder method requires email to be provided as a parameter"
"createGuestOrder method requires GuestOrderParams"
);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { apiService } from "../../../src/apiService";
import { getSessionContext } from "@shopware-pwa/shopware-6-client";
import { SessionContext } from "@shopware-pwa/commons/interfaces/response/SessionContext";

jest.mock("../../../src/apiService");
const mockedAxios = apiService as jest.Mocked<typeof apiService>;

describe("ContextService - getSessionContext", () => {
beforeEach(() => {
jest.resetAllMocks();
});
it("should return sessionContext", async () => {
mockedAxios.get.mockResolvedValueOnce({ data: { token: "qwerty" } });

const result: SessionContext = await getSessionContext();
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith("/context");
expect(result.token).toEqual("qwerty");
});
});
2 changes: 1 addition & 1 deletion packages/shopware-6-client/src/services/cartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getCheckoutCartLineItemEndpoint,
} from "../endpoints";
import { apiService } from "../apiService";
import { ContextTokenResponse } from "@shopware-pwa/commons/interfaces/response/ContextTokenResponse";
import { ContextTokenResponse } from "@shopware-pwa/commons/interfaces/response/SessionContext";
import { CartItemType } from "@shopware-pwa/commons/interfaces/cart/CartItemType";

/**
Expand Down
16 changes: 7 additions & 9 deletions packages/shopware-6-client/src/services/checkoutService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getCheckoutGuestOrderEndpoint,
} from "../endpoints";
import { Order } from "@shopware-pwa/commons/interfaces/models/checkout/order/Order";
import { GuestOrderParams } from "@shopware-pwa/commons/interfaces/request/GuestOrderParams";

/**
* Creates an order for logged in users
Expand All @@ -18,19 +19,16 @@ export async function createOrder(): Promise<Order> {
/**
* Creates an order for not logged in users
* Should be used when the user is logged out, but has items in the cart
* @param email - customers's email
* @alpha
*/
export async function createGuestOrder(email: string): Promise<Order> {
if (!email) {
throw new Error(
"createGuestOrder method requires email to be provided as a parameter"
);
export async function createGuestOrder(
params: GuestOrderParams
): Promise<Order> {
if (!params) {
throw new Error("createGuestOrder method requires GuestOrderParams");
}

const resp = await apiService.post(getCheckoutGuestOrderEndpoint(), {
email,
});
const resp = await apiService.post(getCheckoutGuestOrderEndpoint(), params);

return resp.data?.data;
}
16 changes: 15 additions & 1 deletion packages/shopware-6-client/src/services/contextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import { Language } from "@shopware-pwa/commons/interfaces/models/framework/lang
import { Salutation } from "@shopware-pwa/commons/interfaces/models/system/salutation/Salutation";
import { SearchResult } from "@shopware-pwa/commons/interfaces/response/SearchResult";
import { UpdateContextParams } from "@shopware-pwa/commons/interfaces/request/UpdateContextParams";
import { ContextTokenResponse } from "@shopware-pwa/commons/interfaces/response/ContextTokenResponse";
import {
ContextTokenResponse,
SessionContext,
} from "@shopware-pwa/commons/interfaces/response/SessionContext";

/**
* @throws ClientApiError
Expand All @@ -32,6 +35,17 @@ async function updateContext(
return { contextToken };
}

/**
* Loads session context, containing all session-related data.
*
* @throws ClientApiErrosr
* @alpha
*/
export async function getSessionContext(): Promise<SessionContext> {
const resp = await apiService.get(getContextEndpoint());
return resp.data;
}

/**
* Set the current session's shipping address to correspoding to id
* @throws ClientApiError
Expand Down
2 changes: 1 addition & 1 deletion packages/shopware-6-client/src/services/customerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Customer } from "@shopware-pwa/commons/interfaces/models/checkout/custo
import { apiService } from "../apiService";
import { CustomerAddress } from "@shopware-pwa/commons/interfaces/models/checkout/customer/CustomerAddress";
import { CustomerRegistrationParams } from "@shopware-pwa/commons/interfaces/request/CustomerRegistrationParams";
import { ContextTokenResponse } from "@shopware-pwa/commons/interfaces/response/ContextTokenResponse";
import { ContextTokenResponse } from "@shopware-pwa/commons/interfaces/response/SessionContext";
import { Order } from "@shopware-pwa/commons/interfaces/models/checkout/order/Order";

/**
Expand Down

0 comments on commit e241104

Please sign in to comment.