UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

150 lines • 6.75 kB
import { z } from 'zod'; import { globalOptionsZod } from '../../../../Command.js'; import { cli } from '../../../../cli/cli.js'; import request from '../../../../request.js'; import { entraGroup } from '../../../../utils/entraGroup.js'; import { odata } from '../../../../utils/odata.js'; import { planner } from '../../../../utils/planner.js'; import { validation } from '../../../../utils/validation.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; import { formatting } from '../../../../utils/formatting.js'; export const options = z.strictObject({ ...globalOptionsZod.shape, id: z.string().optional().alias('i'), title: z.string().optional().alias('t'), bucketId: z.string().optional(), bucketName: z.string().optional(), planId: z.string().optional(), planTitle: z.string().optional(), rosterId: z.string().optional(), ownerGroupId: z.string() .refine(val => validation.isValidGuid(val), { message: 'The value is not a valid GUID.' }) .optional(), ownerGroupName: z.string().optional(), force: z.boolean().optional().alias('f') }); class PlannerTaskRemoveCommand extends GraphCommand { get name() { return commands.TASK_REMOVE; } get description() { return 'Removes the Microsoft Planner task from a plan'; } get schema() { return options; } getRefinedSchema(schema) { return schema .refine(opts => [opts.id, opts.title].filter(x => x !== undefined).length === 1, { message: `Specify exactly one of the following options: 'id' or 'title'.`, params: { customCode: 'optionSet', options: ['id', 'title'] } }) .refine(opts => opts.id !== undefined || opts.bucketId !== undefined || [opts.planId, opts.planTitle, opts.rosterId].filter(x => x !== undefined).length === 1, { message: `Specify exactly one of the following options: 'planId', 'planTitle' or 'rosterId'.`, params: { customCode: 'optionSet', options: ['planId', 'planTitle', 'rosterId'] } }) .refine(opts => opts.id !== undefined || [opts.bucketId, opts.bucketName].filter(x => x !== undefined).length === 1, { message: `Specify exactly one of the following options: 'bucketId' or 'bucketName'.`, params: { customCode: 'optionSet', options: ['bucketId', 'bucketName'] } }) .refine(opts => !(opts.bucketName && !opts.rosterId) || [opts.planId, opts.planTitle].filter(x => x !== undefined).length === 1, { message: `Specify exactly one of the following options: 'planId' or 'planTitle'.`, params: { customCode: 'optionSet', options: ['planId', 'planTitle'] } }) .refine(opts => !opts.planTitle || [opts.ownerGroupId, opts.ownerGroupName].filter(x => x !== undefined).length === 1, { message: `Specify exactly one of the following options: 'ownerGroupId' or 'ownerGroupName'.`, params: { customCode: 'optionSet', options: ['ownerGroupId', 'ownerGroupName'] } }) .refine(opts => !opts.id || !(opts.bucketId || opts.bucketName || opts.planId || opts.planTitle || opts.rosterId || opts.ownerGroupId || opts.ownerGroupName), { message: `Don't specify bucketId, bucketName, planId, planTitle, rosterId, ownerGroupId or ownerGroupName when using id.` }); } async commandAction(logger, args) { const removeTask = async () => { try { const task = await this.getTask(args.options); if (this.verbose) { await logger.logToStderr(`Removing task '${task.title}' ...`); } const requestOptions = { url: `${this.resource}/v1.0/planner/tasks/${task.id}`, headers: { accept: 'application/json;odata.metadata=none', 'if-match': task['@odata.etag'] }, responseType: 'json' }; await request.delete(requestOptions); } catch (err) { this.handleRejectedODataJsonPromise(err); } }; if (args.options.force) { await removeTask(); } else { const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove the task ${args.options.id || args.options.title}?` }); if (result) { await removeTask(); } } } async getTask(options) { const { id, title } = options; if (id) { const requestOptions = { url: `${this.resource}/v1.0/planner/tasks/${id}`, headers: { accept: 'application/json' }, responseType: 'json' }; return await request.get(requestOptions); } const bucketId = await this.getBucketId(options); const tasks = await odata.getAllItems(`${this.resource}/v1.0/planner/buckets/${bucketId}/tasks?$select=title,id`, 'minimal'); const filteredTasks = tasks.filter(b => title.toLocaleLowerCase() === b.title.toLocaleLowerCase()); if (filteredTasks.length === 0) { throw `The specified task ${title} does not exist`; } if (filteredTasks.length > 1) { const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', filteredTasks); return await cli.handleMultipleResultsFound(`Multiple tasks with title '${title}' found.`, resultAsKeyValuePair); } return filteredTasks[0]; } async getBucketId(options) { const { bucketId, bucketName } = options; if (bucketId) { return bucketId; } const planId = await this.getPlanId(options); return planner.getBucketIdByTitle(bucketName, planId); } async getPlanId(options) { const { planId, planTitle, rosterId } = options; if (planId) { return planId; } if (options.rosterId) { return planner.getPlanIdByRosterId(rosterId); } else { const groupId = await this.getGroupId(options); return planner.getPlanIdByTitle(planTitle, groupId); } } async getGroupId(options) { const { ownerGroupId, ownerGroupName } = options; if (ownerGroupId) { return ownerGroupId; } return entraGroup.getGroupIdByDisplayName(ownerGroupName); } } export default new PlannerTaskRemoveCommand(); //# sourceMappingURL=task-remove.js.map