Skip to content

Commit

Permalink
Merge pull request #829 from UofT-Frosh-Orientation/retreat-accessibi…
Browse files Browse the repository at this point in the history
…lity

testing waiver file displaying
  • Loading branch information
ashleyleal authored Jul 30, 2024
2 parents 8c55cd8 + bb18f19 commit d913487
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
19 changes: 16 additions & 3 deletions client/src/pages/FroshRetreat/FroshRetreat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ const RetreatRegistration = () => {
}
};

const handleViewWaiver = async () => {
try {
const { axios } = useAxios();
const response = await axios.get(`/frosh/view-waiver/`, {
responseType: 'blob', // handling binary data
});
const blob = new Blob([response.data], { type: 'application/pdf' });
const url = window.URL.createObjectURL(blob);
window.open(url);
} catch (e) {
console.error(e);
setSnackbar('Error viewing waiver', true);
}
};

return (
<div style={{ margin: '0 20px' }}>
<p style={{ textAlign: 'center' }}>
Expand Down Expand Up @@ -339,9 +354,7 @@ const RetreatRegistration = () => {
<Button
label="View Uploaded Waiver"
isSecondary
onClick={() => {
window.open(`/view-waiver`, '_blank').focus();
}}
onClick={handleViewWaiver}
style={{ marginBottom: '25px' }}
/>
) : (
Expand Down
8 changes: 8 additions & 0 deletions server/routes.http
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ post {{$dotenv API_BASE_URL}}/frosh/upload-waiver

###

get {{$dotenv API_BASE_URL}}/view-waiver

###

get {{$dotenv API_BASE_URL}}/user/view-waiver

###

put {{$dotenv API_BASE_URL}}/frosh/info

###
Expand Down
31 changes: 29 additions & 2 deletions server/src/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const UserController = {
lastName,
preferredName,
);
if (req.file){
user.waiver = req.file.path
if (req.file) {
user.waiver = req.file.path;
}
}
return res.status(200).send({ message: 'Success!', user: user.getResponseObject() });
Expand Down Expand Up @@ -422,6 +422,33 @@ const UserController = {
next(e);
}
},

async viewWaiver(req, res) {
try {
const userId = req.params.id; // get user id from request
const user = await UserServices.getUserByID(userId); // get user from DB

if (!user) {
console.log('User not found.');
return res.status(404).send('User not found.');
}

if (!user.waiver) {
console.log('No waiver found for this user.');
return res.status(404).send('No waiver found for this user.');
}

const { filename, data, contentType } = user.waiver;

res.setHeader('Content-Disposition', `inline; filename="${filename}"`);
res.setHeader('Content-Type', contentType);
res.send(data);
} catch (error) {
console.error(error);
console.log('Error retrieving file.');
res.status(500).send('Error retrieving file.');
}
},
};

module.exports = UserController;
3 changes: 3 additions & 0 deletions server/src/loaders/routerLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const scuntMissionRouter = require('../routes/scuntMissionRoutes');
const scuntTeamRouter = require('../routes/scuntTeamRoutes');
const scuntGameSettingsRouter = require('../routes/scuntGameSettingsRoutes');
const uploadRouter = require('../middlewares/upload');
const { viewWaiver } = require('../controllers/UserController');

const routerLoader = (app) => {
app.use('/frosh', froshRouter);
Expand All @@ -24,6 +25,8 @@ const routerLoader = (app) => {
app.use('/skule-hunt-game-controls', scuntGameSettingsRouter);
app.use('/scunt', scuntRouter);
app.use('/upload-waiver', userRouter);
app.use('/view-waiver', userRouter);
app.get('/user/view-waiver/:id', viewWaiver);
app.use('/frosh', uploadRouter);
//default route
app.get('*', (req, res) => {
Expand Down
42 changes: 42 additions & 0 deletions server/src/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,46 @@ router.delete('/:id', checkLoggedIn, hasAuthScopes(['accounts:delete']), UserCon
*/
router.put('/user-exist', checkLoggedIn, checkUserType('frosh'), UserController.userExist);

/**
* @swagger
* /user/view-waiver:
* get:
* summary: Retrieves the waiver file for the authenticated user
* description: This endpoint retrieves the waiver file associated with the authenticated user. The user must be logged in to access this resource.
* security:
* - bearerAuth: []
* responses:
* '200':
* description: Successfully retrieved the waiver file
* content:
* application/pdf:
* schema:
* type: string
* format: binary
* '401':
* description: User not authenticated
* content:
* text/plain:
* schema:
* type: string
* example: User not authenticated.
* '404':
* description: User or waiver file not found
* content:
* text/plain:
* schema:
* type: string
* example: User not found or No waiver found for this user.
* '500':
* description: Internal server error
* content:
* text/plain:
* schema:
* type: string
* example: Error retrieving file.
* tags:
* - User
*/
router.get('/view-waiver/:id', checkLoggedIn, UserController.viewWaiver);

module.exports = router;

0 comments on commit d913487

Please sign in to comment.