oak-general-business/lib/utils/message/wechatPublic.js

80 lines
2.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.wechatPublicHandler = void 0;
const uuid_1 = require("oak-domain/lib/utils/uuid");
const index_1 = require("./index");
const wechatPublicHandler = async ({ message, applications, system, context }) => {
const { userId, type } = message;
const notificationDatas = [];
const apps = applications.filter(ele => ele.type === 'wechatPublic');
const [user] = await context.select('user', {
data: {
id: 1,
refId: 1,
},
filter: {
id: userId
}
}, { dontCollect: true });
const userIds = user.refId ? [userId, user.refId] : [userId];
const wechatUsers = await context.select('wechatUser', {
data: {
id: 1,
applicationId: 1,
openId: 1,
},
filter: {
applicationId: {
$in: apps.map((ele) => ele.id),
},
userId: {
$in: userIds
},
},
}, { dontCollect: true });
for (const app of apps) {
// 如果是wechatMp或者wechat还要保证用户已经有openId
const wechatUser = wechatUsers.find(ele => ele.applicationId === app.id);
const messageTypeTemplates = await context.select('messageTypeTemplate', {
data: {
id: 1,
templateId: 1,
template: {
id: 1,
wechatId: 1,
applicationId: 1,
},
type: 1,
},
filter: {
type,
template: {
applicationId: app.id,
},
},
}, { dontCollect: true });
const messageTypeTemplate = messageTypeTemplates.find(ele => ele.template.applicationId === app.id && ele.type === type);
if (messageTypeTemplate && wechatUser) {
const converter = index_1.ConverterDict[type] && index_1.ConverterDict[type].wechatPublic;
const disperseResult = converter &&
(await converter(message, apps, app, context));
if (disperseResult) {
const { data, wechatMpAppId } = disperseResult;
notificationDatas.push({
id: await (0, uuid_1.generateNewIdAsync)(),
data,
channel: 'wechatPublic',
applicationId: app.id,
templateId: messageTypeTemplate.template.wechatId,
data1: {
openId: wechatUser.openId,
wechatMpAppId,
}
});
}
}
}
return notificationDatas;
};
exports.wechatPublicHandler = wechatPublicHandler;