refactor(arch): 落地平台架构演进第一二阶段整改,消除OOM与越权风险并支持高并发缓冲与AI多租户BYOK
- [P0安全底座] 多租户数据闭环:公开留资与雷达足迹自动逆向追溯绑定名片 tenantId 与归属销售 userId - [P0性能稳定] 消除内存全量加载:公海池分批带条件扫描回收,雷达国家分布改用 SQL 原生 GROUP BY 聚合查询彻底消除 OOM 隐患 - [P0商业变现] 商业化配额门禁切面:在 Cool-Admin modifyBefore 生命周期织入 assertQuotaAvailable 校验,阻断超额名片创建 - [P0环境解耦] 客户端解耦本地硬编码:request.uts 支持 api_base_url 本地覆盖,生产数据库名修正为 crmmpdata - [P1高并发削峰] 雷达足迹批量缓冲引擎:引入 CrmRadarBufferService 环形缓冲队列与阈值/定时批量 Bulk Insert 落盘 - [P2中枢租户化] AI 智能中枢多租户 BYOK 与 Token 成本风控:支持企业自带 Key 隔离、Token 消耗账本、用量配额门禁与名片人设个性化联动 - [跨端规范] 对齐 Uni-app x 移动微官网原生首屏与底部原生 tabBar 规范 - [测试驱动] 全工程 116 个测试套件,1,175 个单元与集成测试用例全部 100% 绿色通过,管理后台 Vite 打包无错误
这个提交包含在:
@@ -289,8 +289,12 @@ describe('后台 Bento 画布 CRUD (Admin Card Bento CoolController)', () => {
|
||||
fieldEq?: string[];
|
||||
addOrderBy?: Record<string, string>;
|
||||
};
|
||||
expect(pageQueryOp.fieldEq).toEqual(['a.cardId', 'a.type', 'a.status']);
|
||||
expect(pageQueryOp.addOrderBy).toEqual({ sortOrder: 'asc' });
|
||||
expect(pageQueryOp.fieldEq).toEqual([
|
||||
{ column: 'a.card_id', requestParam: 'cardId' },
|
||||
'a.type',
|
||||
'a.status',
|
||||
]);
|
||||
expect(pageQueryOp.addOrderBy).toEqual({ sort_order: 'asc' });
|
||||
});
|
||||
|
||||
it('应将 getBlocksByCardId / batchSaveLayout / toggleStatus 注册为 serviceApis', () => {
|
||||
|
||||
@@ -52,7 +52,7 @@ describe('后台名片 CRUD (Admin Card Info CoolController)', () => {
|
||||
expect.arrayContaining(['a.name'])
|
||||
);
|
||||
expect(pageQueryOp.fieldEq).toEqual(expect.arrayContaining(['a.status']));
|
||||
expect(pageQueryOp.addOrderBy).toEqual({ updateTime: 'desc' });
|
||||
expect(pageQueryOp.addOrderBy).toEqual({ update_time: 'desc' });
|
||||
});
|
||||
|
||||
it('insertParam 应从后台登录态写入 userId', () => {
|
||||
|
||||
@@ -55,9 +55,12 @@ describe('后台博客文章与动态发布中心 (Card Article CoolController &
|
||||
expect.arrayContaining(['a.title', 'a.summary'])
|
||||
);
|
||||
expect(pageQueryOp.fieldEq).toEqual(
|
||||
expect.arrayContaining(['a.cardId', 'a.isPublished'])
|
||||
expect.arrayContaining([
|
||||
{ column: 'a.card_id', requestParam: 'cardId' },
|
||||
{ column: 'a.is_published', requestParam: 'isPublished' },
|
||||
])
|
||||
);
|
||||
expect(pageQueryOp.addOrderBy).toEqual({ updateTime: 'desc' });
|
||||
expect(pageQueryOp.addOrderBy).toEqual({ update_time: 'desc' });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -48,11 +48,13 @@ describe('后台出海案例作品集 (Admin Card Cases CRUD)', () => {
|
||||
|
||||
it('pageQueryOp 应支持 a.cardId 精确筛选与 title/client/metrics 模糊搜索', () => {
|
||||
const pageQueryOp = adminCardCasesCurdOption.pageQueryOp;
|
||||
expect(pageQueryOp.fieldEq).toEqual(expect.arrayContaining(['a.cardId']));
|
||||
expect(pageQueryOp.fieldEq).toEqual(
|
||||
expect.arrayContaining([{ column: 'a.card_id', requestParam: 'cardId' }])
|
||||
);
|
||||
expect(pageQueryOp.keyWordLikeFields).toEqual(
|
||||
expect.arrayContaining(['a.title', 'a.client', 'a.metrics'])
|
||||
);
|
||||
expect(pageQueryOp.addOrderBy).toMatchObject({ sortOrder: 'asc' });
|
||||
expect(pageQueryOp.addOrderBy).toMatchObject({ sort_order: 'asc' });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
import { CardContactService } from '../../../src/modules/card/service/contact';
|
||||
import {
|
||||
CardMutualExchangeService,
|
||||
createMutualExchangeInvitation,
|
||||
} from '../../../src/modules/card/service/mutual-exchange';
|
||||
|
||||
describe('P0 名片大厅人脉与双向互换落库', () => {
|
||||
let contactService: CardContactService;
|
||||
let exchangeService: CardMutualExchangeService;
|
||||
let contactStore: any[];
|
||||
let leadStore: any[];
|
||||
let cardStore: any[];
|
||||
let exchangeStore: any[];
|
||||
|
||||
function createRepo(store: any[], idStart = 1) {
|
||||
let seq = idStart;
|
||||
return {
|
||||
findOne: jest.fn(async (opts: { where?: Record<string, unknown> }) => {
|
||||
const where = opts?.where || {};
|
||||
return (
|
||||
store.find(row =>
|
||||
Object.keys(where).every(key => row[key] === (where as any)[key])
|
||||
) || null
|
||||
);
|
||||
}),
|
||||
find: jest.fn(async (opts?: { where?: Record<string, unknown> }) => {
|
||||
const where = opts?.where || {};
|
||||
return store.filter(row =>
|
||||
Object.keys(where).every(key => row[key] === (where as any)[key])
|
||||
);
|
||||
}),
|
||||
save: jest.fn(async (item: any) => {
|
||||
if (item.id) {
|
||||
const idx = store.findIndex(row => row.id === item.id);
|
||||
if (idx >= 0) {
|
||||
store[idx] = { ...store[idx], ...item };
|
||||
return store[idx];
|
||||
}
|
||||
}
|
||||
const saved = { ...item, id: item.id || seq++ };
|
||||
store.push(saved);
|
||||
return saved;
|
||||
}),
|
||||
createQueryBuilder: jest.fn(() => {
|
||||
const qb: any = {
|
||||
alias: 'a',
|
||||
leftJoin: jest.fn().mockReturnThis(),
|
||||
addSelect: jest.fn().mockReturnThis(),
|
||||
where: jest.fn().mockReturnThis(),
|
||||
andWhere: jest.fn().mockReturnThis(),
|
||||
orderBy: jest.fn().mockReturnThis(),
|
||||
skip: jest.fn().mockReturnThis(),
|
||||
take: jest.fn().mockReturnThis(),
|
||||
getCount: jest.fn(async () =>
|
||||
store.filter(row => row.ownerUserId === 11).length
|
||||
),
|
||||
getRawAndEntities: jest.fn(async () => ({
|
||||
entities: store.filter(row => row.ownerUserId === 11),
|
||||
raw: [],
|
||||
})),
|
||||
};
|
||||
return qb;
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
contactStore = [];
|
||||
leadStore = [];
|
||||
exchangeStore = [];
|
||||
cardStore = [
|
||||
{
|
||||
id: 101,
|
||||
userId: 11,
|
||||
tenantId: 1,
|
||||
name: 'Alex Rivera',
|
||||
title: 'VP',
|
||||
company: 'OmniCard',
|
||||
phone: '+8613800000001',
|
||||
email: 'alex@omni.test',
|
||||
slug: 'alex',
|
||||
status: 'published',
|
||||
},
|
||||
{
|
||||
id: 202,
|
||||
userId: 22,
|
||||
tenantId: 2,
|
||||
name: 'Tariq Mansour',
|
||||
title: 'Director',
|
||||
company: 'Dubai Energy',
|
||||
phone: '+971501234567',
|
||||
email: 'tariq@dubai.test',
|
||||
slug: 'tariq',
|
||||
status: 'published',
|
||||
},
|
||||
{
|
||||
id: 103,
|
||||
userId: 33,
|
||||
tenantId: 1,
|
||||
name: 'Chen Wei',
|
||||
title: 'Sales',
|
||||
company: 'OmniCard',
|
||||
phone: '+8613800000003',
|
||||
email: 'chen@omni.test',
|
||||
slug: 'chen-wei',
|
||||
status: 'published',
|
||||
},
|
||||
];
|
||||
|
||||
contactService = new CardContactService();
|
||||
contactService.cardContactEntity = createRepo(contactStore) as any;
|
||||
contactService.cardInfoEntity = createRepo(cardStore, 300) as any;
|
||||
contactService.crmLeadEntity = createRepo(leadStore, 500) as any;
|
||||
|
||||
exchangeService = new CardMutualExchangeService();
|
||||
exchangeService.exchangeEntity = createRepo(exchangeStore, 800) as any;
|
||||
exchangeService.cardContactService = contactService;
|
||||
exchangeService.cardInfoEntity = contactService.cardInfoEntity;
|
||||
exchangeService.crmLeadEntity = contactService.crmLeadEntity;
|
||||
});
|
||||
|
||||
it('不能把自己的名片收入自己的大厅', async () => {
|
||||
await expect(
|
||||
contactService.upsertContact({
|
||||
ownerUserId: 11,
|
||||
ownerCardId: 101,
|
||||
peerCardId: 101,
|
||||
source: 'collect',
|
||||
})
|
||||
).rejects.toThrow(/不能收藏自己/);
|
||||
});
|
||||
|
||||
it('收藏他人名片只写一行人脉,重复收藏复用同一行', async () => {
|
||||
const first = await contactService.collect({
|
||||
ownerUserId: 11,
|
||||
peerCardId: 202,
|
||||
});
|
||||
const second = await contactService.collect({
|
||||
ownerUserId: 11,
|
||||
peerCardId: 202,
|
||||
});
|
||||
|
||||
expect(contactStore).toHaveLength(1);
|
||||
expect(first.id).toBe(second.id);
|
||||
expect(first.peerCardId).toBe(202);
|
||||
expect(first.source).toBe('collect');
|
||||
expect(first.ownerUserId).toBe(11);
|
||||
});
|
||||
|
||||
it('确认互换后双方各有一条人脉和一条线索', async () => {
|
||||
const invite = await exchangeService.initiateExchange({
|
||||
senderCardId: 101,
|
||||
recipientCardId: 202,
|
||||
});
|
||||
expect(invite.status).toBe('REQUESTED');
|
||||
expect(exchangeStore).toHaveLength(1);
|
||||
|
||||
const outcome = await exchangeService.confirmExchange(invite.id, 202);
|
||||
|
||||
expect(outcome.status).toBe('ACCEPTED');
|
||||
expect(outcome.isDualSynced).toBe(true);
|
||||
expect(contactStore).toHaveLength(2);
|
||||
expect(leadStore).toHaveLength(2);
|
||||
|
||||
const alexContact = contactStore.find(row => row.ownerUserId === 11);
|
||||
const tariqContact = contactStore.find(row => row.ownerUserId === 22);
|
||||
expect(alexContact.peerCardId).toBe(202);
|
||||
expect(tariqContact.peerCardId).toBe(101);
|
||||
expect(alexContact.source).toBe('exchange');
|
||||
|
||||
expect(leadStore.every(row => row.source === 'exchange')).toBe(true);
|
||||
expect(leadStore.find(row => row.cardId === 101)?.name).toBe('Tariq Mansour');
|
||||
expect(leadStore.find(row => row.cardId === 202)?.name).toBe('Alex Rivera');
|
||||
});
|
||||
|
||||
it('大厅分页只返回当前用户的人脉', async () => {
|
||||
await contactService.collect({ ownerUserId: 11, peerCardId: 202 });
|
||||
await contactService.upsertContact({
|
||||
ownerUserId: 22,
|
||||
peerCardId: 101,
|
||||
source: 'collect',
|
||||
});
|
||||
|
||||
const page = await contactService.pageContacts({
|
||||
ownerUserId: 11,
|
||||
page: 1,
|
||||
size: 20,
|
||||
});
|
||||
|
||||
expect(page.list).toHaveLength(1);
|
||||
expect(page.list[0].peerCardId).toBe(202);
|
||||
expect(page.pagination.total).toBe(1);
|
||||
});
|
||||
|
||||
it('团队墙只返回同租户已发布名片,并排除自己', async () => {
|
||||
const page = await contactService.pageTeamWall({
|
||||
tenantId: 1,
|
||||
excludeUserId: 11,
|
||||
page: 1,
|
||||
size: 20,
|
||||
});
|
||||
|
||||
expect(page.list).toHaveLength(1);
|
||||
expect(page.list[0].peerCardId).toBe(103);
|
||||
expect(page.list[0].peerName).toBe('Chen Wei');
|
||||
expect(page.list.find(row => row.peerCardId === 101)).toBeUndefined();
|
||||
expect(page.list.find(row => row.peerCardId === 202)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('纯函数互换邀约仍拒绝自己与自己互换', () => {
|
||||
expect(() => createMutualExchangeInvitation(101, 101)).toThrow(
|
||||
/不能与自己互换/
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,138 @@
|
||||
import { CardEventService, normalizeEventCode } from '../../../src/modules/card/service/event';
|
||||
|
||||
describe('P2 展会现场活动码与同场名片墙', () => {
|
||||
let eventService: CardEventService;
|
||||
let eventStore: any[];
|
||||
let memberStore: any[];
|
||||
let cardStore: any[];
|
||||
|
||||
function createRepo(store: any[], idStart = 1) {
|
||||
let seq = idStart;
|
||||
return {
|
||||
findOne: jest.fn(async (opts: { where?: Record<string, unknown> }) => {
|
||||
const where = opts?.where || {};
|
||||
return (
|
||||
store.find(row =>
|
||||
Object.keys(where).every(key => row[key] === (where as any)[key])
|
||||
) || null
|
||||
);
|
||||
}),
|
||||
find: jest.fn(async (opts?: { where?: Record<string, unknown> }) => {
|
||||
const where = opts?.where || {};
|
||||
return store.filter(row =>
|
||||
Object.keys(where).every(key => row[key] === (where as any)[key])
|
||||
);
|
||||
}),
|
||||
save: jest.fn(async (item: any) => {
|
||||
if (item.id) {
|
||||
const idx = store.findIndex(row => row.id === item.id);
|
||||
if (idx >= 0) {
|
||||
store[idx] = { ...store[idx], ...item };
|
||||
return store[idx];
|
||||
}
|
||||
}
|
||||
const saved = { ...item, id: item.id || seq++ };
|
||||
store.push(saved);
|
||||
return saved;
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
eventStore = [];
|
||||
memberStore = [];
|
||||
cardStore = [
|
||||
{
|
||||
id: 101,
|
||||
userId: 11,
|
||||
tenantId: 1,
|
||||
name: 'Alex Rivera',
|
||||
title: 'VP',
|
||||
company: 'OmniCard',
|
||||
slug: 'alex',
|
||||
status: 'published',
|
||||
},
|
||||
{
|
||||
id: 103,
|
||||
userId: 33,
|
||||
tenantId: 1,
|
||||
name: 'Chen Wei',
|
||||
title: 'Sales',
|
||||
company: 'OmniCard',
|
||||
slug: 'chen-wei',
|
||||
status: 'published',
|
||||
},
|
||||
{
|
||||
id: 202,
|
||||
userId: 22,
|
||||
tenantId: 2,
|
||||
name: 'Tariq Mansour',
|
||||
title: 'Director',
|
||||
company: 'Dubai Energy',
|
||||
slug: 'tariq',
|
||||
status: 'published',
|
||||
},
|
||||
];
|
||||
eventService = new CardEventService();
|
||||
eventService.cardEventEntity = createRepo(eventStore) as any;
|
||||
eventService.cardEventMemberEntity = createRepo(memberStore, 50) as any;
|
||||
eventService.cardInfoEntity = createRepo(cardStore, 300) as any;
|
||||
});
|
||||
|
||||
it('normalizeEventCode 只接受 6 位字母数字', () => {
|
||||
expect(normalizeEventCode('ces26a')).toBe('CES26A');
|
||||
expect(() => normalizeEventCode('12')).toThrow(/活动码/);
|
||||
expect(() => normalizeEventCode('CES-26')).toThrow(/活动码/);
|
||||
});
|
||||
|
||||
it('首次加入活动码会创建现场并写入成员', async () => {
|
||||
const result = await eventService.joinEvent({
|
||||
userId: 11,
|
||||
code: 'ces26a',
|
||||
});
|
||||
|
||||
expect(result.event.code).toBe('CES26A');
|
||||
expect(result.event.status).toBe('active');
|
||||
expect(result.member.userId).toBe(11);
|
||||
expect(result.member.cardId).toBe(101);
|
||||
expect(eventStore).toHaveLength(1);
|
||||
expect(memberStore).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('同一用户重复加入同一现场只保留一行成员', async () => {
|
||||
await eventService.joinEvent({ userId: 11, code: 'CES26A' });
|
||||
await eventService.joinEvent({ userId: 11, code: 'ces26a' });
|
||||
|
||||
expect(eventStore).toHaveLength(1);
|
||||
expect(memberStore).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('现场墙只返回同场他人已发布名片', async () => {
|
||||
await eventService.joinEvent({ userId: 11, code: 'CES26A' });
|
||||
await eventService.joinEvent({ userId: 33, code: 'CES26A' });
|
||||
await eventService.joinEvent({ userId: 22, code: 'GITEX1' });
|
||||
|
||||
const page = await eventService.pageEventFloor({
|
||||
userId: 11,
|
||||
page: 1,
|
||||
size: 20,
|
||||
});
|
||||
|
||||
expect(page.list).toHaveLength(1);
|
||||
expect(page.list[0].peerCardId).toBe(103);
|
||||
expect(page.list[0].peerName).toBe('Chen Wei');
|
||||
expect(page.list[0].source).toBe('event');
|
||||
expect(page.list.find(row => row.peerCardId === 101)).toBeUndefined();
|
||||
expect(page.list.find(row => row.peerCardId === 202)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('未加入现场时墙为空', async () => {
|
||||
const page = await eventService.pageEventFloor({
|
||||
userId: 11,
|
||||
page: 1,
|
||||
size: 20,
|
||||
});
|
||||
expect(page.list).toHaveLength(0);
|
||||
expect(page.pagination.total).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -361,7 +361,7 @@ describe('T17 名片内容级多语言分支 (card_locale)', () => {
|
||||
fieldEq?: string[];
|
||||
};
|
||||
expect(pageQueryOp.fieldEq).toEqual(
|
||||
expect.arrayContaining(['a.cardId', 'a.locale'])
|
||||
expect.arrayContaining([{ column: 'a.card_id', requestParam: 'cardId' }, 'a.locale'])
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { CardInfoService } from '../../../src/modules/card/service/info';
|
||||
import { TeamQuotaService } from '../../../src/modules/team/service/quota';
|
||||
import { CoolCommException } from '@cool-midway/core';
|
||||
|
||||
describe('商业化配额门禁: 新增名片配额校验与租户自动绑定', () => {
|
||||
let cardInfoService: CardInfoService;
|
||||
let mockQuotaService: any;
|
||||
let mockPortalService: any;
|
||||
let mockRepo: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mockRepo = {
|
||||
save: jest.fn().mockImplementation(async (item: any) => ({ id: 1, ...item })),
|
||||
};
|
||||
|
||||
mockPortalService = {
|
||||
clearPortalCache: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
mockQuotaService = {
|
||||
assertQuotaAvailable: jest.fn().mockImplementation(async (tenantId: number, type: string) => {
|
||||
if (tenantId === 999) {
|
||||
throw new CoolCommException('当前套餐名片配额已达上限,请升级套餐', 400);
|
||||
}
|
||||
return { used: 1, limit: 5, exceeded: false };
|
||||
}),
|
||||
};
|
||||
|
||||
cardInfoService = new CardInfoService();
|
||||
cardInfoService.cardInfoEntity = mockRepo;
|
||||
cardInfoService.cardPortalService = mockPortalService;
|
||||
cardInfoService.teamQuotaService = mockQuotaService;
|
||||
});
|
||||
|
||||
it('当租户配额超限时,modifyBefore 新增拦截必须阻断并抛出 CoolCommException', async () => {
|
||||
const cardData = {
|
||||
name: '超限测试名片',
|
||||
tenantId: 999,
|
||||
};
|
||||
|
||||
await expect(
|
||||
cardInfoService.modifyBefore(cardData, 'add')
|
||||
).rejects.toThrow(/配额/);
|
||||
|
||||
expect(mockQuotaService.assertQuotaAvailable).toHaveBeenCalledWith(999, 'cards', 1);
|
||||
});
|
||||
|
||||
it('当租户配额充足时,modifyBefore 新增允许通过并自动锁定 tenantId', async () => {
|
||||
const cardData = {
|
||||
name: '合规名片',
|
||||
tenantId: 88,
|
||||
};
|
||||
|
||||
await expect(
|
||||
cardInfoService.modifyBefore(cardData, 'add')
|
||||
).resolves.not.toThrow();
|
||||
|
||||
expect(mockQuotaService.assertQuotaAvailable).toHaveBeenCalledWith(88, 'cards', 1);
|
||||
});
|
||||
});
|
||||
在新工单中引用
屏蔽一个用户