@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
85 lines • 3.68 kB
JavaScript
import request from '../../../../request.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import { validation } from '../../../../utils/validation.js';
import { calendarGroup } from '../../../../utils/calendarGroup.js';
export const options = z.strictObject({
...globalOptionsZod.shape,
userId: z.string()
.refine(userId => validation.isValidGuid(userId), {
error: e => `'${e.input}' is not a valid GUID.`
}).optional(),
userName: z.string()
.refine(userName => validation.isValidUserPrincipalName(userName), {
error: e => `'${e.input}' is not a valid UPN.`
}).optional(),
name: z.string(),
calendarGroupId: z.string().optional(),
calendarGroupName: z.string().optional(),
color: z.enum(['auto', 'lightBlue', 'lightGreen', 'lightOrange', 'lightGray', 'lightYellow', 'lightTeal', 'lightPink', 'lightBrown', 'lightRed', 'maxColor']).optional().default('auto'),
defaultOnlineMeetingProvider: z.enum(['none', 'teamsForBusiness']).optional().default('teamsForBusiness'),
default: z.boolean().optional()
});
class OutlookCalendarAddCommand extends GraphCommand {
get name() {
return commands.CALENDAR_ADD;
}
get description() {
return 'Creates a new calendar for a user';
}
get schema() {
return options;
}
getRefinedSchema(schema) {
return schema
.refine(options => !(options.userId && options.userName), {
error: 'Specify either userId or userName, but not both'
})
.refine(options => !(options.calendarGroupId && options.calendarGroupName), {
error: 'Specify either calendarGroupId or calendarGroupName, but not both'
});
}
async commandAction(logger, args) {
try {
const userIdentifier = args.options.userId ?? args.options.userName;
let requestUrl = `${this.resource}/v1.0/users('${userIdentifier}')/`;
if (args.options.calendarGroupId || args.options.calendarGroupName) {
let calendarGroupId = args.options.calendarGroupId;
if (args.options.calendarGroupName) {
const group = await calendarGroup.getUserCalendarGroupByName(userIdentifier, args.options.calendarGroupName, 'id');
calendarGroupId = group.id;
}
requestUrl += `calendarGroups/${calendarGroupId}/calendars`;
}
else {
requestUrl += 'calendars';
}
if (args.options.verbose) {
await logger.logToStderr(`Creating a calendar for the user ${userIdentifier}...`);
}
const requestOptions = {
url: requestUrl,
headers: {
accept: 'application/json;odata.metadata=none',
'content-type': 'application/json'
},
responseType: 'json',
data: {
name: args.options.name,
color: args.options.color,
defaultOnlineMeetingProvider: args.options.defaultOnlineMeetingProvider,
isDefaultCalendar: args.options.default
}
};
const result = await request.post(requestOptions);
await logger.log(result);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
}
export default new OutlookCalendarAddCommand();
//# sourceMappingURL=calendar-add.js.map