@unchainedshop/plugins
Version:
Official plugin collection for the Unchained Engine with payment, delivery, and pricing adapters
92 lines (91 loc) • 3.56 kB
JavaScript
import { WorkerAdapter } from '@unchainedshop/core';
const { BULKGATE_APPLICATION_ID, BULKGATE_APPLICATION_TOKEN } = process.env;
export const BulkGateWorker = {
...WorkerAdapter,
key: 'shop.unchained.worker-plugin.bulkgate',
label: 'Send SMS through BulkGate',
version: '1.0.0',
type: 'BULKGATE',
doWork: async ({ from, to, text, unicode = false, country, schedule, promotional = false, ...params }) => {
try {
const url = promotional
? 'https://portal.bulkgate.com/api/1.0/simple/promotional'
: 'https://portal.bulkgate.com/api/1.0/simple/transactional';
const requestBody = {
application_id: BULKGATE_APPLICATION_ID,
application_token: BULKGATE_APPLICATION_TOKEN,
number: to,
text: text || '',
unicode: unicode,
sender_id: from ? 'gText' : 'gSystem',
sender_id_value: from || '',
...(country && { country }),
...(schedule && { schedule }),
...params,
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
},
body: JSON.stringify(requestBody),
});
const responseData = (await response.json());
if (responseData.error || responseData.type) {
return {
success: false,
error: {
name: 'BULKGATE_ERROR',
message: responseData.error ||
`Failed to send ${promotional ? 'promotional' : 'transactional'} SMS`,
type: responseData.type || null,
code: responseData.code || response.status,
detail: responseData.detail || null,
},
};
}
if (responseData.data) {
if (!promotional && responseData.data.status === 'accepted') {
return {
success: true,
result: {
sms_id: responseData.data.sms_id,
price: responseData.data.price,
credit: responseData.data.credit,
number: responseData.data.number,
status: responseData.data.status,
},
error: null,
};
}
if (promotional || responseData.data.status) {
return {
success: true,
result: responseData.data,
error: null,
};
}
}
return {
success: false,
error: {
name: 'BULKGATE_ERROR',
message: 'Unexpected response format from BulkGate API',
response: responseData,
},
};
}
catch (err) {
return {
success: false,
error: {
name: err.name,
message: err.message,
stack: err.stack,
},
};
}
},
};
export default BulkGateWorker;