Skip to content

Commit b778640

Browse files
Feedback from Adam
1 parent da0f7f2 commit b778640

2 files changed

Lines changed: 18 additions & 60 deletions

File tree

src/m365/outlook/commands/calendargroup/calendargroup-remove.spec.ts

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { cli } from '../../../../cli/cli.js';
88
import request from '../../../../request.js';
99
import { telemetry } from '../../../../telemetry.js';
1010
import { accessToken } from '../../../../utils/accessToken.js';
11+
import { calendarGroup } from '../../../../utils/calendarGroup.js';
1112
import { pid } from '../../../../utils/pid.js';
1213
import { session } from '../../../../utils/session.js';
1314
import { sinonUtil } from '../../../../utils/sinonUtil.js';
@@ -20,17 +21,6 @@ describe(commands.CALENDARGROUP_REMOVE, () => {
2021
const userId = 'b743445a-112c-4fda-9afd-05943f9c7b36';
2122
const userName = 'john.doe@contoso.com';
2223

23-
const calendarGroupsResponse = {
24-
value: [
25-
{
26-
id: calendarGroupId,
27-
name: calendarGroupName,
28-
changeKey: 'nfZyf7VcrEKLNoU37KWlkQAAA0x0+w==',
29-
classId: '0006f0b7-0000-0000-c000-000000000046'
30-
}
31-
]
32-
};
33-
3424
let log: string[];
3525
let logger: Logger;
3626
let commandInfo: CommandInfo;
@@ -77,6 +67,7 @@ describe(commands.CALENDARGROUP_REMOVE, () => {
7767
afterEach(() => {
7868
sinonUtil.restore([
7969
accessToken.isAppOnlyAccessToken,
70+
calendarGroup.getUserCalendarGroupByName,
8071
request.get,
8172
request.delete,
8273
cli.promptForConfirmation
@@ -186,13 +177,7 @@ describe(commands.CALENDARGROUP_REMOVE, () => {
186177
});
187178

188179
it('removes the calendar group specified by name for the signed-in user', async () => {
189-
sinon.stub(request, 'get').callsFake(async (opts) => {
190-
if (opts.url === `https://graph.microsoft.com/v1.0/me/calendarGroups?$filter=name eq '${calendarGroupName}'`) {
191-
return calendarGroupsResponse;
192-
}
193-
194-
throw 'Invalid request';
195-
});
180+
sinon.stub(calendarGroup, 'getUserCalendarGroupByName').resolves({ id: calendarGroupId, name: calendarGroupName });
196181

197182
const deleteRequestStub = sinon.stub(request, 'delete').callsFake(async (opts) => {
198183
if (opts.url === `https://graph.microsoft.com/v1.0/me/calendarGroups/${calendarGroupId}`) {
@@ -210,13 +195,7 @@ describe(commands.CALENDARGROUP_REMOVE, () => {
210195
});
211196

212197
it('removes the calendar group specified by name for the signed-in user (verbose)', async () => {
213-
sinon.stub(request, 'get').callsFake(async (opts) => {
214-
if (opts.url === `https://graph.microsoft.com/v1.0/me/calendarGroups?$filter=name eq '${calendarGroupName}'`) {
215-
return calendarGroupsResponse;
216-
}
217-
218-
throw 'Invalid request';
219-
});
198+
sinon.stub(calendarGroup, 'getUserCalendarGroupByName').resolves({ id: calendarGroupId, name: calendarGroupName });
220199

221200
const deleteRequestStub = sinon.stub(request, 'delete').callsFake(async (opts) => {
222201
if (opts.url === `https://graph.microsoft.com/v1.0/me/calendarGroups/${calendarGroupId}`) {
@@ -245,7 +224,7 @@ describe(commands.CALENDARGROUP_REMOVE, () => {
245224

246225
it('removes the calendar group specified by id for a user specified by userName', async () => {
247226
const deleteRequestStub = sinon.stub(request, 'delete').callsFake(async (opts) => {
248-
if (opts.url === `https://graph.microsoft.com/v1.0/users('${userName}')/calendarGroups/${calendarGroupId}`) {
227+
if (opts.url === `https://graph.microsoft.com/v1.0/users('john.doe%40contoso.com')/calendarGroups/${calendarGroupId}`) {
249228
return;
250229
}
251230

@@ -257,13 +236,7 @@ describe(commands.CALENDARGROUP_REMOVE, () => {
257236
});
258237

259238
it('removes the calendar group specified by name for a user specified by userId', async () => {
260-
sinon.stub(request, 'get').callsFake(async (opts) => {
261-
if (opts.url === `https://graph.microsoft.com/v1.0/users('${userId}')/calendarGroups?$filter=name eq '${calendarGroupName}'`) {
262-
return calendarGroupsResponse;
263-
}
264-
265-
throw 'Invalid request';
266-
});
239+
sinon.stub(calendarGroup, 'getUserCalendarGroupByName').resolves({ id: calendarGroupId, name: calendarGroupName });
267240

268241
const deleteRequestStub = sinon.stub(request, 'delete').callsFake(async (opts) => {
269242
if (opts.url === `https://graph.microsoft.com/v1.0/users('${userId}')/calendarGroups/${calendarGroupId}`) {
@@ -278,16 +251,10 @@ describe(commands.CALENDARGROUP_REMOVE, () => {
278251
});
279252

280253
it('removes the calendar group specified by name for a user specified by userName', async () => {
281-
sinon.stub(request, 'get').callsFake(async (opts) => {
282-
if (opts.url === `https://graph.microsoft.com/v1.0/users('${userName}')/calendarGroups?$filter=name eq '${calendarGroupName}'`) {
283-
return calendarGroupsResponse;
284-
}
285-
286-
throw 'Invalid request';
287-
});
254+
sinon.stub(calendarGroup, 'getUserCalendarGroupByName').resolves({ id: calendarGroupId, name: calendarGroupName });
288255

289256
const deleteRequestStub = sinon.stub(request, 'delete').callsFake(async (opts) => {
290-
if (opts.url === `https://graph.microsoft.com/v1.0/users('${userName}')/calendarGroups/${calendarGroupId}`) {
257+
if (opts.url === `https://graph.microsoft.com/v1.0/users('john.doe%40contoso.com')/calendarGroups/${calendarGroupId}`) {
291258
return;
292259
}
293260

@@ -325,17 +292,11 @@ describe(commands.CALENDARGROUP_REMOVE, () => {
325292
});
326293

327294
it('throws error when calendar group specified by name is not found', async () => {
328-
sinon.stub(request, 'get').callsFake(async (opts) => {
329-
if (opts.url === `https://graph.microsoft.com/v1.0/me/calendarGroups?$filter=name eq '${calendarGroupName}'`) {
330-
return { value: [] };
331-
}
332-
333-
throw 'Invalid request';
334-
});
295+
sinon.stub(calendarGroup, 'getUserCalendarGroupByName').rejects(new Error(`The specified calendar group '${calendarGroupName}' does not exist.`));
335296

336297
await assert.rejects(
337298
command.action(logger, { options: commandOptionsSchema.parse({ name: calendarGroupName, force: true }) }),
338-
new CommandError(`Calendar group with name '${calendarGroupName}' not found.`)
299+
new CommandError(`The specified calendar group '${calendarGroupName}' does not exist.`)
339300
);
340301
});
341302

src/m365/outlook/commands/calendargroup/calendargroup-remove.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { CalendarGroup } from '@microsoft/microsoft-graph-types';
21
import { z } from 'zod';
32
import { globalOptionsZod } from '../../../../Command.js';
43
import GraphCommand from '../../../base/GraphCommand.js';
54
import { Logger } from '../../../../cli/Logger.js';
65
import { cli } from '../../../../cli/cli.js';
76
import commands from '../../commands.js';
87
import { validation } from '../../../../utils/validation.js';
9-
import { odata } from '../../../../utils/odata.js';
108
import { accessToken } from '../../../../utils/accessToken.js';
119
import auth from '../../../../Auth.js';
1210
import request, { CliRequestOptions } from '../../../../request.js';
11+
import { formatting } from '../../../../utils/formatting.js';
12+
import { calendarGroup } from '../../../../utils/calendarGroup.js';
1313

1414
export const options = z.strictObject({
1515
...globalOptionsZod.shape,
@@ -67,12 +67,14 @@ class OutlookCalendarGroupRemoveCommand extends GraphCommand {
6767
}
6868

6969
let endpoint: string;
70+
let graphUserId: string;
7071

7172
if (args.options.userId || args.options.userName) {
72-
const userIdentifier = args.options.userId ?? args.options.userName;
73-
endpoint = `${this.resource}/v1.0/users('${userIdentifier}')`;
73+
graphUserId = (args.options.userId ?? args.options.userName)!;
74+
endpoint = `${this.resource}/v1.0/users('${formatting.encodeQueryParameter(graphUserId)}')`;
7475
}
7576
else {
77+
graphUserId = 'me';
7678
endpoint = `${this.resource}/v1.0/me`;
7779
}
7880

@@ -83,13 +85,8 @@ class OutlookCalendarGroupRemoveCommand extends GraphCommand {
8385
await logger.logToStderr(`Retrieving calendar group by name '${args.options.name}'...`);
8486
}
8587

86-
const calendarGroups = await odata.getAllItems<CalendarGroup>(`${endpoint}/calendarGroups?$filter=name eq '${args.options.name}'`);
87-
88-
if (calendarGroups.length === 0) {
89-
throw `Calendar group with name '${args.options.name}' not found.`;
90-
}
91-
92-
calendarGroupId = calendarGroups[0].id!;
88+
const calendarGroupResult = await calendarGroup.getUserCalendarGroupByName(graphUserId, args.options.name);
89+
calendarGroupId = calendarGroupResult.id!;
9390
}
9491

9592
if (this.verbose) {

0 commit comments

Comments
 (0)