Skip to content

Commit

Permalink
Adding getDataRetetionPeriod to AlSession (#328)
Browse files Browse the repository at this point in the history
* Adding getDataRetetionPeriod to AlSession

* adding unit test

* fix lint
  • Loading branch information
dev4ndy authored Oct 19, 2023
1 parent d9b8d34 commit 8c3f3b3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@al/core",
"version": "1.2.3",
"version": "1.2.4",
"description": "Node Enterprise Packages for Alert Logic (NEPAL) Core Library",
"main": "./dist/index.cjs.js",
"types": "./dist/index.d.ts",
Expand Down
27 changes: 26 additions & 1 deletion src/session/al-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
deepMerge
} from "../common/utility";
import { SubscriptionsClient } from "../subscriptions-client";
import { AlEntitlementCollection } from "../subscriptions-client/types";
import { AlEntitlementCollection, DefaultDataRetentionPolicy } from "../subscriptions-client/types";
import {
AlActingAccountChangedEvent,
AlActingAccountResolvedEvent,
Expand Down Expand Up @@ -627,6 +627,31 @@ export class AlSessionInstance
this.resolvedAccount.entitlements = collection;
}

/**
* Get the data retention period in months based on the product's entitlement.
* If the entitlement is not available or the unit is unrecognized, the default value is used.
* @returns {number} The data retention period in months.
*/
public getDataRetetionPeriod(): number {
try {
const product = this.resolvedAccount.entitlements.getProduct( 'log_data_retention' );
let durationUnit = product?.value_type || DefaultDataRetentionPolicy.Unit;
let durationValue = product?.value || DefaultDataRetentionPolicy.Value;

if ( !['months', 'years'].includes( durationUnit ) ) {
console.warn( "The retention policy period is not recognized, the default retention period will be used." );
durationUnit = DefaultDataRetentionPolicy.Unit;
durationValue = DefaultDataRetentionPolicy.Value;
}

const durationMonths = durationUnit === 'years' ? durationValue * 12 : durationValue;
return durationMonths;
} catch ( error ) {
console.warn( "An error occurred while fetching the retention policy, the default retention period will be used." );
return DefaultDataRetentionPolicy.Value;
}
}

/**
* Convenience method to retrieve the array of accounts managed by the current acting account (or a specific
* other account, if specified)..
Expand Down
33 changes: 33 additions & 0 deletions test/session/al-session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
exampleActing,
exampleSession,
} from '../mocks/session-data.mocks';
import { DefaultDataRetentionPolicy } from "../../src/subscriptions-client";



const sessionDescriptor = {
Expand Down Expand Up @@ -553,6 +555,37 @@ describe('AlSession', () => {
} );
} );

describe('getDataRetentionPeriod', () => {
it('should return the data retention period in months for valid input', () => {
// Mock resolvedAccount.entitlements.getProduct to return a valid product
session['resolvedAccount'].entitlements.getProduct = () => ({ value_type: 'months', value: 21, productId: 'log_data_retention', active: true, expires: new Date() });

const result = session.getDataRetetionPeriod();

expect(result).to.equal(21);
});

it('should return the default data retention period for unrecognized unit', () => {
// Mock resolvedAccount.entitlements.getProduct to return an unrecognized unit
session['resolvedAccount'].entitlements.getProduct = () => ({ value_type: 'days', value: 6, productId: 'log_data_retention', active: true, expires: new Date() });

const result = session.getDataRetetionPeriod();

expect(result).to.equal(DefaultDataRetentionPolicy.Value);
});

it('should return the default data retention period on error', () => {
// Mock resolvedAccount.entitlements.getProduct to throw an error
session['resolvedAccount'].entitlements.getProduct = () => {
throw new Error('Some error');
}

const result = session.getDataRetetionPeriod();

expect(result).to.equal(DefaultDataRetentionPolicy.Value);
});
});

} );

} );

0 comments on commit 8c3f3b3

Please sign in to comment.