-
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
11 changed files
with
127 additions
and
14 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
24 changes: 24 additions & 0 deletions
24
services/api/src/domain/usecases/commands/parse/parsers/change-room-join-policy-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,24 @@ | ||
import { z } from 'zod'; | ||
import { CommandParser, ParsedCommand } from '../command.parser'; | ||
import { JoinPolicy } from '@entities/room.entity'; | ||
|
||
const schema = z | ||
.tuple([ | ||
z.literal('set'), | ||
z.literal('room'), | ||
z.literal('join'), | ||
z.literal('policy'), | ||
z.enum([JoinPolicy.Anyone, JoinPolicy.Invite]), | ||
]) | ||
.rest(z.string()) | ||
.transform<ParsedCommand>(([, , , , joinPolicy]) => ({ | ||
tag: 'changeRoomJoinPolicy', | ||
params: { newJoinPolicy: joinPolicy }, | ||
})); | ||
|
||
export const changeRoomJoinPolicyParser = new CommandParser({ | ||
matchTokens: ['set', 'room', 'join', 'policy'], | ||
schema, | ||
signature: `/set room join policy {'anyone', 'invite'}`, | ||
summary: 'set the room join policy', | ||
}); |
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
57 changes: 57 additions & 0 deletions
57
services/api/src/domain/usecases/rooms/change-room-join-policy.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,57 @@ | ||
import { MembershipsRepository } from '@entities/memberships.repository'; | ||
import { Dispatcher, DraftMessage, UpdatedEntity } from '@entities/messages'; | ||
import { JoinPolicy } from '@entities/room.entity'; | ||
import { RoomsRepository } from '@entities/rooms.repository'; | ||
import { User } from '@entities/users'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthService, Role } from '@usecases/auth.service'; | ||
|
||
export type ChangeRoomJoinPolicyParams = { | ||
roomId: string; | ||
authenticatedUser: User; | ||
newJoinPolicy: JoinPolicy; | ||
}; | ||
|
||
@Injectable() | ||
export class ChangeRoomJoinPolicyUseCase { | ||
constructor( | ||
private readonly rooms: RoomsRepository, | ||
private readonly memberships: MembershipsRepository, | ||
private readonly authService: AuthService, | ||
private readonly dispatcher: Dispatcher, | ||
) {} | ||
|
||
async exec({ | ||
roomId, | ||
authenticatedUser, | ||
newJoinPolicy, | ||
}: ChangeRoomJoinPolicyParams): Promise<void> { | ||
const room = await this.rooms.getRoom(roomId); | ||
|
||
await this.authService.authorize({ | ||
user: authenticatedUser, | ||
subject: room, | ||
action: Role.Manage, | ||
}); | ||
|
||
await this.rooms.updateRoom({ | ||
id: roomId, | ||
joinPolicy: newJoinPolicy, | ||
}); | ||
|
||
const message: DraftMessage = { | ||
content: `Room join policy updated to ${newJoinPolicy}`, | ||
roomId: room.id, | ||
authorId: 'system', | ||
updatedEntities: [UpdatedEntity.Room], | ||
}; | ||
|
||
await this.dispatcher.send(message); | ||
} | ||
} | ||
|
||
const titleCase = (s: string): string => { | ||
const titleCaseWord = (word: string) => | ||
word[0].toUpperCase() + word.substring(1); | ||
return s.split(' ').map(titleCaseWord).join(' '); | ||
}; |
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