Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
Signed-off-by: Shaanjot Gill <[email protected]>
  • Loading branch information
shaangill025 committed Feb 25, 2025
1 parent 986df68 commit 854b13e
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 30 deletions.
14 changes: 9 additions & 5 deletions strr-api/src/strr_api/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ def find_by_account(
query = query.filter_by(status=filter_criteria.status.upper())
if filter_criteria.registration_type:
query = query.filter_by(registration_type=filter_criteria.registration_type.upper())
sort_column = getattr(Application, filter_criteria.sort_by, Application.application_date)
if filter_criteria.sort_order and filter_criteria.sort_order.lower() == "desc":
query = query.order_by(sort_column.desc())
sort_column = getattr(Application, filter_criteria.sort_by, Application.id)
if filter_criteria.sort_order and filter_criteria.sort_order.lower() == "asc":
query = query.order_by(sort_column.asc())
else:
query = query.order_by(sort_column)
query = query.order_by(sort_column.desc())

paginated_result = query.paginate(per_page=filter_criteria.limit, page=filter_criteria.page)
return paginated_result
Expand All @@ -197,7 +197,11 @@ def search_applications(cls, filter_criteria: ApplicationSearch):
query = query.filter_by(status=filter_criteria.status.upper())
else:
query = query.filter(Application.status != Application.Status.DRAFT)
query = query.order_by(Application.id.desc())
sort_column = getattr(Application, filter_criteria.sort_by, Application.id)
if filter_criteria.sort_order and filter_criteria.sort_order.lower() == "asc":
query = query.order_by(sort_column.asc())
else:
query = query.order_by(sort_column.desc())
paginated_result = query.paginate(per_page=filter_criteria.limit, page=filter_criteria.page)
return paginated_result

Expand Down
4 changes: 2 additions & 2 deletions strr-api/src/strr_api/models/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ApplicationSearch:
page: int
limit: int
status: str
sort_by: str = "application_date"
sort_order: str = "asc"
sort_by: str = "id"
sort_order: str = "desc"
search_text: str = None
registration_type: str = None
41 changes: 32 additions & 9 deletions strr-api/src/strr_api/resources/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ def get_applications():
- in: query
name: sortBy
type: string
default: application_date
default: id
description: Field to sort by (e.g., application_date, id, status)
- in: query
name: sortOrder
type: string
enum: [asc, desc]
default: asc
default: desc
description: Sort order (ascending or descending)
responses:
200:
Expand All @@ -195,19 +195,19 @@ def get_applications():
page = request.args.get("page", 1)
limit = request.args.get("limit", 50)
registration_type = request.args.get("registrationType")
sort_by = request.args.get("sortBy", "application_date")
sort_order = request.args.get("sortOrder", "asc")
sort_by = request.args.get("sortBy", "id")
sort_order = request.args.get("sortOrder", "desc")
if sort_by not in VALID_SORT_FIELDS:
sort_by = "application_date"
sort_by = "id"
if sort_order not in ["asc", "desc"]:
sort_order = "asc"
sort_order = "desc"
filter_criteria = ApplicationSearch(
status=status,
page=int(page),
limit=int(limit),
registration_type=registration_type,
sort_by=sort_by,
sort_order=sort_order
sort_order=sort_order,
)
application_list = ApplicationService.list_applications(account_id, filter_criteria=filter_criteria)
return jsonify(application_list), HTTPStatus.OK
Expand Down Expand Up @@ -754,6 +754,17 @@ def search_applications():
name: limit
type: integer
default: 50
- in: query
name: sortBy
type: string
default: id
description: Field to sort by (e.g., application_date, id, status)
- in: query
name: sortOrder
type: string
enum: [asc, desc]
default: desc
description: Sort order (ascending or descending)
responses:
200:
description:
Expand All @@ -767,11 +778,23 @@ def search_applications():
status = request.args.get("status", None)
page = request.args.get("page", 1)
limit = request.args.get("limit", 50)

sort_by = request.args.get("sortBy", "id")
sort_order = request.args.get("sortOrder", "desc")
if sort_by not in VALID_SORT_FIELDS:
sort_by = "id"
if sort_order not in ["asc", "desc"]:
sort_order = "desc"
if search_text and len(search_text) < 3:
return error_response(HTTPStatus.BAD_REQUEST, "Search term must be at least 3 characters long.")

filter_criteria = ApplicationSearch(status=status, page=int(page), limit=int(limit), search_text=search_text)
filter_criteria = ApplicationSearch(
status=status,
page=int(page),
limit=int(limit),
search_text=search_text,
sort_by=sort_by,
sort_order=sort_order,
)

application_list = ApplicationService.search_applications(filter_criteria=filter_criteria)
return jsonify(application_list), HTTPStatus.OK
Expand Down
162 changes: 148 additions & 14 deletions strr-api/tests/postman/strr-api.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
"response": []
},
{
"name": "List applications (default sort)",
"name": "List applications (sort by application_date ascending)",
"event": [
{
"listen": "test",
Expand Down Expand Up @@ -346,19 +346,29 @@
}
],
"url": {
"raw": "{{api_url}}/applications",
"raw": "{{api_url}}/applications?sortBy=application_date&sortOrder=asc",
"host": [
"{{api_url}}"
],
"path": [
"applications"
],
"query": [
{
"key": "sortBy",
"value": "application_date"
},
{
"key": "sortOrder",
"value": "asc"
}
]
}
},
"response": []
},
{
"name": "List applications (sort by id descending)",
"name": "List applications (default sort)",
"event": [
{
"listen": "test",
Expand Down Expand Up @@ -417,22 +427,12 @@
}
],
"url": {
"raw": "{{api_url}}/applications?sortBy=id&sortOrder=desc",
"raw": "{{api_url}}/applications",
"host": [
"{{api_url}}"
],
"path": [
"applications"
],
"query": [
{
"key": "sortBy",
"value": "id"
},
{
"key": "sortOrder",
"value": "desc"
}
]
}
},
Expand Down Expand Up @@ -553,6 +553,45 @@
},
{
"name": "Search Applications",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var jsonData = pm.response.json();",
"",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"pm.test(\"Response contains applications array\", function () {",
" pm.expect(jsonData).to.have.property('applications');",
" pm.expect(jsonData.applications).to.be.an('array');",
"});",
"",
"pm.test(\"Applications are sorted by id in descending order by default\", function () {",
" if (jsonData.applications.length < 2) {",
" pm.test.skip(\"Not enough applications to verify sorting\");",
" return;",
" }",
" ",
" let isSorted = true;",
" for (let i = 1; i < jsonData.applications.length; i++) {",
" const prevId = jsonData.applications[i-1].header.registrationId || 0;",
" const currId = jsonData.applications[i].header.registrationId || 0;",
" if (prevId < currId) {",
" isSorted = false;",
" break;",
" }",
" }",
" pm.expect(isSorted).to.be.true;",
"});"
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"auth": {
"type": "bearer",
Expand Down Expand Up @@ -599,6 +638,101 @@
},
"response": []
},
{
"name": "Search Applications (sort by application_date ascending)",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var jsonData = pm.response.json();",
"",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"pm.test(\"Response contains applications array\", function () {",
" pm.expect(jsonData).to.have.property('applications');",
" pm.expect(jsonData.applications).to.be.an('array');",
"});",
"",
"pm.test(\"Applications are sorted by application_date in ascending order\", function () {",
" if (jsonData.applications.length < 2) {",
" pm.test.skip(\"Not enough applications to verify sorting\");",
" return;",
" }",
" ",
" let isSorted = true;",
" for (let i = 1; i < jsonData.applications.length; i++) {",
" const prevDate = new Date(jsonData.applications[i-1].header.applicationDateTime);",
" const currDate = new Date(jsonData.applications[i].header.applicationDateTime);",
" if (prevDate > currDate) {",
" isSorted = false;",
" break;",
" }",
" }",
" pm.expect(isSorted).to.be.true;",
"});"
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{token}}",
"type": "string"
}
]
},
"method": "GET",
"header": [
{
"key": "Account-Id",
"value": "{{account_id}}",
"type": "text"
}
],
"url": {
"raw": "{{api_url}}/applications/search?text=018-851-570&limit=10&page=1&sortBy=application_date&sortOrder=asc",
"host": [
"{{api_url}}"
],
"path": [
"applications",
"search"
],
"query": [
{
"key": "text",
"value": "018-851-570"
},
{
"key": "limit",
"value": "10"
},
{
"key": "page",
"value": "1"
},
{
"key": "sortBy",
"value": "application_date"
},
{
"key": "sortOrder",
"value": "asc"
}
]
}
},
"response": []
},
{
"name": "Staff - Get Application LTSA",
"event": [
Expand Down

0 comments on commit 854b13e

Please sign in to comment.