-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from peoray/campaign-endpoints
- Loading branch information
Showing
7 changed files
with
323 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { Campaign } from './campaign' | ||
import { | ||
IFetchCampaignHistoryResponse, | ||
IFetchCampaignsResponse, | ||
ISendCampaign, | ||
ISendCampaignResponse, | ||
} from '../../../types' | ||
|
||
const campaignInstance = new Campaign('api-key') | ||
|
||
const fetchCampaignsResponseData: IFetchCampaignsResponse = { | ||
data: [ | ||
{ | ||
campaign_id: 'C60e30b2c806da', | ||
phone_book: 'test', | ||
sender: 'Termii', | ||
camp_type: 'scheduled', | ||
channel: '-', | ||
total_recipients: 0, | ||
run_at: '2022-07-05 00:00:00', | ||
status: 'Scheduled', | ||
created_at: '2021-07-05T13:37:48.000000Z', | ||
}, | ||
], | ||
links: { | ||
first: 'https://api.ng.termii.com/api/sms/campaigns?page=1', | ||
last: 'https://api.ng.termii.com/api/sms/campaigns?page=1', | ||
prev: null, | ||
next: null, | ||
}, | ||
meta: { | ||
current_page: 1, | ||
from: 1, | ||
last_page: 1, | ||
path: 'https://api.ng.termii.com/api/sms/campaigns', | ||
per_page: 15, | ||
to: 11, | ||
total: 11, | ||
}, | ||
} | ||
|
||
const fetchCampaignHistoryResponseData: IFetchCampaignHistoryResponse = { | ||
data: [ | ||
{ | ||
id: 64, | ||
sender: 'Termii', | ||
receiver: '2347089509657', | ||
message: 'Hi This is from Termii Campaign', | ||
message_abbreviation: 'Hi This is from Termii Campaign', | ||
amount: 1, | ||
channel: 'Generic', | ||
sms_type: 'plain', | ||
message_id: | ||
'0011551727393226622357573897694282599004522965786793671662369098835884420487860904021011663', | ||
status: 'Sent', | ||
date_created: '2020-01-26 07:24:29', | ||
last_updated: '2020-01-26 07:24:29', | ||
}, | ||
], | ||
links: { | ||
first: 'https://api.ng.termii.com/api/sms/campaigns/C5dbae7faa2b65?page=1', | ||
last: 'https://api.ng.termii.com/api/sms/campaigns/C5dbae7faa2b65?page=1', | ||
prev: null, | ||
next: null, | ||
}, | ||
meta: { | ||
current_page: 1, | ||
from: 1, | ||
last_page: 1, | ||
path: 'https://api.ng.termii.com/api/sms/campaigns/C5dbae7faa2b65', | ||
per_page: 15, | ||
to: 1, | ||
total: 1, | ||
}, | ||
} | ||
|
||
const payload: ISendCampaign = { | ||
country_code: '234', | ||
sender_id: 'Termii', | ||
message: 'Welcome to Termii.', | ||
channel: 'generic', | ||
message_type: 'Plain', | ||
phonebook_id: '2d9f4a02-85b8-4d84-9f5b-30f93ef472e2', | ||
delimiter: ',', | ||
remove_duplicate: 'yes', | ||
campaign_type: 'personalized', | ||
schedule_time: '30-06-2024 6:00', | ||
schedule_sms_status: 'scheduled', | ||
} | ||
|
||
const sendCampaignResponse: ISendCampaignResponse = { | ||
message: 'Your campaign has been scheduled', | ||
} | ||
|
||
describe('Contact', () => { | ||
it('should fetch list of campaigns', async () => { | ||
try { | ||
const response = await campaignInstance.fetchCampaigns() | ||
|
||
expect(response).toEqual(fetchCampaignsResponseData) | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(Error) | ||
} | ||
}) | ||
|
||
it('should fetch list of campaign history', async () => { | ||
try { | ||
const response = await campaignInstance.fetchCampaignHistory('12345678') | ||
|
||
expect(response).toEqual(fetchCampaignHistoryResponseData) | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(Error) | ||
} | ||
}) | ||
|
||
it('should send a campaign', async () => { | ||
try { | ||
const response = await campaignInstance.sendCampaign(payload) | ||
|
||
expect(response).toEqual(sendCampaignResponse) | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(Error) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { TermiiCore } from '../../../api' | ||
import { | ||
IFetchCampaignsResponse, | ||
IFetchCampaignHistoryResponse, | ||
ISendCampaign, | ||
ISendCampaignResponse, | ||
} from '../../../types' | ||
import { IAxiosStruct, handleErrors } from '../../../utils' | ||
|
||
export class Campaign extends TermiiCore { | ||
constructor(apiKey: string) { | ||
super(apiKey) | ||
} | ||
|
||
public async fetchCampaigns(page?: number): Promise<IFetchCampaignsResponse> { | ||
try { | ||
const requestObj: IAxiosStruct = { | ||
method: 'GET', | ||
url: `sms/campaigns`, | ||
page, | ||
} | ||
|
||
const response = await this.useRequest(requestObj) | ||
|
||
return response?.data as IFetchCampaignsResponse | ||
} catch (error) { | ||
return handleErrors(error) | ||
} | ||
} | ||
|
||
public async fetchCampaignHistory( | ||
campaign_id: string, | ||
page?: number | ||
): Promise<IFetchCampaignHistoryResponse> { | ||
try { | ||
const requestObj: IAxiosStruct = { | ||
method: 'GET', | ||
url: `sms/campaigns/${campaign_id}`, | ||
page, | ||
} | ||
|
||
const response = await this.useRequest(requestObj) | ||
|
||
return response?.data as IFetchCampaignHistoryResponse | ||
} catch (error) { | ||
console.error(error) | ||
return handleErrors(error) | ||
} | ||
} | ||
|
||
public async sendCampaign( | ||
data: ISendCampaign | ||
): Promise<ISendCampaignResponse> { | ||
try { | ||
const requestObj: IAxiosStruct = { | ||
method: 'POST', | ||
url: `/sms/campaigns/send`, | ||
data, | ||
} | ||
|
||
const response = await this.useRequest(requestObj) | ||
|
||
return response?.data as ISendCampaignResponse | ||
} catch (error) { | ||
return handleErrors(error) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { BaseMetaResponse, MessageResponse } from './constants' | ||
|
||
interface Campaign { | ||
campaign_id: string | ||
phone_book: string | ||
sender: string | ||
camp_type: string | ||
channel: string | ||
total_recipients: number | ||
run_at: string | ||
status: string | ||
created_at: string | ||
} | ||
|
||
interface CampaignHistory { | ||
id: number | ||
sender: string | ||
receiver: string | ||
message: string | ||
message_abbreviation: string | ||
amount: number | ||
channel: string | ||
sms_type: string | ||
message_id: string | ||
status: string | ||
date_created: string | ||
last_updated: string | ||
} | ||
|
||
export interface IFetchCampaignsResponse extends BaseMetaResponse<Campaign> {} | ||
export interface IFetchCampaignHistoryResponse | ||
extends BaseMetaResponse<CampaignHistory> {} | ||
|
||
export interface ISendCampaign { | ||
country_code: string | ||
sender_id: string | ||
message: string | ||
channel: string | ||
message_type: string | ||
phonebook_id: string | ||
delimiter: string | ||
remove_duplicate: string | ||
campaign_type: string | ||
schedule_time: string | ||
schedule_sms_status: string | ||
} | ||
|
||
export interface ISendCampaignResponse extends MessageResponse {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters