Skip to content
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

feat: cart discount #377

Merged
merged 1 commit into from
Jan 16, 2025
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.26.3",
"version": "0.27.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
22 changes: 18 additions & 4 deletions src/modules/commerce/cart/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ClientType } from '../../../index';
import { ADD_TO_CART_MUTATION, CLEAR_CART_MUTATION, GET_CART_QUERY } from '../../../mutations';
import { CartData, CartItemData, CartItemInput } from '../../../types/commerce';
import {
ADD_TO_CART_MUTATION,
CLEAR_CART_MUTATION,
GET_CART_QUERY,
CART_DISCOUNT_CODES_UPDATE
} from '../../../mutations';
import { CartData, CartDataDiscount, CartItemData, CartItemInput } from '../../../types/commerce';

export class Cart {
constructor(protected client: ClientType) { }
Expand All @@ -10,7 +15,7 @@ export class Cart {
mutation: ADD_TO_CART_MUTATION,
variables: { input },
});

return response.data;
}

Expand All @@ -29,4 +34,13 @@ export class Cart {

return response.data;
}
}

public async updateDiscountCodes(discountCodes: string[]): Promise<CartDataDiscount> {
const response = await this.client.mutate({
mutation: CART_DISCOUNT_CODES_UPDATE,
variables: { discountCodes },
});

return response.data;
}
}
16 changes: 16 additions & 0 deletions src/mutations/commerce.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,19 @@ export const GET_CART_QUERY = gql`
}
}
`;

export const CART_DISCOUNT_CODES_UPDATE = gql`
mutation($discountCodes: [String!]!) {
cartDiscountCodesUpdate(discountCodes: $discountCodes) {
id
total
items {
id
name
price
quantity
attributes
}
}
}
`;
14 changes: 14 additions & 0 deletions src/types/commerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ export interface CartData {
};
}

export interface CartDataDiscount {
cartDiscountCodesUpdate: {
id: string;
total: number;
items: {
id: number;
name: string;
price: number;
quantity: number;
attributes: string;
}[];
};
}

export interface OrderItemInput {
input: {
cartId: 'default';
Expand Down
29 changes: 29 additions & 0 deletions test/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,33 @@ describe('Test the Commerce', () => {
expect(getCart.cart.id).toBeDefined();
expect(getCart.cart.items).toBeInstanceOf(Array);
});

it('updates discount codes in cart', async () => {
const client = getClient();
const cart = client.cart;

// Test adding discount codes
const updatedCart = await cart.updateDiscountCodes(['TEST_CODE']);
expect(updatedCart).toBeDefined();
expect(updatedCart.cartDiscountCodesUpdate.id).toBeDefined();
expect(updatedCart.cartDiscountCodesUpdate.items).toBeInstanceOf(Array);

// Optional: Test clearing discount codes
const clearedCart = await cart.updateDiscountCodes([]);
expect(clearedCart).toBeDefined();
expect(clearedCart.cartDiscountCodesUpdate.id).toBeDefined();
});

// Optional: Test error case
it('handles invalid discount codes', async () => {
const client = getClient();
const cart = client.cart;

try {
await cart.updateDiscountCodes(['INVALID_CODE']);
} catch (error) {
expect(error).toBeDefined();
// Add more specific error checks based on your API's error response
}
});
});
Loading