Skip to content

Commit

Permalink
feat: New fields in retention-policy and `retention-policy-assignme…
Browse files Browse the repository at this point in the history
…nt` (#466)
  • Loading branch information
congminh1254 authored Apr 24, 2023
1 parent 1c6073f commit f960e59
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 28 deletions.
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@oclif/plugin-help": "^2.2.1",
"@oclif/plugin-not-found": "^1.2.0",
"archiver": "^3.0.0",
"box-node-sdk": "^2.8.0",
"box-node-sdk": "^2.9.0",
"chalk": "^2.4.1",
"cli-progress": "^2.1.0",
"csv": "^3.1.0",
Expand Down
32 changes: 29 additions & 3 deletions src/commands/retention-policies/assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@ class RetentionPoliciesAssignCommand extends BoxCommand {

let assignType = flags['assign-to-type'];
let assignID = flags['assign-to-id'];
let options = {};

if (flags.hasOwnProperty('filter-field')) {
options.filter_fields = flags['filter-field'];
}
if (flags.hasOwnProperty('start-date-field')) {
options.start_date_field = flags['start-date-field'];
}

if (assignType === 'enterprise') {
assignment = await this.client.retentionPolicies.assign(args.policyID, assignType);
assignment = await this.client.retentionPolicies.assign(args.policyID, assignType, null, options);
} else {
if (!assignID) {
throw new BoxCLIError('An ID of the content to assign the retention policy to is required');
}
assignment = await this.client.retentionPolicies.assign(args.policyID, assignType, assignID);
assignment = await this.client.retentionPolicies.assign(args.policyID, assignType, assignID, options);
}
await this.output(assignment);
}
}

RetentionPoliciesAssignCommand.description = 'Assign a retention policy assignment';
RetentionPoliciesAssignCommand.examples = ['box retention-policies:assign 12345 --assign-to-type folder --assign-to-id 22222'];
RetentionPoliciesAssignCommand.examples = ['box retention-policies:assign 12345 --assign-to-type folder --assign-to-id 22222 --filter-field=fieldName=fieldValue --start-date-field=upload_date'];
RetentionPoliciesAssignCommand._endpoint = 'post_retention_policy_assignments';

RetentionPoliciesAssignCommand.flags = {
Expand All @@ -42,6 +50,24 @@ RetentionPoliciesAssignCommand.flags = {
'assign-to-id': flags.string({
description: 'The ID of the content to assign the retention policy to',
}),
'filter-field': flags.string({
description: 'Metadata fields to filter against, if assigning to a metadata template.' +
'Allow properties: field, value. Example: --filter-field=fieldName=fieldValue',
multiple: true,
parse(input) {
const filter = {};
input = input.split('=');
if (input.length !== 2) {
throw new BoxCLIError('Invalid filter field');
}
filter.field = input[0];
filter.value = input[1];
return filter;
}
}),
'start-date-field': flags.string({
description: 'The date the retention policy assignment begins',
}),
};

RetentionPoliciesAssignCommand.args = [
Expand Down
28 changes: 27 additions & 1 deletion src/commands/retention-policies/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ class RetentionPoliciesCreateCommand extends BoxCommand {
} else if (flags['non-modifiable']) {
options.retention_type = this.client.retentionPolicies.retentionTypes.NON_MODIFIABLE;
}
if (flags.hasOwnProperty('description')) {
options.description = flags.description;
}
if (flags.hasOwnProperty('custom-notification-recipient')) {
options.custom_notification_recipients = flags['custom-notification-recipient'];
}

let policy = await this.client.retentionPolicies.create(args.policyName, policyType, dispositionAction, options);
await this.output(policy);
}
}

RetentionPoliciesCreateCommand.description = 'Create a new retention policy';
RetentionPoliciesCreateCommand.examples = ['box retention-policies:create "Tax Documents" --retention-length 2555 --retention-type "non_modifiable" --disposition-action permanently_delete'];
RetentionPoliciesCreateCommand.examples = ['box retention-policies:create "Tax Documents" --retention-length 2555 --disposition-action=remove_retention --notify-owners --allow-extension --description "Tax documents for 2018" --custom-notification-recipient=id=12345,login=user@box.com'];
RetentionPoliciesCreateCommand._endpoint = 'post_retention_policies';

RetentionPoliciesCreateCommand.flags = {
Expand Down Expand Up @@ -87,6 +93,26 @@ RetentionPoliciesCreateCommand.flags = {
'remove_retention'
]
}),
description: flags.string({
required: false,
description: 'The additional text description of the retention policy'
}),
'custom-notification-recipient': flags.string({
description: 'A list of users notified when the retention policy duration is about to end. ' +
'Allowed properties are: id, type, login, name',
multiple: true,
parse(input) {
const user = {
type: 'user',
};

for (const part of input.split(',')) {
const [key, value] = part.split('=');
user[key] = value;
}
return user;
}
}),
};

RetentionPoliciesCreateCommand.args = [
Expand Down
68 changes: 68 additions & 0 deletions test/commands/retention-policies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,41 @@ describe('Retention Policies', () => {
.it('should send optional params when flags are passed', ctx => {
assert.equal(ctx.stdout, fixture);
});

test
.nock(TEST_API_ROOT, api => api
.post('/2.0/retention_policies', {
...expectedBody,
policy_type: 'finite',
retention_length: retentionLength,
description: 'This is a description',
custom_notification_recipients: [{
type: 'user',
id: '12345',
login: 'user@box.com'
}, {
type: 'user',
id: '34567',
login: 'user2@box.com'
}]
})
.reply(201, fixture)
)
.stdout()
.command([
'retention-policies:create',
policyName,
`--disposition-action=${dispositionAction}`,
`--retention-length=${retentionLength}`,
'--description=This is a description',
'--custom-notification-recipient=id=12345,login=user@box.com',
'--custom-notification-recipient=id=34567,login=user2@box.com',
'--json',
'--token=test'
])
.it('should create a new retention policy with custom description and custom notification recipients', ctx => {
assert.equal(ctx.stdout, fixture);
});
});

describe('retention-policies:update', () => {
Expand Down Expand Up @@ -449,6 +484,7 @@ describe('Retention Policies', () => {
.post('/2.0/retention_policy_assignments', {
...expectedBody,
assign_to: {
id: null,
type: 'enterprise',
}
})
Expand All @@ -466,6 +502,38 @@ describe('Retention Policies', () => {
assert.equal(ctx.stdout, fixture);
});

test
.nock(TEST_API_ROOT, api => api
.post('/2.0/retention_policy_assignments', {
...expectedBody,
assign_to: {
type: 'enterprise',
id: null
},
filter_fields: [
{
field: 'a0f4ee4e-1dc1-4h90-a8a9-aef55fc681d4',
value: '0c27b756-0p87-4fe0-a43a-59fb661ccc4e'
}
],
start_date_field: 'upload_date',
})
.reply(201, fixture)
)
.stdout()
.command([
'retention-policies:assign',
policyId,
'--assign-to-type=enterprise',
'--filter-field=a0f4ee4e-1dc1-4h90-a8a9-aef55fc681d4=0c27b756-0p87-4fe0-a43a-59fb661ccc4e',
'--start-date-field=upload_date',
'--json',
'--token=test'
])
.it('should assign policy to the enterprise with custom filter fields and start date field', ctx => {
assert.equal(ctx.stdout, fixture);
});

// @TODO(2018-08-21): Add test for missing ID error scenario
});

Expand Down

0 comments on commit f960e59

Please sign in to comment.