Skip to content

Commit

Permalink
Merge pull request #135 from GTBitsOfGood/eric/129-total-user-count
Browse files Browse the repository at this point in the history
return total user count
  • Loading branch information
dpang314 committed Sep 25, 2023
2 parents 393aa98 + 751c72e commit a037946
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
4 changes: 3 additions & 1 deletion backend/server/mongodb/actions/TrainingLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export async function createTrainingLog(

export async function getTrainingLogs(userId: Types.ObjectId | string) {
await dbConnect();
const trainingLogs = await TrainingLogModel.find({ handler: userId }).sort({ date: -1 });
const trainingLogs = await TrainingLogModel.find({ handler: userId }).sort({
date: -1,
});
return trainingLogs;
}

Expand Down
30 changes: 19 additions & 11 deletions backend/server/mongodb/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,20 @@ export async function adminGetUsers(
}),
};

let query: any;

Check warning on line 105 in backend/server/mongodb/actions/User.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

if (!filter || filter === UserFilter.NONPROFIT_USERS) {
return UserModel.find({
query = {
...searchQuery,
roles: { $nin: [Role.NONPROFIT_ADMIN] },
}).limit(pageSize);
};
}

if (filter === UserFilter.UNVERIFIED_USERS) {
return UserModel.find({ ...searchQuery, verifiedByAdmin: false }).limit(
pageSize
);
query = {
...searchQuery,
verifiedByAdmin: false,
};
}

// Hours is on the Animal, not the users
Expand All @@ -121,35 +124,40 @@ export async function adminGetUsers(
totalHours: { $gte: 800 },
}).select("handler");

return UserModel.find({
query = {
_id: {
...(afterId && { $gt: afterId }),
$in: handlers.map((item) => item.handler),
},
}).limit(pageSize);
};
}

if (filter === UserFilter.WITHOUT_800_HOURS_USERS) {
const handlers = await AnimalModel.find({
totalHours: { $gte: 800 },
}).select("handler");

return UserModel.find({
query = {
_id: {
...(afterId && { $gt: afterId }),
// some verified users do not have an animal
$nin: handlers.map((item) => item.handler),
},
verifiedByAdmin: true,
}).limit(pageSize);
};
}

if (filter === UserFilter.NONPROFIT_ADMINS) {
return UserModel.find({
query = {
...searchQuery,
roles: { $in: [Role.NONPROFIT_ADMIN] },
}).limit(pageSize);
};
}

const users = await UserModel.find(query).limit(pageSize);
const totalCount = await UserModel.countDocuments(query);

return { users, totalCount };
}

export async function verifyUserEmail(userId: Types.ObjectId) {
Expand Down
12 changes: 10 additions & 2 deletions backend/src/pages/api/admin/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ export default APIWrapper({
| undefined;
const searchText: string = req.body.searchText as string;

const users = await adminGetUsers(pageSize, afterId, filter, searchText);
const { users, totalCount } = await adminGetUsers(
pageSize,
afterId,
filter,
searchText
);

return users;
return {
users,
totalCount,
};
},
},
});
34 changes: 23 additions & 11 deletions mobile/screens/Admin/AdminUserListScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useEffect } from "react";
import { StyleSheet, View, TextInput, BackHandler } from "react-native";
import { StyleSheet, View, TextInput, Text, BackHandler } from "react-native";
import UserEntry from "../../components/UserEntry";
import { ButtonDirection, Screens, User, UserFilter } from "../../utils/types";
import { adminGetUsers } from "../../actions/Admin";
Expand All @@ -16,6 +16,7 @@ export default function AdminUserList(props: any) {
const [currentPage, setCurrentPage] = useState(0);
const [error, setError] = useState("");
const [searchText, setSearchText] = useState("");
const [totalUserCount, setTotalUserCount] = useState<number>(0);

const removeUserFromList = (errorMessage: string, userId: Types.ObjectId) => {
if (errorMessage) {
Expand All @@ -30,12 +31,16 @@ export default function AdminUserList(props: any) {
};

async function loadUsers() {
const users = await ErrorWrapper({
const result = await ErrorWrapper({
functionToExecute: adminGetUsers,
errorHandler: setError,
parameters: [PAGE_SIZE, undefined, filter, searchText],
});
setAllUsers([users]);

if (result) {
setAllUsers([[...result.users]]);
setTotalUserCount(result.totalCount);
}
}

useEffect(() => {
Expand All @@ -45,32 +50,38 @@ export default function AdminUserList(props: any) {
});
loadUsers().catch().then();
}, []);

const processNext = async (direction: ButtonDirection) => {
let newPage = currentPage;

if (direction === ButtonDirection.BUTTON_BACKWARD) {
setCurrentPage(Math.max(currentPage - 1, 0));
newPage = Math.max(currentPage - 1, 0);
}

if (direction === ButtonDirection.BUTTON_FORWARD) {
// If we are on last page and have a full page of users, we want to load more users
const lastPage = allUsers.length - 1;
if (currentPage === lastPage) {

// If we are on the last page and it's full, fetch more users
if (currentPage === lastPage && allUsers[lastPage].length === PAGE_SIZE) {
const afterId = allUsers[lastPage][allUsers[lastPage].length - 1]._id;

const newUsers = await ErrorWrapper({
functionToExecute: adminGetUsers,
errorHandler: setError,
parameters: [PAGE_SIZE, afterId, filter],
parameters: [PAGE_SIZE, afterId, filter, searchText],
});
if (newUsers && newUsers.length > 0) {
allUsers.push(newUsers);
setAllUsers(allUsers);
setCurrentPage(currentPage + 1);

if (newUsers && newUsers.users.length > 0) {
setAllUsers([...allUsers, newUsers.users]);
newPage = currentPage + 1;
}
} else if (currentPage !== lastPage) {
// We are in a page in the middle
setCurrentPage(currentPage + 1);
newPage = currentPage + 1;
}
}
setCurrentPage(newPage);
};

return (
Expand Down Expand Up @@ -112,6 +123,7 @@ export default function AdminUserList(props: any) {
onEndEditing={loadUsers}
/>
</View>
<Text>Total Users: {totalUserCount}</Text>
{allUsers.length > 0 &&
allUsers[currentPage].map((user, index) => {
return (
Expand Down

0 comments on commit a037946

Please sign in to comment.