Skip to content

Commit

Permalink
Merge pull request tenfold#3 from callinize/feature/group-join-filter…
Browse files Browse the repository at this point in the history
…-expression

Added searchExpressions and Group joins support
  • Loading branch information
andreysmyntyna authored May 8, 2018
2 parents ccc1bf9 + ff2c6d9 commit 2bf700d
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ log/*.log
tags
tmp
npm-debug.log

.idea
91 changes: 76 additions & 15 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
this.Searcher = (function() {

/** @constructor */
function Searcher(recordType, batchSize, lowerBound, searchFilters, searchColumns) {
function Searcher(recordType, batchSize, lowerBound, searchFilters, searchColumns, advancedOptions) {
this.SEARCH_FILTER_NAME_KEY = 'name';
this.SEARCH_FILTER_OPERATOR_KEY = 'operator';
this.SEARCH_FILTER_VALUE_KEY = 'value';
Expand All @@ -37,19 +37,31 @@ this.Searcher = (function() {
this.searchFilters = [];
this.searchColumns = [];
this.results = [];
this.advancedOptions = advancedOptions || {
useFilterExpressions: false,
groupJoins: false
};

intBatchSize = parseInt(this.originalBatchSize);
if((intBatchSize % 1000) != 0) {
intBatchSize = intBatchSize + (1000 - (intBatchSize % 1000));
}
this.batchSize = intBatchSize;

this.createSearchFilters();
this.generateLowerBoundFilter();
this.createFilters();
this.createSearchColumns();
this.generateSortColumn();
}

Searcher.prototype.createFilters = function () {
if (this.advancedOptions.useFilterExpressions) {
this.searchFilters = this.rawSearchFilters;
} else {
this.createSearchFilters();
this.generateLowerBoundFilter();
}
}

/**
* Iterates over `rawSearchFilters` calling generating an nlobjSearchFilter
* from each
Expand Down Expand Up @@ -155,10 +167,10 @@ this.Searcher = (function() {
* @return {nlobjSearchFilter} The generated search filter
*/
Searcher.prototype.getSearchFilterObject = function(searchFilterData) {
name = searchFilterData[this.SEARCH_FILTER_NAME_KEY];
operator = searchFilterData[this.SEARCH_FILTER_OPERATOR_KEY];
value = searchFilterData[this.SEARCH_FILTER_VALUE_KEY];
join = searchFilterData[this.SEARCH_FILTER_JOIN_KEY] || null;
var name = searchFilterData[this.SEARCH_FILTER_NAME_KEY];
var operator = searchFilterData[this.SEARCH_FILTER_OPERATOR_KEY];
var value = searchFilterData[this.SEARCH_FILTER_VALUE_KEY];
var join = searchFilterData[this.SEARCH_FILTER_JOIN_KEY] || null;

filter = NetsuiteToolkit.searchFilter(name, join, operator, value);
return filter;
Expand All @@ -174,8 +186,8 @@ this.Searcher = (function() {
*/
Searcher.prototype.createSearchColumns = function() {
for(index in this.rawSearchColumns) {
searchColumnData = this.rawSearchColumns[index];
searchColumnObject = this.getSearchColumnObject(searchColumnData);
var searchColumnData = this.rawSearchColumns[index];
var searchColumnObject = this.getSearchColumnObject(searchColumnData);
this.searchColumns.push(searchColumnObject);
}
}
Expand Down Expand Up @@ -203,9 +215,9 @@ this.Searcher = (function() {
* @return {nlobjSearchColumn} The generated search column
*/
Searcher.prototype.getSearchColumnObject = function(searchColumnData) {
name = searchColumnData[this.SEARCH_COLUMN_NAME_KEY];
join = searchColumnData[this.SEARCH_COLUMN_JOIN_KEY];
column = NetsuiteToolkit.searchColumn(name, join);
var name = searchColumnData[this.SEARCH_COLUMN_NAME_KEY];
var join = searchColumnData[this.SEARCH_COLUMN_JOIN_KEY];
var column = NetsuiteToolkit.searchColumn(name, join);
return column;
}

Expand Down Expand Up @@ -328,9 +340,56 @@ this.Searcher = (function() {
* @return {null}
*/
Searcher.prototype.appendResults = function(resultsBlock) {
this.results = this.results.concat(resultsBlock);
if (this.advancedOptions.groupJoins) {
this.appendResultsGroupJoins(resultsBlock);
} else {
this.results = this.results.concat(resultsBlock);
}
}

/**
* Concatenates the given result set onto the result list and group join fields into nested object
*
* @method
* @param {Array} resultsBlock The array containing a set of results from Netsuite
* @memberof Searcher
* @return {null}
*/
Searcher.prototype.appendResultsGroupJoins = function(resultsBlock) {
var newResultsBlock = [];
for ( var i = 0; resultsBlock != null && i < resultsBlock.length; i++ )
{
var newResult = {"id":resultsBlock[i].getId(), "recordtype":resultsBlock[i].getRecordType(), "columns":{}};
var result = resultsBlock[i];
var columns = result.getAllColumns();
var columnLen = columns.length;

for (var x = 0; x < columnLen; x++) {
var column = columns[x];
var name = column.getName();
var join = column.getJoin();
var value = result.getValue(column);
var text = result.getText(column);
var joinTitle = join;

if (join) {
if(!(joinTitle in newResult.columns)) newResult.columns[joinTitle] = {};
newResult.columns[joinTitle][name] = value;
if(text && text !== value) {
newResult.columns[joinTitle][name + '.text'] = text;
}
} else {
newResult.columns[name] = value;
if(text && text !== value) {
newResult.columns[name + '.text'] = text;
}
}
}
newResultsBlock.push(newResult);
}
this.results = this.results.concat(newResultsBlock);
}

/**
* Generates a list of params given in the HTTP request
*
Expand All @@ -345,6 +404,8 @@ this.Searcher = (function() {
params['lower_bound'] = this.originalLowerBound;
params['search_filters'] = this.rawSearchFilters;
params['search_columns'] = this.rawSearchColumns;
params['advanced_options'] = this.advancedOptions;

return params;
}

Expand All @@ -367,15 +428,15 @@ this.Searcher = (function() {

/**
* The script function that Netsuite will call to execute the Search process
*
*
* @method
* @param {object} request The object representing the HTTP request body
* @memberof global
* @return {object} The formatted results of the request
*/
var searchPostHandler = function(request) {
searcher = new Searcher(request['record_type'], request['batch_size'], request['lower_bound'],
request['search_filters'], request['search_columns']);
request['search_filters'], request['search_columns'], request['advanced_options']);
searcher.executeSearch();
return searcher.reply();
}
65 changes: 63 additions & 2 deletions spec/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ describe("Searcher", function() {
'operator': 'anyof'
}
];
var searchExpression = [
['displayname', 'is', 'VENDOR-STYLE-SIZE' ],
'and',
['upccode', 'anyof', ['123456789012', '098765432109']]
];
var searchColumns = [
{
'name': 'custitem22',
Expand All @@ -42,6 +47,7 @@ describe("Searcher", function() {
describe('#init(recordType, batchSize, lowerBound, searchFilters, searchColumns', function() {

beforeEach(function() {
spyOn(Searcher.prototype, 'createFilters').andCallThrough();
spyOn(Searcher.prototype, 'createSearchFilters');
spyOn(Searcher.prototype, 'generateLowerBoundFilter');
spyOn(Searcher.prototype, 'createSearchColumns');
Expand Down Expand Up @@ -170,6 +176,38 @@ describe("Searcher", function() {

});

describe('#init(recordType, batchSize, lowerBound, searchFilters, searchColumns, advancedOptions', function() {

beforeEach(function() {
spyOn(Searcher.prototype, 'createFilters').andCallThrough();
spyOn(Searcher.prototype, 'createSearchFilters');
spyOn(Searcher.prototype, 'generateLowerBoundFilter');
spyOn(Searcher.prototype, 'createSearchColumns');
spyOn(Searcher.prototype, 'generateSortColumn');

var advancedOptions = {
useFilterExpressions: true,
groupJoins: true
};
this.newSearcher = new Searcher(recordType, batchSize, lowerBound,
searchExpression, searchColumns, advancedOptions);
});

it("should set the searchFilters to an provided Expression", function() {
expect(this.newSearcher.searchFilters).toEqual(searchExpression);
});

it("should NOT call createSearchFilters", function() {
expect(this.newSearcher.createSearchFilters).not.toHaveBeenCalled();
});

it("should NOT call generateLowerBoundFilter", function() {
expect(this.newSearcher.generateLowerBoundFilter).not.toHaveBeenCalled();
});

});


describe('#createSearchFilters', function() {

beforeEach(function() {
Expand Down Expand Up @@ -291,7 +329,7 @@ describe("Searcher", function() {
spyOn(searcher, 'getSearchFilterObject').andReturn(netsuiteSearchFilterObject);
searcher.generateLowerBoundFilter();
});

it("should call getSearchFilterObject with the current lowerBound", function() {
expect(searcher.getSearchFilterObject).toHaveBeenCalledWith(this.searchFilterData);
});
Expand Down Expand Up @@ -518,6 +556,29 @@ describe("Searcher", function() {

});

describe('#searchIteration (Group join)', function() {
it("should call appendResultsGroupJoins", function() {
this.resultsBlock = [{}];
spyOn(searcher, 'appendResultsGroupJoins');
searcher.advancedOptions = {
groupJoins: true
};
searcher.appendResults(this.resultsBlock);
expect(searcher.appendResultsGroupJoins).toHaveBeenCalledWith(this.resultsBlock);
});

it("should NOT call appendResultsGroupJoins", function() {
this.resultsBlock = [{}];
spyOn(searcher, 'appendResultsGroupJoins');
searcher.advancedOptions = {
groupJoins: false
};
searcher.appendResults(this.resultsBlock);
expect(searcher.appendResultsGroupJoins).not.toHaveBeenCalled();
});
});


describe('#isExecutionDone(resultsBlock)', function() {

it("should be true if resultsBlock is undefined", function() {
Expand Down Expand Up @@ -622,7 +683,7 @@ describe("Searcher", function() {
this.resultsBlock = ([{}, {}, {}, {}, this.resultRow]);
this.recordId = searcher.extractLowerBound(this.resultsBlock);
});

it("should call getId on the resultRow", function() {
expect(this.resultRow.getId).toHaveBeenCalled();
});
Expand Down
6 changes: 3 additions & 3 deletions spec/upsert.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Upserter', function() {
});

it('should initialize the reply_list as an empty Array', function() {
expect(upserter.reply_list).toEqual([]);
expect(upserter.reply_list).toEqual([]);
});

it('should initialize the exception to null', function() {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('Upserter', function() {
});

it('should call formatReply on NetsuiteToolkit', function() {
expect(NetsuiteToolkit.formatReply).toHaveBeenCalledWith(upserter.params, upserter.replyList);
expect(NetsuiteToolkit.formatReply).toHaveBeenCalledWith(upserter.params, upserter.reply_list);
});

it('should return the output of formatreply', function() {
Expand Down Expand Up @@ -296,7 +296,7 @@ describe('UpsertRequest', function() {
spyOn(this.fake_processor, 'execute');
upsert_request.executeSublistProcessor(sublist_one);
});

it('should call the constructor of NetsuiteToolkit.SublistProcessor', function() {
expect(NetsuiteToolkit.SublistProcessor).toHaveBeenCalledWith(upsert_request.record,
sublist_one);
Expand Down

0 comments on commit 2bf700d

Please sign in to comment.