@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
115 lines • 5.38 kB
JavaScript
import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { validation } from '../../../../utils/validation.js';
import request from '../../../../request.js';
import { accessToken } from '../../../../utils/accessToken.js';
import auth from '../../../../Auth.js';
import { formatting } from '../../../../utils/formatting.js';
import { calendarGroup } from '../../../../utils/calendarGroup.js';
export const options = z.strictObject({
...globalOptionsZod.shape,
id: z.string().optional(),
name: z.string().optional(),
userId: z.string().refine(id => validation.isValidGuid(id), {
error: e => `'${e.input}' is not a valid GUID.`
}).optional(),
userName: z.string().refine(name => validation.isValidUserPrincipalName(name), {
error: e => `'${e.input}' is not a valid UPN.`
}).optional(),
newName: z.string()
});
class OutlookCalendarGroupSetCommand extends GraphCommand {
get name() {
return commands.CALENDARGROUP_SET;
}
get description() {
return 'Updates a calendar group 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.id && !options.name), {
error: 'Specify either id or name.'
})
.refine(options => !(options.id && options.name), {
error: 'Specify either id or name, but not both.'
});
}
async commandAction(logger, args) {
try {
const token = auth.connection.accessTokens[auth.defaultResource].accessToken;
const isAppOnlyAccessToken = accessToken.isAppOnlyAccessToken(token);
let userUrl;
let graphUserId;
if (isAppOnlyAccessToken) {
if (!args.options.userId && !args.options.userName) {
throw 'When running with application permissions either userId or userName is required.';
}
graphUserId = (args.options.userId ?? args.options.userName);
userUrl = `${this.resource}/v1.0/users('${formatting.encodeQueryParameter(graphUserId)}')`;
if (this.verbose) {
await logger.logToStderr(`Updating calendar group using application permissions for user '${graphUserId}'...`);
}
}
else if (args.options.userId || args.options.userName) {
const currentUserId = accessToken.getUserIdFromAccessToken(token);
const currentUserName = accessToken.getUserNameFromAccessToken(token);
const isOtherUser = (args.options.userId && args.options.userId !== currentUserId) ||
(args.options.userName && args.options.userName.toLowerCase() !== currentUserName?.toLowerCase());
if (isOtherUser) {
const scopes = accessToken.getScopesFromAccessToken(token);
const hasSharedScope = scopes.some(s => s === 'Calendars.ReadWrite.Shared');
if (!hasSharedScope) {
throw `To update calendar groups of other users, the Entra ID application used for authentication must have the Calendars.ReadWrite.Shared delegated permission assigned.`;
}
}
graphUserId = (args.options.userId ?? args.options.userName);
userUrl = `${this.resource}/v1.0/users('${formatting.encodeQueryParameter(graphUserId)}')`;
if (this.verbose) {
await logger.logToStderr(`Updating calendar group using delegated permissions for user '${graphUserId}'...`);
}
}
else {
graphUserId = accessToken.getUserIdFromAccessToken(token);
userUrl = `${this.resource}/v1.0/me`;
if (this.verbose) {
await logger.logToStderr('Updating calendar group for the signed-in user...');
}
}
let calendarGroupId;
if (args.options.id) {
calendarGroupId = args.options.id;
}
else {
const calendarGroupResult = await calendarGroup.getUserCalendarGroupByName(graphUserId, args.options.name);
calendarGroupId = calendarGroupResult.id;
}
if (this.verbose) {
await logger.logToStderr(`Updating calendar group '${calendarGroupId}'...`);
}
const requestOptions = {
url: `${userUrl}/calendarGroups/${calendarGroupId}`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json',
data: {
name: args.options.newName
}
};
await request.patch(requestOptions);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
}
export default new OutlookCalendarGroupSetCommand();
//# sourceMappingURL=calendargroup-set.js.map