Skip to content

Commit

Permalink
Merge pull request #130 from jbrunton/extract-message-parser
Browse files Browse the repository at this point in the history
refactor: extract message parser
  • Loading branch information
jbrunton authored Aug 12, 2024
2 parents 08bafe9 + 647abac commit 69190d1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pulumi/domain/usecases/domain/get-domain-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("getDomainConfig", () => {
});
});

it("returns staging domain when the environment is production", () => {
it("returns staging domain when the environment is staging", () => {
const config = getDomainConfig({
environment: "staging",
stackName: "staging",
Expand Down
8 changes: 6 additions & 2 deletions services/api/src/app/messages/messages.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HttpException, Injectable } from '@nestjs/common';
import { CreateMessageDto } from './dto/create-message.dto';
import { isCommand, parseMessage } from './parse-message';
import { User, systemUser } from '@entities/user.entity';
import { SendMessageUseCase } from '@usecases/messages/send';
import { CommandService } from '@app/messages/command.service';
import { isCommand, parseMessage } from '@usecases/messages/parse-message';

@Injectable()
export class MessagesService {
Expand All @@ -16,7 +16,11 @@ export class MessagesService {
incoming: CreateMessageDto,
authenticatedUser: User,
): Promise<void> {
const message = parseMessage(incoming, authenticatedUser);
const message = parseMessage({
...incoming,
authorId: authenticatedUser.id,
});

try {
if (isCommand(message)) {
await this.command.exec(message, authenticatedUser);
Expand Down
36 changes: 36 additions & 0 deletions services/api/src/domain/usecases/messages/parse-message.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { parseMessage } from './parse-message';

describe('parseMessage', () => {
const roomId = 'room-id';
const authorId = 'user-id';

it('parses commands', () => {
expect(
parseMessage({ content: '/lorem 3 words', roomId, authorId }),
).toEqual({
canonicalInput: '/lorem 3 words',
roomId,
tokens: ['lorem', '3', 'words'],
});
});

it('ignores excess whitespace in commands', () => {
expect(
parseMessage({ content: '/lorem 3 words', roomId, authorId }),
).toEqual({
canonicalInput: '/lorem 3 words',
roomId,
tokens: ['lorem', '3', 'words'],
});
});

it('parses normal messages', () => {
expect(
parseMessage({ content: 'Hello, World!', roomId, authorId }),
).toEqual({
content: 'Hello, World!',
roomId,
authorId,
});
});
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { Command } from '@entities/command.entity';
import { DraftMessage } from '@entities/message.entity';
import { User } from '@entities/user.entity';
import { CreateMessageDto } from 'src/app/messages/dto/create-message.dto';

export type ParsedMessage = DraftMessage | Command;

export const isCommand = (message: ParsedMessage): message is Command => {
return (message as Command).tokens !== undefined;
};

export const parseMessage = (
{ content, roomId }: CreateMessageDto,
authenticatedUser: User,
): ParsedMessage => {
type ParseMessageParams = {
content: string;
roomId: string;
authorId: string;
};

export const parseMessage = ({
content,
roomId,
authorId,
}: ParseMessageParams): ParsedMessage => {
if (content.startsWith('/')) {
const tokens = content.slice(1).split(' ');
const tokens = content
.slice(1)
.split(' ')
.filter((token) => token.length > 0);

const canonicalInput = `/${tokens.join(' ')}`;

const command: Command = {
Expand All @@ -29,7 +38,7 @@ export const parseMessage = (
const message: ParsedMessage = {
content,
roomId,
authorId: authenticatedUser.id,
authorId,
};

return message;
Expand Down

0 comments on commit 69190d1

Please sign in to comment.