@elusion-sdk/briq
Version:
A modern TypeScript SDK for Briq SMS API integration
128 lines • 4.9 kB
JavaScript
import { ENDPOINTS } from "../utils/constants";
import { validateCampaignName, validateISODate, validatePaginationParams, validateUUID, } from "../utils/validators";
import { NotFoundError, ValidationError } from "../utils/errors";
import { BaseService } from "./BaseService";
export class CampaignService extends BaseService {
async create(request) {
this.validateRequired(request, ["name", "workspace_id", "launch_date"]);
validateCampaignName(request.name);
validateUUID(request.workspace_id, "Workspace ID");
if (request.description && request.description.length > 500) {
throw new ValidationError("Description too long. Maximum 500 characters allowed");
}
const sanitizedRequest = {
...this.sanitizeInput(request),
settings: {
sendRate: 60,
retryFailures: true,
maxRetries: 3,
stopOnFailure: false,
trackClicks: false,
trackReplies: false,
},
};
try {
return await this.client.post(ENDPOINTS.CAMPAIGNS.CREATE, sanitizedRequest);
}
catch (error) {
throw new Error(`Failed to create campaign: ${this.formatError(error)}`);
}
}
async list(params = {}) {
if (Object.keys(params).length > 0) {
validatePaginationParams(params);
}
if (params.workspace_id) {
validateUUID(params.workspace_id, "Workspace ID");
}
if (params.search) {
params.search = params.search.trim();
if (params.search.length > 100) {
throw new ValidationError("Search term too long. Maximum 100 characters allowed");
}
}
try {
const response = await this.client.get(ENDPOINTS.CAMPAIGNS.GET_ALL, { params });
if (response.data && Array.isArray(response.data)) {
return {
success: true,
data: response.data,
pagination: {
page: params.page || 1,
limit: params.limit || 20,
total: response.data.length,
totalPages: Math.ceil(response.data.length / (params.limit || 20)),
hasNext: false,
hasPrev: (params.page || 1) > 1,
},
};
}
return response;
}
catch (error) {
throw new Error(`Failed to list campaigns: ${this.formatError(error)}`);
}
}
async getById(campaignId) {
validateUUID(campaignId, "Campaign ID");
try {
return await this.client.get(ENDPOINTS.CAMPAIGNS.GET_BY_ID(campaignId));
}
catch (error) {
if (error.statusCode === 404) {
throw new NotFoundError("Campaign", campaignId);
}
throw new Error(`Failed to get campaign: ${this.formatError(error)}`);
}
}
async update(campaignId, request) {
validateUUID(campaignId, "Campaign ID");
if (Object.keys(request).length === 0) {
throw new ValidationError("Update request cannot be empty");
}
if (request.name !== undefined) {
validateCampaignName(request.name);
}
if (request.launch_date !== undefined) {
validateISODate(request.launch_date, "Schedule date");
}
if (request.description && request.description.length > 500) {
throw new ValidationError("Description too long. Maximum 500 characters allowed");
}
const sanitizedRequest = this.sanitizeInput(request);
try {
return await this.client.patch(ENDPOINTS.CAMPAIGNS.UPDATE(campaignId), sanitizedRequest);
}
catch (error) {
if (error.statusCode === 404) {
throw new NotFoundError("Campaign", campaignId);
}
throw new Error(`Failed to update campaign: ${this.formatError(error)}`);
}
}
async delete(campaignId) {
validateUUID(campaignId, "Campaign ID");
try {
return await this.client.delete(`campaign/${campaignId}`);
}
catch (error) {
if (error.statusCode === 404) {
throw new NotFoundError("Campaign", campaignId);
}
throw new Error(`Failed to delete campaign: ${this.formatError(error)}`);
}
}
async exists(campaignId) {
try {
await this.getById(campaignId);
return true;
}
catch (error) {
if (error instanceof NotFoundError) {
return false;
}
throw error;
}
}
}
//# sourceMappingURL=CampaignService.js.map