Skip to content

Conversation

@mgrochalskisoldevelo
Copy link
Contributor

Created handling for offset and limit parameters.
Added endpoint to get products count

Description
Acceptance criteria:

  • Endpoint supports COUNT request (or parameter)
    
  • Endpoint supports “offset” and “limit” parameters
    

TICKET: https://govstack-global.atlassian.net/browse/TECH-408

* Fix directory structure for server
* Fix type in error message
* Fix that yarn/npm installs run after source files are copied
Comment on lines 14 to 15
const limit = FiltersValidation.validateFilter(this.req.query.limit, Infinity);
const offset = FiltersValidation.validateFilter(this.req.query.offset, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is behavior when offset exceeds number of entries in database?
Should we pass count and nextOffset as a part of the payload or is it redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I tested doesn't show results

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We agreed with Karolina that she will check if there is more records to get, so I think it's not necessary. What do you think @dborowiecki ?

Comment on lines 2 to 6
static validateFilter(filter, defaultValue) {
return filter === undefined || !filter.match(/^\d+$/)
? defaultValue
: filter;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should allow Infinity on regular calls.
Can you add check for max limit (either configurable or fixed on 1k) just for safety measures?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, please confirm if the solution meets your expectations @dborowiecki

Copy link
Contributor

@dborowiecki dborowiecki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should execute "generic" pagination inside of requests directly. I'd suggest to implement middleware that can be attached to many requests instead.


static maxLimitCheck(limit) {
if (limit > this.MAX_REPORTS_IN_REQUEST) {
console.log(`Provided limit parameter: ${limit} exceeded maximum limit: ${this.MAX_REPORTS_IN_REQUEST} and has been set to: ${this.MAX_REPORTS_IN_REQUEST}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change to warning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed


let validatedLimit;
validatedLimit = FiltersValidation.isFilterValid(limit) ? limit : null;
validatedLimit = FiltersValidation.maxLimitCheck(validatedLimit);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is behavior in case of validatedLimit===null?
Also I think we can refactor this validation. I think there's no need to have single function for offset and limit. In addition FiltersValidation can be renamed to be more descriptive (those filters are specific to pagination). Also with that much code I don't like putting this lines inside of request handler directly. It can be tricky to manage with more handlers. Can we create separate middleware function that would be attached to routing for endpoints with pagination? This way we can ensure consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added middleware to handle pagination filters, please let me know if that is what you meant @dborowiecki

ReportModel.aggregate(getLatestReportPipeline(filters)).exec(callback);
const aggregation = ReportModel.aggregate(getLatestReportPipeline(filters));

if (filters.validatedOffset !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change attribute names to be consistent with db naming conventions? In the "repository" we don't have information if offset is or is not valid, also it can be used in different context depending on business logic. I think skip and limit could be better names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to offset and limit

@@ -0,0 +1,16 @@
/* eslint-disable no-console */
module.exports = class FiltersValidation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add information regarding pagination to class name. If we decide to use middleware then function should also provide name

@dborowiecki dborowiecki merged commit daff284 into main Mar 7, 2023
@dborowiecki dborowiecki deleted the TECH-408/AddPaginationToGetReportEndpoint branch March 7, 2023 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants