Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddPeople DeletePeople #46

Merged
merged 7 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/commands/addProjectScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
}

// Save the project name and ask for the next piece of information
ctx.scene.session.project = new Project('', 0, '', '', [[]], []);
const project = ctx.scene.session.project;
project.setName(text);
project.setUserId(ctx.from.id);
ctx.scene.session.project = Project.createBlankProject(
text,
ctx.from.id,
);

Check warning on line 59 in src/commands/addProjectScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/addProjectScene.ts#L56-L59

Added lines #L56 - L59 were not covered by tests
await ctx.reply(
`Project name saved. Please enter a short description for your project.`,
);
Expand Down Expand Up @@ -123,14 +123,9 @@
// Save the project description and ask for the next piece of information
debug(`Valid project members' inputs: ${text}`);
const project = ctx.scene.session.project;
project.setPersons(text);
createProject(
project.getUserid(),
project.getName(),
project.getDescription(),
project.getPersons(),
project.getAdjMatrix(),
);
const personArr = text.split(',');
project.setPersons(personArr);
createProject(project);

Check warning on line 128 in src/commands/addProjectScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/addProjectScene.ts#L126-L128

Added lines #L126 - L128 were not covered by tests
await ctx.reply(`Project members saved. Exiting scene now.`);
return ctx.scene.enter('mainMenu', ctx.scene.session);
} catch (error) {
Expand Down
70 changes: 70 additions & 0 deletions src/commands/editProject/addPeopleScene.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import createDebug from 'debug';

import { Scenes } from 'telegraf';

import {
UnknownError,
InvalidInputTypeError,
InvalidTextError,
} from '../../exceptions';

import { updateProject } from '../../db/functions';

import { BotContext, updateSessionDataBetweenScenes } from '../../BotContext';
import { parsePeopleListString } from '../../util/userInput';

const debug = createDebug('bot:edit_project_description_command');

const addPeople = async (ctx: BotContext) => {
debug('Entered addPeople scene.');
updateSessionDataBetweenScenes(ctx);
await ctx.reply(
`Please enter the project members' names, delimited by commas and no spaces. To cancel, type "Back".`,
);
return ctx.wizard.next();
};

const askForProjectMembers = async (ctx: BotContext) => {
try {
if (!ctx.message) {
throw new UnknownError(
'An unknown error occurred. Please try again later.',
);
}
if (!('text' in ctx.message)) {
throw new InvalidInputTypeError(
'Invalid input type. Please enter a text message.',
);
}
const text = ctx.message.text;
if (!text) {
throw new InvalidTextError(
'Please enter a valid string representing group members, delimited by commas and no spaces.',
);
}
if (ctx.message?.text === 'Back') {
debug('User selected "Back"');
return ctx.scene.enter('manageProject', ctx.scene.session);
}
debug(`Project members' inputs: ${text}`);
const project = ctx.scene.session.project;
const personArr = parsePeopleListString(text);
project.addPeople(personArr);
updateProject(project);
await ctx.reply(`Project members saved. Exiting scene now.`);
return ctx.scene.enter('manageProject', ctx.scene.session);
} catch (error) {
const errorMessage = (error as Error).message;
debug(errorMessage);
await ctx.reply(errorMessage);
return ctx.scene.reenter();
}
};

const addPeopleScene = new Scenes.WizardScene(
'addPeople',
addPeople,
askForProjectMembers,
);

export { addPeopleScene };

Check warning on line 70 in src/commands/editProject/addPeopleScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/editProject/addPeopleScene.ts#L2-L70

Added lines #L2 - L70 were not covered by tests
71 changes: 71 additions & 0 deletions src/commands/editProject/deletePeopleScene.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import createDebug from 'debug';

import { Scenes } from 'telegraf';

import {
UnknownError,
InvalidInputTypeError,
InvalidTextError,
} from '../../exceptions';

import { updateProject } from '../../db/functions';

import { BotContext, updateSessionDataBetweenScenes } from '../../BotContext';
import { parsePeopleListString } from '../../util/userInput';

const debug = createDebug('bot:edit_project_description_command');

const deletePeople = async (ctx: BotContext) => {
debug('Entered deletePeople scene.');
updateSessionDataBetweenScenes(ctx);
await ctx.reply(
`Please enter the project members' names, delimited by commas and no spaces. To cancel, type "Back".`,
);
return ctx.wizard.next();
};

const askForProjectMembers = async (ctx: BotContext) => {
try {
if (!ctx.message) {
throw new UnknownError(
'An unknown error occurred. Please try again later.',
);
}
if (!('text' in ctx.message)) {
throw new InvalidInputTypeError(
'Invalid input type. Please enter a text message.',
);
}
const text = ctx.message.text;
if (!text) {
throw new InvalidTextError(
'Please enter a valid string representing group members, delimited by commas and no spaces.',
);
}
if (ctx.message?.text === 'Back') {
debug('User selected "Back"');
return ctx.scene.enter('manageProject', ctx.scene.session);
}
debug(`Project members' inputs: ${text}`);
const project = ctx.scene.session.project;
const personArr = parsePeopleListString(text);
debug(`Removing ${personArr.length} project members: ${personArr}`);
project.removePersons(personArr);
updateProject(project);
await ctx.reply(`Project members removed. Exiting scene now.`);
return ctx.scene.enter('manageProject', ctx.scene.session);
} catch (error) {
const errorMessage = (error as Error).message;
debug(errorMessage);
await ctx.reply(errorMessage);
return ctx.scene.reenter();
}
};

const deletePeopleScene = new Scenes.WizardScene(
'deletePeople',
deletePeople,
askForProjectMembers,
);

export { deletePeopleScene };

Check warning on line 71 in src/commands/editProject/deletePeopleScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/editProject/deletePeopleScene.ts#L2-L71

Added lines #L2 - L71 were not covered by tests
8 changes: 4 additions & 4 deletions src/commands/editProject/editProjectDescriptionScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { UnknownError, InvalidInputTypeError } from '../../exceptions';

import { saveProject } from '../../db/functions';
import { updateProject } from '../../db/functions';

Check warning on line 7 in src/commands/editProject/editProjectDescriptionScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/editProject/editProjectDescriptionScene.ts#L7

Added line #L7 was not covered by tests

import { BotContext, updateSessionDataBetweenScenes } from '../../BotContext';

Expand Down Expand Up @@ -39,9 +39,9 @@
}

ctx.scene.session.project.setDescription(ctx.message.text);
saveProject(ctx.scene.session.project);
await ctx.reply(`Project description updated. Returning to menu.`);
return ctx.scene.enter('mainMenu', ctx.scene.session);
updateProject(ctx.scene.session.project);
await ctx.reply(`Project description updated.`);
return ctx.scene.enter('editProject', ctx.scene.session);

Check warning on line 44 in src/commands/editProject/editProjectDescriptionScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/editProject/editProjectDescriptionScene.ts#L42-L44

Added lines #L42 - L44 were not covered by tests
} catch (error) {
const errorMessage = (error as Error).message;
debug(errorMessage);
Expand Down
8 changes: 4 additions & 4 deletions src/commands/editProject/editProjectNameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
} from '../../exceptions';

import { BotContext, updateSessionDataBetweenScenes } from '../../BotContext';
import { saveProject } from '../../db/functions';
import { updateProject } from '../../db/functions';

Check warning on line 12 in src/commands/editProject/editProjectNameScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/editProject/editProjectNameScene.ts#L12

Added line #L12 was not covered by tests

const debug = createDebug('bot:edit_project_name_command');

Expand Down Expand Up @@ -64,9 +64,9 @@
}

ctx.scene.session.project.setName(ctx.message.text);
saveProject(ctx.scene.session.project);
await ctx.reply(`Project name updated. Returning to menu.`);
return ctx.scene.enter('mainMenu', ctx.scene.session);
updateProject(ctx.scene.session.project);
await ctx.reply(`Project name updated.`);
return ctx.scene.enter('editProject', ctx.scene.session);

Check warning on line 69 in src/commands/editProject/editProjectNameScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/editProject/editProjectNameScene.ts#L67-L69

Added lines #L67 - L69 were not covered by tests
} catch (error) {
const errorMessage = (error as Error).message;
debug(errorMessage);
Expand Down
20 changes: 7 additions & 13 deletions src/commands/editProject/editProjectScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
await ctx.reply(
`What do you want to edit?`,
Markup.keyboard([
['Add Person', 'Delete Person'],
['Add People', 'Delete People'],

Check warning on line 22 in src/commands/editProject/editProjectScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/editProject/editProjectScene.ts#L22

Added line #L22 was not covered by tests
['Edit Project Name', 'Edit Project Description'],
['Back'],
]).resize(),
Expand All @@ -40,18 +40,12 @@
'Invalid input type. Please enter a text message.',
);
}
if (ctx.message?.text === 'Add Person') {
debug('User selected "Add Person"');
// TODO: add person scene
// return ctx.scene.enter('addPerson', ctx.scene.session);
await ctx.reply('This feature is not yet implemented.');
return ctx.scene.enter('mainMenu');
} else if (ctx.message?.text === 'Delete Person') {
debug('User selected "Delete Person"');
// TODO: delete person scene
// return ctx.scene.enter('deletePerson', ctx.scene.session);
await ctx.reply('This feature is not yet implemented.');
return ctx.scene.enter('mainMenu');
if (ctx.message?.text === 'Add People') {
debug('User selected "Add People"');
return ctx.scene.enter('addPeople', ctx.scene.session);
} else if (ctx.message?.text === 'Delete People') {
debug('User selected "Delete People"');
return ctx.scene.enter('deletePeople', ctx.scene.session);

Check warning on line 48 in src/commands/editProject/editProjectScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/editProject/editProjectScene.ts#L43-L48

Added lines #L43 - L48 were not covered by tests
} else if (ctx.message?.text === 'Edit Project Name') {
debug('User selected "Edit Project Name"');
return ctx.scene.enter('editProjectName', ctx.scene.session);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/generateGroupingsScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { BotContext, updateSessionDataBetweenScenes } from '../BotContext';

import { saveProject } from '../db/functions';
import { updateProject } from '../db/functions';

Check warning on line 11 in src/commands/generateGroupingsScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/generateGroupingsScene.ts#L11

Added line #L11 was not covered by tests

const debug = createDebug('bot:generate_groupings_command');

Expand Down Expand Up @@ -55,7 +55,7 @@
const logic = new AlgorithmRunner(ctx.scene.session.project, numGroups);
const groupings = logic.prettyPrintGroupings();
logic.updateInteractionsBasedOnGeneratedGroupings();
saveProject(ctx.scene.session.project);
updateProject(ctx.scene.session.project);

Check warning on line 58 in src/commands/generateGroupingsScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/generateGroupingsScene.ts#L58

Added line #L58 was not covered by tests
const out = `Here are the groupings:\n${groupings}.
Do not delete this message as you will need it to view the groupings again.`;
await ctx.reply(out);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/resetInteractionsScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { BotContext, updateSessionDataBetweenScenes } from '../BotContext';

import { saveProject } from '../db/functions';
import { updateProject } from '../db/functions';

Check warning on line 9 in src/commands/resetInteractionsScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/resetInteractionsScene.ts#L9

Added line #L9 was not covered by tests

const debug = createDebug('bot:reset_interactions_command');

Expand Down Expand Up @@ -44,7 +44,7 @@

if (ctx.message?.text === 'Yes') {
ctx.scene.session.project.resetInteractions();
saveProject(ctx.scene.session.project);
updateProject(ctx.scene.session.project);

Check warning on line 47 in src/commands/resetInteractionsScene.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/resetInteractionsScene.ts#L47

Added line #L47 was not covered by tests
await ctx.reply('Interactions have been reset.');
} else if (ctx.message?.text === 'No') {
await ctx.reply('Interactions have not been reset.');
Expand Down
Loading