diff --git a/src/client.ts b/src/client.ts index a3aa87cab..6d9a517bb 100644 --- a/src/client.ts +++ b/src/client.ts @@ -39,11 +39,14 @@ import { BlockListResponse, Campaign, CampaignData, + CampaignFilters, + CampaignQueryOptions, ChannelAPIResponse, ChannelData, ChannelFilters, ChannelMute, ChannelOptions, + ChannelResponse, ChannelSort, ChannelStateOptions, CheckPushResponse, @@ -115,6 +118,9 @@ import { PushProviderListResponse, PushProviderUpsertResponse, ReactionResponse, + Recipient, + RecipientFilters, + RecipientQueryOptions, ReservedMessageFields, ReviewFlagReportOptions, ReviewFlagReportResponse, @@ -124,6 +130,8 @@ import { SearchPayload, Segment, SegmentData, + SegmentFilters, + SegmentQueryOptions, SendFileAPIResponse, StreamChatOptions, SyncOptions, @@ -2683,26 +2691,20 @@ export class StreamChat(this.baseURL + `/segments/${id}`); - return segment; - } - - /** - * listSegments - List Campaign Segments + * querySegments - Query Campaign Segments * * * @return {Segment[]} Segments */ - async listSegments(options: { limit?: number; offset?: number }) { - const { segments } = await this.get<{ segments: Segment[] }>(this.baseURL + `/segments`, options); - return segments; + async querySegments(filters: SegmentFilters, options: SegmentQueryOptions = {}) { + return await this.get<{ + segments: Segment[]; + }>(this.baseURL + `/segments`, { + payload: { + filter_conditions: filters, + ...options, + }, + }); } /** @@ -2742,26 +2744,23 @@ export class StreamChat(this.baseURL + `/campaigns/${id}`); - return campaign; - } - - /** - * listCampaigns - List Campaigns + * queryCampaigns - Query Campaigns * * * @return {Campaign[]} Campaigns */ - async listCampaigns(options: { limit?: number; offset?: number }) { - const { campaigns } = await this.get<{ campaigns: Campaign[] }>(this.baseURL + `/campaigns`, options); - return campaigns; + async queryCampaigns(filters: CampaignFilters, options: CampaignQueryOptions = {}) { + return await this.get<{ + campaigns: Campaign[]; + segments: Record; + channels?: Record>; + users?: Record>; + }>(this.baseURL + `/campaigns`, { + payload: { + filter_conditions: filters, + ...options, + }, + }); } /** @@ -2843,6 +2842,27 @@ export class StreamChat(this.baseURL + `/campaigns/${id}/test`, { users }); } + /** + * queryRecipients - Query Campaign Recipient Results + * + * + * @return {Recipient[]} Recipients + */ + async queryRecipients(filters: RecipientFilters, options: RecipientQueryOptions = {}) { + return await this.get<{ + campaigns: Record; + recipients: Recipient[]; + segments: Record; + channels?: Record>; + users?: Record>; + }>(this.baseURL + `/recipients`, { + payload: { + filter_conditions: filters, + ...options, + }, + }); + } + /** * enrichURL - Get OpenGraph data of the given link * diff --git a/src/types.ts b/src/types.ts index 687458d63..56c02831d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1896,7 +1896,7 @@ export type EndpointName = | 'GetRateLimits' | 'CreateSegment' | 'GetSegment' - | 'ListSegments' + | 'QuerySegments' | 'UpdateSegment' | 'DeleteSegment' | 'CreateCampaign' @@ -2278,46 +2278,68 @@ export type DeleteUserOptions = { export type SegmentData = { description: string; - // TODO: define this type in more detail - filter: { - channel?: object; - user?: object; - }; + filter: {}; name: string; + type: 'channel' | 'user'; }; export type Segment = { - app_pk: number; created_at: string; id: string; + in_use: boolean; + size: number; + status: 'computing' | 'ready'; updated_at: string; - recipients?: number; } & SegmentData; +export type CampaignSortField = { + field: string; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + value: any; +}; + +export type CampaignSort = { + fields: CampaignSortField[]; + direction?: 'asc' | 'desc'; +}; + +export type CampaignQueryOptions = { + limit?: number; + sort?: CampaignSort; +}; + +export type SegmentQueryOptions = CampaignQueryOptions; +export type RecipientQueryOptions = CampaignQueryOptions; + +// TODO: add better typing +export type SegmentFilters = {}; +export type CampaignFilters = {}; +export type RecipientFilters = {}; + export type CampaignData = { attachments: Attachment[]; + channel_type: string; defaults: Record; name: string; segment_id: string; text: string; description?: string; - push_notifications?: boolean; sender_id?: string; }; export type CampaignStatus = { - errors: string[]; - status: 'draft' | 'stopped' | 'scheduled' | 'completed' | 'failed' | 'canceled' | 'in_progress'; + status: 'draft' | 'stopped' | 'scheduled' | 'completed' | 'failed' | 'in_progress'; completed_at?: string; + errored_messages?: number; failed_at?: string; - progress?: number; resumed_at?: string; scheduled_at?: string; + scheduled_for?: string; + sent_messages?: number; stopped_at?: string; }; export type Campaign = { - app_pk: string; created_at: string; id: string; updated_at: string; @@ -2329,6 +2351,17 @@ export type TestCampaignResponse = { invalid_users?: Record; }; +export type Recipient = { + campaign_id: string; + channel_cid: string; + created_at: string; + status: 'pending' | 'sent' | 'failed'; + updated_at: string; + details?: string; + message_id?: string; + receiver_id?: string; +}; + export type TaskStatus = { created_at: string; status: string;