Skip to content

Commit 93a15c5

Browse files
authored
fixed a bug with queues (#101)
1 parent 17b00bb commit 93a15c5

File tree

9 files changed

+102
-40
lines changed

9 files changed

+102
-40
lines changed

services/bot/src/commands/base.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ export abstract class CommandBase {
134134
return 'There are no projects registered.';
135135
}
136136
}
137+
/**
138+
* Returns the default queue or the queue identified for the given initiative context.
139+
*
140+
* @param initiative - This is the initiative for which to get the named queue.
141+
* @param project - This is the project for which to get the queue on.
142+
* @todo Throw errors instead of returning error string.
143+
* @returns Queue model or error string for the given initiative context.
144+
*/
145+
public static async getQueue (initiative: IInitiative, project: IProject): Promise<IQueue | string> {
146+
const queueName = initiative.data.queue?.toUpperCase() || project.currentQueue;
147+
const queueObject = project.queues.filter(i => i.name === queueName);
148+
if (queueObject.length > 0) {
149+
return queueObject[0];
150+
} else {
151+
return `A queue with name "${initiative.data.queue?.toUpperCase()}" does not exist.`;
152+
}
153+
}
137154

138155
/**
139156
* Adds the invoking user to the database.
@@ -179,28 +196,25 @@ export abstract class CommandBase {
179196
*
180197
* @access public
181198
* @static
182-
* @param queues - A list of all queues to search through.
183199
* @param queue - The name of the queue to which to add the user.
184200
* @param user - The user to add to the queue.
185201
* @returns The queue that was updated.
186202
*/
187-
public static addToQueue (queues: IQueue[], queue: string, user: IPerson): IQueue {
188-
LOGGER.verbose(`Queue Name: ${queue}`);
189-
LOGGER.verbose(`Queues: ${queues}`);
190-
const queueObject = queues.filter(i => i.name === queue.toUpperCase())[0];
203+
public static addToQueue (queue: IQueue, user: IPerson): IQueue {
204+
LOGGER.verbose(`Queue Name: ${queue.name}`);
191205
const now = new Date();
192-
const atHeadTime = queueObject.members.length === 0 ? now : null;
193-
queueObject.members.push({
206+
const atHeadTime = queue.members.length === 0 ? now : null;
207+
queue.members.push({
194208
person: user,
195209
enqueuedAt: now,
196210
atHeadTime: atHeadTime
197211
});
198-
queueObject.history.push({
199-
name: queueObject.name,
200-
members: queueObject.members,
212+
queue.history.push({
213+
name: queue.name,
214+
members: queue.members,
201215
time: new Date()
202216
});
203-
return queueObject;
217+
return queue;
204218
}
205219

206220
/**
@@ -232,31 +246,29 @@ export abstract class CommandBase {
232246
* @access public
233247
* @static
234248
* @async
235-
* @param queues - A list of all queues to search through.
236249
* @param queue - The name of the queue to which to remove the user.
237250
* @param user - The user to remove from the queue.
238251
* @param user.id - The user ID for the user to remove from the queue.
239252
* @param user.displayName - The display name for the user to remove from the queue.
240253
* @returns The queue that was updated.
241254
*/
242-
public static async removeFromQueue (queues: IQueue[], queue: string, user: IRemoveFromQueueUser): Promise<IQueue | string> {
243-
const queueObject = queues.filter(i => i.name === queue)[0];
244-
const idx = queueObject.members.findIndex(p => p.person.displayName === user.displayName);
255+
public static async removeFromQueue (queue: IQueue, user: IRemoveFromQueueUser): Promise<IQueue | string> {
256+
const idx = queue.members.findIndex(p => p.person.displayName === user.displayName);
245257
if (idx === -1) {
246-
return `User "${user.displayName}" was not found in queue "${queueObject.name}"`;
258+
return `User "${user.displayName}" was not found in queue "${queue.name}"`;
247259
} else {
248-
const removed = queueObject.members.splice(idx, 1)[0];
249-
queueObject.history.push({
250-
name: queueObject.name,
251-
members: queueObject.members,
260+
const removed = queue.members.splice(idx, 1)[0];
261+
queue.history.push({
262+
name: queue.name,
263+
members: queue.members,
252264
time: new Date()
253265
});
254266
const now = new Date().getTime();
255267

256268
// If we removed the person at the head of the queue and
257269
// there are more people in the queue
258-
if (queueObject.members.length > 0 && idx === 0) {
259-
const head = queueObject.members[0];
270+
if (queue.members.length > 0 && idx === 0) {
271+
const head = queue.members[0];
260272
head.atHeadTime = new Date();
261273
}
262274
// Update person object
@@ -283,7 +295,7 @@ export abstract class CommandBase {
283295
LOGGER.verbose(await PERSON_MODEL.find({ id: removed.person.id }).exec());
284296

285297
await PERSON_MODEL.updateOne({ id: removed.person.id }, updateData).exec();
286-
return queueObject;
298+
return queue;
287299
}
288300
}
289301

services/bot/src/commands/queue/addMe.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,20 @@ export class AddMe extends CommandBase implements ICommand {
2727
const project = await CommandBase.getProject(initiative);
2828
if (typeof project === 'string') return String(project);
2929

30+
// Get queue
31+
let queue = await CommandBase.getQueue(initiative, project);
32+
if (typeof queue === 'string') return String(queue);
33+
3034
// Create user if they don't exist
3135
const user = await CommandBase.addUser(initiative.user.id, initiative.user.displayName);
3236

33-
// Get queue
34-
const queueName = initiative.data.queue?.toUpperCase() || project.currentQueue;
35-
3637
// Add to end of queue
37-
const queue = CommandBase.addToQueue(project.queues, queueName, user);
38+
queue = CommandBase.addToQueue(queue, user);
3839

3940
// Save the project
4041
await project.save();
4142

4243
// Return response
43-
return `Successfully added "${user.displayName}" to queue "${queueName}".\n\n${CommandBase.queueToString(queue)}`;
44+
return `Successfully added "${user.displayName}" to queue "${queue.name}".\n\n${CommandBase.queueToString(queue)}`;
4445
}
4546
}

services/bot/src/commands/queue/addPerson.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class AddPerson extends CommandBase implements ICommand {
3232
const project = await CommandBase.getProject(initiative) as ProjectDocument;
3333
if (typeof project === 'string') return String(project);
3434

35+
// Get queue
36+
let queue = await CommandBase.getQueue(initiative, project);
37+
if (typeof queue === 'string') return String(queue);
38+
3539
if (initiative.mentions.length > 1) {
3640
return 'You cannot add more than one person at once';
3741
} else if (initiative.mentions.length == 0) {
@@ -43,16 +47,13 @@ export class AddPerson extends CommandBase implements ICommand {
4347
// Create user if they don't exist
4448
const user = await CommandBase.addUser(webexId, webexPerson.displayName);
4549

46-
// Get queue
47-
const queueName = initiative.data.queue?.toUpperCase() || project.currentQueue;
48-
4950
// Add to end of queue
50-
const queue = CommandBase.addToQueue(project.queues, queueName, user);
51+
queue = CommandBase.addToQueue(queue, user);
5152

5253
// Save the project
5354
await project.save();
5455

5556
// Return response
56-
return `Successfully added "${user.displayName}" to queue "${queueName}".\n\n${CommandBase.queueToString(queue)}`;
57+
return `Successfully added "${user.displayName}" to queue "${queue.name}".\n\n${CommandBase.queueToString(queue)}`;
5758
}
5859
}

services/bot/src/commands/queue/removeMe.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export class RemoveMe extends CommandBase implements ICommand {
2828
const project = await CommandBase.getProject(initiative);
2929
if (typeof project === 'string') return String(project);
3030

31-
// Get queue
32-
const queueName = initiative.data.queue?.toUpperCase() || project.currentQueue;
31+
let queue = await CommandBase.getQueue(initiative, project);
32+
if (typeof queue === 'string') return String(queue);
3333

3434
// remove only the first instance
35-
const queue = await CommandBase.removeFromQueue(project.queues, queueName, { id: initiative.user.id, displayName: initiative.user.displayName });
35+
queue = await CommandBase.removeFromQueue(queue, { id: initiative.user.id, displayName: initiative.user.displayName });
3636
if (typeof queue === 'string') return String(queue);
3737

3838
// Save the project
@@ -49,6 +49,6 @@ export class RemoveMe extends CommandBase implements ICommand {
4949
tag += ', you\'re at the front of the queue!';
5050
}
5151
// Return response
52-
return `Successfully removed "${initiative.user.displayName}" from queue "${queueName}".\n\n${CommandBase.queueToString(queue)}${tag ? '\n\n' + tag : ''}`;
52+
return `Successfully removed "${initiative.user.displayName}" from queue "${queue.name}".\n\n${CommandBase.queueToString(queue)}${tag ? '\n\n' + tag : ''}`;
5353
}
5454
}

services/bot/src/commands/queue/removePerson.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export class RemovePerson extends CommandBase implements ICommand {
3333
if (typeof project === 'string') return String(project);
3434

3535
// Get queue
36-
const queueName = initiative.data.queue?.toUpperCase() || project.currentQueue;
36+
let queue = await CommandBase.getQueue(initiative, project);
37+
if (typeof queue === 'string') return String(queue);
3738

3839
if (initiative.mentions.length > 1) {
3940
return 'You cannot remove more than one person at once';
@@ -44,7 +45,7 @@ export class RemovePerson extends CommandBase implements ICommand {
4445
const webexPerson = await BOT.people.get(webexId);
4546

4647
// remove only the first instance
47-
const queue = await CommandBase.removeFromQueue(project.queues, queueName, { id: webexId, displayName: webexPerson.displayName });
48+
queue = await CommandBase.removeFromQueue(queue, { id: webexId, displayName: webexPerson.displayName });
4849
if (typeof queue === 'string') return String(queue);
4950

5051
// Save the project
@@ -61,6 +62,6 @@ export class RemovePerson extends CommandBase implements ICommand {
6162
tag += ', you\'re at the front of the queue!';
6263
}
6364
// Return response
64-
return `Successfully removed "${webexPerson.displayName}" from queue "${queueName}".\n\n${CommandBase.queueToString(queue)}${tag ? '\n\n' + tag : ''}`;
65+
return `Successfully removed "${webexPerson.displayName}" from queue "${queue.name}".\n\n${CommandBase.queueToString(queue)}${tag ? '\n\n' + tag : ''}`;
6566
}
6667
}

services/bot/tests/commands/queue/addMe.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ describe('Adding me to a queue errors when it should', () => {
2828
expect(await new AddMe().relax({ ...TEST_INITIATIVE, user: SUPER_ADMIN })).toEqual('There are no projects registered.');
2929
expect(await PROJECT_MODEL.find({}).exec()).toHaveLength(0);
3030
});
31+
test('errors when non-default queue is specified that does not exist', async () => {
32+
const project = await CREATE_PROJECT();
33+
const queue = project.queues.filter(i => i.name === project.currentQueue)[0];
34+
expect(queue.members).toHaveLength(0);
35+
expect(await new AddMe().relax({ ...TEST_INITIATIVE, data: { queue: 'FOO' } })).toEqual(
36+
'A queue with name "FOO" does not exist.'
37+
);
38+
expect(queue.members).toHaveLength(0);
39+
expect(await PERSON_MODEL.find({ id: STANDARD_USER.id }).exec()).toHaveLength(0);
40+
});
3141
});
3242

3343
describe('Adding me to a queue works appropriately', () => {

services/bot/tests/commands/queue/addPerson.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ describe('Adding a person to a queue errors when it should', () => {
4646
);
4747
expect(await PERSON_MODEL.find({ id: USER_ID }).exec()).toHaveLength(0);
4848
});
49+
test('errors when non-default queue is specified that does not exist', async () => {
50+
const project = await CREATE_PROJECT();
51+
const queue = project.queues.filter(i => i.name === project.currentQueue)[0];
52+
expect(queue.members).toHaveLength(0);
53+
expect(await new AddPerson().relax({
54+
...TEST_INITIATIVE,
55+
data: { queue: 'FOO' },
56+
user: PROJECT_ADMIN,
57+
mentions: ['1']
58+
})).toEqual('A queue with name "FOO" does not exist.');
59+
expect(queue.members).toHaveLength(0);
60+
expect(await PERSON_MODEL.find({ id: STANDARD_USER.id }).exec()).toHaveLength(0);
61+
});
4962
});
5063

5164
describe('Adding a person to a queue works appropriately', () => {

services/bot/tests/commands/queue/removeMe.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ describe('Removing me from a queue works appropriately', () => {
4040
expect(await PERSON_MODEL.find({ id: STANDARD_USER.id }).exec()).toHaveLength(0);
4141
});
4242

43+
test('errors when non-default queue is specified that does not exist', async () => {
44+
const project = await CREATE_PROJECT();
45+
const queue = project.queues.filter(i => i.name === project.currentQueue)[0];
46+
expect(queue.members).toHaveLength(0);
47+
expect(await new RemoveMe().relax({ ...TEST_INITIATIVE, data: { queue: 'FOO' } })).toEqual(
48+
'A queue with name "FOO" does not exist.'
49+
);
50+
expect(queue.members).toHaveLength(0);
51+
expect(await PERSON_MODEL.find({ id: STANDARD_USER.id }).exec()).toHaveLength(0);
52+
});
53+
4354
test('errors when there is someone in the queue but its not the user', async () => {
4455
const project = await CREATE_PROJECT();
4556
const queue = project.queues.filter(i => i.name === project.currentQueue)[0];

services/bot/tests/commands/queue/removePerson.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ describe('Removing a person from a queue errors when it should', () => {
6868
project = (await PROJECT_MODEL.find({ name: project.name }).exec())[0];
6969
expect(project.queues.filter(i => i.name === project.currentQueue)[0].members).toHaveLength(0);
7070
});
71+
test('errors when non-default queue is specified that does not exist', async () => {
72+
const project = await CREATE_PROJECT();
73+
const queue = project.queues.filter(i => i.name === project.currentQueue)[0];
74+
expect(queue.members).toHaveLength(0);
75+
expect(await new RemovePerson().relax({
76+
...TEST_INITIATIVE,
77+
data: { queue: 'FOO' },
78+
user: PROJECT_ADMIN,
79+
mentions: ['1', '2']
80+
})).toEqual('A queue with name "FOO" does not exist.');
81+
expect(queue.members).toHaveLength(0);
82+
expect(await PERSON_MODEL.find({ id: STANDARD_USER.id }).exec()).toHaveLength(0);
83+
});
7184
});
7285

7386
describe('Removing a person from a queue works appropriately', () => {

0 commit comments

Comments
 (0)