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

Commit

Permalink
feat: account association response (#901)
Browse files Browse the repository at this point in the history
* feat: account association response

* feat: account association response

* Feat: Add code to promotion filters (#902)

* feat: search code is eq, for promotion codes

* Update promotions.d.ts

feat: code filter

---------

Co-authored-by: Steve Ramage <[email protected]>
Co-authored-by: yasamanloghman <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2024
1 parent b5e0d6e commit e7738df
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 17 deletions.
60 changes: 51 additions & 9 deletions src/endpoints/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CartEndpoint extends BaseExtend {
}

Get() {
const { includes } = this
const {includes} = this

return this.request.send(
buildURL(`${this.endpoint}/${this.cartId}`, {
Expand All @@ -27,7 +27,7 @@ class CartEndpoint extends BaseExtend {
}

Items() {
const { includes, sort, limit, offset, filter } = this
const {includes, sort, limit, offset, filter} = this

this.call = this.request.send(
buildURL(`${this.endpoint}/${this.cartId}/items`, {
Expand All @@ -51,7 +51,7 @@ class CartEndpoint extends BaseExtend {
return this.request.send(`${this.endpoint}/${this.cartId}/shipping-groups/${shippingGroupId}`, 'GET')
}

CreateShippingGroup(body){
CreateShippingGroup(body) {
const shippingObject = Object.assign(body, {
type: 'shipping-type'
})
Expand Down Expand Up @@ -161,7 +161,7 @@ class CartEndpoint extends BaseExtend {
BulkAddCartCustomDiscount(body, options) {
return this.request.send(`${this.endpoint}/${this.cartId}/custom-discounts`, 'POST', {
data: body,
...(options && { options })
...(options && {options})
})
}

Expand All @@ -179,7 +179,7 @@ class CartEndpoint extends BaseExtend {
BulkAdd(body, options) {
return this.request.send(`${this.endpoint}/${this.cartId}/items`, 'POST', {
data: body,
...(options && { options })
...(options && {options})
})
}

Expand Down Expand Up @@ -215,6 +215,48 @@ class CartEndpoint extends BaseExtend {
)
}

AddAccountAssociation(accountId, token) {
const body = [
{
type: 'account',
id: accountId
}
]
return this.request.send(
`${this.endpoint}/${this.cartId}/relationships/accounts`,
'POST',
body,
undefined,
this,
true,
null,
token
? { 'EP-ACCOUNT-MANAGEMENT-AUTHENTICATION-TOKEN': token }
: undefined
)
}

RemoveAccountAssociation(accountId, token) {
const body = [
{
type: 'account',
id: accountId
}
]
return this.request.send(
`${this.endpoint}/${this.cartId}/relationships/accounts`,
'DELETE',
body,
undefined,
this,
true,
null,
token
? { 'EP-ACCOUNT-MANAGEMENT-AUTHENTICATION-TOKEN': token }
: undefined
)
}

RemoveItem(itemId) {
return this.request.send(
`${this.endpoint}/${this.cartId}/items/${itemId}`,
Expand All @@ -232,7 +274,7 @@ class CartEndpoint extends BaseExtend {
return this.request.send(
`${this.endpoint}/${this.cartId}/items/${itemId}`,
'PUT',
{ ...body, ...data },
{...body, ...data},
null,
null,
true,
Expand All @@ -242,7 +284,7 @@ class CartEndpoint extends BaseExtend {
}

UpdateItems(items) {
const body = items.map(({ id, quantity, type, ...rest }) =>
const body = items.map(({id, quantity, type, ...rest}) =>
buildCartItemData(id, quantity, type, rest)
)

Expand Down Expand Up @@ -278,7 +320,7 @@ class CartEndpoint extends BaseExtend {
BulkAddItemTax(body, options) {
return this.request.send(`${this.endpoint}/${this.cartId}/taxes`, 'POST', {
data: body,
...(options && { options })
...(options && {options})
})
}

Expand Down Expand Up @@ -362,7 +404,7 @@ class CartEndpoint extends BaseExtend {
'POST',
{
data: body,
...(options && { options })
...(options && {options})
},
token
)
Expand Down
37 changes: 29 additions & 8 deletions src/types/cart.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
Resource,
QueryableResource,
ResourceIncluded,
Identifiable
Identifiable,
ResourceList
} from './core'
import { Address } from './address'
import { Price, FormattedPrice } from './price'
Expand Down Expand Up @@ -151,6 +152,14 @@ export interface CartItemsResponse {
snapshot_date?: string
}

export interface AccountAssociationData{
type: string
id: string
}

export interface AccountAssociationResponse extends ResourceList<AccountAssociationData> {}


export interface BulkAddOptions {
add_all_or_nothing: boolean
}
Expand Down Expand Up @@ -504,7 +513,6 @@ export interface CartEndpoint
/**
* Customer Cart Associations
* Description: You can create an association between a customer and a cart with the capability to delete any associations as required.
* DOCS: https://documentation.elasticpath.com/commerce-cloud/docs/api/carts-and-orders/carts/customer-cart-associations.html
* @param customerId the id of the customer.
* @param token a customer token to access specific customer orders.
*/
Expand All @@ -514,16 +522,29 @@ export interface CartEndpoint
): Promise<CartItemsResponse>

/**
* Customer Cart Associations
* Add an Account Cart Associations
* Description: You can create an association between a customer and a cart with the capability to delete any associations as required.
* DOCS: https://documentation.elasticpath.com/commerce-cloud/docs/api/carts-and-orders/carts/customer-cart-associations.html
* @param customerId the id of the customer.
*
* @param accountId the id of the account.
* @param token a customer token to access specific customer orders.
*/
AddCustomerAssociation(
customerId: string,
AddAccountAssociation(
accountId: string,
token: string
): Promise<CartItemsResponse>
): Promise<AccountAssociationResponse>

/**
* Remove an Account Cart Associations
* Description: You can create an association between a customer and a cart with the capability to delete any associations as required.
*
* @param accountId the id of the account.
* @param token a customer token to access specific customer orders.
*/
RemoveAccountAssociation(
accountId: string,
token: string
): Promise<{}>


/**
* @deprecated Use UpdateItem method
Expand Down
51 changes: 51 additions & 0 deletions test/unit/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1291,4 +1291,55 @@ describe('Moltin cart', () => {
assert.isObject(response)
})
})

it('should add an account id association to the cart using a JWT', () => {
// Intercept the API request
nock(apiUrl, {
reqheaders: {
Authorization: 'Bearer a550d8cbd4a4627013452359ab69694cd446615a',
'EP-ACCOUNT-MANAGEMENT-AUTHENTICATION-TOKEN': 'testtoken'
}
})
.post('/carts/5/relationships/accounts', {
data: [
{
type: 'account',
id: 'account-1'
}
]
})
.reply(200, {})

return Moltin.Cart('5')
.AddAccountAssociation('account-1', 'testtoken')
.then(response => {
assert.isObject(response)
})
})


it('should remove an account id association from the cart using a JWT', () => {
// Intercept the API request
nock(apiUrl, {
reqheaders: {
Authorization: 'Bearer a550d8cbd4a4627013452359ab69694cd446615a',
'EP-ACCOUNT-MANAGEMENT-AUTHENTICATION-TOKEN': 'testtoken'
}
})
.delete('/carts/5/relationships/accounts', {
data: [
{
type: 'account',
id: 'account-1'
}
]
})
.reply(204, {})

return Moltin.Cart('5')
.RemoveAccountAssociation('account-1', 'testtoken')
.then(response => {
assert.isObject(response)
})
})
})

0 comments on commit e7738df

Please sign in to comment.