Skip to content

Commit

Permalink
feat(customer): add register method (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Kucmus committed Oct 15, 2019
1 parent 732a726 commit bde0b53
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
import { CustomerService } from "../../src/index";

const generateUniqueEmail = (): string => {
const currentTime = Date.now();
const email = `mkucmus+${currentTime}@divante.com`;
return email;
};

const basicCustomerData = {
salutationId:
"c370eb5cd1df4d4dbcc78f055b693e79" /** TODO: get the mocked one if possible */,
firstName: "Test",
lastName: "Test",
password: "test123456",
email: generateUniqueEmail(),
billingAddress: {
countryId:
"3ff87c8746dc40ecaea1495eb254278e" /** TODO: get the mocked one if possible */,
street: "Example 128",
zipcode: "555-444",
city: "Breslau",
phoneNumber: "+48 5543333333"
}
};

const loginCredentials = {
username: "mkucmus@divante.com",
password: "test123456"
};

describe("CustomerService", () => {
describe("register", () => {
it("should register the new customer with basic data provided", async () => {
try {
const result = await CustomerService.register(basicCustomerData);
expect(result).toHaveProperty("data");
expect(result.data).toHaveLength(32);
} catch (e) {
console.error("Connection problem", e);
}
});
it("should never register a customer without billing address", async () => {
try {
await CustomerService.register(
Object.assign(basicCustomerData, {
billingAddress: {}
})
);
} catch (e) {
e.response && expect(e.response.status).toEqual(400);
}
});
it("should never register a customer with too short password", async () => {
try {
await CustomerService.register(
Object.assign(basicCustomerData, {
password: "short"
})
);
} catch (e) {
e.response && expect(e.response.status).toEqual(400);
}
});
});
describe("login", () => {
it("should return context token", async () => {
try {
Expand Down
39 changes: 39 additions & 0 deletions packages/shopware-6-client/src/services/customerService.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
import axios from "axios";
import { config } from "../settings";
import { CUSTOMER_ENDPOINT } from "../endpoints";
import { BillingAddress } from "../interfaces/models/checkout/customer/BillingAddress";
import { ShippingAddress } from "../interfaces/models/checkout/customer/ShippingAddress";

interface CustomerLoginParam {
username: string;
password: string;
}

interface CustomerRegisterParam {
salutationId: string;
firstName: string;
lastName: string;
password: string;
email: string;
title?: string;
birthdayYear?: number;
birthdayMonth?: number;
birthdayDay?: number;
billingAddress: Partial<
BillingAddress
> /** TODO: replace Partial with correct optional fields in BillingAddress */;
shippingAddress?: ShippingAddress;
}

interface CustomerLoginResponse {
"sw-context-token": string;
}

interface CustomerRegisterResponse {
data: string;
}

/**
* Usage example:
* ```ts
Expand All @@ -20,8 +42,24 @@ interface CustomerLoginResponse {
export interface CustomerService {
login: (params: CustomerLoginParam) => Promise<CustomerLoginResponse>;
logout: (contextToken?: string) => Promise<null>;
register: (
params: CustomerRegisterParam
) => Promise<CustomerRegisterResponse>;
}

/**
* @description Register a customer
*/
const register = async function(
params: CustomerRegisterParam
): Promise<CustomerRegisterResponse> {
const resp = await axios.post(
`${config.endpoint}${CUSTOMER_ENDPOINT}`,
params
);
return resp.data;
};

/**
* @description Get the context token for current user
*/
Expand Down Expand Up @@ -56,6 +94,7 @@ const logout = async function(contextToken?: string): Promise<null> {
* @description Expose public methods of the service
*/
export const CustomerService: CustomerService = {
register,
login,
logout
};

0 comments on commit bde0b53

Please sign in to comment.