Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
holi-app-donations
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
app
holi-app-donations
Commits
37438b5e
Commit
37438b5e
authored
1 year ago
by
Stephanie Freitag
Browse files
Options
Downloads
Patches
Plain Diff
HOLI-5487: apply review hints
parent
e8e846fb
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/betterplace.ts
+8
-23
8 additions, 23 deletions
app/betterplace.ts
app/betterplace_test.ts
+10
-13
10 additions, 13 deletions
app/betterplace_test.ts
with
18 additions
and
36 deletions
app/betterplace.ts
+
8
−
23
View file @
37438b5e
...
@@ -256,24 +256,6 @@ const randomPageForToday = () =>
...
@@ -256,24 +256,6 @@ const randomPageForToday = () =>
(
new
Date
().
setUTCHours
(
0
,
0
,
0
,
0
)
/
100000
)
%
MAX_PAGE_FOR_RANDOMIZATION
+
(
new
Date
().
setUTCHours
(
0
,
0
,
0
,
0
)
/
100000
)
%
MAX_PAGE_FOR_RANDOMIZATION
+
2
;
2
;
const
fetchRandomPage
=
(
pageNumber
:
number
,
language
:
BetterPlaceLanguage
=
DEFAULT_LANGUAGE
,
):
Promise
<
ProjectsResponse
>
=>
{
logger
.
info
(
`fetching random projects from page
${
pageNumber
}
`
);
const
start
=
Date
.
now
();
return
fetchPageOfProjects
({
offset
:
pageNumber
*
DEFAULT_PAGE_SIZE
,
limit
:
DEFAULT_PAGE_SIZE
,
},
language
)
.
finally
(()
=>
{
const
duration
=
Date
.
now
()
-
start
;
logger
.
debug
(
`fetching random projects from page
${
pageNumber
}
took
${
duration
}
ms`
,
);
});
};
export
const
fetchProjects
=
(
export
const
fetchProjects
=
(
{
offset
=
0
,
limit
=
DEFAULT_PAGE_SIZE
,
location
}:
ProjectsParameters
,
{
offset
=
0
,
limit
=
DEFAULT_PAGE_SIZE
,
location
}:
ProjectsParameters
,
language
:
BetterPlaceLanguage
=
DEFAULT_LANGUAGE
,
language
:
BetterPlaceLanguage
=
DEFAULT_LANGUAGE
,
...
@@ -283,11 +265,14 @@ export const fetchProjects = (
...
@@ -283,11 +265,14 @@ export const fetchProjects = (
const
isFiltering
=
!!
location
;
const
isFiltering
=
!!
location
;
const
isFirstPageOfUnfilteredList
=
offset
<
limit
&&
!
isFiltering
;
const
isFirstPageOfUnfilteredList
=
offset
<
limit
&&
!
isFiltering
;
if
(
isFirstPageOfUnfilteredList
)
{
if
(
isFirstPageOfUnfilteredList
)
{
return
fetchRandomPage
(
randomPage
,
language
)
logger
.
info
(
`fetching random projects from page
${
randomPage
}
`
);
.
then
((
result
)
=>
({
return
fetchPageOfProjects
({
...
result
,
offset
:
randomPage
*
DEFAULT_PAGE_SIZE
,
data
:
result
.
data
.
slice
(
0
,
limit
),
limit
:
DEFAULT_PAGE_SIZE
,
}));
},
language
).
then
((
result
)
=>
({
...
result
,
data
:
result
.
data
.
slice
(
0
,
limit
),
}));
}
}
// After one randomly selected page, "normal" pagination starts with offset 0 and skips the random page that was already displayed
// After one randomly selected page, "normal" pagination starts with offset 0 and skips the random page that was already displayed
const
afterRandomPage
=
offset
>
randomPage
*
DEFAULT_PAGE_SIZE
;
const
afterRandomPage
=
offset
>
randomPage
*
DEFAULT_PAGE_SIZE
;
...
...
This diff is collapsed.
Click to expand it.
app/betterplace_test.ts
+
10
−
13
View file @
37438b5e
...
@@ -43,6 +43,9 @@ const emptyResponse = {
...
@@ -43,6 +43,9 @@ const emptyResponse = {
type
UrlPrefix
=
string
;
type
UrlPrefix
=
string
;
const
projectsUrlWithDefaultOptions
=
"
https://api.betterplace.org/de/api_v4/projects.json?facets=completed%3Afalse%7Cclosed%3Afalse%7Cprohibit_donations%3Afalse&order=rank%3ADESC
"
;
const
stubFetchByUrlPrefix
=
(
const
stubFetchByUrlPrefix
=
(
responses
:
Record
<
UrlPrefix
,
ResponsePayload
>
,
responses
:
Record
<
UrlPrefix
,
ResponsePayload
>
,
)
=>
{
)
=>
{
...
@@ -175,10 +178,7 @@ const queryProject = async (
...
@@ -175,10 +178,7 @@ const queryProject = async (
};
};
const
urlForPage
=
(
page
=
1
,
pageSize
=
10
)
=>
const
urlForPage
=
(
page
=
1
,
pageSize
=
10
)
=>
new
URL
(
new
URL
(
`
${
projectsUrlWithDefaultOptions
}
&page=
${
page
}
&per_page=
${
pageSize
}
`
);
"
https://api.betterplace.org/de/api_v4/projects.json?
"
+
`facets=completed%3Afalse%7Cclosed%3Afalse%7Cprohibit_donations%3Afalse&order=rank%3ADESC&page=
${
page
}
&per_page=
${
pageSize
}
`
,
);
describe
(
"
betterplace
"
,
()
=>
{
describe
(
"
betterplace
"
,
()
=>
{
let
fetchStub
:
Stub
;
let
fetchStub
:
Stub
;
...
@@ -209,9 +209,8 @@ describe("betterplace", () => {
...
@@ -209,9 +209,8 @@ describe("betterplace", () => {
await
fetchProjects
({
limit
:
10
,
offset
:
20
,
location
:
"
Hamburg
"
},
"
de
"
);
await
fetchProjects
({
limit
:
10
,
offset
:
20
,
location
:
"
Hamburg
"
},
"
de
"
);
const
expectedUrl
=
new
URL
(
const
expectedUrl
=
new
URL
(
"
https://api.betterplace.org/de/api_v4/projects.json?
"
+
projectsUrlWithDefaultOptions
+
"
facets=completed%3Afalse%7Cclosed%3Afalse%7Cprohibit_donations%3Afalse&order=rank%3ADESC&page=3&per_page=10
"
+
"
&page=3&per_page=10&scope=location&q=Hamburg
"
,
"
&scope=location&q=Hamburg
"
,
);
);
assertSpyCall
(
fetchStub
,
0
,
{
args
:
[
expectedUrl
]
});
assertSpyCall
(
fetchStub
,
0
,
{
args
:
[
expectedUrl
]
});
});
});
...
@@ -229,9 +228,8 @@ describe("betterplace", () => {
...
@@ -229,9 +228,8 @@ describe("betterplace", () => {
);
);
const
expectedUrl
=
new
URL
(
const
expectedUrl
=
new
URL
(
"
https://api.betterplace.org/de/api_v4/projects.json?
"
+
projectsUrlWithDefaultOptions
+
"
facets=completed%3Afalse%7Cclosed%3Afalse%7Cprohibit_donations%3Afalse&order=rank%3ADESC&page=3&per_page=10
"
+
"
&page=3&per_page=10&scope=location&q=Stra%C3%9Fe+123%2C+20535+Hamburg
"
,
"
&scope=location&q=Stra%C3%9Fe+123%2C+20535+Hamburg
"
,
);
);
assertSpyCall
(
fetchStub
,
0
,
{
args
:
[
expectedUrl
]
});
assertSpyCall
(
fetchStub
,
0
,
{
args
:
[
expectedUrl
]
});
});
});
...
@@ -308,9 +306,8 @@ describe("betterplace", () => {
...
@@ -308,9 +306,8 @@ describe("betterplace", () => {
await
fetchProjects
({
limit
:
10
,
offset
:
0
,
location
:
"
Hamburg
"
},
"
de
"
);
await
fetchProjects
({
limit
:
10
,
offset
:
0
,
location
:
"
Hamburg
"
},
"
de
"
);
const
expectedUrl
=
new
URL
(
const
expectedUrl
=
new
URL
(
"
https://api.betterplace.org/de/api_v4/projects.json?
"
+
projectsUrlWithDefaultOptions
+
"
facets=completed%3Afalse%7Cclosed%3Afalse%7Cprohibit_donations%3Afalse&order=rank%3ADESC&page=1&per_page=10
"
+
"
&page=1&per_page=10&scope=location&q=Hamburg
"
,
"
&scope=location&q=Hamburg
"
,
);
);
assertSpyCall
(
fetchStub
,
0
,
{
args
:
[
expectedUrl
]
});
assertSpyCall
(
fetchStub
,
0
,
{
args
:
[
expectedUrl
]
});
});
});
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment