feat(server): cluster level event system (#9884)

This commit is contained in:
forehalo
2025-01-25 14:51:03 +00:00
parent 0d2c2ea21e
commit 6370f45928
43 changed files with 634 additions and 364 deletions
@@ -1,10 +1,9 @@
import { EventEmitter2 } from '@nestjs/event-emitter';
import { TestingModule } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import ava, { TestFn } from 'ava';
import Sinon from 'sinon';
import { EmailAlreadyUsed } from '../../base';
import { EmailAlreadyUsed, EventBus } from '../../base';
import { Permission } from '../../models/common';
import { UserModel } from '../../models/user';
import { WorkspaceMemberStatus } from '../../models/workspace';
@@ -46,7 +45,7 @@ test('should create a new user', async t => {
});
test('should trigger user.created event', async t => {
const event = t.context.module.get(EventEmitter2);
const event = t.context.module.get(EventBus);
const spy = Sinon.spy();
event.on('user.created', spy);
@@ -117,7 +116,7 @@ test('should not update email to an existing one', async t => {
});
test('should trigger user.updated event', async t => {
const event = t.context.module.get(EventEmitter2);
const event = t.context.module.get(EventBus);
const spy = Sinon.spy();
event.on('user.updated', spy);
@@ -217,7 +216,7 @@ test('should fulfill user', async t => {
});
test('should trigger user.updated event when fulfilling user', async t => {
const event = t.context.module.get(EventEmitter2);
const event = t.context.module.get(EventBus);
const createSpy = Sinon.spy();
const updateSpy = Sinon.spy();
event.on('user.created', createSpy);
@@ -250,7 +249,7 @@ test('should delete user', async t => {
});
test('should trigger user.deleted event', async t => {
const event = t.context.module.get(EventEmitter2);
const event = t.context.module.get(EventBus);
const spy = Sinon.spy();
event.on('user.deleted', spy);
@@ -1,10 +1,9 @@
import { EventEmitter2 } from '@nestjs/event-emitter';
import { TestingModule } from '@nestjs/testing';
import { PrismaClient, WorkspaceMemberStatus } from '@prisma/client';
import ava, { TestFn } from 'ava';
import Sinon from 'sinon';
import { Config } from '../../base/config';
import { Config, EventBus } from '../../base';
import { Permission } from '../../models/common';
import { UserModel } from '../../models/user';
import { WorkspaceModel } from '../../models/workspace';
@@ -305,7 +304,7 @@ test('should grant member with read permission and Pending status by default', a
email: 'test2@affine.pro',
});
const event = t.context.module.get(EventEmitter2);
const event = t.context.module.get(EventBus);
const updatedSpy = Sinon.spy();
event.on('workspace.members.updated', updatedSpy);
const member1 = await t.context.workspace.grantMember(
@@ -829,7 +828,7 @@ test('should delete workspace member in Pending, Accepted status', async t => {
);
t.is(member.status, WorkspaceMemberStatus.Pending);
const event = t.context.module.get(EventEmitter2);
const event = t.context.module.get(EventBus);
const updatedSpy = Sinon.spy();
event.on('workspace.members.updated', updatedSpy);
let success = await t.context.workspace.deleteMember(
@@ -880,7 +879,7 @@ test('should trigger workspace.members.requestDeclined event when delete workspa
);
t.is(member.status, WorkspaceMemberStatus.UnderReview);
const event = t.context.module.get(EventEmitter2);
const event = t.context.module.get(EventBus);
const updatedSpy = Sinon.spy();
const requestDeclinedSpy = Sinon.spy();
event.on('workspace.members.updated', updatedSpy);
@@ -925,7 +924,7 @@ test('should trigger workspace.members.requestDeclined event when delete workspa
);
t.is(member.status, WorkspaceMemberStatus.NeedMoreSeatAndReview);
const event = t.context.module.get(EventEmitter2);
const event = t.context.module.get(EventBus);
const updatedSpy = Sinon.spy();
const requestDeclinedSpy = Sinon.spy();
event.on('workspace.members.updated', updatedSpy);