@@ -6,6 +6,7 @@ const discordRolesModel = require("../models/discordactions");
66const discordServices = require ( "../services/discordService" ) ;
77const { fetchAllUsers, fetchUser } = require ( "../models/users" ) ;
88const { generateCloudFlareHeaders } = require ( "../utils/discord-actions" ) ;
9+ const { addLog } = require ( "../models/logs" ) ;
910const discordDeveloperRoleId = config . get ( "discordDeveloperRoleId" ) ;
1011const discordMavenRoleId = config . get ( "discordMavenRoleId" ) ;
1112
@@ -63,6 +64,60 @@ const createGroupRole = async (req, res) => {
6364 }
6465} ;
6566
67+ /**
68+ * Controller function to handle the soft deletion of a group role.
69+ *
70+ * @param {Object } req - The request object
71+ * @param {Object } res - The response object
72+ * @returns {Promise<void> }
73+ */
74+ const deleteGroupRole = async ( req , res ) => {
75+ const { groupId } = req . params ;
76+
77+ try {
78+ const { roleExists, existingRoles } = await discordRolesModel . isGroupRoleExists ( { groupId } ) ;
79+
80+ if ( ! roleExists ) {
81+ return res . boom . notFound ( "Group role not found" ) ;
82+ }
83+
84+ const roleData = existingRoles . data ( ) ;
85+
86+ const discordDeletion = await discordServices . deleteGroupRoleFromDiscord ( roleData . roleid ) ;
87+
88+ if ( ! discordDeletion . success ) {
89+ return res . boom . badImplementation ( discordDeletion . message ) ;
90+ }
91+
92+ const { isSuccess } = await discordRolesModel . deleteGroupRole ( groupId , req . userData . id ) ;
93+
94+ if ( ! isSuccess ) {
95+ logger . error ( `Role deleted from Discord but failed to delete from database for groupId: ${ groupId } ` ) ;
96+ return res . boom . badImplementation ( "Group role deletion failed" ) ;
97+ }
98+
99+ const groupDeletionLog = {
100+ type : "group-role-deletion" ,
101+ meta : {
102+ userId : req . userData . id ,
103+ } ,
104+ body : {
105+ groupId : groupId ,
106+ roleName : roleData . rolename ,
107+ discordRoleId : roleData . roleid ,
108+ action : "delete" ,
109+ } ,
110+ } ;
111+ await addLog ( groupDeletionLog . type , groupDeletionLog . meta , groupDeletionLog . body ) ;
112+ return res . status ( 200 ) . json ( {
113+ message : "Group role deleted successfully" ,
114+ } ) ;
115+ } catch ( error ) {
116+ logger . error ( `Error while deleting group role: ${ error } ` ) ;
117+ return res . boom . badImplementation ( "Internal server error" ) ;
118+ }
119+ } ;
120+
66121/**
67122 * Gets all group-roles
68123 *
@@ -491,4 +546,5 @@ module.exports = {
491546 setRoleToUsersWith31DaysPlusOnboarding,
492547 getUserDiscordInvite,
493548 generateInviteForUser,
549+ deleteGroupRole,
494550} ;
0 commit comments