generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat-debug
- Loading branch information
Showing
12 changed files
with
152 additions
and
138 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
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
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 |
---|---|---|
@@ -1,28 +1,73 @@ | ||
import { NextFunction } from "express"; | ||
import { CustomRequest, CustomResponse } from "../types/global"; | ||
const ApplicationModel = require("../models/applications"); | ||
|
||
const checkCanGenerateDiscordLink = async (req: CustomRequest, res: CustomResponse, next: NextFunction) => { | ||
const { discordId, roles, id: userId, profileStatus } = req.userData; | ||
const { id: userId, roles } = req.userData; | ||
const isSuperUser = roles.super_user; | ||
const userIdInQuery = req.query.userId; | ||
const currentTime = Date.now(); | ||
const cutoffTime = 1725147849000; // Todo will remove this Hotfix time for 31 August 2024 | ||
|
||
if (isSuperUser) { | ||
return next(); | ||
} | ||
|
||
if (userIdInQuery && userIdInQuery !== userId && !isSuperUser) { | ||
return res.boom.forbidden("User should be super user to generate link for other users"); | ||
} | ||
|
||
if (!isSuperUser && discordId) { | ||
return res.boom.forbidden("Only users who have never joined discord can generate invite link"); | ||
if (currentTime >= cutoffTime) { | ||
return res.boom.forbidden("Discord invite link generation is not allowed after the cutoff time."); | ||
} | ||
|
||
if (roles.archived) { | ||
return res.boom.forbidden("Archived users cannot generate invite"); | ||
} | ||
try { | ||
const applications = await ApplicationModel.getUserApplications(userId); | ||
|
||
if (!applications || applications.length === 0) { | ||
return res.boom.forbidden("No applications found."); | ||
} | ||
|
||
if (!isSuperUser && !roles.maven && !roles.designer && !roles.product_manager && profileStatus !== "VERIFIED") { | ||
return res.boom.forbidden("Only selected roles can generate discord link directly"); | ||
} | ||
const approvedApplication = applications.find((application: { status: string; }) => application.status === 'accepted'); | ||
|
||
if (!approvedApplication) { | ||
return res.boom.forbidden("Only users with an approved application can generate a Discord invite link."); | ||
} | ||
|
||
return next(); | ||
return next(); | ||
} catch (error) { | ||
return res.boom.badImplementation("An error occurred while checking user applications."); | ||
} | ||
}; | ||
|
||
export default checkCanGenerateDiscordLink; | ||
|
||
// <------ We have to revisit this later -------> | ||
// <--- https://github.com/Real-Dev-Squad/website-backend/issues/2078 ---> | ||
|
||
|
||
// const checkCanGenerateDiscordLink = async (req: CustomRequest, res: CustomResponse, next: NextFunction) => { | ||
// const { discordId, roles, id: userId, profileStatus } = req.userData; | ||
// const isSuperUser = roles.super_user; | ||
// const userIdInQuery = req.query.userId; | ||
|
||
// if (userIdInQuery && userIdInQuery !== userId && !isSuperUser) { | ||
// return res.boom.forbidden("User should be super user to generate link for other users"); | ||
// } | ||
|
||
// if (!isSuperUser && discordId) { | ||
// return res.boom.forbidden("Only users who have never joined discord can generate invite link"); | ||
// } | ||
|
||
// if (roles.archived) { | ||
// return res.boom.forbidden("Archived users cannot generate invite"); | ||
// } | ||
|
||
// if (!isSuperUser && !roles.maven && !roles.designer && !roles.product_manager && profileStatus !== "VERIFIED") { | ||
// return res.boom.forbidden("Only selected roles can generate discord link directly"); | ||
// } | ||
|
||
// return next(); | ||
// }; | ||
|
||
module.exports = checkCanGenerateDiscordLink; |
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
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
Oops, something went wrong.