oak-general-business/es/utils/message/sms.js

41 lines
1.3 KiB
JavaScript

import assert from 'assert';
import { ConverterDict } from './index';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
export const smsHandler = async ({ message, applications, system, messageTypeTemplates, context }) => {
const notificationDatas = [];
const smsNotification = await tryMakeSmsNotification(message, context);
if (smsNotification) {
notificationDatas.push(smsNotification);
}
return notificationDatas;
};
export async function tryMakeSmsNotification(message, context) {
const { userId, type, entity, entityId, router } = message;
assert(userId);
const [mobile] = await context.select('mobile', {
data: {
id: 1,
mobile: 1,
},
filter: {
userId,
},
indexFrom: 0,
count: 1,
}, { dontCollect: true });
if (mobile) {
const converter = ConverterDict[type] && ConverterDict[type].toSms;
if (converter) {
const dispersedData = await converter(message, context);
if (dispersedData) {
return {
id: await generateNewIdAsync(),
data: dispersedData,
channel: 'sms',
data1: mobile,
};
}
}
}
}