修复了同步微信tag时异步未等待导致的致命问题
This commit is contained in:
parent
b60cb44f15
commit
8989a5a61e
|
|
@ -1,5 +1,5 @@
|
|||
import { EntityDict } from '../oak-app-domain';
|
||||
import { BRC } from '../types/RuntimeCxt';
|
||||
import { EntityDict } from "../oak-app-domain";
|
||||
import { BRC } from "../types/RuntimeCxt";
|
||||
export declare function createTag<ED extends EntityDict>(params: {
|
||||
applicationId: string;
|
||||
name: string;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.oneKeySync = exports.syncTag = exports.deleteTag = exports.editTag = exports.getTags = exports.createTag = void 0;
|
||||
exports.createTag = createTag;
|
||||
exports.getTags = getTags;
|
||||
exports.editTag = editTag;
|
||||
exports.deleteTag = deleteTag;
|
||||
exports.syncTag = syncTag;
|
||||
exports.oneKeySync = oneKeySync;
|
||||
const tslib_1 = require("tslib");
|
||||
const WechatSDK_1 = tslib_1.__importDefault(require("oak-external-sdk/lib/WechatSDK"));
|
||||
const assert_1 = require("oak-domain/lib/utils/assert");
|
||||
|
|
@ -8,7 +13,7 @@ const types_1 = require("oak-domain/lib/types");
|
|||
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
||||
const wechatPublicException_1 = require("../utils/wechatPublicException");
|
||||
async function getWechatPublicConfig(applicationId, context) {
|
||||
const [application] = await context.select('application', {
|
||||
const [application] = await context.select("application", {
|
||||
data: {
|
||||
id: 1,
|
||||
config: 1,
|
||||
|
|
@ -18,12 +23,12 @@ async function getWechatPublicConfig(applicationId, context) {
|
|||
id: applicationId,
|
||||
},
|
||||
}, {
|
||||
dontCollect: true
|
||||
dontCollect: true,
|
||||
});
|
||||
return application;
|
||||
}
|
||||
async function getWechatPublicTags(applicationId, context) {
|
||||
const wechatPublicTags = await context.select('wechatPublicTag', {
|
||||
const wechatPublicTags = await context.select("wechatPublicTag", {
|
||||
data: {
|
||||
id: 1,
|
||||
wechatId: 1,
|
||||
|
|
@ -33,12 +38,12 @@ async function getWechatPublicTags(applicationId, context) {
|
|||
applicationId,
|
||||
},
|
||||
}, {
|
||||
dontCollect: true
|
||||
dontCollect: true,
|
||||
});
|
||||
return wechatPublicTags;
|
||||
}
|
||||
async function getWechatPublicTagById(applicationId, id, context) {
|
||||
const [wechatPublicTag] = await context.select('wechatPublicTag', {
|
||||
const [wechatPublicTag] = await context.select("wechatPublicTag", {
|
||||
data: {
|
||||
id: 1,
|
||||
wechatId: 1,
|
||||
|
|
@ -50,7 +55,7 @@ async function getWechatPublicTagById(applicationId, id, context) {
|
|||
applicationId,
|
||||
},
|
||||
}, {
|
||||
dontCollect: true
|
||||
dontCollect: true,
|
||||
});
|
||||
return wechatPublicTag;
|
||||
}
|
||||
|
|
@ -58,7 +63,7 @@ async function createTag(params, context) {
|
|||
const application = await getWechatPublicConfig(params.applicationId, context);
|
||||
(0, assert_1.assert)(application);
|
||||
const { type, config, systemId } = application;
|
||||
(0, assert_1.assert)(type === 'wechatPublic');
|
||||
(0, assert_1.assert)(type === "wechatPublic");
|
||||
let appId, appSecret;
|
||||
const config2 = config;
|
||||
appId = config2.appId;
|
||||
|
|
@ -67,12 +72,11 @@ async function createTag(params, context) {
|
|||
const result = await wechatInstance.createTag({ name: params.name });
|
||||
return result;
|
||||
}
|
||||
exports.createTag = createTag;
|
||||
async function getTags(params, context) {
|
||||
const application = await getWechatPublicConfig(params.applicationId, context);
|
||||
(0, assert_1.assert)(application);
|
||||
const { type, config, systemId } = application;
|
||||
(0, assert_1.assert)(type === 'wechatPublic');
|
||||
(0, assert_1.assert)(type === "wechatPublic");
|
||||
let appId, appSecret;
|
||||
const config2 = config;
|
||||
appId = config2.appId;
|
||||
|
|
@ -81,26 +85,27 @@ async function getTags(params, context) {
|
|||
const result = await wechatInstance.getTags();
|
||||
return result;
|
||||
}
|
||||
exports.getTags = getTags;
|
||||
async function editTag(params, context) {
|
||||
const application = await getWechatPublicConfig(params.applicationId, context);
|
||||
(0, assert_1.assert)(application);
|
||||
const { type, config, systemId } = application;
|
||||
(0, assert_1.assert)(type === 'wechatPublic');
|
||||
(0, assert_1.assert)(type === "wechatPublic");
|
||||
let appId, appSecret;
|
||||
const config2 = config;
|
||||
appId = config2.appId;
|
||||
appSecret = config2.appSecret;
|
||||
const wechatInstance = WechatSDK_1.default.getInstance(appId, type, appSecret);
|
||||
const result = await wechatInstance.editTag({ id: params.id, name: params.name });
|
||||
const result = await wechatInstance.editTag({
|
||||
id: params.id,
|
||||
name: params.name,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
exports.editTag = editTag;
|
||||
async function deleteTag(params, context) {
|
||||
const application = await getWechatPublicConfig(params.applicationId, context);
|
||||
(0, assert_1.assert)(application);
|
||||
const { type, config, systemId } = application;
|
||||
(0, assert_1.assert)(type === 'wechatPublic');
|
||||
(0, assert_1.assert)(type === "wechatPublic");
|
||||
let appId, appSecret;
|
||||
const config2 = config;
|
||||
appId = config2.appId;
|
||||
|
|
@ -109,107 +114,111 @@ async function deleteTag(params, context) {
|
|||
const result = await wechatInstance.deleteTag({ id: params.wechatId });
|
||||
return result;
|
||||
}
|
||||
exports.deleteTag = deleteTag;
|
||||
async function syncTag(params, context) {
|
||||
const application = await getWechatPublicConfig(params.applicationId, context);
|
||||
(0, assert_1.assert)(application);
|
||||
const { type, config, systemId } = application;
|
||||
(0, assert_1.assert)(type === 'wechatPublic');
|
||||
(0, assert_1.assert)(type === "wechatPublic");
|
||||
const wechatPublicTag = await getWechatPublicTagById(params.applicationId, params.id, context);
|
||||
(0, assert_1.assert)(wechatPublicTag);
|
||||
try {
|
||||
const { tags } = await getTags({
|
||||
applicationId: params.applicationId
|
||||
applicationId: params.applicationId,
|
||||
}, context);
|
||||
if (!wechatPublicTag.wechatId) {
|
||||
const result = await createTag({
|
||||
applicationId: params.applicationId,
|
||||
name: wechatPublicTag.text,
|
||||
}, context);
|
||||
await context.operate('wechatPublicTag', {
|
||||
await context.operate("wechatPublicTag", {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
action: 'update',
|
||||
action: "update",
|
||||
data: {
|
||||
syncAt: Date.now(),
|
||||
sync: true,
|
||||
iState: 'success',
|
||||
wechatId: result.tag.id
|
||||
iState: "success",
|
||||
wechatId: result.tag.id,
|
||||
},
|
||||
filter: {
|
||||
id: params.id,
|
||||
}
|
||||
},
|
||||
}, {});
|
||||
}
|
||||
if (wechatPublicTag.wechatId && tags.find((ele) => ele.id === wechatPublicTag.wechatId && ele.name !== wechatPublicTag.text)) {
|
||||
if (wechatPublicTag.wechatId &&
|
||||
tags.find((ele) => ele.id === wechatPublicTag.wechatId &&
|
||||
ele.name !== wechatPublicTag.text)) {
|
||||
const result = await editTag({
|
||||
applicationId: params.applicationId,
|
||||
id: wechatPublicTag.wechatId,
|
||||
name: wechatPublicTag.text,
|
||||
}, context);
|
||||
await context.operate('wechatPublicTag', {
|
||||
await context.operate("wechatPublicTag", {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
action: 'update',
|
||||
action: "update",
|
||||
data: {
|
||||
syncAt: Date.now(),
|
||||
sync: true,
|
||||
iState: 'success',
|
||||
iState: "success",
|
||||
},
|
||||
filter: {
|
||||
id: params.id,
|
||||
}
|
||||
},
|
||||
}, {});
|
||||
}
|
||||
if (wechatPublicTag.wechatId && tags.find((ele) => ele.id === wechatPublicTag.wechatId && ele.name === wechatPublicTag.text)) {
|
||||
if (wechatPublicTag.wechatId &&
|
||||
tags.find((ele) => ele.id === wechatPublicTag.wechatId &&
|
||||
ele.name === wechatPublicTag.text)) {
|
||||
const result = await deleteTag({
|
||||
applicationId: params.applicationId,
|
||||
id: wechatPublicTag.id,
|
||||
wechatId: wechatPublicTag.wechatId,
|
||||
}, context);
|
||||
await context.operate('wechatPublicTag', {
|
||||
await context.operate("wechatPublicTag", {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
action: 'remove',
|
||||
action: "remove",
|
||||
data: {},
|
||||
filter: {
|
||||
id: params.id,
|
||||
}
|
||||
},
|
||||
}, {});
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
await context.operate('wechatPublicTag', {
|
||||
await context.operate("wechatPublicTag", {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
action: 'update',
|
||||
action: "update",
|
||||
data: {
|
||||
sync: false,
|
||||
iState: 'fail',
|
||||
iState: "fail",
|
||||
},
|
||||
filter: {
|
||||
id: params.id,
|
||||
}
|
||||
},
|
||||
}, {});
|
||||
throw new types_1.OakPreConditionUnsetException(`${(0, wechatPublicException_1.handleException)(e)}`);
|
||||
}
|
||||
}
|
||||
exports.syncTag = syncTag;
|
||||
async function oneKeySync(params, context) {
|
||||
const application = await getWechatPublicConfig(params.applicationId, context);
|
||||
(0, assert_1.assert)(application);
|
||||
const { type, config, systemId } = application;
|
||||
(0, assert_1.assert)(type === 'wechatPublic');
|
||||
(0, assert_1.assert)(type === "wechatPublic");
|
||||
const wechatPublicTags = await getWechatPublicTags(params.applicationId, context);
|
||||
try {
|
||||
const tags = await getTags(params, context);
|
||||
if (wechatPublicTags && wechatPublicTags.length > 0) {
|
||||
const tagsToAdd = tags.tags?.filter((ele) => {
|
||||
return !wechatPublicTags.some(item => item?.wechatId === ele.id);
|
||||
return !wechatPublicTags.some((item) => item?.wechatId === ele.id);
|
||||
});
|
||||
const tagsToRemove = tags.tags?.length ? wechatPublicTags.filter((ele) => {
|
||||
return !tags.tags?.some((item) => item.id === ele?.wechatId);
|
||||
}) : wechatPublicTags;
|
||||
tagsToAdd?.map(async (ele) => {
|
||||
await context.operate('wechatPublicTag', {
|
||||
const tagsToRemove = tags.tags?.length
|
||||
? wechatPublicTags.filter((ele) => {
|
||||
return !tags.tags?.some((item) => item.id === ele?.wechatId);
|
||||
})
|
||||
: wechatPublicTags;
|
||||
for (const ele of tagsToAdd || []) {
|
||||
await context.operate("wechatPublicTag", {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
action: 'create',
|
||||
action: "create",
|
||||
data: {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
applicationId: params.applicationId,
|
||||
|
|
@ -217,40 +226,42 @@ async function oneKeySync(params, context) {
|
|||
text: ele.name,
|
||||
sync: true,
|
||||
syncAt: Date.now(),
|
||||
iState: 'success',
|
||||
iState: "success",
|
||||
},
|
||||
}, {});
|
||||
});
|
||||
tagsToRemove?.map(async (ele) => {
|
||||
await context.operate('wechatPublicTag', {
|
||||
}
|
||||
// Remove tags
|
||||
for (const ele of tagsToRemove || []) {
|
||||
await context.operate("wechatPublicTag", {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
action: 'remove',
|
||||
action: "remove",
|
||||
data: {},
|
||||
filter: {
|
||||
id: ele.id
|
||||
}
|
||||
id: ele.id,
|
||||
},
|
||||
}, {});
|
||||
});
|
||||
wechatPublicTags?.map(async (ele) => {
|
||||
await context.operate('wechatPublicTag', {
|
||||
}
|
||||
// Update tags
|
||||
for (const ele of wechatPublicTags || []) {
|
||||
await context.operate("wechatPublicTag", {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
action: 'update',
|
||||
action: "update",
|
||||
data: {
|
||||
syncAt: Date.now(),
|
||||
sync: true,
|
||||
iState: 'success'
|
||||
iState: "success",
|
||||
},
|
||||
filter: {
|
||||
id: ele.id
|
||||
}
|
||||
id: ele.id,
|
||||
},
|
||||
}, {});
|
||||
});
|
||||
}
|
||||
}
|
||||
else {
|
||||
tags.tags?.map(async (ele) => {
|
||||
await context.operate('wechatPublicTag', {
|
||||
for (const ele of tags.tags || []) {
|
||||
await context.operate("wechatPublicTag", {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
action: 'create',
|
||||
action: "create",
|
||||
data: {
|
||||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||||
applicationId: params.applicationId,
|
||||
|
|
@ -258,14 +269,13 @@ async function oneKeySync(params, context) {
|
|||
text: ele.name,
|
||||
sync: true,
|
||||
syncAt: Date.now(),
|
||||
iState: 'success',
|
||||
iState: "success",
|
||||
},
|
||||
}, {});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
throw new types_1.OakPreConditionUnsetException(`${(0, wechatPublicException_1.handleException)(e)}`);
|
||||
}
|
||||
}
|
||||
exports.oneKeySync = oneKeySync;
|
||||
|
|
|
|||
|
|
@ -1,383 +1,445 @@
|
|||
|
||||
import { EntityDict } from '../oak-app-domain';
|
||||
import { EntityDict } from "../oak-app-domain";
|
||||
import WechatSDK, {
|
||||
WechatPublicInstance,
|
||||
} from 'oak-external-sdk/lib/WechatSDK';
|
||||
import { assert } from 'oak-domain/lib/utils/assert';
|
||||
import {
|
||||
WechatPublicConfig,
|
||||
} from '../oak-app-domain/Application/Schema';
|
||||
import { OakPreConditionUnsetException } from 'oak-domain/lib/types';
|
||||
WechatPublicInstance,
|
||||
} from "oak-external-sdk/lib/WechatSDK";
|
||||
import { assert } from "oak-domain/lib/utils/assert";
|
||||
import { WechatPublicConfig } from "../oak-app-domain/Application/Schema";
|
||||
import { OakPreConditionUnsetException } from "oak-domain/lib/types";
|
||||
import { generateNewIdAsync } from "oak-domain/lib/utils/uuid";
|
||||
import { Tag } from '../types/WeChat';
|
||||
import { handleException } from '../utils/wechatPublicException';
|
||||
import { BRC } from '../types/RuntimeCxt';
|
||||
import { Tag } from "../types/WeChat";
|
||||
import { handleException } from "../utils/wechatPublicException";
|
||||
import { BRC } from "../types/RuntimeCxt";
|
||||
|
||||
async function getWechatPublicConfig<ED extends EntityDict>(
|
||||
applicationId: string,
|
||||
context: BRC<ED>
|
||||
applicationId: string,
|
||||
context: BRC<ED>
|
||||
) {
|
||||
const [application] = await context.select(
|
||||
'application',
|
||||
{
|
||||
data: {
|
||||
id: 1,
|
||||
config: 1,
|
||||
type: 1,
|
||||
},
|
||||
filter: {
|
||||
id: applicationId,
|
||||
},
|
||||
},
|
||||
{
|
||||
dontCollect: true
|
||||
}
|
||||
);
|
||||
return application;
|
||||
const [application] = await context.select(
|
||||
"application",
|
||||
{
|
||||
data: {
|
||||
id: 1,
|
||||
config: 1,
|
||||
type: 1,
|
||||
},
|
||||
filter: {
|
||||
id: applicationId,
|
||||
},
|
||||
},
|
||||
{
|
||||
dontCollect: true,
|
||||
}
|
||||
);
|
||||
return application;
|
||||
}
|
||||
|
||||
async function getWechatPublicTags<ED extends EntityDict>(
|
||||
applicationId: string,
|
||||
context: BRC<ED>
|
||||
applicationId: string,
|
||||
context: BRC<ED>
|
||||
) {
|
||||
const wechatPublicTags = await context.select(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
data: {
|
||||
id: 1,
|
||||
wechatId: 1,
|
||||
applicationId: 1,
|
||||
},
|
||||
filter: {
|
||||
applicationId,
|
||||
},
|
||||
},
|
||||
{
|
||||
dontCollect: true
|
||||
}
|
||||
);
|
||||
return wechatPublicTags;
|
||||
const wechatPublicTags = await context.select(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
data: {
|
||||
id: 1,
|
||||
wechatId: 1,
|
||||
applicationId: 1,
|
||||
},
|
||||
filter: {
|
||||
applicationId,
|
||||
},
|
||||
},
|
||||
{
|
||||
dontCollect: true,
|
||||
}
|
||||
);
|
||||
return wechatPublicTags;
|
||||
}
|
||||
|
||||
async function getWechatPublicTagById<ED extends EntityDict>(
|
||||
applicationId: string,
|
||||
id: string,
|
||||
context: BRC<ED>
|
||||
applicationId: string,
|
||||
id: string,
|
||||
context: BRC<ED>
|
||||
) {
|
||||
const [wechatPublicTag] = await context.select(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
data: {
|
||||
id: 1,
|
||||
wechatId: 1,
|
||||
text: 1,
|
||||
applicationId: 1,
|
||||
},
|
||||
filter: {
|
||||
id,
|
||||
applicationId,
|
||||
},
|
||||
},
|
||||
{
|
||||
dontCollect: true
|
||||
}
|
||||
);
|
||||
return wechatPublicTag;
|
||||
const [wechatPublicTag] = await context.select(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
data: {
|
||||
id: 1,
|
||||
wechatId: 1,
|
||||
text: 1,
|
||||
applicationId: 1,
|
||||
},
|
||||
filter: {
|
||||
id,
|
||||
applicationId,
|
||||
},
|
||||
},
|
||||
{
|
||||
dontCollect: true,
|
||||
}
|
||||
);
|
||||
return wechatPublicTag;
|
||||
}
|
||||
|
||||
export async function createTag<ED extends EntityDict>(
|
||||
params: {
|
||||
applicationId: string,
|
||||
name: string,
|
||||
},
|
||||
context: BRC<ED>,
|
||||
params: {
|
||||
applicationId: string;
|
||||
name: string;
|
||||
},
|
||||
context: BRC<ED>
|
||||
): Promise<any> {
|
||||
const application = await getWechatPublicConfig<ED>(params.applicationId, context);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === 'wechatPublic')
|
||||
let appId: string, appSecret: string;
|
||||
const config2 = config as WechatPublicConfig;
|
||||
appId = config2.appId;
|
||||
appSecret = config2.appSecret;
|
||||
const wechatInstance = WechatSDK.getInstance(
|
||||
appId!,
|
||||
type!,
|
||||
appSecret!
|
||||
) as WechatPublicInstance;
|
||||
const result = await wechatInstance.createTag({ name: params.name });
|
||||
return result;
|
||||
const application = await getWechatPublicConfig<ED>(
|
||||
params.applicationId,
|
||||
context
|
||||
);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === "wechatPublic");
|
||||
let appId: string, appSecret: string;
|
||||
const config2 = config as WechatPublicConfig;
|
||||
appId = config2.appId;
|
||||
appSecret = config2.appSecret;
|
||||
const wechatInstance = WechatSDK.getInstance(
|
||||
appId!,
|
||||
type!,
|
||||
appSecret!
|
||||
) as WechatPublicInstance;
|
||||
const result = await wechatInstance.createTag({ name: params.name });
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getTags<ED extends EntityDict>(
|
||||
params: {
|
||||
applicationId: string
|
||||
},
|
||||
context: BRC<ED>,
|
||||
params: {
|
||||
applicationId: string;
|
||||
},
|
||||
context: BRC<ED>
|
||||
): Promise<any> {
|
||||
const application = await getWechatPublicConfig<ED>(params.applicationId, context);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === 'wechatPublic')
|
||||
let appId: string, appSecret: string;
|
||||
const config2 = config as WechatPublicConfig;
|
||||
appId = config2.appId;
|
||||
appSecret = config2.appSecret;
|
||||
const wechatInstance = WechatSDK.getInstance(
|
||||
appId!,
|
||||
type!,
|
||||
appSecret!
|
||||
) as WechatPublicInstance;
|
||||
const result = await wechatInstance.getTags();
|
||||
return result;
|
||||
const application = await getWechatPublicConfig<ED>(
|
||||
params.applicationId,
|
||||
context
|
||||
);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === "wechatPublic");
|
||||
let appId: string, appSecret: string;
|
||||
const config2 = config as WechatPublicConfig;
|
||||
appId = config2.appId;
|
||||
appSecret = config2.appSecret;
|
||||
const wechatInstance = WechatSDK.getInstance(
|
||||
appId!,
|
||||
type!,
|
||||
appSecret!
|
||||
) as WechatPublicInstance;
|
||||
const result = await wechatInstance.getTags();
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function editTag<ED extends EntityDict>(
|
||||
params: {
|
||||
applicationId: string,
|
||||
id: number,
|
||||
name: string,
|
||||
},
|
||||
context: BRC<ED>,
|
||||
params: {
|
||||
applicationId: string;
|
||||
id: number;
|
||||
name: string;
|
||||
},
|
||||
context: BRC<ED>
|
||||
): Promise<any> {
|
||||
const application = await getWechatPublicConfig<ED>(params.applicationId, context);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === 'wechatPublic');
|
||||
let appId: string, appSecret: string;
|
||||
const config2 = config as WechatPublicConfig;
|
||||
appId = config2.appId;
|
||||
appSecret = config2.appSecret;
|
||||
const wechatInstance = WechatSDK.getInstance(
|
||||
appId!,
|
||||
type!,
|
||||
appSecret!
|
||||
) as WechatPublicInstance;
|
||||
const result = await wechatInstance.editTag({ id: params.id, name: params.name });
|
||||
return result;
|
||||
const application = await getWechatPublicConfig<ED>(
|
||||
params.applicationId,
|
||||
context
|
||||
);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === "wechatPublic");
|
||||
let appId: string, appSecret: string;
|
||||
const config2 = config as WechatPublicConfig;
|
||||
appId = config2.appId;
|
||||
appSecret = config2.appSecret;
|
||||
const wechatInstance = WechatSDK.getInstance(
|
||||
appId!,
|
||||
type!,
|
||||
appSecret!
|
||||
) as WechatPublicInstance;
|
||||
const result = await wechatInstance.editTag({
|
||||
id: params.id,
|
||||
name: params.name,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function deleteTag<ED extends EntityDict>(
|
||||
params: {
|
||||
applicationId: string,
|
||||
id: string,
|
||||
wechatId: number,
|
||||
},
|
||||
context: BRC<ED>,
|
||||
params: {
|
||||
applicationId: string;
|
||||
id: string;
|
||||
wechatId: number;
|
||||
},
|
||||
context: BRC<ED>
|
||||
): Promise<any> {
|
||||
const application = await getWechatPublicConfig<ED>(params.applicationId, context);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === 'wechatPublic');
|
||||
let appId: string, appSecret: string;
|
||||
const config2 = config as WechatPublicConfig;
|
||||
appId = config2.appId;
|
||||
appSecret = config2.appSecret;
|
||||
const wechatInstance = WechatSDK.getInstance(
|
||||
appId!,
|
||||
type!,
|
||||
appSecret!
|
||||
) as WechatPublicInstance;
|
||||
const result = await wechatInstance.deleteTag({ id: params.wechatId });
|
||||
return result;
|
||||
const application = await getWechatPublicConfig<ED>(
|
||||
params.applicationId,
|
||||
context
|
||||
);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === "wechatPublic");
|
||||
let appId: string, appSecret: string;
|
||||
const config2 = config as WechatPublicConfig;
|
||||
appId = config2.appId;
|
||||
appSecret = config2.appSecret;
|
||||
const wechatInstance = WechatSDK.getInstance(
|
||||
appId!,
|
||||
type!,
|
||||
appSecret!
|
||||
) as WechatPublicInstance;
|
||||
const result = await wechatInstance.deleteTag({ id: params.wechatId });
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function syncTag<ED extends EntityDict>(
|
||||
params: {
|
||||
applicationId: string,
|
||||
id: string,
|
||||
},
|
||||
context: BRC<ED>,
|
||||
params: {
|
||||
applicationId: string;
|
||||
id: string;
|
||||
},
|
||||
context: BRC<ED>
|
||||
): Promise<any> {
|
||||
const application = await getWechatPublicConfig<ED>(params.applicationId, context);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === 'wechatPublic');
|
||||
const wechatPublicTag = await getWechatPublicTagById<ED>(params.applicationId, params.id, context);
|
||||
assert(wechatPublicTag);
|
||||
try {
|
||||
const { tags } = await getTags<ED>({
|
||||
applicationId: params.applicationId!
|
||||
}, context);
|
||||
if (!wechatPublicTag.wechatId) {
|
||||
const result = await createTag<ED>({
|
||||
applicationId: params.applicationId!,
|
||||
name: wechatPublicTag.text!,
|
||||
}, context);
|
||||
await context.operate(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: 'update',
|
||||
data: {
|
||||
syncAt: Date.now(),
|
||||
sync: true,
|
||||
iState: 'success',
|
||||
wechatId: result.tag.id
|
||||
},
|
||||
filter: {
|
||||
id: params.id,
|
||||
}
|
||||
},
|
||||
{}
|
||||
)
|
||||
}
|
||||
if (wechatPublicTag.wechatId && tags.find((ele: Tag) => ele.id === wechatPublicTag.wechatId && ele.name !== wechatPublicTag.text)) {
|
||||
const result = await editTag<ED>({
|
||||
applicationId: params.applicationId!,
|
||||
id: wechatPublicTag.wechatId!,
|
||||
name: wechatPublicTag.text!,
|
||||
}, context);
|
||||
await context.operate(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: 'update',
|
||||
data: {
|
||||
syncAt: Date.now(),
|
||||
sync: true,
|
||||
iState: 'success',
|
||||
},
|
||||
filter: {
|
||||
id: params.id,
|
||||
}
|
||||
},
|
||||
{}
|
||||
)
|
||||
}
|
||||
if (wechatPublicTag.wechatId && tags.find((ele: Tag) => ele.id === wechatPublicTag.wechatId && ele.name === wechatPublicTag.text)) {
|
||||
const result = await deleteTag<ED>({
|
||||
applicationId: params.applicationId!,
|
||||
id: wechatPublicTag.id!,
|
||||
wechatId: wechatPublicTag.wechatId!,
|
||||
}, context);
|
||||
await context.operate(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: 'remove',
|
||||
data: {},
|
||||
filter: {
|
||||
id: params.id,
|
||||
}
|
||||
},
|
||||
{}
|
||||
)
|
||||
}
|
||||
} catch (e) {
|
||||
await context.operate(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: 'update',
|
||||
data: {
|
||||
sync: false,
|
||||
iState: 'fail',
|
||||
},
|
||||
filter: {
|
||||
id: params.id,
|
||||
}
|
||||
},
|
||||
{}
|
||||
);
|
||||
throw new OakPreConditionUnsetException(`${handleException(e as string)}`);
|
||||
const application = await getWechatPublicConfig<ED>(
|
||||
params.applicationId,
|
||||
context
|
||||
);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === "wechatPublic");
|
||||
const wechatPublicTag = await getWechatPublicTagById<ED>(
|
||||
params.applicationId,
|
||||
params.id,
|
||||
context
|
||||
);
|
||||
assert(wechatPublicTag);
|
||||
try {
|
||||
const { tags } = await getTags<ED>(
|
||||
{
|
||||
applicationId: params.applicationId!,
|
||||
},
|
||||
context
|
||||
);
|
||||
if (!wechatPublicTag.wechatId) {
|
||||
const result = await createTag<ED>(
|
||||
{
|
||||
applicationId: params.applicationId!,
|
||||
name: wechatPublicTag.text!,
|
||||
},
|
||||
context
|
||||
);
|
||||
await context.operate(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: "update",
|
||||
data: {
|
||||
syncAt: Date.now(),
|
||||
sync: true,
|
||||
iState: "success",
|
||||
wechatId: result.tag.id,
|
||||
},
|
||||
filter: {
|
||||
id: params.id,
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
if (
|
||||
wechatPublicTag.wechatId &&
|
||||
tags.find(
|
||||
(ele: Tag) =>
|
||||
ele.id === wechatPublicTag.wechatId &&
|
||||
ele.name !== wechatPublicTag.text
|
||||
)
|
||||
) {
|
||||
const result = await editTag<ED>(
|
||||
{
|
||||
applicationId: params.applicationId!,
|
||||
id: wechatPublicTag.wechatId!,
|
||||
name: wechatPublicTag.text!,
|
||||
},
|
||||
context
|
||||
);
|
||||
await context.operate(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: "update",
|
||||
data: {
|
||||
syncAt: Date.now(),
|
||||
sync: true,
|
||||
iState: "success",
|
||||
},
|
||||
filter: {
|
||||
id: params.id,
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
if (
|
||||
wechatPublicTag.wechatId &&
|
||||
tags.find(
|
||||
(ele: Tag) =>
|
||||
ele.id === wechatPublicTag.wechatId &&
|
||||
ele.name === wechatPublicTag.text
|
||||
)
|
||||
) {
|
||||
const result = await deleteTag<ED>(
|
||||
{
|
||||
applicationId: params.applicationId!,
|
||||
id: wechatPublicTag.id!,
|
||||
wechatId: wechatPublicTag.wechatId!,
|
||||
},
|
||||
context
|
||||
);
|
||||
await context.operate(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: "remove",
|
||||
data: {},
|
||||
filter: {
|
||||
id: params.id,
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
await context.operate(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: "update",
|
||||
data: {
|
||||
sync: false,
|
||||
iState: "fail",
|
||||
},
|
||||
filter: {
|
||||
id: params.id,
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
throw new OakPreConditionUnsetException(
|
||||
`${handleException(e as string)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function oneKeySync<ED extends EntityDict>(
|
||||
params: {
|
||||
applicationId: string,
|
||||
|
||||
},
|
||||
context: BRC<ED>,
|
||||
params: {
|
||||
applicationId: string;
|
||||
},
|
||||
context: BRC<ED>
|
||||
): Promise<any> {
|
||||
const application = await getWechatPublicConfig<ED>(params.applicationId, context);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === 'wechatPublic');
|
||||
const wechatPublicTags = await getWechatPublicTags<ED>(params.applicationId, context);
|
||||
try {
|
||||
const tags = await getTags<ED>(params, context);
|
||||
if (wechatPublicTags && wechatPublicTags.length > 0) {
|
||||
const tagsToAdd = tags.tags?.filter((ele: Tag) => {
|
||||
return !wechatPublicTags.some(item => item?.wechatId === ele.id)
|
||||
});
|
||||
const tagsToRemove = tags.tags?.length ? wechatPublicTags.filter((ele) => {
|
||||
return !tags.tags?.some((item: Tag) => item.id === ele?.wechatId);
|
||||
}) : wechatPublicTags;
|
||||
tagsToAdd?.map(async (ele: Tag) => {
|
||||
await context.operate(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: 'create',
|
||||
data: {
|
||||
id: await generateNewIdAsync(),
|
||||
applicationId: params.applicationId,
|
||||
wechatId: ele.id,
|
||||
text: ele.name,
|
||||
sync: true,
|
||||
syncAt: Date.now(),
|
||||
iState: 'success',
|
||||
},
|
||||
},
|
||||
{}
|
||||
)
|
||||
});
|
||||
tagsToRemove?.map(async (ele) => {
|
||||
await context.operate(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: 'remove',
|
||||
data: {},
|
||||
filter: {
|
||||
id: ele.id
|
||||
}
|
||||
},
|
||||
{}
|
||||
)
|
||||
});
|
||||
wechatPublicTags?.map(async (ele) => {
|
||||
await context.operate(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: 'update',
|
||||
data: {
|
||||
syncAt: Date.now(),
|
||||
sync: true,
|
||||
iState: 'success'
|
||||
},
|
||||
filter: {
|
||||
id: ele.id
|
||||
}
|
||||
},
|
||||
{}
|
||||
)
|
||||
})
|
||||
} else {
|
||||
tags.tags?.map(async (ele: Tag) => {
|
||||
await context.operate(
|
||||
'wechatPublicTag',
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: 'create',
|
||||
data: {
|
||||
id: await generateNewIdAsync(),
|
||||
applicationId: params.applicationId,
|
||||
wechatId: ele.id,
|
||||
text: ele.name,
|
||||
sync: true,
|
||||
syncAt: Date.now(),
|
||||
iState: 'success',
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
});
|
||||
}
|
||||
} catch (e: any) {
|
||||
throw new OakPreConditionUnsetException(`${handleException(e)}`);
|
||||
const application = await getWechatPublicConfig<ED>(
|
||||
params.applicationId,
|
||||
context
|
||||
);
|
||||
assert(application);
|
||||
const { type, config, systemId } = application!;
|
||||
assert(type === "wechatPublic");
|
||||
const wechatPublicTags = await getWechatPublicTags<ED>(
|
||||
params.applicationId,
|
||||
context
|
||||
);
|
||||
try {
|
||||
const tags = await getTags<ED>(params, context);
|
||||
if (wechatPublicTags && wechatPublicTags.length > 0) {
|
||||
const tagsToAdd = tags.tags?.filter((ele: Tag) => {
|
||||
return !wechatPublicTags.some(
|
||||
(item) => item?.wechatId === ele.id
|
||||
);
|
||||
});
|
||||
const tagsToRemove = tags.tags?.length
|
||||
? wechatPublicTags.filter((ele) => {
|
||||
return !tags.tags?.some(
|
||||
(item: Tag) => item.id === ele?.wechatId
|
||||
);
|
||||
})
|
||||
: wechatPublicTags;
|
||||
|
||||
for (const ele of tagsToAdd || []) {
|
||||
await context.operate(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: "create",
|
||||
data: {
|
||||
id: await generateNewIdAsync(),
|
||||
applicationId: params.applicationId,
|
||||
wechatId: ele.id,
|
||||
text: ele.name,
|
||||
sync: true,
|
||||
syncAt: Date.now(),
|
||||
iState: "success",
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
// Remove tags
|
||||
for (const ele of tagsToRemove || []) {
|
||||
await context.operate(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: "remove",
|
||||
data: {},
|
||||
filter: {
|
||||
id: ele.id,
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
// Update tags
|
||||
for (const ele of wechatPublicTags || []) {
|
||||
await context.operate(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: "update",
|
||||
data: {
|
||||
syncAt: Date.now(),
|
||||
sync: true,
|
||||
iState: "success",
|
||||
},
|
||||
filter: {
|
||||
id: ele.id,
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
} else {
|
||||
for (const ele of tags.tags || []) {
|
||||
await context.operate(
|
||||
"wechatPublicTag",
|
||||
{
|
||||
id: await generateNewIdAsync(),
|
||||
action: "create",
|
||||
data: {
|
||||
id: await generateNewIdAsync(),
|
||||
applicationId: params.applicationId,
|
||||
wechatId: ele.id,
|
||||
text: ele.name,
|
||||
sync: true,
|
||||
syncAt: Date.now(),
|
||||
iState: "success",
|
||||
},
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
throw new OakPreConditionUnsetException(`${handleException(e)}`);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue