Skip to content

Add Customer resource #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ secrets.json
.nvmrc

# VSCode settings
.vscode
.vscode

#Emacs
*~
22 changes: 22 additions & 0 deletions src/checkout-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import handleCheckoutMutation from './handle-checkout-mutation';
// GraphQL
import checkoutNodeQuery from './graphql/checkoutNodeQuery.graphql';
import checkoutCreateMutation from './graphql/checkoutCreateMutation.graphql';
import checkoutCustomerAssociateV2 from './graphql/checkoutCustomerAssociateV2.graphql';
import checkoutLineItemsAddMutation from './graphql/checkoutLineItemsAddMutation.graphql';
import checkoutLineItemsRemoveMutation from './graphql/checkoutLineItemsRemoveMutation.graphql';
import checkoutLineItemsReplaceMutation from './graphql/checkoutLineItemsReplaceMutation.graphql';
Expand All @@ -16,6 +17,7 @@ import checkoutGiftCardsAppendMutation from './graphql/checkoutGiftCardsAppendMu
import checkoutGiftCardRemoveV2Mutation from './graphql/checkoutGiftCardRemoveV2Mutation.graphql';
import checkoutEmailUpdateV2Mutation from './graphql/checkoutEmailUpdateV2Mutation.graphql';
import checkoutShippingAddressUpdateV2Mutation from './graphql/checkoutShippingAddressUpdateV2Mutation.graphql';
import checkoutShippingLineUpdateMutation from './graphql/checkoutShippingLineUpdateMutation.graphql';

/**
* The JS Buy SDK checkout resource
Expand Down Expand Up @@ -78,6 +80,12 @@ class CheckoutResource extends Resource {
.then(handleCheckoutMutation('checkoutCreate', this.graphQLClient));
}

associateCustomer(checkoutId, customerAccessToken) {
return this.graphQLClient
.send(checkoutCustomerAssociateV2, {checkoutId, customerAccessToken})
.then(handleCheckoutMutation('checkoutCustomerAssociateV2', this.graphQLClient));
}

/**
* Replaces the value of checkout's custom attributes and/or note with values defined in the input
*
Expand Down Expand Up @@ -326,6 +334,20 @@ class CheckoutResource extends Resource {
.send(checkoutShippingAddressUpdateV2Mutation, {checkoutId, shippingAddress})
.then(handleCheckoutMutation('checkoutShippingAddressUpdateV2', this.graphQLClient));
}

/**
* Updates the shipping lines on an existing checkout.
*
* @param {String} checkoutId The ID of the checkout to update shipping address.
* @param {Object} shippingRateHandle A unique identifier to a Checkout’s shipping provide
* @return {Promise|GraphModel} A promise resolving with the updated checkout.
*/
updateShippingLineAddress(checkoutId, shippingRateHandle) {
return this.graphQLClient
.send(checkoutShippingLineUpdateMutation, {checkoutId, shippingRateHandle})
.then(handleCheckoutMutation('checkoutShippingLineUpdate', this.graphQLClient));
}

}

export default CheckoutResource;
2 changes: 2 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ProductResource from './product-resource';
import CollectionResource from './collection-resource';
import ShopResource from './shop-resource';
import CheckoutResource from './checkout-resource';
import CustomerResource from './customer-resource';
import ImageResource from './image-resource';
import {version} from '../package.json';

Expand Down Expand Up @@ -80,6 +81,7 @@ class Client {
this.collection = new CollectionResource(this.graphQLClient);
this.shop = new ShopResource(this.graphQLClient);
this.checkout = new CheckoutResource(this.graphQLClient);
this.customer = new CustomerResource(this.graphQLClient);
this.image = new ImageResource(this.graphQLClient);
}

Expand Down
206 changes: 206 additions & 0 deletions src/customer-resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import Resource from './resource';
import defaultResolver from './default-resolver';
import handleCustomerMutation from './handle-customer-mutation';

// GraphQL
import customerNodeQuery from './graphql/customerNodeQuery.graphql';

import customerCreateMutation from './graphql/customerCreateMutation.graphql';

import customerAccessTokenCreateMutation from './graphql/customerAccessTokenCreateMutation.graphql';
import customerAccessTokenCreateWithMultipassMutation from './graphql/customerAccessTokenCreateWithMultipassMutation.graphql';
import customerAccessTokenDeleteMutation from './graphql/customerAccessTokenDeleteMutation.graphql';
import customerAccessTokenRenewMutation from './graphql/customerAccessTokenRenewMutation.graphql';

import customerActivateByUrlMutation from './graphql/customerActivateByUrlMutation.graphql';
import customerActivateMutation from './graphql/customerActivateMutation.graphql';

import customerAddressCreateMutation from './graphql/customerAddressCreateMutation.graphql';
import customerAddressDeleteMutation from './graphql/customerAddressDeleteMutation.graphql';
import customerAddressUpdateMutation from './graphql/customerAddressUpdateMutation.graphql';

import customerDefaultAddressUpdateMutation from './graphql/customerDefaultAddressUpdateMutation.graphql';
import customerRecoverMutation from './graphql/customerRecoverMutation.graphql';
import customerResetByUrlMutation from './graphql/customerResetByUrlMutation.graphql';
import customerResetMutation from './graphql/customerResetMutation.graphql';

import customerUpdateMutation from './graphql/customerUpdateMutation.graphql';

/**
* The JS Buy SDK customer resource
* @class
*/
class CustomerResource extends Resource {
fetch(customerAccessToken) {
return this.graphQLClient
.send(customerNodeQuery, {customerAccessToken})
.then(defaultResolver('customer'))
}

/**
* Creates a customer.
*
* @example
* const input = {
* email: "[email protected]",
* password: "HiZqFuDvDdQ7"
* };
*
* client.customer.create(input).then((customer) => {
* // Do something with the newly created customer
* });
*
* @param {Object} [input] An input object containing of:
* @param {String} [input.email] The customer’s email
* @param {String} [input.password] The login password used by the customer.
* @return {Promise|GraphModel} A promise resolving with the created customer.
*/
create(input) {
return this.graphQLClient
.send(customerCreateMutation, {input})
.then(handleCustomerMutation('customerCreate'));
}

/**
* Update a customer.
*
*/
update(customerAccessToken, customer) {
return this.graphQLClient
.send(customerUpdateMutation, {customerAccessToken, customer})
.then(handleCustomerMutation('customerUpdate'));
}

/**
* Create a access token.
*
*/
createAccessToken(input) {
return this.graphQLClient
.send(customerAccessTokenCreateMutation, {input})
.then(handleCustomerMutation('customerAccessTokenCreate'));
}

/**
* Delete a access token.
*
*/
deleteAccessToken(customerAccessToken) {
return this.graphQLClient
.send(customerAccessTokenDeleteMutation, {customerAccessToken})
.then(handleCustomerMutation('customerAccessTokenDelete'));
}

/**
* Renew a access token.
*
*/
renewAccessToken(customerAccessToken) {
return this.graphQLClient
.send(customerAccessTokenRenewMutation, {customerAccessToken})
.then(handleCustomerMutation('customerAccessTokenRenew'));
}

/**
* Create a access token with Multipass.
*
*/
createAccessTokenWithMultipass(multipassToken) {
return this.graphQLClient
.send(customerAccessTokenCreateWithMultipassMutation, {multipassToken})
.then(handleCustomerMutation('customerAccessTokenCreateWithMultipass'));
}

/**
* Activate customer account.
*
*/
activate(id, input) {
return this.graphQLClient
.send(customerActivateMutation, {id, input})
.then(handleCustomerMutation('customerActivate'));
}

/**
* Activate customer account by URL.
*
*/
activateByUrl(activationUrl, password) {
return this.graphQLClient
.send(customerActivateByUrlMutation, {activationUrl, password})
.then(handleCustomerMutation('customerActivateByUrl'));
}

/**
* Create customer's address.
*
*/
createAddress(customerAccessToken, address) {
return this.graphQLClient
.send(customerAddressCreateMutation, {customerAccessToken, address})
.then(handleCustomerMutation('customerAddressCreate'));
}

/**
* Delete customer's address.
*
*/
deleteAddress(id, customerAccessToken) {
return this.graphQLClient
.send(customerAddressDeleteMutation, {id, customerAccessToken})
.then(handleCustomerMutation('customerAddressDelete'));
}

/**
* Update customer's address.
*
*/
updateAddress(customerAccessToken, id, address) {
return this.graphQLClient
.send(customerAddressUpdateMutation, {customerAccessToken, id, address})
.then(handleCustomerMutation('customerAddressUpdate'));
}

/**
* Update customer's default address.
*
*/
updateDefaultAddress(customerAccessToken, addressId) {
return this.graphQLClient
.send(customerDefaultAddressUpdateMutation, {customerAccessToken, addressId})
.then(handleCustomerMutation('customerDefaultAddressUpdate'));
}

/**
* Recover customer.
*
*/
recover(email) {
return this.graphQLClient
.send(customerRecoverMutation, {email})
.then(handleCustomerMutation('customerRecover'));
}

/**
* Reset customer password.
*
*/
reset(id, input) {
return this.graphQLClient
.send(customerResetMutation, {id, input})
.then(handleCustomerMutation('customerReset'));
}

/**
* Reset customer password by Url.
*
*/
resetByUrl(resetUrl, password) {
return this.graphQLClient
.send(customerResetByUrlMutation, {resetUrl, password})
.then(handleCustomerMutation('customerResetByUrl'));
}

}

export default CustomerResource;
18 changes: 18 additions & 0 deletions src/graphql/checkoutCustomerAssociateV2.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mutation checkoutCustomerAssociateV2($checkoutId: ID!, $customerAccessToken: String!) {
checkoutCustomerAssociateV2(
checkoutId: $checkoutId
customerAccessToken: $customerAccessToken
) {
checkout {
id
}
checkoutUserErrors {
code
field
message
}
customer {
id
}
}
}
15 changes: 15 additions & 0 deletions src/graphql/checkoutShippingLineUpdateMutation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mutation checkoutShippingLineUpdate($checkoutId: ID!, $shippingRateHandle: String!) {
checkoutShippingLineUpdate(
checkoutId: $checkoutId
shippingRateHandle: $shippingRateHandle
) {
checkout {
id
}
checkoutUserErrors {
code
field
message
}
}
}
13 changes: 13 additions & 0 deletions src/graphql/customerAccessTokenCreateMutation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mutation customerAccessTokenCreate($input: CustomerAccessTokenCreateInput!) {
customerAccessTokenCreate(input: $input) {
customerAccessToken {
accessToken
expiresAt
}
customerUserErrors {
code
field
message
}
}
}
13 changes: 13 additions & 0 deletions src/graphql/customerAccessTokenCreateWithMultipassMutation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mutation customerAccessTokenCreateWithMultipass($multipassToken: String!) {
customerAccessTokenCreateWithMultipass(multipassToken: $multipassToken) {
customerAccessToken {
accessToken
expiresAt
}
customerUserErrors {
code
field
message
}
}
}
10 changes: 10 additions & 0 deletions src/graphql/customerAccessTokenDeleteMutation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mutation customerAccessTokenDelete($customerAccessToken: String!) {
customerAccessTokenDelete(customerAccessToken: $customerAccessToken) {
deletedAccessToken
deletedCustomerAccessTokenId
userErrors {
field
message
}
}
}
12 changes: 12 additions & 0 deletions src/graphql/customerAccessTokenRenewMutation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mutation customerAccessTokenRenew($customerAccessToken: String!) {
customerAccessTokenRenew(customerAccessToken: $customerAccessToken) {
customerAccessToken {
accessToken
expiresAt
}
userErrors {
field
message
}
}
}
16 changes: 16 additions & 0 deletions src/graphql/customerActivateByUrlMutation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mutation customerActivateByUrl($activationUrl: URL!, $password: String!) {
customerActivateByUrl(activationUrl: $activationUrl, password: $password) {
customer {
id
}
customerAccessToken {
accessToken
expiresAt
}
customerUserErrors {
code
field
message
}
}
}
Loading