Skip to content

Commit 65a39da

Browse files
committed
New command: m365 outlook mail searchfolder add
1 parent 045f272 commit 65a39da

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/m365/outlook/commands/mail/mail-searchfolder-add.spec.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,14 @@ describe(commands.MAIL_SEARCHFOLDER_ADD, () => {
169169

170170
throw 'Invalid request';
171171
});
172-
await command.action(logger, { options: { userId: userId, folderName: 'Contoso', messageFilter: `subject eq 'Contoso'`, sourceFoldersIds: 'AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAA=' } });
172+
173+
const parsedSchema = commandOptionsSchema.safeParse({
174+
userId: userId,
175+
folderName: 'Contoso',
176+
messageFilter: `subject eq 'Contoso'`,
177+
sourceFoldersIds: 'AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAA='
178+
});
179+
await command.action(logger, { options: parsedSchema.data });
173180
assert(loggerLogSpy.calledOnceWithExactly(response));
174181
});
175182

@@ -182,7 +189,16 @@ describe(commands.MAIL_SEARCHFOLDER_ADD, () => {
182189

183190
throw 'Invalid request';
184191
});
185-
await command.action(logger, { options: { userName: userName, folderName: 'Contoso', messageFilter: `subject eq 'Contoso'`, sourceFoldersIds: 'AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAA=,AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAB=', includeNestedFodlers: true, verbose: true } });
192+
193+
const parsedSchema = commandOptionsSchema.safeParse({
194+
userName: userName,
195+
folderName: 'Contoso',
196+
messageFilter: `subject eq 'Contoso'`,
197+
sourceFoldersIds: 'AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAA=,AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAB=',
198+
includeNestedFolders: true,
199+
verbose: true
200+
});
201+
await command.action(logger, { options: parsedSchema.data });
186202
assert(loggerLogSpy.calledOnceWithExactly(responseWithNestedFolders));
187203
});
188204

@@ -196,6 +212,12 @@ describe(commands.MAIL_SEARCHFOLDER_ADD, () => {
196212
}
197213
});
198214

199-
await assert.rejects(command.action(logger, { options: { userId: userId, folderName: 'Contoso', messageFilter: `subject eq 'Contoso'`, sourceFoldersIds: 'foo' } } as any), new CommandError('Id is malformed.'));
215+
const parsedSchema = commandOptionsSchema.safeParse({
216+
userId: userId,
217+
folderName: 'Contoso',
218+
messageFilter: `subject eq 'Contoso'`,
219+
sourceFoldersIds: 'foo'
220+
});
221+
await assert.rejects(command.action(logger, { options: parsedSchema.data }), new CommandError('Id is malformed.'));
200222
});
201223
});

src/m365/outlook/commands/mail/mail-searchfolder-add.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ import { validation } from '../../../../utils/validation.js';
1111

1212
const options = globalOptionsZod
1313
.extend({
14-
userId: zod.alias('i', z.string().optional()),
15-
userName: zod.alias('n', z.string().optional()),
14+
userId: zod.alias('i', z.string()
15+
.refine(userId => validation.isValidGuid(userId), userId => ({
16+
message: `'${userId}' is not a valid GUID.`
17+
})).optional()),
18+
userName: zod.alias('n', z.string()
19+
.refine(userName => validation.isValidUserPrincipalName(userName), userName => ({
20+
message: `'${userName}' is not a valid UPN.`
21+
})).optional()),
1622
folderName: z.string(),
1723
messageFilter: z.string(),
1824
sourceFoldersIds: z.string().transform((value) => value.split(',')).pipe(z.string().array()),
@@ -43,15 +49,7 @@ class OutlookMailSearchFolderAddCommand extends GraphCommand {
4349
return schema
4450
.refine(options => !options.userId !== !options.userName, {
4551
message: 'Specify either userId or userName, but not both'
46-
})
47-
.refine(options => (!options.userId && !options.userName) || options.userName || (options.userId && validation.isValidGuid(options.userId)), options => ({
48-
message: `The '${options.userId}' must be a valid GUID`,
49-
path: ['userId']
50-
}))
51-
.refine(options => (!options.userId && !options.userName) || options.userId || (options.userName && validation.isValidUserPrincipalName(options.userName)), options => ({
52-
message: `The '${options.userName}' must be a valid user principal name`,
53-
path: ['userName']
54-
}));
52+
});
5553
}
5654

5755
public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {

0 commit comments

Comments
 (0)