Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nanddeepn committed Dec 18, 2023
1 parent e37cc14 commit 2e72b1c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
27 changes: 16 additions & 11 deletions src/m365/spo/commands/site/site-recyclebinitem-restore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
import commands from '../../commands.js';
import command from './site-recyclebinitem-restore.js';
import { settingsNames } from '../../../../settingsNames.js';

describe(commands.SITE_RECYCLEBINITEM_RESTORE, () => {
let cli: Cli;

Check failure on line 17 in src/m365/spo/commands/site/site-recyclebinitem-restore.spec.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Cannot find name 'Cli'.
Expand Down Expand Up @@ -47,7 +46,8 @@ describe(commands.SITE_RECYCLEBINITEM_RESTORE, () => {

afterEach(() => {
sinonUtil.restore([
request.post
request.post,
cli.getSettingWithDefaultValue
]);
});

Expand Down Expand Up @@ -99,17 +99,22 @@ describe(commands.SITE_RECYCLEBINITEM_RESTORE, () => {
assert.strictEqual(actual, true);
});

it('fails validation if ids, allPrimary, and allSecondary options are passed (multiple options)', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
return false;
it('validates for a correct input with ids', async () => {
const actual = await command.validate({
options: {
siteUrl: 'https://contoso.sharepoint.com', ids: '5fb84a1f-6ab5-4d07-a6aa-31bba6de9526,5fb84a1f-6ab5-4d07-a6aa-31bba6de9527'
}
}, commandInfo);
assert.strictEqual(actual, true);
});

return defaultValue;
});

const actual = await command.validate({ options: { ids: '5fb84a1f-6ab5-4d07-a6aa-31bba6de9526,5fb84a1f-6ab5-4d07-a6aa-31bba6de9527', allPrimary: true, allSecondary: true } }, commandInfo);
assert.notStrictEqual(actual, true);
it('validates for a correct input with allPrimary and allSecondary', async () => {
const actual = await command.validate({
options: {
siteUrl: 'https://contoso.sharepoint.com', allPrimary: true, allSecondary: true
}
}, commandInfo);
assert.strictEqual(actual, true);
});

it('restores specified items from the recycle bin', async () => {
Expand Down
45 changes: 24 additions & 21 deletions src/m365/spo/commands/site/site-recyclebinitem-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class SpoSiteRecycleBinItemRestoreCommand extends SpoCommand {
this.optionSets.push(
{
options: ['ids'],
runsWhen: (args) => args.options.allPrimary !== undefined && args.options.allSecondary !== undefined
runsWhen: (args) => args.options.allPrimary === undefined && args.options.allSecondary === undefined
}
);
}
Expand All @@ -90,18 +90,9 @@ class SpoSiteRecycleBinItemRestoreCommand extends SpoCommand {

let requestUrl: string = `${args.options.siteUrl}/_api`;

if (args.options.ids) {
requestUrl += `/site/RecycleBin/RestoreByIds`;
}
else if (args.options.allPrimary) {
requestUrl += `/web/RecycleBin/RestoreAll`;
}
else if (args.options.allSecondary) {
requestUrl += `/site/RecycleBin/RestoreAll`;
}

try {
if (args.options.ids) {
requestUrl += `/site/RecycleBin/RestoreByIds`;
const ids: string[] = formatting.splitAndTrim(args.options.ids);
const idsChunks: string[][] = [];

Expand All @@ -128,22 +119,34 @@ class SpoSiteRecycleBinItemRestoreCommand extends SpoCommand {
);
}
else {
const requestOptions: CliRequestOptions = {
url: requestUrl,
headers: {
'accept': 'application/json;odata=nometadata',
'content-type': 'application/json'
},
responseType: 'json'
};

await request.post(requestOptions);
if (args.options.allPrimary) {
requestUrl += `/web/RecycleBin/RestoreAll`;
await this.restoreRecycleBinItems(requestUrl);
}

if (args.options.allSecondary) {
requestUrl += `/site/RecycleBin/RestoreAll`;
await this.restoreRecycleBinItems(requestUrl);
}
}
}
catch (err: any) {
this.handleRejectedODataJsonPromise(err);
}
}

private async restoreRecycleBinItems(requestUrl: string): Promise<void> {
const requestOptions: CliRequestOptions = {
url: requestUrl,
headers: {
'accept': 'application/json;odata=nometadata',
'content-type': 'application/json'
},
responseType: 'json'
};

return await request.post(requestOptions);
}
}

export default new SpoSiteRecycleBinItemRestoreCommand();

0 comments on commit 2e72b1c

Please sign in to comment.