Skip to content

Commit

Permalink
feat: update getChannelProducts method to support pagination and addi…
Browse files Browse the repository at this point in the history
…tional attributes
  • Loading branch information
jaimetorresmctekk committed Jan 8, 2025
1 parent 66daff5 commit 909fee2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
20 changes: 12 additions & 8 deletions src/modules/channels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import {

import { GET_CHANNEL_SOCIAL_CHANNELS } from '../../queries';
import { GET_CHANNEL_PRODUCTS } from '../../queries';
import { ChannelProductsInterface } from 'types/channel-products';
import {
ChannelProductsInterface,
ChannelProductsProps,
} from 'types/channel-products';

export class Channels {
constructor(protected client: ClientType) {}
Expand All @@ -33,18 +36,19 @@ export class Channels {
return response.data.socialChannels.data as ChannelInterface[];
}

public async getChannelProducts(
id: string,
first: number,
whereCondition: WhereCondition
): Promise<ChannelProductsInterface[]> {
public async getChannelProducts({
id,
first,
whereCondition,
page,
}: ChannelProductsProps): Promise<ChannelProductsInterface> {
const response = await this.client.query({
query: GET_CHANNEL_PRODUCTS,
variables: { id, first, whereCondition },
variables: { id, first, whereCondition, page },
fetchPolicy: 'no-cache',
partialRefetch: true,
});
return response.data.channelProducts.data;
return response.data.channelProducts;
}

public async createChannel(
Expand Down
21 changes: 19 additions & 2 deletions src/queries/channel-products.query.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { gql } from '@apollo/client/core';

export const GET_CHANNEL_PRODUCTS = gql`
query GetChannelProducts($id: String!, $first: Int!, $whereCondition: QueryChannelProductsWhereWhereConditions) {
channelProducts(id: $id, first: $first, where: $whereCondition) {
query GetChannelProducts(
$id: String!
$first: Int!
$whereCondition: QueryChannelProductsWhereWhereConditions
$page: Int
) {
channelProducts(
id: $id
first: $first
where: $whereCondition
page: $page
) {
data {
id
uuid
Expand All @@ -21,6 +31,13 @@ export const GET_CHANNEL_PRODUCTS = gql`
value
}
}
paginatorInfo {
count
currentPage
lastPage
perPage
total
}
}
}
`;
22 changes: 11 additions & 11 deletions src/types/channel-products.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { VariantInterface } from './inventory';
import { ProductInterface } from './inventory';
import { PaginatorInfo } from './paginator';
import { WhereCondition } from './index';

export interface ChannelProductsInterface {
export type ChannelProductsProps = {
id: string;
uuid: string;
name: string;
description: string;
variants?: VariantInterface[];
attributes: {
slug: string;
name: string;
value: string;
}
first: number;
whereCondition: WhereCondition;
page: number;
};
export interface ChannelProductsInterface {
data: ProductInterface[];
paginatorInfo?: PaginatorInfo;
}
14 changes: 8 additions & 6 deletions test/channel-products.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initializeClient, getClient } from './setupClient';
import dotenv from 'dotenv';
import { WhereCondition } from '../dist/types/leads';

dotenv.config();

Expand All @@ -17,15 +18,16 @@ describe('Test the KanvasCore client', () => {
const channelId = process.env.KANVAS_CHHANNEL_ID as string;
if (channelId) {
const client = getClient();
const channelProducts = await client.channels.getChannelProducts(
channelId,
10,
{
const channelProducts = await client.channels.getChannelProducts({
id: channelId,
first: 10,
whereCondition: {
column: 'UUID',
operator: 'EQ',
value: '786dd0f8-eba9-48fd-a87e-497c4c220437',
}
);
},
page: 1,
});
expect(channelProducts).toBeDefined();
}
});
Expand Down

0 comments on commit 909fee2

Please sign in to comment.