-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7efcd5
commit a06f466
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import express from 'express'; | ||
import ApiError from '../util/apiError.ts'; | ||
import StatusCode from '../util/statusCode.ts'; | ||
import { | ||
toggleRequestByID, | ||
getAllChaptersFromDB, | ||
} from '../services/chapter.service.ts'; | ||
|
||
const getAllChapters = async ( | ||
req: express.Request, | ||
res: express.Response, | ||
next: express.NextFunction, | ||
) => { | ||
return ( | ||
getAllChaptersFromDB() | ||
.then((userList) => { | ||
res.status(StatusCode.OK).send(userList); | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
.catch((e) => { | ||
next(ApiError.internal('Unable to retrieve all users')); | ||
}) | ||
); | ||
}; | ||
|
||
const toggleRequest = async ( | ||
req: express.Request, | ||
res: express.Response, | ||
next: express.NextFunction, | ||
) => { | ||
const { id } = req.params; | ||
if (!id) { | ||
next(ApiError.missingFields(['id'])); | ||
return; | ||
} | ||
|
||
toggleRequestByID(id) | ||
.then(() => { | ||
res.sendStatus(StatusCode.OK); | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
.catch((e) => { | ||
next(ApiError.internal('Unable to toggle status.')); | ||
}); | ||
}; | ||
|
||
export { toggleRequest, getAllChapters }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import express from 'express'; | ||
import { isAdmin } from '../controllers/admin.middleware.ts'; | ||
import { | ||
toggleRequest, | ||
getAllChapters, | ||
} from '../controllers/chapter.controller.ts'; | ||
import { isAuthenticated } from '../controllers/auth.middleware.ts'; | ||
import 'dotenv/config'; | ||
|
||
const router = express.Router(); | ||
|
||
router.put('/toggleRequests/:id', isAuthenticated, isAdmin, toggleRequest); | ||
|
||
router.get('/all', isAuthenticated, isAdmin, getAllChapters); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import { Chapter } from '../models/chapter.model.ts'; | ||
|
||
const removeSensitiveDataQuery = [ | ||
'-password', | ||
'-verificationToken', | ||
'-resetPasswordToken', | ||
'-resetPasswordTokenExpiryDate', | ||
]; | ||
|
||
const toggleRequestByID = async (id: string) => { | ||
const chapter = await Chapter.findByIdAndUpdate(id, [ | ||
{ $set: { isAcceptingRequests: { $not: '$isAcceptingRequests' } } }, | ||
]).exec(); | ||
return chapter; | ||
}; | ||
|
||
const getAllChaptersFromDB = async () => { | ||
const userList = await Chapter.find({}) | ||
.select(removeSensitiveDataQuery) | ||
.exec(); | ||
return userList; | ||
}; | ||
|
||
export { toggleRequestByID, getAllChaptersFromDB }; |