Skip to content

Commit

Permalink
chore: move member leave as a event
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Jan 8, 2025
1 parent c352bff commit 1fbb7cb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
5 changes: 5 additions & 0 deletions packages/backend/server/src/base/event/def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface WorkspaceEvents {
}>;
ownerTransferred: Payload<{ email: string; workspaceId: Workspace['id'] }>;
updated: Payload<{ workspaceId: Workspace['id']; count: number }>;
leave: Payload<{
ownerEmail: string;
inviteeName: string;
workspaceId: Workspace['id'];
}>;
removed: Payload<{ workspaceId: Workspace['id']; userId: User['id'] }>;
};
deleted: Payload<Workspace['id']>;
Expand Down
56 changes: 34 additions & 22 deletions packages/backend/server/src/core/workspaces/resolvers/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ export class WorkspaceService {
return true;
}

async sendInviteEmail(inviteId: string) {
const target = await this.getInviteeEmailTarget(inviteId);

if (!target) {
return;
}

const owner = await this.permission.getWorkspaceOwner(target.workspace.id);

await this.mailer.sendMemberInviteMail(target.email, inviteId, {
workspace: target.workspace,
user: {
avatar: owner.avatarUrl || '',
name: owner.name || '',
},
});
}

// ================ Team ================

async sendTeamWorkspaceUpgradedEmail(workspaceId: string) {
const workspace = await this.getWorkspaceInfo(workspaceId);
const owner = await this.permission.getWorkspaceOwner(workspaceId);
Expand Down Expand Up @@ -182,24 +202,6 @@ export class WorkspaceService {
}
}

async sendInviteEmail(inviteId: string) {
const target = await this.getInviteeEmailTarget(inviteId);

if (!target) {
return;
}

const owner = await this.permission.getWorkspaceOwner(target.workspace.id);

await this.mailer.sendMemberInviteMail(target.email, inviteId, {
workspace: target.workspace,
user: {
avatar: owner.avatarUrl || '',
name: owner.name || '',
},
});
}

async sendReviewApproveEmail(inviteId: string) {
const target = await this.getInviteeEmailTarget(inviteId);
if (!target) return;
Expand Down Expand Up @@ -236,9 +238,17 @@ export class WorkspaceService {
await this.mailer.sendOwnershipTransferredEmail(email, workspace);
}

async sendMemberRemoved(email: string, ws: { id: string }) {
const workspace = await this.getWorkspaceInfo(ws.id);
await this.mailer.sendMemberRemovedEmail(email, workspace);
@OnEvent('workspace.members.leave')
async onMemberLeave({
ownerEmail,
inviteeName,
workspaceId,
}: EventPayload<'workspace.members.leave'>) {
const workspace = await this.getWorkspaceInfo(workspaceId);
await this.mailer.sendMemberLeaveEmail(ownerEmail, {
workspace,
inviteeName,
});
}

@OnEvent('workspace.members.removed')
Expand All @@ -248,7 +258,9 @@ export class WorkspaceService {
}: EventPayload<'workspace.members.requestDeclined'>) {
const user = await this.user.findUserById(userId);
if (!user) return;
await this.sendMemberRemoved(user.email, { id: workspaceId });

const workspace = await this.getWorkspaceInfo(workspaceId);
await this.mailer.sendMemberRemovedEmail(user.email, workspace);
}

@OnEvent('workspace.subscription.notify')
Expand Down
16 changes: 8 additions & 8 deletions packages/backend/server/src/core/workspaces/resolvers/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ export class TeamWorkspaceResolver {
await this.workspaceService.sendReviewRequestedEmail(inviteId);
}

@OnEvent('workspace.members.requestApproved')
async onApproveRequest({
inviteId,
}: EventPayload<'workspace.members.requestApproved'>) {
// send approve mail
await this.workspaceService.sendReviewApproveEmail(inviteId);
}

@OnEvent('workspace.members.requestDeclined')
async onDeclineRequest({
userId,
Expand All @@ -361,14 +369,6 @@ export class TeamWorkspaceResolver {
);
}

@OnEvent('workspace.members.requestApproved')
async onApproveRequest({
inviteId,
}: EventPayload<'workspace.members.requestApproved'>) {
// send approve mail
await this.workspaceService.sendReviewApproveEmail(inviteId);
}

@OnEvent('workspace.members.roleChanged')
async onRoleChanged({
userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
DocNotFound,
EventEmitter,
InternalServerError,
MailService,
MemberQuotaExceeded,
RequestMutex,
SpaceAccessDenied,
Expand Down Expand Up @@ -78,7 +77,6 @@ export class WorkspaceResolver {

constructor(
private readonly cache: Cache,
private readonly mailer: MailService,
private readonly prisma: PrismaClient,
private readonly permissions: PermissionService,
private readonly quota: QuotaManagementService,
Expand Down Expand Up @@ -610,14 +608,14 @@ export class WorkspaceResolver {
_workspaceName?: string
) {
await this.permissions.checkWorkspace(workspaceId, user.id);
const { name: workspaceName } =
await this.workspaceService.getWorkspaceInfo(workspaceId);

const owner = await this.permissions.getWorkspaceOwner(workspaceId);

if (sendLeaveMail) {
await this.mailer.sendMemberLeaveEmail(owner.email, {
workspaceName,
this.event.emit('workspace.members.leave', {
ownerEmail: owner.email,
inviteeName: user.name,
workspaceId,
});
}

Expand Down

0 comments on commit 1fbb7cb

Please sign in to comment.