From 909fee2c302ef776519f879fd1894a5add4c85c1 Mon Sep 17 00:00:00 2001 From: jaimetorresmctekk Date: Wed, 8 Jan 2025 08:50:33 -0500 Subject: [PATCH] feat: update getChannelProducts method to support pagination and additional attributes --- src/modules/channels/index.ts | 20 ++++++++++++-------- src/queries/channel-products.query.ts | 21 +++++++++++++++++++-- src/types/channel-products.ts | 22 +++++++++++----------- test/channel-products.test.ts | 14 ++++++++------ 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/modules/channels/index.ts b/src/modules/channels/index.ts index 6dd48be..614919d 100644 --- a/src/modules/channels/index.ts +++ b/src/modules/channels/index.ts @@ -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) {} @@ -33,18 +36,19 @@ export class Channels { return response.data.socialChannels.data as ChannelInterface[]; } - public async getChannelProducts( - id: string, - first: number, - whereCondition: WhereCondition - ): Promise { + public async getChannelProducts({ + id, + first, + whereCondition, + page, + }: ChannelProductsProps): Promise { 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( diff --git a/src/queries/channel-products.query.ts b/src/queries/channel-products.query.ts index 8ec1063..e701f14 100644 --- a/src/queries/channel-products.query.ts +++ b/src/queries/channel-products.query.ts @@ -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 @@ -21,6 +31,13 @@ export const GET_CHANNEL_PRODUCTS = gql` value } } + paginatorInfo { + count + currentPage + lastPage + perPage + total + } } } `; diff --git a/src/types/channel-products.ts b/src/types/channel-products.ts index ae11a88..e1c276c 100644 --- a/src/types/channel-products.ts +++ b/src/types/channel-products.ts @@ -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; } diff --git a/test/channel-products.test.ts b/test/channel-products.test.ts index 64b263e..1d94801 100644 --- a/test/channel-products.test.ts +++ b/test/channel-products.test.ts @@ -1,5 +1,6 @@ import { initializeClient, getClient } from './setupClient'; import dotenv from 'dotenv'; +import { WhereCondition } from '../dist/types/leads'; dotenv.config(); @@ -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(); } });