Skip to content

Commit

Permalink
feat: campaigns api (#1225)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalnarkhede committed Feb 22, 2024
1 parent 29bfb9b commit bcb8ad7
Show file tree
Hide file tree
Showing 5 changed files with 345 additions and 101 deletions.
97 changes: 97 additions & 0 deletions src/campaign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { StreamChat } from './client';
import { APIResponse, CampaignData, DefaultGenerics, ExtendableGenerics } from './types';

export class Campaign<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> {
id: string | null;
data?: CampaignData;
client: StreamChat<StreamChatGenerics>;

constructor(client: StreamChat<StreamChatGenerics>, id: string | null, data?: CampaignData) {
this.client = client;
this.id = id;
this.data = data;
}

async create() {
const body = {
id: this.id,
message_template: this.data?.message_template,
segment_ids: this.data?.segment_ids,
sender_id: this.data?.sender_id,
channel_template: this.data?.channel_template,
create_channels: this.data?.create_channels,
description: this.data?.description,
name: this.data?.name,
user_ids: this.data?.user_ids,
};

const result = await this.client.createCampaign(body);

this.id = result.campaign.id;
this.data = result.campaign;
return result;
}

verifyCampaignId() {
if (!this.id) {
throw new Error(
'Campaign id is missing. Either create the campaign using campaign.create() or set the id during instantiation - const campaign = client.campaign(id)',
);
}
}

async start(scheduledFor?: string) {
this.verifyCampaignId();

return await this.client.startCampaign(this.id as string, scheduledFor);
}

async update(data: Partial<CampaignData>) {
this.verifyCampaignId();

return this.client.updateCampaign(this.id as string, data);
}

async delete() {
this.verifyCampaignId();

return await this.client.delete<APIResponse>(this.client.baseURL + `/campaigns/${this.id}`);
}

async schedule(params: { scheduledFor: number }) {
this.verifyCampaignId();

const { scheduledFor } = params;
const { campaign } = await this.client.patch<{ campaign: Campaign }>(
this.client.baseURL + `/campaigns/${this.id}/schedule`,
{
scheduled_for: scheduledFor,
},
);
return campaign;
}

async stop() {
this.verifyCampaignId();

return this.client.patch<{ campaign: Campaign }>(this.client.baseURL + `/campaigns/${this.id}/stop`);
}

async pause() {
this.verifyCampaignId();

return this.client.patch<{ campaign: Campaign }>(this.client.baseURL + `/campaigns/${this.id}/pause`);
}

async resume() {
this.verifyCampaignId();

return this.client.patch<{ campaign: Campaign }>(this.client.baseURL + `/campaigns/${this.id}/resume`);
}

async get() {
this.verifyCampaignId();

return this.client.getCampaign(this.id as string);
}
}

0 comments on commit bcb8ad7

Please sign in to comment.