Skip to content

Commit

Permalink
feat: campaign missing pieces (#1028)
Browse files Browse the repository at this point in the history
it has some breaking changes but this API had been marked as to be changed.

* feat: campaign missing pieces
* chore: more tweaks
* chore: new changes
* chore: enrich responses
* chore: alias other options
  • Loading branch information
ferhatelmas committed Aug 25, 2022
1 parent d74dadf commit edbcbe2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 45 deletions.
84 changes: 52 additions & 32 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ import {
BlockListResponse,
Campaign,
CampaignData,
CampaignFilters,
CampaignQueryOptions,
ChannelAPIResponse,
ChannelData,
ChannelFilters,
ChannelMute,
ChannelOptions,
ChannelResponse,
ChannelSort,
ChannelStateOptions,
CheckPushResponse,
Expand Down Expand Up @@ -115,6 +118,9 @@ import {
PushProviderListResponse,
PushProviderUpsertResponse,
ReactionResponse,
Recipient,
RecipientFilters,
RecipientQueryOptions,
ReservedMessageFields,
ReviewFlagReportOptions,
ReviewFlagReportResponse,
Expand All @@ -124,6 +130,8 @@ import {
SearchPayload,
Segment,
SegmentData,
SegmentFilters,
SegmentQueryOptions,
SendFileAPIResponse,
StreamChatOptions,
SyncOptions,
Expand Down Expand Up @@ -2683,26 +2691,20 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
}

/**
* getSegment - Get a Campaign Segment
*
* @param {string} id Segment ID
*
* @return {Segment} A Segment
*/
async getSegment(id: string) {
const { segment } = await this.get<{ segment: Segment }>(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,
},
});
}

/**
Expand Down Expand Up @@ -2742,26 +2744,23 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
}

/**
* getCampaign - Get a Campaign
*
* @param {string} id Campaign ID
*
* @return {Campaign} A Campaign
*/
async getCampaign(id: string) {
const { campaign } = await this.get<{ campaign: Campaign }>(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<string, Segment>;
channels?: Record<string, ChannelResponse<StreamChatGenerics>>;
users?: Record<string, UserResponse<StreamChatGenerics>>;
}>(this.baseURL + `/campaigns`, {
payload: {
filter_conditions: filters,
...options,
},
});
}

/**
Expand Down Expand Up @@ -2843,6 +2842,27 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
return await this.post<APIResponse & TestCampaignResponse>(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<string, Campaign>;
recipients: Recipient[];
segments: Record<string, Segment>;
channels?: Record<string, ChannelResponse<StreamChatGenerics>>;
users?: Record<string, UserResponse<StreamChatGenerics>>;
}>(this.baseURL + `/recipients`, {
payload: {
filter_conditions: filters,
...options,
},
});
}

/**
* enrichURL - Get OpenGraph data of the given link
*
Expand Down
59 changes: 46 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,7 @@ export type EndpointName =
| 'GetRateLimits'
| 'CreateSegment'
| 'GetSegment'
| 'ListSegments'
| 'QuerySegments'
| 'UpdateSegment'
| 'DeleteSegment'
| 'CreateCampaign'
Expand Down Expand Up @@ -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<string, string>;
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;
Expand All @@ -2329,6 +2351,17 @@ export type TestCampaignResponse = {
invalid_users?: Record<string, string>;
};

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;
Expand Down

0 comments on commit edbcbe2

Please sign in to comment.