Skip to content

Commit

Permalink
Merge pull request #377 from bakaphp/feat/cart-discount
Browse files Browse the repository at this point in the history
feat: cart discount
  • Loading branch information
kaioken authored Jan 16, 2025
2 parents 2de4b88 + b5bedb2 commit 46d6d53
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
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
}
});
});

0 comments on commit 46d6d53

Please sign in to comment.