Skip to content

Commit

Permalink
New command: viva engage community set
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinM85 committed Oct 25, 2024
1 parent fd332f4 commit 4f69f8e
Show file tree
Hide file tree
Showing 7 changed files with 486 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/docs/cmd/viva/engage/engage-community-set.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Global from '/docs/cmd/_global.mdx';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# viva engage community set

Updates an existing Viva Engage community

## Usage

```sh
m365 viva engage community set [options]
```

## Options

```md definition-list
`-i, --id [id]`
: The id of the community. Specify either `id` or `displayName`, but not both.

`-d, --displayName [displayName]`
: The name of the community. Specify either `id` or `displayName`, but not both.

`--newDisplayName [newDisplayName]`
: New name for the community. The maximum length is 255 characters.

`--description [description]`
: The description of the community. The maximum length is 1024 characters.

`--privacy [privacy]`
: Defines the privacy level of the community. The possible values are: `public`, and `private`.
```

<Global />

## Examples

Update info about the community specified by id

```sh
m365 viva engage community set --id eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI0NzY5MTM1ODIwOSJ9 --newDisplayName 'Developers' --description 'Community for all devs' --privacy public
```

Update info about the community specified by name

```sh
m365 viva engage community set --displayName 'Developrs' --newDisplayName 'Developers'
```

## Response

The command won't return a response on success.
5 changes: 5 additions & 0 deletions docs/src/config/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4489,6 +4489,11 @@ const sidebars: SidebarsConfig = {
label: 'engage community list',
id: 'cmd/viva/engage/engage-community-list'
},
{
type: 'doc',
label: 'engage community set',
id: 'cmd/viva/engage/engage-community-set'
},
{
type: 'doc',
label: 'engage group list',
Expand Down
1 change: 1 addition & 0 deletions src/m365/viva/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default {
ENGAGE_COMMUNITY_ADD: `${prefix} engage community add`,
ENGAGE_COMMUNITY_GET: `${prefix} engage community get`,
ENGAGE_COMMUNITY_LIST: `${prefix} engage community list`,
ENGAGE_COMMUNITY_SET: `${prefix} engage community set`,
ENGAGE_GROUP_LIST: `${prefix} engage group list`,
ENGAGE_GROUP_USER_ADD: `${prefix} engage group user add`,
ENGAGE_GROUP_USER_REMOVE: `${prefix} engage group user remove`,
Expand Down
152 changes: 152 additions & 0 deletions src/m365/viva/commands/engage/engage-community-set.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import assert from 'assert';
import sinon from 'sinon';
import auth from '../../../../Auth.js';
import { Logger } from '../../../../cli/Logger.js';
import { CommandError } from '../../../../Command.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 './engage-community-set.js';
import { CommandInfo } from '../../../../cli/CommandInfo.js';
import { vivaEngage } from '../../../../utils/vivaEngage.js';
import { cli } from '../../../../cli/cli.js';

describe(commands.ENGAGE_COMMUNITY_SET, () => {
const communityId = 'eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI0NzY5MTM1ODIwOSJ9';
const displayName = 'Software Engineers';
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.patch
]);
});

after(() => {
sinon.restore();
auth.connection.active = false;
});

it('has correct name', () => {
assert.strictEqual(command.name, commands.ENGAGE_COMMUNITY_SET);
});

it('has a description', () => {
assert.notStrictEqual(command.description, null);
});

it('passes validation when id is specified', async () => {
const actual = await command.validate({ options: { id: communityId, description: 'Community for all devs' } }, commandInfo);
assert.strictEqual(actual, true);
});

it('passes validation when displayName is specified', async () => {
const actual = await command.validate({ options: { displayName: 'Software Engineers', description: 'Community for all devs' } }, commandInfo);
assert.strictEqual(actual, true);
});

it('fails validation when newDisplayName, description or privacy is not specified', async () => {
const actual = await command.validate({ options: { displayName: 'Software Engineers' } }, commandInfo);
assert.notStrictEqual(actual, true);
});

it('fails validation if newDisplayName is more than 255 characters', async () => {
const actual = await command.validate({
options: {
id: communityId,
newDisplayName: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries."
}
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('fails validation if description is more than 1024 characters', async () => {
const actual = await command.validate({
options: {
displayName: 'Software engineers',
description: `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text.All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet.`
}
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('fails validation when invalid privacy option is provided', async () => {
const actual = await command.validate({
options: {
displayName: 'Software engineers',
privacy: 'invalid'
}
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('updates info about a community specified by id', async () => {
const patchRequestStub = sinon.stub(request, 'patch').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities/${communityId}`) {
return;
}

throw 'Invalid request';
});

await command.action(logger, { options: { id: communityId, newDisplayName: 'Software Engineers', verbose: true } });
assert(patchRequestStub.called);
});

it('updates info about a community specified by displayName', async () => {
sinon.stub(vivaEngage, 'getCommunityIdByDisplayName').resolves(communityId);
const patchRequestStub = sinon.stub(request, 'patch').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities/${communityId}`) {
return;
}

throw 'Invalid request';
});

await command.action(logger, { options: { displayName: displayName, description: 'Community for all devs', privacy: 'Public', verbose: true } });
assert(patchRequestStub.called);
});

it('handles error when updating Viva Engage community failed', async () => {
sinon.stub(request, 'patch').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities/${communityId}`) {
throw { error: { message: 'An error has occurred' } };
}
throw `Invalid request`;
});

await assert.rejects(
command.action(logger, { options: { id: communityId } } as any),
new CommandError('An error has occurred')
);
});
});
140 changes: 140 additions & 0 deletions src/m365/viva/commands/engage/engage-community-set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import GlobalOptions from '../../../../GlobalOptions.js';
import { Logger } from '../../../../cli/Logger.js';
import request, { CliRequestOptions } from '../../../../request.js';
import { vivaEngage } from '../../../../utils/vivaEngage.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';

interface CommandArgs {
options: Options;
}

interface Options extends GlobalOptions {
id?: string;
displayName?: string;
newDisplayName?: string;
description?: string;
privacy?: string;
}

class VivaEngageCommunitySetCommand extends GraphCommand {
private privacyOptions: string[] = ['public', 'private'];

public get name(): string {
return commands.ENGAGE_COMMUNITY_SET;
}

public get description(): string {
return 'Updates an existing Viva Engage community';
}

constructor() {
super();

this.#initTelemetry();
this.#initOptions();
this.#initValidators();
this.#initTypes();
this.#initOptionSets();
}

#initTelemetry(): void {
this.telemetry.push((args: CommandArgs) => {
Object.assign(this.telemetryProperties, {
id: typeof args.options.id !== 'undefined',
displayName: typeof args.options.displayName !== 'undefined',
newDisplayName: typeof args.options.newDisplayName !== 'undefined',
description: typeof args.options.description !== 'undefined',
privacy: typeof args.options.privacy !== 'undefined'
});
});
}

#initOptions(): void {
this.options.unshift(
{
option: '-i, --id [id]'
},
{
option: '-d, --displayName [displayName]'
},
{
option: '--newDisplayName [newDisplayName]'
},
{
option: '--description [description]'
},
{
option: '--privacy [privacy]',
autocomplete: this.privacyOptions
}
);
}

#initValidators(): void {
this.validators.push(
async (args: CommandArgs) => {
if (args.options.newDisplayName && args.options.newDisplayName.length > 255) {
return `The maximum amount of characters for 'newDisplayName' is 255.`;
}

if (args.options.description && args.options.description.length > 1024) {
return `The maximum amount of characters for 'description' is 1024.`;
}

if (args.options.privacy && this.privacyOptions.map(x => x.toLowerCase()).indexOf(args.options.privacy.toLowerCase()) === -1) {
return `${args.options.privacy} is not a valid privacy. Allowed values are ${this.privacyOptions.join(', ')}`;
}

if (!args.options.newDisplayName && !args.options.description && !args.options.privacy) {
return 'Specify at least newDisplayName, description, or privacy.';
}

return true;
}
);
}

#initTypes(): void {
this.types.string.push('id', 'displayName', 'newDisplayName', 'description', 'privacy');
}

#initOptionSets(): void {
this.optionSets.push({ options: ['id', 'displayName'] });
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {

let communityId = args.options.id;

if (args.options.displayName) {
communityId = await vivaEngage.getCommunityIdByDisplayName(args.options.displayName);
}

if (this.verbose) {
await logger.logToStderr(`Updating Viva Engage community with ID ${communityId}...`);
}

const requestOptions: CliRequestOptions = {
url: `${this.resource}/v1.0/employeeExperience/communities/${communityId}`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json',
data: {
description: args.options.description,
displayName: args.options.newDisplayName,
privacy: args.options.privacy
}
};

try {
await request.patch(requestOptions);
}
catch (err: any) {
this.handleRejectedODataJsonPromise(err);
}
}
}

export default new VivaEngageCommunitySetCommand();
Loading

0 comments on commit 4f69f8e

Please sign in to comment.