UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

285 lines • 11.9 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 { taskPriority } from '../../taskPriority.js'; const allowedAppliedCategories = ['category1', 'category2', 'category3', 'category4', 'category5', 'category6']; export const options = z.strictObject({ ...globalOptionsZod.shape, id: z.string().alias('i'), title: z.string().optional().alias('t'), 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(), bucketId: z.string().optional(), bucketName: z.string().optional(), startDateTime: z.string() .refine(val => validation.isValidISODateTime(val), { message: 'The startDateTime is not a valid ISO date string.' }) .optional(), dueDateTime: z.string() .refine(val => validation.isValidISODateTime(val), { message: 'The dueDateTime is not a valid ISO date string.' }) .optional(), percentComplete: z.string() .refine(val => !isNaN(Number(val)) && Number(val) >= 0 && Number(val) <= 100, { message: 'percentComplete should be between 0 and 100.' }) .optional(), assignedToUserIds: z.string() .superRefine((val, ctx) => { const result = validation.isValidGuidArray(val); if (result !== true) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `The following GUIDs are invalid for the option 'assignedToUserIds': ${result}.` }); } }) .optional(), assignedToUserNames: z.string() .superRefine((val, ctx) => { const result = validation.isValidUserPrincipalNameArray(val); if (result !== true) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `The following user principal names are invalid for the option 'assignedToUserNames': ${result}.` }); } }) .optional(), assigneePriority: z.string().optional(), description: z.string().optional(), appliedCategories: z.string() .refine(val => val.split(',').every(cat => allowedAppliedCategories.includes(cat.toLocaleLowerCase().trim())), { message: `The appliedCategories contains invalid value. Specify either ${allowedAppliedCategories.join(', ')} as properties.` }) .optional(), orderHint: z.string().optional(), priority: z.string() .refine(val => { const num = Number(val); if (!isNaN(num)) { return num >= 0 && num <= 10 && Number.isInteger(num); } return taskPriority.priorityValues.map(l => l.toLowerCase()).includes(val.toLowerCase()); }, { message: `The value is not a valid priority. Allowed values are 0-10 or ${taskPriority.priorityValues.join('|')}.` }) .optional() }); class PlannerTaskSetCommand extends GraphCommand { get name() { return commands.TASK_SET; } get description() { return 'Updates a Microsoft Planner Task'; } get schema() { return options; } getRefinedSchema(schema) { return schema .refine(opts => !(opts.bucketId && opts.bucketName), { message: `Specify either 'bucketId' or 'bucketName', but not both.`, params: { customCode: 'optionSet', options: ['bucketId', 'bucketName'] } }) .refine(opts => !opts.bucketName || [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.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.assignedToUserIds && opts.assignedToUserNames), { message: 'Specify either assignedToUserIds or assignedToUserNames but not both.', params: { customCode: 'optionSet', options: ['assignedToUserIds', 'assignedToUserNames'] } }); } async commandAction(logger, args) { try { this.bucketId = await this.getBucketId(args.options); this.assignments = await this.generateUserAssignments(args.options); const etag = await this.getTaskEtag(args.options.id); const appliedCategories = this.generateAppliedCategories(args.options); const data = this.mapRequestBody(args.options, appliedCategories); const requestOptions = { url: `${this.resource}/v1.0/planner/tasks/${args.options.id}`, headers: { 'accept': 'application/json;odata.metadata=none', 'If-Match': etag, 'Prefer': 'return=representation' }, responseType: 'json', data: data }; const newTask = await request.patch(requestOptions); const result = await this.updateTaskDetails(args.options, newTask); await logger.log(result); } catch (err) { this.handleRejectedODataJsonPromise(err); } } async updateTaskDetails(options, newTask) { if (!options.description) { return newTask; } const taskId = newTask.id; const etag = await this.getTaskDetailsEtag(taskId); const requestOptionsTaskDetails = { url: `${this.resource}/v1.0/planner/tasks/${taskId}/details`, headers: { 'accept': 'application/json;odata.metadata=none', 'If-Match': etag, 'Prefer': 'return=representation' }, responseType: 'json', data: { description: options.description } }; const taskDetails = await request.patch(requestOptionsTaskDetails); return { ...newTask, ...taskDetails }; } async getTaskDetailsEtag(taskId) { const requestOptions = { url: `${this.resource}/v1.0/planner/tasks/${formatting.encodeQueryParameter(taskId)}/details`, headers: { accept: 'application/json' }, responseType: 'json' }; const response = await request.get(requestOptions); return response['@odata.etag']; } async getTaskEtag(taskId) { const requestOptions = { url: `${this.resource}/v1.0/planner/tasks/${formatting.encodeQueryParameter(taskId)}`, headers: { accept: 'application/json' }, responseType: 'json' }; const response = await request.get(requestOptions); return response['@odata.etag']; } generateAppliedCategories(options) { if (!options.appliedCategories) { return {}; } const categories = {}; options.appliedCategories.toLocaleLowerCase().split(',').forEach(x => categories[x.trim()] = true); return categories; } async generateUserAssignments(options) { const assignments = {}; if (!options.assignedToUserIds && !options.assignedToUserNames) { return assignments; } const userIds = await this.getUserIds(options); userIds.forEach(x => assignments[x] = { '@odata.type': '#microsoft.graph.plannerAssignment', orderHint: ' !' }); return assignments; } async getUserIds(options) { if (options.assignedToUserIds) { return options.assignedToUserIds.split(',').map(o => o.trim()); } const userNames = options.assignedToUserNames; const userArr = userNames.split(',').map(o => o.trim()); const promises = userArr.map(user => { const requestOptions = { url: `${this.resource}/v1.0/users?$filter=userPrincipalName eq '${formatting.encodeQueryParameter(user)}'&$select=id,userPrincipalName`, headers: { 'accept ': 'application/json;odata.metadata=none' }, responseType: 'json' }; return request.get(requestOptions); }); const usersRes = await Promise.all(promises); let userUpns = []; userUpns = usersRes.map(res => res.value[0]?.userPrincipalName); const userIds = usersRes.map(res => res.value[0]?.id); const invalidUsers = userArr.filter(user => !userUpns.some((upn) => upn?.toLowerCase() === user.toLowerCase())); if (invalidUsers && invalidUsers.length > 0) { throw `Cannot proceed with planner task update. The following users provided are invalid : ${invalidUsers.join(',')}`; } return userIds; } async getBucketId(options) { if (options.bucketId) { return options.bucketId; } if (!options.bucketName) { return undefined; } 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); } mapRequestBody(options, appliedCategories) { const requestBody = {}; if (options.title) { requestBody.title = options.title; } if (this.bucketId) { requestBody.bucketId = this.bucketId; } if (options.startDateTime) { requestBody.startDateTime = options.startDateTime; } if (options.dueDateTime) { requestBody.dueDateTime = options.dueDateTime; } if (options.percentComplete) { requestBody.percentComplete = Number(options.percentComplete); } if (this.assignments && Object.keys(this.assignments).length > 0) { requestBody.assignments = this.assignments; } if (options.assigneePriority) { requestBody.assigneePriority = options.assigneePriority; } if (appliedCategories && Object.keys(appliedCategories).length > 0) { requestBody.appliedCategories = appliedCategories; } if (options.orderHint) { requestBody.orderHint = options.orderHint; } if (options.priority !== undefined) { requestBody.priority = taskPriority.getPriorityValue(options.priority); } return requestBody; } } export default new PlannerTaskSetCommand(); //# sourceMappingURL=task-set.js.map