Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fetching more measurements than the first page only #892

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/services/measurements.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import axios from "axios";
import { MeasurementCategory } from "components/Measurements/models/Category";
import { MeasurementEntry } from "components/Measurements/models/Entry";
import { getMeasurementCategories, getMeasurementCategory } from "services/measurements";

jest.mock("axios");

describe('measurement service tests', () => {
test('GET measurement categories', async () => {
const measurementResponse = {
count: 2,
next: null,
previous: null,
results: [
{
"id": 1,
"name": "Weight",
"unit": "kg"
}
]
};

const measurementEntryResponse = {
count: 2,
next: null,
previous: null,
results: [
{
"id": 1,
"category": 1,
"value": 80,
"date": "2021-01-01",
"notes": ""
}
]
};

// @ts-ignore
axios.get.mockImplementation((url: string) => {
if (url.includes("measurement-category")) {
return Promise.resolve({ data: measurementResponse });
} else if (url.includes("measurement/?category=1")) {
return Promise.resolve({ data: measurementEntryResponse });
}
});

const result = await getMeasurementCategories();
expect(axios.get).toHaveBeenCalledTimes(2);

expect(result).toStrictEqual([
new MeasurementCategory(1, "Weight", "kg", [
new MeasurementEntry(1, 1, new Date("2021-01-01"), 80, "")
])
]);
});

test('GET measurement category', async () => {
const measurementResponse = {
"id": 1,
"name": "Weight",
"unit": "kg"
};

const measurementEntryResponse = {
count: 2,
next: null,
previous: null,
results: [
{
"id": 1,
"category": 1,
"value": 80,
"date": "2021-01-01",
"notes": ""
}
]
};

// @ts-ignore
axios.get.mockImplementation((url: string) => {
if (url.includes("measurement-category/1")) {
return Promise.resolve({ data: measurementResponse });
} else if (url.includes("measurement/?category=1")) {
return Promise.resolve({ data: measurementEntryResponse });
}
});

const result = await getMeasurementCategory(1);
expect(axios.get).toHaveBeenCalledTimes(2);

expect(result).toStrictEqual(
new MeasurementCategory(1, "Weight", "kg", [
new MeasurementEntry(1, 1, new Date("2021-01-01"), 80, "")
])
);
});
});
38 changes: 24 additions & 14 deletions src/services/measurements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ApiMeasurementCategoryType, ApiMeasurementEntryType } from 'types';
import { dateToYYYYMMDD } from "utils/date";
import { makeHeader, makeUrl } from "utils/url";
import { ResponseType } from "./responseType";
import { fetchPaginated } from 'utils/requests';

export const API_MEASUREMENTS_CATEGORY_PATH = 'measurement-category';
export const API_MEASUREMENTS_ENTRY_PATH = 'measurement';
Expand All @@ -20,19 +21,23 @@ export const getMeasurementCategories = async (): Promise<MeasurementCategory[]>
const categories = receivedCategories.results.map(l => adapter.fromJson(l));

// Load entries for each category
const entryResponses = categories.map((category) => {
return axios.get<ResponseType<any>>(
makeUrl(API_MEASUREMENTS_ENTRY_PATH, { query: { category: category.id } }),
{ headers: makeHeader() },
);
const entryResponses = categories.map(async (category) => {
const out: MeasurementEntry[] = [];
const url = makeUrl(API_MEASUREMENTS_ENTRY_PATH, { query: { category: category.id } });

// Collect all pages of entries
for await (const page of fetchPaginated(url, makeHeader())) {
for (const entries of page) {
out.push(entryAdapter.fromJson(entries));
}
}
return out;
});
const settingsResponses = await Promise.all(entryResponses);

// Save entries to each category
let categoryId: number;
settingsResponses.forEach((response) => {
const entries = response.data.results.map(l => entryAdapter.fromJson(l));

settingsResponses.forEach((entries) => {
if (entries.length > 0) {
categoryId = entries[0].category;
categories.findLast(c => c.id === categoryId)!.entries = entries;
Expand All @@ -49,13 +54,18 @@ export const getMeasurementCategory = async (id: number): Promise<MeasurementCat
);

const category = new MeasurementCategoryAdapter().fromJson(receivedCategories);

const { data: receivedEntries } = await axios.get<ResponseType<ApiMeasurementEntryType>>(
makeUrl(API_MEASUREMENTS_ENTRY_PATH, { query: { category: id.toString() } }),
{ headers: makeHeader(), }
);
const adapter = new MeasurementEntryAdapter();
category.entries = receivedEntries.results.map(l => adapter.fromJson(l));
const measurements: MeasurementEntry[] = [];
const url = makeUrl(API_MEASUREMENTS_ENTRY_PATH, { query: { category: category.id } });

// Collect all pages of entries
for await (const page of fetchPaginated(url, makeHeader())) {
for (const entries of page) {
measurements.push(adapter.fromJson(entries));
}
}

category.entries = measurements;

return category;
};
Expand Down