Skip to content

Commit a948edc

Browse files
committed
allUsers
1 parent 272667e commit a948edc

11 files changed

Lines changed: 73 additions & 38 deletions

File tree

src/backend/src/controllers/reimbursement-requests.controllers.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,11 @@ export default class ReimbursementRequestsController {
274274
}
275275

276276
static async getAllReimbursementRequests(req: Request, res: Response, next: NextFunction) {
277-
console.log('here');
278277
try {
279278
const reimbursementRequests: ReimbursementRequest[] = await ReimbursementRequestService.getAllReimbursementRequests(
280279
req.currentUser,
281280
req.organization
282281
);
283-
console.log(reimbursementRequests);
284282
res.status(200).json(reimbursementRequests);
285283
} catch (error: unknown) {
286284
console.error(error);

src/backend/src/controllers/users.controllers.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ import UsersService from '../services/users.services';
33
import { AccessDeniedException } from '../utils/errors.utils';
44
import { Task } from 'shared';
55
export default class UsersController {
6-
static async getAllUsers(req: Request, res: Response, next: NextFunction) {
6+
static async getAllUsers(_req: Request, res: Response, next: NextFunction) {
77
try {
8-
const allOrgs = req.query.allOrgs === 'true';
9-
const users = allOrgs
10-
? await UsersService.getAllUsers()
11-
: await UsersService.getAllUsers(req.organization.organizationId);
8+
const users = await UsersService.getAllUsers();
129
res.status(200).json(users);
1310
} catch (error: unknown) {
1411
console.error(error);
1512
next(error);
1613
}
1714
}
1815

16+
static async getAllOrgUsers(req: Request, res: Response, next: NextFunction) {
17+
try {
18+
const users = await UsersService.getAllOrgUsers(req.organization);
19+
res.status(200).json(users);
20+
} catch (error: unknown) {
21+
next(error);
22+
}
23+
}
24+
1925
static async getCurrentUser(req: Request, res: Response, next: NextFunction) {
2026
try {
2127
const user = await UsersService.getCurrentUser(req.currentUser);

src/backend/src/routes/users.routes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Theme } from '@prisma/client';
22
import express from 'express';
3-
import { body, query } from 'express-validator';
3+
import { body } from 'express-validator';
44
import UsersController from '../controllers/users.controllers';
55
import { isRole, nonEmptyString, intMinZero, validateInputs, isDate } from '../utils/validation.utils';
66

77
const userRouter = express.Router();
88

9-
userRouter.get('/all', query('allOrgs').isBoolean().optional(), validateInputs, UsersController.getAllUsers);
9+
userRouter.get('/', UsersController.getAllUsers);
10+
userRouter.get('/organization', UsersController.getAllOrgUsers);
1011
userRouter.post(
1112
'/scheduleSettings',
1213
body('userIds').isArray(),

src/backend/src/services/reimbursement-requests.services.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,6 @@ export default class ReimbursementRequestService {
896896
* @returns an array of the prisma version of the reimbursement requests transformed to the shared version
897897
*/
898898
static async getAllReimbursementRequests(user: User, organization: Organization): Promise<ReimbursementRequest[]> {
899-
console.log(organization.organizationId);
900899
await isUserHeadOrOnFinance(user, organization.organizationId);
901900

902901
const reimbursementRequests = await prisma.reimbursement_Request.findMany({

src/backend/src/services/users.services.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,33 @@ import { validateUserIsPartOfFinanceTeamOrHead } from '../utils/reimbursement-re
3333
export default class UsersService {
3434
/**
3535
* Gets all of the users from the database
36-
* @param organizationId the id of the organization to get the users for
3736
* @returns a list of all the users
3837
*/
39-
static async getAllUsers(organizationId?: string): Promise<User[]> {
38+
static async getAllUsers(): Promise<User[]> {
4039
const users = await prisma.user.findMany({
41-
...(organizationId ? { where: { organizations: { some: { organizationId } } } } : {}),
42-
...(organizationId
43-
? getUserQueryArgs(organizationId)
44-
: {
45-
select: {
46-
roles: true,
47-
userId: true,
48-
firstName: true,
49-
lastName: true,
50-
email: true
51-
}
52-
})
40+
select: {
41+
roles: true,
42+
userId: true,
43+
firstName: true,
44+
lastName: true,
45+
email: true
46+
}
47+
});
48+
49+
users.sort((a, b) => a.firstName.localeCompare(b.firstName));
50+
51+
return users.map(userTransformer);
52+
}
53+
54+
/**
55+
* Gets all of the users in the current organization
56+
* @param organization the organization to get the users from
57+
* @returns a list of all the users in the current organization
58+
*/
59+
static async getAllOrgUsers(organization: Organization): Promise<User[]> {
60+
const users = await prisma.user.findMany({
61+
where: { organizations: { some: { organizationId: organization.organizationId } } },
62+
...getUserQueryArgs(organization.organizationId)
5363
});
5464

5565
users.sort((a, b) => a.firstName.localeCompare(b.firstName));

src/backend/src/utils/auth.utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const requireJwtDev = (req: Request, res: Response, next: NextFunction) =
6464
req.path === '/users/auth/login/dev' || // logins dont have cookies yet
6565
req.path === '/' || // base route is available so aws can listen and check the health
6666
req.method === 'OPTIONS' || // this is a pre-flight request and those don't send cookies
67-
req.path === '/users/all' || // dev login needs the list of users to log in
67+
req.path === '/users' || // dev login needs the list of users to log in
6868
req.path === '/slack' // slack http endpoint is only used from slack api
6969
) {
7070
next();
@@ -184,7 +184,7 @@ export const getUserAndOrganization = async (req: Request, res: Response, next:
184184
req.path === '/users/auth/login/dev' ||
185185
req.path === '/' || // base route is available so aws can listen and check the health
186186
req.method === 'OPTIONS' || // this is a pre-flight request and those don't send cookies
187-
req.path === '/users/all?allOrgs=true' || // dev login needs the list of users to log in
187+
req.path === '/users' || // dev login needs the list of users to log in
188188
req.path === '/slack' || // slack http endpoint is only used from slack api
189189
req.path.startsWith('/notifications') // Notifications route has its own auth, only called from gh
190190
) {

src/backend/src/utils/slack.utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,14 @@ export const blockToString = async (block: SlackRichTextBlock) => {
550550
*/
551551
export const blockToMentionedUsers = async (
552552
block: SlackRichTextBlock,
553-
organizationId: string,
553+
_organizationId: string,
554554
channelId: string
555555
): Promise<string[]> => {
556556
switch (block.type) {
557557
case 'broadcast':
558558
switch (block.range) {
559559
case 'everyone':
560-
const usersInOrg = await UsersService.getAllUsers(organizationId);
560+
const usersInOrg = await UsersService.getAllUsers();
561561
return usersInOrg.map((user) => user.userId);
562562
case 'channel':
563563
case 'here':

src/frontend/src/apis/users.api.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@ import { taskTransformer } from './transformers/tasks.transformers';
2828
/**
2929
* Fetches all users.
3030
*/
31-
export const getAllUsers = (allOrgs?: boolean) => {
32-
return axios.get<UserWithRole[]>(apiUrls.allUsers(allOrgs), {
31+
export const getAllUsers = () => {
32+
return axios.get<UserWithRole[]>(apiUrls.users(), {
33+
transformResponse: (data) => JSON.parse(data).map(userTransformer)
34+
});
35+
};
36+
37+
/**
38+
* All users in the current organization with their roles.
39+
* @returns the users in the current organization with their roles
40+
*/
41+
export const getAllOrgUsers = () => {
42+
return axios.get<UserWithRole[]>(apiUrls.orgUsers(), {
3343
transformResponse: (data) => JSON.parse(data).map(userTransformer)
3444
});
3545
};

src/frontend/src/hooks/users.hooks.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,22 @@ export const useCurrentUser = (): AuthenticatedUser => {
5151
};
5252

5353
/**
54-
* Custom React Hook to supply all users.
54+
* Custom React Hook to supply all users. (only users in the current org)
5555
*/
56-
export const useAllUsers = (allOrgs?: boolean) => {
57-
return useQuery<UserWithRole[], Error>(['users', allOrgs], async () => {
58-
const { data } = await getAllUsers(allOrgs);
56+
export const useAllUsers = () => {
57+
return useQuery<UserWithRole[], Error>(['users'], async () => {
58+
const { data } = await getAllUsers();
59+
return data;
60+
});
61+
};
62+
63+
/**
64+
* Custom React Hook to supply all users for login (no org filtering).
65+
* @returns all users regardless of org
66+
*/
67+
export const useAllLoginUsers = () => {
68+
return useQuery<UserWithRole[], Error>(['users', 'login'], async () => {
69+
const { data } = await getAllUsers();
5970
return data;
6071
});
6172
};

src/frontend/src/pages/LoginPage/LoginDev.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import InputLabel from '@mui/material/InputLabel';
1010
import LoginIcon from '@mui/icons-material/Login';
1111
import FormControl from '@mui/material/FormControl';
1212
import LoadingIndicator from '../../components/LoadingIndicator';
13-
import { useAllUsers } from '../../hooks/users.hooks';
13+
import { useAllLoginUsers } from '../../hooks/users.hooks';
1414
import { fullNamePipe } from '../../utils/pipes';
1515
import { rankUserRole } from 'shared';
1616
import { FormEvent } from 'react';
@@ -28,7 +28,7 @@ const LoginDev: React.FC<LoginDevProps> = ({ devSetUser, devFormSubmit }) => {
2828
if (import.meta.env.MODE !== 'development') return <></>;
2929

3030
// eslint-disable-next-line react-hooks/rules-of-hooks
31-
const { isLoading, data: usersList } = useAllUsers(true);
31+
const { isLoading, data: usersList } = useAllLoginUsers();
3232

3333
if (!usersList || isLoading) return <LoadingIndicator />;
3434

0 commit comments

Comments
 (0)