-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: redesign commands into command pattern with previous comman…
…ds implemented
- Loading branch information
1 parent
cfece54
commit 037a859
Showing
7 changed files
with
130 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { SendCodewarsLeaderboardToChannelInput } from "application/usecases/sendCodewarsLeaderboardToChannel/sendCodewarsLeaderboardToChannelInput"; | ||
import { Command } from "../../types"; | ||
import KataService from "../../domain/service/kataService/kataService"; | ||
import ChatService from "../../domain/service/chatService"; | ||
import KataLeaderboardUser from "../../domain/service/kataService/kataLeaderboardUser"; | ||
|
||
export default class CodewarsLeaderboardCommand implements Command { | ||
readonly name = "!cwl"; | ||
|
||
private chatService: ChatService; | ||
|
||
private kataService: KataService; | ||
|
||
constructor(chatService: ChatService, kataService: KataService) { | ||
this.chatService = chatService; | ||
this.kataService = kataService; | ||
} | ||
|
||
private formatLeaderboard(leaderboard: KataLeaderboardUser[]): string { | ||
let output = "```"; | ||
let position = 1; | ||
|
||
const leaderboardTotalEntriesToShow = 10; | ||
const leaderboardEntries = leaderboard.slice(0, leaderboardTotalEntriesToShow); | ||
const leaderboardEntriesLeft = leaderboard.length - leaderboardTotalEntriesToShow; | ||
|
||
leaderboardEntries.forEach((user: KataLeaderboardUser) => { | ||
const pointsCollection = user.getPoints().map((points: number) => points || 0); | ||
|
||
output += `${position}. ${user.getUsername()} - ${user.getScore()} - [${pointsCollection.join(",")}] points | ||
`; | ||
|
||
position += 1; | ||
}); | ||
|
||
output += "```"; | ||
|
||
if (leaderboardEntriesLeft > 1) { | ||
output += ` | ||
... e ${leaderboardEntriesLeft} outras participações em https://codewars.devpt.co`; | ||
} else if (leaderboardEntriesLeft === 1) { | ||
output += ` | ||
... e 1 outra participação em https://codewars.devpt.co`; | ||
} | ||
|
||
return output; | ||
} | ||
|
||
async execute({ channelId }: SendCodewarsLeaderboardToChannelInput): Promise<void> { | ||
const leaderboard = await this.kataService.getLeaderboard(); | ||
|
||
if (leaderboard.length === 0) { | ||
this.chatService.sendMessageToChannel("Ainda não existem participantes nesta ediçăo do desafio.", channelId); | ||
return; | ||
} | ||
|
||
const formattedLeaderboard = this.formatLeaderboard(leaderboard); | ||
this.chatService.sendMessageToChannel(formattedLeaderboard, channelId); | ||
} | ||
} |
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,19 @@ | ||
import { Command, Context } from "../../types"; | ||
import ChatService from "../../domain/service/chatService"; | ||
|
||
export default class DontAskToAskCommand implements Command { | ||
readonly name = "!ja"; | ||
|
||
private readonly message: string = | ||
"Olá! Experimenta fazer a pergunta diretamente e contar o que já tentaste! Sabe mais aqui :point_right: https://dontasktoask.com/pt-pt/"; | ||
|
||
private chatService: ChatService; | ||
|
||
constructor(chatService: ChatService) { | ||
this.chatService = chatService; | ||
} | ||
|
||
async execute(context: Context): Promise<void> { | ||
await this.chatService.sendMessageToChannel(this.message, context.channelId); | ||
} | ||
} |
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,19 @@ | ||
import { Command, Context } from "../../types"; | ||
import ChatService from "../../domain/service/chatService"; | ||
|
||
export default class OnlyCodeQuestionsCommand implements Command { | ||
readonly name = "!oc"; | ||
|
||
private chatService: ChatService; | ||
|
||
private readonly message: string = | ||
":warning: Este servidor é APENAS para questões relacionadas com programação! :warning:"; | ||
|
||
constructor(chatService: ChatService) { | ||
this.chatService = chatService; | ||
} | ||
|
||
async execute(context: Context): Promise<void> { | ||
await this.chatService.sendMessageToChannel(this.message, context.channelId); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,84 +1,44 @@ | ||
import { promises as fs } from "fs"; | ||
import path from "path"; | ||
import { Context } from "../../types"; | ||
import CodewarsLeaderboardCommand from "../../application/command/codewarsLeaderboardCommand"; | ||
import DontAskToAskCommand from "../../application/command/dontAskToAskCommand"; | ||
import OnlyCodeQuestionsCommand from "../../application/command/onlyCodeQuestionsCommand"; | ||
import { Command, Context } from "../../types"; | ||
import UseCaseNotFound from "../exception/useCaseNotFound"; | ||
import SendMessageToChannelUseCase from "../../application/usecases/sendMessageToChannel/sendMessageToChannelUseCase"; | ||
import MessageRepository from "../repository/messageRepository"; | ||
import ChatService from "./chatService"; | ||
import LoggerService from "./loggerService"; | ||
import ChannelResolver from "./channelResolver"; | ||
import KataService from "./kataService/kataService"; | ||
import SendCodewarsLeaderboardToChannelUseCase from "../../application/usecases/sendCodewarsLeaderboardToChannel/sendCodewarsLeaderboardToChannelUseCase"; | ||
import LoggerService from "./loggerService"; | ||
|
||
export default class CommandUseCaseResolver { | ||
private messageRepository: MessageRepository; | ||
|
||
private chatService: ChatService; | ||
private commands: Command[] = []; | ||
|
||
private loggerService: LoggerService; | ||
|
||
private channelResolver: ChannelResolver; | ||
|
||
private kataService: KataService; | ||
|
||
private commandMessages: Record<string, string> = {}; | ||
|
||
constructor({ | ||
messageRepository, | ||
chatService, | ||
loggerService, | ||
channelResolver, | ||
kataService, | ||
loggerService, | ||
}: { | ||
messageRepository: MessageRepository; | ||
chatService: ChatService; | ||
loggerService: LoggerService; | ||
channelResolver: ChannelResolver; | ||
kataService: KataService; | ||
loggerService: LoggerService; | ||
}) { | ||
this.messageRepository = messageRepository; | ||
this.chatService = chatService; | ||
this.loggerService = loggerService; | ||
this.channelResolver = channelResolver; | ||
this.kataService = kataService; | ||
} | ||
|
||
private async loadCommands(): Promise<void> { | ||
const filePath = path.join(__dirname, "commands.json"); | ||
const data = await fs.readFile(filePath, "utf-8"); | ||
this.commandMessages = JSON.parse(data); | ||
this.commands.push( | ||
new CodewarsLeaderboardCommand(chatService, kataService), | ||
new DontAskToAskCommand(chatService), | ||
new OnlyCodeQuestionsCommand(chatService) | ||
); | ||
} | ||
|
||
async resolveByCommand(command: string, context: Context): Promise<void> { | ||
this.loggerService.log(`Command received: "${command}"`); | ||
|
||
const deps = { | ||
messageRepository: this.messageRepository, | ||
chatService: this.chatService, | ||
loggerService: this.loggerService, | ||
channelResolver: this.channelResolver, | ||
kataService: this.kataService, | ||
}; | ||
|
||
if (Object.keys(this.commandMessages).length === 0) { | ||
await this.loadCommands(); | ||
} | ||
|
||
if (this.commandMessages[command]) { | ||
new SendMessageToChannelUseCase(deps).execute({ | ||
channelId: context.channelId, | ||
message: this.commandMessages[command], | ||
}); | ||
return; | ||
} | ||
const commandInstance = this.commands.find((cmd) => cmd.name === command); | ||
|
||
if (command === "!cwl") { | ||
new SendCodewarsLeaderboardToChannelUseCase(deps).execute({ | ||
channelId: context.channelId, | ||
}); | ||
return; | ||
if (!commandInstance) { | ||
throw new UseCaseNotFound().byCommand(command); | ||
} | ||
|
||
throw new UseCaseNotFound().byCommand(command); | ||
await commandInstance.execute(context); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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