Skip to content

Commit 272667e

Browse files
committed
note 2 self: switch all to /users, make /organization, investigate availability, rr
1 parent fb31c60 commit 272667e

10 files changed

Lines changed: 47 additions & 33 deletions

File tree

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

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

276276
static async getAllReimbursementRequests(req: Request, res: Response, next: NextFunction) {
277+
console.log('here');
277278
try {
278279
const reimbursementRequests: ReimbursementRequest[] = await ReimbursementRequestService.getAllReimbursementRequests(
279280
req.currentUser,
280281
req.organization
281282
);
283+
console.log(reimbursementRequests);
282284
res.status(200).json(reimbursementRequests);
283285
} catch (error: unknown) {
286+
console.error(error);
284287
next(error);
285288
}
286289
}

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@ import { Task } from 'shared';
55
export default class UsersController {
66
static async getAllUsers(req: Request, res: Response, next: NextFunction) {
77
try {
8-
const users = await UsersService.getAllUsers(req.organization.organizationId);
9-
10-
res.status(200).json(users);
11-
} catch (error: unknown) {
12-
next(error);
13-
}
14-
}
15-
16-
static async getAllOrganizationUsers(req: Request, res: Response, next: NextFunction) {
17-
try {
18-
const users = await UsersService.getAllUsers(req.organization.organizationId);
19-
8+
const allOrgs = req.query.allOrgs === 'true';
9+
const users = allOrgs
10+
? await UsersService.getAllUsers()
11+
: await UsersService.getAllUsers(req.organization.organizationId);
2012
res.status(200).json(users);
2113
} catch (error: unknown) {
14+
console.error(error);
2215
next(error);
2316
}
2417
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { Theme } from '@prisma/client';
22
import express from 'express';
3-
import { body } from 'express-validator';
3+
import { body, query } 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('/', UsersController.getAllUsers);
10-
userRouter.post('/scheduleSettings', nonEmptyString(body('userIds.*')), UsersController.getManyUsersWithScheduleSettings);
9+
userRouter.get('/all', query('allOrgs').isBoolean().optional(), validateInputs, UsersController.getAllUsers);
10+
userRouter.post(
11+
'/scheduleSettings',
12+
body('userIds').isArray(),
13+
nonEmptyString(body('userIds.*')),
14+
validateInputs,
15+
UsersController.getManyUsersWithScheduleSettings
16+
);
1117
userRouter.get('/:userId', UsersController.getSingleUser);
1218
userRouter.get('/:userId/settings', UsersController.getUserSettings);
1319
userRouter.get('/secure-settings/current-user', UsersController.getCurrentUserSecureSettings);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ 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);
899900
await isUserHeadOrOnFinance(user, organization.organizationId);
900901

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

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,20 @@ export default class UsersService {
3636
* @param organizationId the id of the organization to get the users for
3737
* @returns a list of all the users
3838
*/
39-
static async getAllUsers(organizationId: string): Promise<User[]> {
39+
static async getAllUsers(organizationId?: string): Promise<User[]> {
4040
const users = await prisma.user.findMany({
41-
where: organizationId
42-
? {
43-
organizations: {
44-
some: {
45-
organizationId
46-
}
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
4751
}
48-
}
49-
: {},
50-
...getUserQueryArgs(organizationId)
52+
})
5153
});
5254

5355
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' || // dev login needs the list of users to log in
67+
req.path === '/users/all' || // 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' || // dev login needs the list of users to log in
187+
req.path === '/users/all?allOrgs=true' || // 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/frontend/src/apis/users.api.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ import { taskTransformer } from './transformers/tasks.transformers';
2828
/**
2929
* Fetches all users.
3030
*/
31-
export const getAllUsers = () => {
32-
return axios.get<UserWithRole[]>(apiUrls.users());
31+
export const getAllUsers = (allOrgs?: boolean) => {
32+
return axios.get<UserWithRole[]>(apiUrls.allUsers(allOrgs), {
33+
transformResponse: (data) => JSON.parse(data).map(userTransformer)
34+
});
3335
};
3436

3537
/**

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ export const useCurrentUser = (): AuthenticatedUser => {
5353
/**
5454
* Custom React Hook to supply all users.
5555
*/
56-
export const useAllUsers = () => {
57-
return useQuery<UserWithRole[], Error>(['users'], async () => {
58-
const { data } = await getAllUsers();
56+
export const useAllUsers = (allOrgs?: boolean) => {
57+
return useQuery<UserWithRole[], Error>(['users', allOrgs], async () => {
58+
const { data } = await getAllUsers(allOrgs);
5959
return data;
6060
});
6161
};
@@ -276,6 +276,11 @@ export const useManyUserTasks = (userIds: string[]) => {
276276
});
277277
};
278278

279+
/**
280+
* Custom react hook to get the users with their schedule settings for all users in the list
281+
* @param userIds ids of users to get schedule settings from
282+
* @returns users with their schedule settings
283+
*/
279284
export const useManyUsersWithScheduleSettings = (userIds: string[]) => {
280285
return useQuery<UserWithScheduleSettings[], Error>(['users', userIds, 'with-schedule-settings'], async () => {
281286
const { data } = await getManyUsersWithScheduleSettings(userIds);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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();
31+
const { isLoading, data: usersList } = useAllUsers(true);
3232

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

src/frontend/src/utils/urls.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const API_URL: string = import.meta.env.VITE_REACT_APP_BACKEND_URL || 'http://lo
1414

1515
/**************** Users Endpoints ****************/
1616
const users = () => `${API_URL}/users`;
17+
const allUsers = (allOrgs?: boolean) => `${users()}/all?allOrgs=${allOrgs ? 'true' : 'false'}`;
1718
const usersById = (id: string) => `${users()}/${id}`;
1819
const usersLogin = () => `${users()}/auth/login`;
1920
const usersLoginDev = () => `${users()}/auth/login/dev`;
@@ -433,6 +434,7 @@ const version = () => `https://api.github.com/repos/Northeastern-Electric-Racing
433434

434435
export const apiUrls = {
435436
users,
437+
allUsers,
436438
usersById,
437439
usersLogin,
438440
usersLoginDev,

0 commit comments

Comments
 (0)