implemented pagination

This commit is contained in:
2024-01-10 15:53:46 +01:00
parent c0e4c364d4
commit ccb628aadb
4 changed files with 191 additions and 22 deletions

View File

@@ -89,22 +89,23 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
});
export const fetchAllLocations = withUser(async (user:AuthenticatedUser, pageIx:number=0, pageSize:number=2000) => {
export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:number) => {
const dbClient = await getDbClient();
const { id: userId } = user;
// fetch `pageSize` locations for the given page index
// fetch all locations for the given year
const locations = await dbClient.collection<BillingLocation>("lokacije")
.find({ userId })
.find({
userId,
"yearMonth.year": year,
})
.sort({
"yearMonth.year": -1,
"yearMonth.month": -1,
name: 1,
})
.skip(pageIx * pageSize)
.limit(pageSize)
.toArray();
return(locations);

View File

@@ -76,7 +76,13 @@ export const fetchAvailableYears = withUser(async (user:AuthenticatedUser) => {
const dbClient = await getDbClient();
// query mnogodb for all `yearMonth` values
const yearMonths = await dbClient.collection<BillingLocation>("lokacije").distinct("yearMonth.year", { userId });
const years = await dbClient.collection<BillingLocation>("lokacije")
.distinct("yearMonth.year", { userId })
return(yearMonths);
// sort the years in descending order
const sortedYears = years.sort((a, b) => b - a);
console.log("sortedYears", sortedYears);
return(sortedYears);
})