Skip to content

Commit bcb8ad7

Browse files
feat: campaigns api (#1225)
1 parent 29bfb9b commit bcb8ad7

File tree

5 files changed

+345
-101
lines changed

5 files changed

+345
-101
lines changed

src/campaign.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { StreamChat } from './client';
2+
import { APIResponse, CampaignData, DefaultGenerics, ExtendableGenerics } from './types';
3+
4+
export class Campaign<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> {
5+
id: string | null;
6+
data?: CampaignData;
7+
client: StreamChat<StreamChatGenerics>;
8+
9+
constructor(client: StreamChat<StreamChatGenerics>, id: string | null, data?: CampaignData) {
10+
this.client = client;
11+
this.id = id;
12+
this.data = data;
13+
}
14+
15+
async create() {
16+
const body = {
17+
id: this.id,
18+
message_template: this.data?.message_template,
19+
segment_ids: this.data?.segment_ids,
20+
sender_id: this.data?.sender_id,
21+
channel_template: this.data?.channel_template,
22+
create_channels: this.data?.create_channels,
23+
description: this.data?.description,
24+
name: this.data?.name,
25+
user_ids: this.data?.user_ids,
26+
};
27+
28+
const result = await this.client.createCampaign(body);
29+
30+
this.id = result.campaign.id;
31+
this.data = result.campaign;
32+
return result;
33+
}
34+
35+
verifyCampaignId() {
36+
if (!this.id) {
37+
throw new Error(
38+
'Campaign id is missing. Either create the campaign using campaign.create() or set the id during instantiation - const campaign = client.campaign(id)',
39+
);
40+
}
41+
}
42+
43+
async start(scheduledFor?: string) {
44+
this.verifyCampaignId();
45+
46+
return await this.client.startCampaign(this.id as string, scheduledFor);
47+
}
48+
49+
async update(data: Partial<CampaignData>) {
50+
this.verifyCampaignId();
51+
52+
return this.client.updateCampaign(this.id as string, data);
53+
}
54+
55+
async delete() {
56+
this.verifyCampaignId();
57+
58+
return await this.client.delete<APIResponse>(this.client.baseURL + `/campaigns/${this.id}`);
59+
}
60+
61+
async schedule(params: { scheduledFor: number }) {
62+
this.verifyCampaignId();
63+
64+
const { scheduledFor } = params;
65+
const { campaign } = await this.client.patch<{ campaign: Campaign }>(
66+
this.client.baseURL + `/campaigns/${this.id}/schedule`,
67+
{
68+
scheduled_for: scheduledFor,
69+
},
70+
);
71+
return campaign;
72+
}
73+
74+
async stop() {
75+
this.verifyCampaignId();
76+
77+
return this.client.patch<{ campaign: Campaign }>(this.client.baseURL + `/campaigns/${this.id}/stop`);
78+
}
79+
80+
async pause() {
81+
this.verifyCampaignId();
82+
83+
return this.client.patch<{ campaign: Campaign }>(this.client.baseURL + `/campaigns/${this.id}/pause`);
84+
}
85+
86+
async resume() {
87+
this.verifyCampaignId();
88+
89+
return this.client.patch<{ campaign: Campaign }>(this.client.baseURL + `/campaigns/${this.id}/resume`);
90+
}
91+
92+
async get() {
93+
this.verifyCampaignId();
94+
95+
return this.client.getCampaign(this.id as string);
96+
}
97+
}

0 commit comments

Comments
 (0)