Skip to content

Commit

Permalink
fix: changes in list, search and tags (#1394)
Browse files Browse the repository at this point in the history
added listing for tags, filter for seach and list, support for old and new response in search
  • Loading branch information
akp111 authored Sep 24, 2024
1 parent 80792b4 commit 665a96d
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 31 deletions.
43 changes: 43 additions & 0 deletions packages/restapi/src/lib/channels/getAllTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { getCAIPAddress, getAPIBaseUrls, getQueryParams } from '../helpers';
import Constants, { ENV } from '../constants';
import { axiosGet } from '../utils/axiosUtil';
import CONSTANTS from '../constantsV2';

/**
* GET v1/channels/tags/all
*/
export type GetAllTagsOptionsType = {
page?: number;
limit?: number;
order?: string;
filter?:string;
env?: ENV;
};

/**
* Returns the tags available based on the filter provided
*/
export const getAllTags = async (options: GetAllTagsOptionsType) => {
const {
page=1,
limit=10,
order=CONSTANTS.FILTER.CHANNEL_LIST.ORDER.DESCENDING,
filter=CONSTANTS.FILTER.TAGS.PUSH,
env = Constants.ENV.PROD,
} = options || {};

const API_BASE_URL = getAPIBaseUrls(env);
const apiEndpoint = `${API_BASE_URL}/v1/channels`;
const queryObj = {
page,
limit,
order,
filter,
}
const requestUrl = `${apiEndpoint}/tags/all?${getQueryParams(queryObj)}`;
return await axiosGet(requestUrl)
.then((response) => response.data?.tags)
.catch((err) => {
console.error(`[EPNS-SDK] - API ${requestUrl}: `, err);
});
};
6 changes: 3 additions & 3 deletions packages/restapi/src/lib/channels/getChannels.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ENV } from '../constants';
import CONSTANTS from '../constantsV2';

import { getAPIBaseUrls, getCAIPAddress } from '../helpers';
import { axiosGet } from '../utils/axiosUtil';
import { parseSettings } from '../utils/parseSettings';

/**
* GET /v1/channels/{addressinCAIP}
*/
Expand All @@ -16,6 +14,7 @@ type getChannelsOptionsType = {
limit?: number;
sort?: string;
order?: string;
filter?: number;
}

export const getChannels = async (options: getChannelsOptionsType) => {
Expand All @@ -25,11 +24,12 @@ export const getChannels = async (options: getChannelsOptionsType) => {
limit = 10,
sort = CONSTANTS.FILTER.CHANNEL_LIST.SORT.SUBSCRIBER,
order = CONSTANTS.FILTER.CHANNEL_LIST.ORDER.DESCENDING,
filter
} = options || {};

const API_BASE_URL = getAPIBaseUrls(env);
const apiEndpoint = `${API_BASE_URL}/v1/channels`;
const requestUrl = `${apiEndpoint}?page=${page}&limit=${limit}&sort=${sort}&order=${order}`;
const requestUrl = `${apiEndpoint}?page=${page}&limit=${limit}&sort=${sort}&order=${order}${filter? '&filter=' + filter : ''}`;

return await axiosGet(requestUrl)
.then((response) => {
Expand Down
1 change: 1 addition & 0 deletions packages/restapi/src/lib/channels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './subscribe';
export * from './subscribeV2';
export * from './unsubscribe';
export * from './unsubscribeV2';
export * from './getAllTags'
38 changes: 28 additions & 10 deletions packages/restapi/src/lib/channels/search.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { getAPIBaseUrls, getQueryParams, getLimit } from '../helpers';
import Constants, {ENV} from '../constants';
import Constants, { ENV } from '../constants';
import { axiosGet } from '../utils/axiosUtil';

/**
* GET /v1/channels/search/
* GET /v1/channels/search/
* optional params: page=(1)&limit=(20{min:1}{max:30})&query=(searchquery)
*
*
*/

export type SearchChannelOptionsType = {
query: string;
env?: ENV;
page?: number;
limit?: number;
}
filter?: number;
// temp fix to support both new and old format
oldFormat?: boolean;
};

export const search = async (
options: SearchChannelOptionsType
) => {
export const search = async (options: SearchChannelOptionsType) => {
const {
query,
env = Constants.ENV.PROD,
page = Constants.PAGINATION.INITIAL_PAGE,
limit = Constants.PAGINATION.LIMIT,
filter,
oldFormat = true,
} = options || {};

if (!query) throw Error('"query" not provided!');
Expand All @@ -31,12 +34,27 @@ export const search = async (
const queryObj = {
page,
limit: getLimit(limit),
query: query
query,
...(filter && { filter }),
};
const requestUrl = `${apiEndpoint}?${getQueryParams(queryObj)}`;
return axiosGet(requestUrl)
.then((response) => response.data.channels)
.then((response) => {
const channels = response.data.channels;
const itemCount = response.data.total || channels.length;

const formattedResponse = {
itemCount,
result: channels,
};

if (typeof oldFormat !== 'undefined' && oldFormat) {
return channels; // Old format: return array directly
}

return formattedResponse; // New format: return {itemCount, result}
})
.catch((err) => {
console.error(`[Push SDK] - API ${requestUrl}: `, err);
});
}
};
5 changes: 5 additions & 0 deletions packages/restapi/src/lib/constantsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ const CONSTANTS = {
ORDER: ChannelListOrderType,
},
NOTIFICATION_TYPE: NotifictaionType,
TAGS: {
USER: 'USER',
PUSH: 'PUSH',
ALL: '*',
},
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export type FeedsOptions = {
export type ChannelSearchOptions = {
page?: number;
limit?: number;
filter?: number;
// temp fix to support both new and old format
oldFormat?: boolean;
};

// Types related to notification
Expand Down Expand Up @@ -152,9 +155,15 @@ export type ChannelListOptions = {
limit?: number;
sort?: ChannelListSortType;
order?: ChannelListOrderType;
filter?: number;
};


export type TagListOptions = {
page?: number;
limit?: number;
order?: ChannelListOrderType;
filter?: "PUSH" | "USER" | "*";
}



Expand Down
6 changes: 6 additions & 0 deletions packages/restapi/src/lib/pushNotification/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ export class Channel extends PushNotificationBaseClass {
const {
page = Constants.PAGINATION.INITIAL_PAGE,
limit = Constants.PAGINATION.LIMIT,
filter,
oldFormat = true
} = options || {};
return await PUSH_CHANNEL.search({
query: query,
page: page,
limit: limit,
filter: filter,
env: this.env,
oldFormat
});
} catch (error) {
throw new Error(`Push SDK Error: API : channel::search : ${error}`);
Expand Down Expand Up @@ -472,6 +476,7 @@ export class Channel extends PushNotificationBaseClass {
limit,
sort = ChannelListSortType.SUBSCRIBER,
order = ChannelListOrderType.DESCENDING,
filter,
} = options || {};

return await PUSH_CHANNEL.getChannels({
Expand All @@ -480,6 +485,7 @@ export class Channel extends PushNotificationBaseClass {
limit,
sort,
order,
filter
});
} catch (error) {
throw new Error(`Push SDK Error: Contract : channel::list : ${error}`);
Expand Down
62 changes: 45 additions & 17 deletions packages/restapi/src/lib/pushNotification/tags.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import Constants, { ENV } from '../constants';
import { SignerType } from '../types';
import { ChannelInfoOptions, ChannelSearchOptions } from './PushNotificationTypes';
import {
ChannelInfoOptions,
ChannelSearchOptions,
TagListOptions
} from './PushNotificationTypes';
import * as PUSH_CHANNEL from '../channels';
import { PushNotificationBaseClass } from './pushNotificationBase';
import { Channel } from './channel';

import ConstantsV2 from '../constantsV2';
export class Tags extends PushNotificationBaseClass {
private channel: Channel;

constructor(channel: Channel, signer?: SignerType, env?: ENV, account?: string) {
constructor(
channel: Channel,
signer?: SignerType,
env?: ENV,
account?: string
) {
super(signer, env, account);
this.channel = channel;
}
Expand All @@ -21,7 +30,7 @@ export class Tags extends PushNotificationBaseClass {
get = async (options?: ChannelInfoOptions) => {
try {
this.checkSignerObjectExists();
const channel = await this.channel.info()
const channel = await this.channel.info();
return await PUSH_CHANNEL.getTags({
channel: channel,
env: this.env,
Expand All @@ -39,41 +48,40 @@ export class Tags extends PushNotificationBaseClass {
add = async (tags: Array<string>) => {
try {
this.checkSignerObjectExists();
const channel = await this.channel.info()
const channel = await this.channel.info();

const resp = await this.channel.update({
name: channel.name,
description: channel.info,
url: channel.url,
icon: channel.icon,
tags: tags
tags: tags,
});

return { tags }

return { tags };
} catch (error) {
throw new Error(`Push SDK Error: Contract : tags::add : ${error}`);
}
};

/**
/**
* @description update tags for a channel
* @param {Array<string>} tags - tags to be added
* @returns the tags if the transaction is successfull
*/
update = async (tags: Array<string>) => {
try {
this.checkSignerObjectExists();
const channel = await this.channel.info()
const channel = await this.channel.info();
await this.channel.update({
name: channel.name,
description: channel.info,
url: channel.url,
icon: channel.icon,
tags: tags
tags: tags,
});

return { tags }
return { tags };
} catch (error) {
throw new Error(`Push SDK Error: Contract : tags::update : ${error}`);
}
Expand All @@ -86,17 +94,16 @@ export class Tags extends PushNotificationBaseClass {
remove = async () => {
try {
this.checkSignerObjectExists();
const channel = await this.channel.info()
const channel = await this.channel.info();
await this.channel.update({
name: channel.name,
description: channel.info,
url: channel.url,
icon: channel.icon,
tags: []
tags: [],
});

return { status: "success" }

return { status: 'success' };
} catch (error) {
throw new Error(`Push SDK Error: Contract : tags::remove : ${error}`);
}
Expand Down Expand Up @@ -125,5 +132,26 @@ export class Tags extends PushNotificationBaseClass {
} catch (error) {
throw new Error(`Push SDK Error: API : channel::tags::search : ${error}`);
}
}
};

list = async (options?: TagListOptions) => {
try {
const {
page = Constants.PAGINATION.INITIAL_PAGE,
limit = Constants.PAGINATION.LIMIT,
order = ConstantsV2.FILTER.CHANNEL_LIST.ORDER.DESCENDING,
filter = ConstantsV2.FILTER.TAGS.PUSH,
} = options || {};

return await PUSH_CHANNEL.getAllTags({
page: page,
limit: limit,
order: order,
filter: filter,
env: this.env,
});
} catch (error) {
throw new Error(`Push SDK Error: API : channel::tags::list : ${error}`);
}
};
}
13 changes: 13 additions & 0 deletions packages/restapi/tests/lib/channel/getChannels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getChannels } from '../../../src/lib/channels/getChannels';
import { ENV } from '../../../src/lib/constants';
import { expect } from 'chai';

describe('PUSH_CHANNELS.getChannels', () => {
it('Should fetch channels based on the filter', async () => {
const res = await getChannels({
filter: 80002,
env: ENV.DEV,
});
console.log(res);
});
});
24 changes: 24 additions & 0 deletions packages/restapi/tests/lib/channel/search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { search } from '../../../src/lib/channels/search';
import { ENV } from '../../../src/lib/constants';
import { expect } from 'chai';

describe('PUSH_CHANNELS.search', () => {
it('Should fetch channels based on the filter', async () => {
const res = await search({
filter: 80002,
env: ENV.DEV,
query: "Node"
});
console.log(res);
});

it.only('Should fetch channels based on the filter in new format', async () => {
const res = await search({
filter: 80002,
env: ENV.DEV,
query: "Node",
oldFormat: false
});
console.log(res);
});
});
Loading

0 comments on commit 665a96d

Please sign in to comment.