Skip to content

Commit

Permalink
New command: m365 outlook mail searchfolder add
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinM85 committed Jan 7, 2025
1 parent 045f272 commit 65a39da
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
28 changes: 25 additions & 3 deletions src/m365/outlook/commands/mail/mail-searchfolder-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ describe(commands.MAIL_SEARCHFOLDER_ADD, () => {

throw 'Invalid request';
});
await command.action(logger, { options: { userId: userId, folderName: 'Contoso', messageFilter: `subject eq 'Contoso'`, sourceFoldersIds: 'AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAA=' } });

const parsedSchema = commandOptionsSchema.safeParse({
userId: userId,
folderName: 'Contoso',
messageFilter: `subject eq 'Contoso'`,
sourceFoldersIds: 'AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAA='
});
await command.action(logger, { options: parsedSchema.data });
assert(loggerLogSpy.calledOnceWithExactly(response));
});

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

throw 'Invalid request';
});
await command.action(logger, { options: { userName: userName, folderName: 'Contoso', messageFilter: `subject eq 'Contoso'`, sourceFoldersIds: 'AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAA=,AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAB=', includeNestedFodlers: true, verbose: true } });

const parsedSchema = commandOptionsSchema.safeParse({
userName: userName,
folderName: 'Contoso',
messageFilter: `subject eq 'Contoso'`,
sourceFoldersIds: 'AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAA=,AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAABxI4iNfZK7SYRiWw9sza20AQA7DGC6yx9ARZqQFWs3P3q1AAAASBOHAAB=',
includeNestedFolders: true,
verbose: true
});
await command.action(logger, { options: parsedSchema.data });
assert(loggerLogSpy.calledOnceWithExactly(responseWithNestedFolders));
});

Expand All @@ -196,6 +212,12 @@ describe(commands.MAIL_SEARCHFOLDER_ADD, () => {
}
});

await assert.rejects(command.action(logger, { options: { userId: userId, folderName: 'Contoso', messageFilter: `subject eq 'Contoso'`, sourceFoldersIds: 'foo' } } as any), new CommandError('Id is malformed.'));
const parsedSchema = commandOptionsSchema.safeParse({
userId: userId,
folderName: 'Contoso',
messageFilter: `subject eq 'Contoso'`,
sourceFoldersIds: 'foo'
});
await assert.rejects(command.action(logger, { options: parsedSchema.data }), new CommandError('Id is malformed.'));
});
});
20 changes: 9 additions & 11 deletions src/m365/outlook/commands/mail/mail-searchfolder-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import { validation } from '../../../../utils/validation.js';

const options = globalOptionsZod
.extend({
userId: zod.alias('i', z.string().optional()),
userName: zod.alias('n', z.string().optional()),
userId: zod.alias('i', z.string()
.refine(userId => validation.isValidGuid(userId), userId => ({
message: `'${userId}' is not a valid GUID.`
})).optional()),
userName: zod.alias('n', z.string()
.refine(userName => validation.isValidUserPrincipalName(userName), userName => ({
message: `'${userName}' is not a valid UPN.`
})).optional()),
folderName: z.string(),
messageFilter: z.string(),
sourceFoldersIds: z.string().transform((value) => value.split(',')).pipe(z.string().array()),
Expand Down Expand Up @@ -43,15 +49,7 @@ class OutlookMailSearchFolderAddCommand extends GraphCommand {
return schema
.refine(options => !options.userId !== !options.userName, {
message: 'Specify either userId or userName, but not both'
})
.refine(options => (!options.userId && !options.userName) || options.userName || (options.userId && validation.isValidGuid(options.userId)), options => ({
message: `The '${options.userId}' must be a valid GUID`,
path: ['userId']
}))
.refine(options => (!options.userId && !options.userName) || options.userId || (options.userName && validation.isValidUserPrincipalName(options.userName)), options => ({
message: `The '${options.userName}' must be a valid user principal name`,
path: ['userName']
}));
});
}

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

0 comments on commit 65a39da

Please sign in to comment.