UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

105 lines 4.06 kB
import { z } from 'zod'; import { globalOptionsZod } from '../../../../Command.js'; import { cli } from '../../../../cli/cli.js'; import request from '../../../../request.js'; import { formatting } from '../../../../utils/formatting.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; export const options = z.strictObject({ ...globalOptionsZod.shape, url: z.string() .refine(val => val.indexOf('https://') === 0 || val.indexOf('http://') === 0, { message: 'The url option should contain a valid URL. A valid URL starts with http(s)://' }) .optional() .alias('u'), alias: z.string().optional(), taskId: z.string().alias('i'), force: z.boolean().optional().alias('f') }); class PlannerTaskReferenceRemoveCommand extends GraphCommand { get name() { return commands.TASK_REFERENCE_REMOVE; } get description() { return 'Removes the reference from the Planner task'; } get schema() { return options; } getRefinedSchema(schema) { return schema .refine(opts => [opts.url, opts.alias].filter(x => x !== undefined).length === 1, { message: `Specify exactly one of the following options: 'url' or 'alias'.`, params: { customCode: 'optionSet', options: ['url', 'alias'] } }); } async commandAction(logger, args) { if (args.options.force) { await this.removeReference(logger, args); } else { const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove the reference from the Planner task?` }); if (result) { await this.removeReference(logger, args); } } } async removeReference(logger, args) { try { const { etag, url } = await this.getTaskDetailsEtagAndUrl(args.options); const requestOptionsTaskDetails = { url: `${this.resource}/v1.0/planner/tasks/${args.options.taskId}/details`, headers: { 'accept': 'application/json;odata.metadata=none', 'If-Match': etag, 'Prefer': 'return=representation' }, responseType: 'json', data: { references: { [formatting.openTypesEncoder(url)]: null } } }; await request.patch(requestOptionsTaskDetails); } catch (err) { this.handleRejectedODataJsonPromise(err); } } async getTaskDetailsEtagAndUrl(options) { const requestOptions = { url: `${this.resource}/v1.0/planner/tasks/${formatting.encodeQueryParameter(options.taskId)}/details`, headers: { accept: 'application/json' }, responseType: 'json' }; let url = options.url; const taskDetails = await request.get(requestOptions); if (options.alias) { const urls = []; if (taskDetails.references) { Object.entries(taskDetails.references).forEach((ref) => { if (ref[1].alias?.toLocaleLowerCase() === options.alias.toLocaleLowerCase()) { urls.push(decodeURIComponent(ref[0])); } }); } if (urls.length === 0) { throw `The specified reference with alias ${options.alias} does not exist`; } if (urls.length > 1) { throw `Multiple references with alias ${options.alias} found. Pass one of the following urls within the "--url" option : ${urls}`; } url = urls[0]; } return { etag: taskDetails['@odata.etag'], url }; } } export default new PlannerTaskReferenceRemoveCommand(); //# sourceMappingURL=task-reference-remove.js.map