-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
207 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
services/api/src/domain/usecases/commands/parse/parsers/approve-request-parser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { z } from 'zod'; | ||
import { CommandParser, ParsedCommand } from '../command.parser'; | ||
|
||
const schema = z | ||
.tuple([z.literal('approve'), z.literal('request'), z.string().email()]) | ||
.transform<ParsedCommand>(([, , email]) => ({ | ||
tag: 'approveRequest', | ||
params: { email }, | ||
})); | ||
|
||
export const approveRequestParser = new CommandParser({ | ||
matchTokens: ['approve', 'request'], | ||
schema, | ||
signature: `/approve request {email}`, | ||
summary: 'approve pending request to join the room', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { | ||
MembershipStatus, | ||
hasPendingRequestTo, | ||
isMemberOf, | ||
} from '@entities/membership.entity'; | ||
import { MembershipsRepository } from '@entities/memberships.repository'; | ||
import { Dispatcher, DraftMessage } from '@entities/messages'; | ||
import { RoomsRepository } from '@entities/rooms.repository'; | ||
import { User, UsersRepository } from '@entities/users'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthService, Role } from '@usecases/auth.service'; | ||
|
||
export type ApproveRequestParams = { | ||
authenticatedUser: User; | ||
roomId: string; | ||
email: string; | ||
}; | ||
|
||
@Injectable() | ||
export class ApproveRequestUseCase { | ||
constructor( | ||
private readonly rooms: RoomsRepository, | ||
private readonly users: UsersRepository, | ||
private readonly memberships: MembershipsRepository, | ||
private readonly auth: AuthService, | ||
private readonly dispatcher: Dispatcher, | ||
) {} | ||
|
||
async exec({ | ||
authenticatedUser, | ||
roomId, | ||
email, | ||
}: ApproveRequestParams): Promise<void> { | ||
const room = await this.rooms.getRoom(roomId); | ||
|
||
await this.auth.authorize({ | ||
user: authenticatedUser, | ||
subject: room, | ||
action: Role.Manage, | ||
}); | ||
|
||
const invitedUser = await this.users.findUser(email); | ||
|
||
if (!invitedUser) { | ||
const message: DraftMessage = { | ||
content: `No user exists with email ${email}`, | ||
roomId: room.id, | ||
authorId: 'system', | ||
recipientId: authenticatedUser.id, | ||
}; | ||
|
||
await this.dispatcher.send(message); | ||
return; | ||
} | ||
|
||
const existingMemberships = await this.memberships.getMemberships( | ||
invitedUser.id, | ||
); | ||
if (isMemberOf(roomId, existingMemberships)) { | ||
const message: DraftMessage = { | ||
content: `${invitedUser.name} is already a member of this room`, | ||
roomId: room.id, | ||
authorId: 'system', | ||
recipientId: authenticatedUser.id, | ||
}; | ||
|
||
await this.dispatcher.send(message); | ||
return; | ||
} else if (!hasPendingRequestTo(roomId, existingMemberships)) { | ||
const message: DraftMessage = { | ||
content: `${invitedUser.name} does not have a pending request to join this room`, | ||
roomId: room.id, | ||
authorId: 'system', | ||
recipientId: authenticatedUser.id, | ||
}; | ||
|
||
await this.dispatcher.send(message); | ||
return; | ||
} | ||
|
||
await this.memberships.createMembership({ | ||
userId: invitedUser.id, | ||
roomId, | ||
status: MembershipStatus.Joined, | ||
}); | ||
|
||
const message: DraftMessage = { | ||
content: `${authenticatedUser.name} approved ${invitedUser.name} to join the room`, | ||
roomId: room.id, | ||
authorId: 'system', | ||
}; | ||
|
||
await this.dispatcher.send(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.