Skip to content

Commit

Permalink
Merge 8490234 into 9fd5912
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonpereyra committed Oct 6, 2023
2 parents 9fd5912 + 8490234 commit 611291b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/api-list-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ module.exports = class ApiListData extends API {

const { result, total } = await this.fetchData(getParams);

const rows = await this.formatRows(result);

if(this.shouldReturnBody)
if(this.shouldReturnBody) {
const rows = await this.formatRows(result);
this.setBody(rows);
}

if(this.shouldReturnTotal)
this.setHeader(TOTAL_HEADER, total);
Expand All @@ -226,6 +226,7 @@ module.exports = class ApiListData extends API {
async fetchData(getParams) {

const data = {};

if(this.shouldReturnBody) {

data.result = await this.model.get(getParams);
Expand All @@ -235,17 +236,16 @@ module.exports = class ApiListData extends API {
data.total = 0;

return data;

}
}
} else
data.result = [];


if(this.shouldReturnTotal) {

const { total } = await this.model.getTotals(getParams.filters);
data.total = total;

}

return data;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/api-list-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,8 @@ describe('Api List Data', () => {
[true, 'true', '1'].forEach(value => {
it(`Should calculate totals when x-janis-only-totals header received as ${value} ${typeof value}`, async () => {

sinon.spy(ApiListData.prototype, 'formatRows');

const myApiList = getApiInstance(ApiListData, {
headers: { 'x-janis-only-totals': value }
});
Expand All @@ -2167,6 +2169,7 @@ describe('Api List Data', () => {

sinon.assert.calledOnce(MyModel.prototype.getTotals);
sinon.assert.notCalled(MyModel.prototype.get);
sinon.assert.notCalled(ApiListData.prototype.formatRows);

assert.deepStrictEqual(myApiList.response.headers, { 'x-janis-total': 1 });
assert.deepStrictEqual(myApiList.response.body, undefined);
Expand Down

0 comments on commit 611291b

Please sign in to comment.