UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

148 lines • 6.53 kB
import { z } from 'zod'; import { globalOptionsZod } from '../../../../Command.js'; import request from '../../../../request.js'; import { entraGroup } from '../../../../utils/entraGroup.js'; import { formatting } from '../../../../utils/formatting.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 { cli } from '../../../../cli/cli.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() }); class PlannerTaskGetCommand extends GraphCommand { get name() { return commands.TASK_GET; } get description() { return 'Retrieves the specified planner task'; } 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) { try { const taskId = await this.getTaskId(args.options); const task = await this.getTask(taskId); const res = await this.getTaskDetails(task); await logger.log(res); } catch (err) { this.handleRejectedODataJsonPromise(err); } } async getTask(taskId) { const requestOptions = { url: `${this.resource}/v1.0/planner/tasks/${formatting.encodeQueryParameter(taskId)}`, headers: { accept: 'application/json;odata.metadata=none' }, responseType: 'json' }; return await request.get(requestOptions); } async getTaskDetails(task) { const requestOptionsTaskDetails = { url: `${this.resource}/v1.0/planner/tasks/${task.id}/details`, headers: { 'accept': 'application/json;odata.metadata=none', 'Prefer': 'return=representation' }, responseType: 'json' }; const taskDetails = await request.get(requestOptionsTaskDetails); return { ...task, ...taskDetails }; } async getTaskId(options) { if (options.id) { return options.id; } const bucketId = await this.getBucketId(options); const requestOptions = { url: `${this.resource}/v1.0/planner/buckets/${bucketId}/tasks?$select=id,title`, headers: { accept: 'application/json;odata.metadata=none' }, responseType: 'json' }; const response = await request.get(requestOptions); const title = options.title; const tasks = response.value.filter(val => val.title?.toLocaleLowerCase() === title.toLocaleLowerCase()); if (!tasks.length) { throw `The specified task ${options.title} does not exist`; } if (tasks.length > 1) { const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', tasks); const result = await cli.handleMultipleResultsFound(`Multiple tasks with title '${options.title}' found.`, resultAsKeyValuePair); return result.id; } return tasks[0].id; } async getBucketId(options) { if (options.bucketId) { return options.bucketId; } const planId = await this.getPlanId(options); return planner.getBucketIdByTitle(options.bucketName, planId); } async getPlanId(options) { if (options.planId) { return options.planId; } if (options.rosterId) { return planner.getPlanIdByRosterId(options.rosterId); } else { const groupId = await this.getGroupId(options); return planner.getPlanIdByTitle(options.planTitle, groupId); } } async getGroupId(options) { if (options.ownerGroupId) { return options.ownerGroupId; } return entraGroup.getGroupIdByDisplayName(options.ownerGroupName); } } export default new PlannerTaskGetCommand(); //# sourceMappingURL=task-get.js.map