forked from CarelessLlama/Blue-Elephant
-
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
14 changed files
with
275 additions
and
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,58 @@ | ||
import createDebug from 'debug'; | ||
|
||
import { Markup } from 'telegraf'; | ||
import { Markup, MiddlewareFn } from 'telegraf'; | ||
|
||
import { deleteProjectInDb } from '../db/functions'; | ||
|
||
import { InvalidTextError } from '../exceptions'; | ||
|
||
import { BotContext, updateSessionDataBetweenScenes } from '../BotContext'; | ||
import { makeSceneWithErrorHandling } from '../util/scene'; | ||
import { getProject, getResponse } from '../util/botContext'; | ||
import { | ||
askAndHandleMenuFactory, | ||
goNextStep, | ||
makeSceneWithErrorHandling, | ||
returnToPreviousMenuFactory, | ||
} from '../util/scene'; | ||
import { getProject } from '../util/botContext'; | ||
|
||
const debug = createDebug('bot:delete_project_command'); | ||
const previousMenu = 'manageProject'; | ||
|
||
/** | ||
* Deletes a project from the database. | ||
* @returns A middleware function that handles the deletion of a project. | ||
*/ | ||
const askForDeleteConfirmation = async (ctx: BotContext) => { | ||
const deleteProject = async (ctx: BotContext, next: () => Promise<void>) => { | ||
updateSessionDataBetweenScenes(ctx); | ||
debug(`Entering deleteProject scene.`); | ||
await ctx.reply( | ||
`You have selected to delete the project. Are you sure?`, | ||
Markup.keyboard(['Yes', 'No']).resize(), | ||
); | ||
return ctx.wizard.next(); | ||
return goNextStep(ctx, next); | ||
}; | ||
|
||
const handleDeleteProject = async (ctx: BotContext) => { | ||
const text = getResponse(ctx); | ||
if (text === 'Yes') { | ||
debug(`User selected to delete the project.`); | ||
const project = getProject(ctx); | ||
await deleteProjectInDb(project.getId()); | ||
await ctx.reply(`Project deleted.`); | ||
return ctx.scene.enter('mainMenu'); | ||
} else if (text === 'No') { | ||
debug(`User selected not to delete the project.`); | ||
await ctx.reply(`Project not deleted. Returning to the main menu.`); | ||
return ctx.scene.enter('mainMenu'); | ||
} else { | ||
throw new InvalidTextError('Please select either Yes or No.'); | ||
} | ||
}; | ||
const question = `Are you sure you want to delete the project?`; | ||
const map = new Map<string, MiddlewareFn<BotContext>>([ | ||
[ | ||
'Yes', | ||
async (ctx) => { | ||
const project = getProject(ctx); | ||
await deleteProjectInDb(project.getId()); | ||
await ctx.reply(`Project deleted.`); | ||
return ctx.scene.enter('mainMenu', Markup.removeKeyboard()); | ||
}, | ||
], | ||
['No', returnToPreviousMenuFactory(previousMenu)], | ||
]); | ||
|
||
const [askForMenuChoice, handleMenuChoice] = askAndHandleMenuFactory( | ||
debug, | ||
undefined, | ||
question, | ||
map, | ||
); | ||
|
||
const deleteProjectScene = makeSceneWithErrorHandling( | ||
'deleteProject', | ||
debug, | ||
askForDeleteConfirmation, | ||
handleDeleteProject, | ||
deleteProject, | ||
askForMenuChoice, | ||
handleMenuChoice, | ||
); | ||
|
||
export { deleteProjectScene }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,52 @@ | ||
import createDebug from 'debug'; | ||
|
||
import { Markup } from 'telegraf'; | ||
import { MiddlewareFn } from 'telegraf'; | ||
|
||
import { BotContext, updateSessionDataBetweenScenes } from '../../BotContext'; | ||
import { makeSceneWithErrorHandling } from '../../util/scene'; | ||
import { getResponse } from '../../util/botContext'; | ||
import { isBackCommand } from '../../util/userInput'; | ||
import { | ||
askAndHandleMenuFactory, | ||
goNextStep, | ||
goToScene, | ||
makeSceneWithErrorHandling, | ||
} from '../../util/scene'; | ||
|
||
const debug = createDebug('bot:edit_project_command'); | ||
const previousMenu = 'manageProject'; | ||
|
||
/** | ||
* Edits an existing project in the database. | ||
* @returns A middleware function that handles the editing of a project. | ||
*/ | ||
const editProject = async (ctx: BotContext) => { | ||
const editProject = async (ctx: BotContext, next: () => Promise<void>) => { | ||
debug('Entered editProject scene.'); | ||
updateSessionDataBetweenScenes(ctx); | ||
// Add project edition logic here | ||
await ctx.reply( | ||
`What do you want to edit?`, | ||
Markup.keyboard([ | ||
['Add People', 'Delete People'], | ||
['Edit Project Name', 'Edit Project Description'], | ||
['Back'], | ||
]).resize(), | ||
); | ||
return ctx.wizard.next(); | ||
return goNextStep(ctx, next); | ||
}; | ||
|
||
const handleEditProjectOption = async (ctx: BotContext) => { | ||
const text = getResponse(ctx); | ||
if (isBackCommand(text)) { | ||
debug('User selected "Back"'); | ||
return ctx.scene.enter('manageProject', ctx.scene.session); | ||
} | ||
switch (text) { | ||
case 'Add People': { | ||
debug('User selected "Add People"'); | ||
return ctx.scene.enter('addPeople', ctx.scene.session); | ||
} | ||
case 'Delete People': { | ||
debug('User selected "Delete People"'); | ||
return ctx.scene.enter('deletePeople', ctx.scene.session); | ||
} | ||
case 'Edit Project Name': { | ||
debug('User selected "Edit Project Name"'); | ||
return ctx.scene.enter('editProjectName', ctx.scene.session); | ||
} | ||
case 'Edit Project Description': { | ||
debug('User selected "Edit Project Description"'); | ||
return ctx.scene.enter('editProjectDescription', ctx.scene.session); | ||
} | ||
default: { | ||
await ctx.reply( | ||
'Invalid option. Please select a valid option from the keyboard.', | ||
); | ||
return ctx.wizard.back(); | ||
} | ||
} | ||
}; | ||
const question = 'What do you want to edit?'; | ||
const mapOptionToScene = new Map<string, MiddlewareFn<BotContext>>([ | ||
['Add People', async (ctx) => goToScene('addPeople', ctx)], | ||
['Delete People', async (ctx) => goToScene('deletePeople', ctx)], | ||
['Edit Project Name', async (ctx) => goToScene('editProjectName', ctx)], | ||
[ | ||
'Edit Project Description', | ||
async (ctx) => goToScene('editProjectDescription', ctx), | ||
], | ||
]); | ||
|
||
const [askForMenuChoice, handleMenuChoice] = askAndHandleMenuFactory( | ||
debug, | ||
previousMenu, | ||
question, | ||
mapOptionToScene, | ||
); | ||
|
||
const editProjectScene = makeSceneWithErrorHandling( | ||
'editProject', | ||
debug, | ||
editProject, | ||
handleEditProjectOption, | ||
askForMenuChoice, | ||
handleMenuChoice, | ||
); | ||
|
||
export { editProjectScene }; |
Oops, something went wrong.