From 50c55295faaec08fc7829373d9a3c58d681301ba Mon Sep 17 00:00:00 2001 From: mkm17 Date: Wed, 30 Oct 2024 22:07:21 +0100 Subject: [PATCH] Adds spp autofillcolumn apply command. Closes #6203 --- .../autofillcolumn/autofillcolumn-apply.mdx | 70 ++++ docs/src/config/sidebars.ts | 9 + src/m365/spp/commands.ts | 1 + .../autofillcolumn-apply.spec.ts | 324 ++++++++++++++++++ .../autofillcolumn/autofillcolumn-apply.ts | 212 ++++++++++++ 5 files changed, 616 insertions(+) create mode 100644 docs/docs/cmd/spp/autofillcolumn/autofillcolumn-apply.mdx create mode 100644 src/m365/spp/commands/autofillcolumn/autofillcolumn-apply.spec.ts create mode 100644 src/m365/spp/commands/autofillcolumn/autofillcolumn-apply.ts diff --git a/docs/docs/cmd/spp/autofillcolumn/autofillcolumn-apply.mdx b/docs/docs/cmd/spp/autofillcolumn/autofillcolumn-apply.mdx new file mode 100644 index 0000000000..ce46852b2d --- /dev/null +++ b/docs/docs/cmd/spp/autofillcolumn/autofillcolumn-apply.mdx @@ -0,0 +1,70 @@ +import Global from '/docs/cmd/_global.mdx'; +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +# spp autofillcolumn apply + +Applies the autofill option to the selected column + +## Usage + +```sh +m365 spp autofillcolumn apply [options] +``` + +## Options + +```md definition-list +`-u, --siteUrl ` +: The URL of the target site. + +`--listTitle [listTitle]` +: The title of the library on which to apply the model. Specify either `listTitle`, `listId`, or `listUrl` but not multiple. + +`--listId [listId]` +: The ID of the library on which to apply the model. Specify either `listTitle`, `listId`, or `listUrl` but not multiple. + +`--listUrl [listUrl]` +: Server or web-relative URL of the library on which to apply the model. Specify either `listTitle`, `listId`, or `listUrl` but not multiple. + +`-i, --columnId [columnId]` +: ID of the column to which the autofill option will be assigned. + +`-t, --columnTitle [columnTitle]` +: Title of the column to which the autofill option will be assigned. + +`--prompt ` +: The text in natural language that will be used to extract specific information or generate information from files within a SharePoint library. +``` + + + +## Examples + +Applies an autofill column on a selected column to a document library based on the list id. + +```sh +m365 spp autofillcolumn apply --siteUrl "https://contoso.sharepoint.com/sites/mainSite" --listId "7645e69d-21fb-4a24-a17a-9bdfa7cb63dc" --columnId "1045e69d-21fb-4214-a25a-9bdfa7cb63a2" --prompt "Write a 2-line summary of the document" +``` + +Applies an autofill column on a selected column to a document library based on the list title. + +```sh +m365 spp autofillcolumn apply --siteUrl "https://contoso.sharepoint.com/sites/mainSite" --listTitle "Documents" --columnId "1045e69d-21fb-4214-a25a-9bdfa7cb63a2" --prompt "Write a 2-line summary of the document" +``` + +Applies an autofill column on a selected column to a document library based on the list url. + +```sh +m365 spp autofillcolumn apply --siteUrl "https://contoso.sharepoint.com/sites/mainSite" --listUrl '/Shared Documents' --columnId "1045e69d-21fb-4214-a25a-9bdfa7cb63a2" --prompt "Write a 2-line summary of the document" +``` + +Applies an autofill column on a selected column to a document library based on the list id and column title. + +```sh +m365 spp autofillcolumn apply --siteUrl "https://contoso.sharepoint.com/sites/mainSite" --listId "7645e69d-21fb-4a24-a17a-9bdfa7cb63dc" --columnTitle "ColumnTitle" --prompt "Write a 2-line summary of the document" +``` + +## Response + +The command won't return a response on success. diff --git a/docs/src/config/sidebars.ts b/docs/src/config/sidebars.ts index a9c08be2d4..9bb8e168f1 100644 --- a/docs/src/config/sidebars.ts +++ b/docs/src/config/sidebars.ts @@ -3964,6 +3964,15 @@ const sidebars: SidebarsConfig = { }, { 'SharePoint Premium (spp)': [ + { + autofillcolumn: [ + { + type: 'doc', + label: 'autofillcolumn apply', + id: 'cmd/spp/autofillcolumn/autofillcolumn-apply' + } + ] + }, { contentcenter: [ { diff --git a/src/m365/spp/commands.ts b/src/m365/spp/commands.ts index 97f9c21b5b..dc65d6cb6c 100644 --- a/src/m365/spp/commands.ts +++ b/src/m365/spp/commands.ts @@ -1,6 +1,7 @@ const prefix: string = 'spp'; export default { + AUTOFILLCOLUMN_APPLY: `${prefix} autofillcolumn apply`, CONTENTCENTER_LIST: `${prefix} contentcenter list`, MODEL_LIST: `${prefix} model list` }; \ No newline at end of file diff --git a/src/m365/spp/commands/autofillcolumn/autofillcolumn-apply.spec.ts b/src/m365/spp/commands/autofillcolumn/autofillcolumn-apply.spec.ts new file mode 100644 index 0000000000..68fef46efd --- /dev/null +++ b/src/m365/spp/commands/autofillcolumn/autofillcolumn-apply.spec.ts @@ -0,0 +1,324 @@ +import assert from 'assert'; +import sinon from 'sinon'; +import auth from '../../../../Auth.js'; +import { cli } from '../../../../cli/cli.js'; +import { CommandInfo } from '../../../../cli/CommandInfo.js'; +import { Logger } from '../../../../cli/Logger.js'; +import request from '../../../../request.js'; +import { telemetry } from '../../../../telemetry.js'; +import { pid } from '../../../../utils/pid.js'; +import { session } from '../../../../utils/session.js'; +import { sinonUtil } from '../../../../utils/sinonUtil.js'; +import commands from '../../commands.js'; +import command from './autofillcolumn-apply.js'; +import { CommandError } from '../../../../Command.js'; + +describe(commands.AUTOFILLCOLUMN_APPLY, () => { + let log: string[]; + let logger: Logger; + let commandInfo: CommandInfo; + + before(() => { + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); + auth.connection.active = true; + commandInfo = cli.getCommandInfo(command); + }); + + beforeEach(() => { + log = []; + logger = { + log: async (msg: string) => { + log.push(msg); + }, + logRaw: async (msg: string) => { + log.push(msg); + }, + logToStderr: async (msg: string) => { + log.push(msg); + } + }; + }); + + afterEach(() => { + sinonUtil.restore([ + request.get, + request.post + ]); + }); + + after(() => { + sinon.restore(); + auth.connection.active = false; + }); + + it('has correct name', () => { + assert.strictEqual(command.name, commands.AUTOFILLCOLUMN_APPLY); + }); + + it('has a description', () => { + assert.notStrictEqual(command.description, null); + }); + + it('passes validation when required parameters are valid with column id and list id', async () => { + const actual = await command.validate({ options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listId: '421b1e42-794b-4c71-93ac-5ed92488b67d', prompt: 'test' } }, commandInfo); + assert.strictEqual(actual, true); + }); + + it('passes validation when required parameters are valid with column title and list id', async () => { + const actual = await command.validate({ options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnTitle: 'ColumnName', listId: '421b1e42-794b-4c71-93ac-5ed92488b67d', prompt: 'test' } }, commandInfo); + assert.strictEqual(actual, true); + }); + + it('passes validation when required parameters are valid with column id and list title', async () => { + const actual = await command.validate({ options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listTitle: 'Documents', prompt: 'test' } }, commandInfo); + assert.strictEqual(actual, true); + }); + + it('passes validation when required parameters are valid with column id and list URL', async () => { + const actual = await command.validate({ options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listUrl: '/Shared Documents', prompt: 'test' } }, commandInfo); + assert.strictEqual(actual, true); + }); + + it('fails validation when siteUrl is not valid', async () => { + const actual = await command.validate({ options: { siteUrl: 'invalidUrl', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listId: '421b1e42-794b-4c71-93ac-5ed92488b67d', prompt: 'test' } }, commandInfo); + assert.notStrictEqual(actual, true); + }); + + it('fails validation when column id is not valid', async () => { + const actual = await command.validate({ options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: 'invalidId', listId: '421b1e42-794b-4c71-93ac-5ed92488b67d', prompt: 'test' } }, commandInfo); + assert.notStrictEqual(actual, true); + }); + + it('fails validation when list id is not valid', async () => { + const actual = await command.validate({ options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listId: 'invalidId', prompt: 'test' } }, commandInfo); + assert.notStrictEqual(actual, true); + }); + + it('apply autofill to column by id and list id', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')/fields/getbyid('9b1b1e42-794b-4c71-93ac-5ed92488b67f')`) { + return { + Id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', + Title: 'ColumnName', + FieldTypeKind: 1 + }; + } + + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')?$select=Id,BaseType`) { + return { + Id: "421b1e42-794b-4c71-93ac-5ed92488b67d", + BaseType: 1 + }; + } + + throw `${opts.url} is invalid request`; + }); + + const stubPost = sinon.stub(request, 'post').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/machinelearning/SetSyntexPoweredColumnPrompts`) { + return; + } + + throw `${opts.url} is invalid request`; + }); + + await command.action(logger, { options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listId: '421b1e42-794b-4c71-93ac-5ed92488b67d', prompt: 'test', verbose: true } }); + assert.deepStrictEqual(stubPost.lastCall.args[0].data, { + docLibId: `{421b1e42-794b-4c71-93ac-5ed92488b67d}`, + syntexPoweredColumnPrompts: JSON.stringify([{ columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', columnName: 'ColumnName', prompt: 'test', isEnabled: true }]) + }); + }); + + it('apply autofill to column by id and list title', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')/fields/getbyid('9b1b1e42-794b-4c71-93ac-5ed92488b67f')`) { + return { + Id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', + Title: 'ColumnName', + FieldTypeKind: 1 + }; + } + + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists/getByTitle('Documents')?$select=Id,BaseType`) { + return { + Id: "421b1e42-794b-4c71-93ac-5ed92488b67d", + BaseType: 1 + }; + } + + throw `${opts.url} is invalid request`; + }); + + const stubPost = sinon.stub(request, 'post').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/machinelearning/SetSyntexPoweredColumnPrompts`) { + return; + } + + throw `${opts.url} is invalid request`; + }); + + await command.action(logger, { options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listTitle: 'Documents', prompt: 'test' } }); + assert.deepStrictEqual(stubPost.lastCall.args[0].data, { + docLibId: `{421b1e42-794b-4c71-93ac-5ed92488b67d}`, + syntexPoweredColumnPrompts: JSON.stringify([{ columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', columnName: 'ColumnName', prompt: 'test', isEnabled: true }]) + }); + }); + + it('apply autofill to column by title and list id', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === + `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')/fields/getbyinternalnameortitle('ColumnName')`) { + return { + Id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', + Title: 'ColumnName', + FieldTypeKind: 1 + }; + } + + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')?$select=Id,BaseType`) { + return { + Id: "421b1e42-794b-4c71-93ac-5ed92488b67d", + BaseType: 1 + }; + } + + throw `${opts.url} is invalid request`; + }); + + const stubPost = sinon.stub(request, 'post').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/machinelearning/SetSyntexPoweredColumnPrompts`) { + return; + } + + throw `${opts.url} is invalid request`; + }); + + await command.action(logger, { options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnTitle: 'ColumnName', listId: '421b1e42-794b-4c71-93ac-5ed92488b67d', prompt: 'test', verbose: true } }); + assert.deepStrictEqual(stubPost.lastCall.args[0].data, { + docLibId: `{421b1e42-794b-4c71-93ac-5ed92488b67d}`, + syntexPoweredColumnPrompts: JSON.stringify([{ columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', columnName: 'ColumnName', prompt: 'test', isEnabled: true }]) + }); + }); + + it('apply autofill to column by id and list url', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')/fields/getbyinternalnameortitle('ColumnName')`) { + return { + Id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', + Title: 'ColumnName', + FieldTypeKind: 1 + }; + } + + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/GetList('%2Fsites%2Fsales%2FShared%20Documents')?$select=Id,BaseType`) { + return { + Id: "421b1e42-794b-4c71-93ac-5ed92488b67d", + BaseType: 1 + }; + } + + throw `${opts.url} is invalid request`; + }); + + const stubPost = sinon.stub(request, 'post').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/machinelearning/SetSyntexPoweredColumnPrompts`) { + return; + } + + throw `${opts.url} is invalid request`; + }); + + await command.action(logger, { options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnTitle: 'ColumnName', prompt: 'test', listUrl: '/Shared Documents' } }); + assert.deepStrictEqual(stubPost.lastCall.args[0].data, { + docLibId: `{421b1e42-794b-4c71-93ac-5ed92488b67d}`, + syntexPoweredColumnPrompts: JSON.stringify([{ columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', columnName: 'ColumnName', prompt: 'test', isEnabled: true }]) + }); + }); + + it('correctly handles error when list is not found', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')/fields/getbyid('9b1b1e42-794b-4c71-93ac-5ed92488b67f')`) { + return { + Id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', + Title: 'ColumnName', + FieldTypeKind: 1 + }; + } + + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')?$select=Id,BaseType`) { + throw { + error: { + "odata.error": { + code: "-1, Microsoft.SharePoint.Client.ResourceNotFoundException", + message: { + lang: "en-US", + value: "List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user." + } + } + } + }; + } + + throw `${opts.url} is invalid request`; + }); + + sinon.stub(request, 'post').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/machinelearning/SetSyntexPoweredColumnPrompts`) { + return; + } + + throw `${opts.url} is invalid request`; + }); + + await assert.rejects(command.action(logger, { options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listId: '421b1e42-794b-4c71-93ac-5ed92488b67d', prompt: 'test', verbose: true } }), new CommandError('List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user.')); + }); + + it('corretly handles error when try to apply autofill column to SP list', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')/fields/getbyid('9b1b1e42-794b-4c71-93ac-5ed92488b67f')`) { + return { + Id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', + Title: 'ColumnName', + FieldTypeKind: 1 + }; + } + + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')?$select=Id,BaseType`) { + return { + Id: "421b1e42-794b-4c71-93ac-5ed92488b67d", + BaseType: 0 + }; + } + + throw `${opts.url} is invalid request`; + }); + + await assert.rejects(command.action(logger, { options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listId: '421b1e42-794b-4c71-93ac-5ed92488b67d', prompt: 'test', verbose: true } }), new CommandError('The specified list is not a document library.')); + }); + + it('corretly handles error when try to apply autofill incorrect column type', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')/fields/getbyid('9b1b1e42-794b-4c71-93ac-5ed92488b67f')`) { + return { + Id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', + Title: 'ColumnName', + FieldTypeKind: 17 + }; + } + + if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/lists(guid'421b1e42-794b-4c71-93ac-5ed92488b67d')?$select=Id,BaseType`) { + return { + Id: "421b1e42-794b-4c71-93ac-5ed92488b67d", + BaseType: 1 + }; + } + + throw `${opts.url} is invalid request`; + }); + + await assert.rejects(command.action(logger, { options: { siteUrl: 'https://contoso.sharepoint.com/sites/sales', columnId: '9b1b1e42-794b-4c71-93ac-5ed92488b67f', listId: '421b1e42-794b-4c71-93ac-5ed92488b67d', prompt: 'test', verbose: true } }), new CommandError('The specified column has incorrect type.')); + }); +}); \ No newline at end of file diff --git a/src/m365/spp/commands/autofillcolumn/autofillcolumn-apply.ts b/src/m365/spp/commands/autofillcolumn/autofillcolumn-apply.ts new file mode 100644 index 0000000000..987def1974 --- /dev/null +++ b/src/m365/spp/commands/autofillcolumn/autofillcolumn-apply.ts @@ -0,0 +1,212 @@ +import { Logger } from '../../../../cli/Logger.js'; +import GlobalOptions from '../../../../GlobalOptions.js'; +import request, { CliRequestOptions } from '../../../../request.js'; +import { formatting } from '../../../../utils/formatting.js'; +import { urlUtil } from '../../../../utils/urlUtil.js'; +import { validation } from '../../../../utils/validation.js'; +import SpoCommand from '../../../base/SpoCommand.js'; +import { ListInstance } from '../../../spo/commands/list/ListInstance.js'; +import commands from '../../commands.js'; + +enum AllowedFieldTypeKind { + Integer = 1, + Text = 2, + Note = 3, + DateTime = 4, + Counter = 5, + Choice = 6, + Boolean = 8, + Number = 9, + Currency = 10, + URL = 11, + Computed = 12, + MultiChoice = 15, + GridChoice = 16, +} + +interface Field { + Id: string; + Title: string; + FieldTypeKind: number; +} + +interface CommandArgs { + options: Options; +} + +interface Options extends GlobalOptions { + siteUrl: string; + listTitle?: string; + listId?: string; + listUrl?: string; + columnId?: string; + columnTitle?: string; + prompt: string; +} + +class SppAutofillcolumnApplyCommand extends SpoCommand { + public get name(): string { + return commands.AUTOFILLCOLUMN_APPLY; + } + + public get description(): string { + return 'Applies the autofill option to the selected column'; + } + + constructor() { + super(); + + this.#initTelemetry(); + this.#initOptions(); + this.#initValidators(); + this.#initOptionSets(); + this.#initTypes(); + } + + #initTelemetry(): void { + this.telemetry.push((args: CommandArgs) => { + Object.assign(this.telemetryProperties, { + listTitle: typeof args.options.listTitle !== 'undefined', + listId: typeof args.options.listId !== 'undefined', + listUrl: typeof args.options.listUrl !== 'undefined', + columnId: typeof args.options.columnId !== 'undefined', + columnTitle: typeof args.options.columnTitle !== 'undefined' + }); + }); + } + + #initOptions(): void { + this.options.unshift( + { + option: '-u, --siteUrl ' + }, + { + option: '--listTitle [listTitle]' + }, + { + option: '--listId [listId]' + }, + { + option: '--listUrl [listUrl]' + }, + { + option: '-i, --columnId [columnId]' + }, + { + option: '-t, --columnTitle [columnTitle]' + }, + { + option: '--prompt ' + } + ); + } + + #initValidators(): void { + this.validators.push( + async (args: CommandArgs) => { + if (args.options.columnId && !validation.isValidGuid(args.options.columnId)) { + return `${args.options.columnId} in parameter columnId is not a valid GUID`; + } + + if (args.options.listId && + !validation.isValidGuid(args.options.listId)) { + return `${args.options.listId} in parameter listId is not a valid GUID`; + } + + return validation.isValidSharePointUrl(args.options.siteUrl); + } + ); + } + + #initOptionSets(): void { + this.optionSets.push({ options: ['listTitle', 'listId', 'listUrl'] }); + this.optionSets.push({ options: ['columnTitle', 'columnId'] }); + } + + #initTypes(): void { + this.types.string.push('siteUrl', 'listTitle', 'listId', 'listUrl', 'columnId', 'columnTitle', 'prompt'); + } + + public async commandAction(logger: Logger, args: CommandArgs): Promise { + try { + if (this.verbose) { + await logger.log(`Applying an autofill column to a column...`); + } + + const siteUrl = urlUtil.removeTrailingSlashes(args.options.siteUrl); + const listInstance = await this.getDocumentLibraryInfo(args); + + if (listInstance.BaseType !== 1) { + throw Error(`The specified list is not a document library.`); + } + + const column = await this.getColumn(args.options, listInstance.Id); + + if (!(column.FieldTypeKind in AllowedFieldTypeKind)) { + throw Error(`The specified column has incorrect type.`); + } + + const requestOptions: CliRequestOptions = { + url: `${siteUrl}/_api/machinelearning/SetSyntexPoweredColumnPrompts`, + headers: { + accept: 'application/json;odata=nometadata' + }, + data: { + docLibId: `{${listInstance.Id}}`, + syntexPoweredColumnPrompts: JSON.stringify([{ columnId: column.Id, columnName: column.Title, prompt: args.options.prompt, isEnabled: true }]) + } + }; + + await request.post(requestOptions); + } + catch (err: any) { + this.handleRejectedODataJsonPromise(err); + } + } + + private getDocumentLibraryInfo(args: CommandArgs): Promise { + let requestUrl = `${args.options.siteUrl}/_api/web`; + + if (args.options.listId) { + requestUrl += `/lists(guid'${formatting.encodeQueryParameter(args.options.listId)}')`; + } + else if (args.options.listTitle) { + requestUrl += `/lists/getByTitle('${formatting.encodeQueryParameter(args.options.listTitle)}')`; + } + else if (args.options.listUrl) { + const listServerRelativeUrl: string = urlUtil.getServerRelativePath(args.options.siteUrl, args.options.listUrl); + requestUrl += `/GetList('${formatting.encodeQueryParameter(listServerRelativeUrl)}')`; + } + + const requestOptions: CliRequestOptions = { + url: `${requestUrl}?$select=Id,BaseType`, + headers: { + 'accept': 'application/json;odata=nometadata' + }, + responseType: 'json' + }; + + return request.get(requestOptions); + } + + private getColumn(options: Options, listId: string): Promise { + let fieldRestUrl: string = ''; + if (options.columnId) { + fieldRestUrl = `/getbyid('${formatting.encodeQueryParameter(options.columnId)}')`; + } + else { + fieldRestUrl = `/getbyinternalnameortitle('${formatting.encodeQueryParameter(options.columnTitle!)}')`; + } + + const requestOptions: CliRequestOptions = { + url: `${options.siteUrl}/_api/web/lists(guid'${formatting.encodeQueryParameter(listId)}')/fields${fieldRestUrl}`, + headers: { + accept: 'application/json;odata=nometadata' + }, + responseType: 'json' + }; + return request.get(requestOptions); + } +} + +export default new SppAutofillcolumnApplyCommand(); \ No newline at end of file