@@ -46,6 +46,31 @@ const createNewRole = async (roleData) => {
4646 }
4747} ;
4848
49+ /**
50+ * Soft deletes a group role by marking it as deleted in the database.
51+ * This function updates the role document in Firestore, setting isDeleted to true
52+ * and recording who deleted it and when.
53+ *
54+ * @param {string } groupId - The ID of the group role to be deleted
55+ * @param {string } deletedBy - The ID of the user performing the deletion for logging purpose
56+ * @returns {Promise<Object> } An object indicating whether the operation was successful
57+ */
58+ const deleteGroupRole = async ( groupId , deletedBy ) => {
59+ try {
60+ const roleRef = admin . firestore ( ) . collection ( "discord-roles" ) . doc ( groupId ) ;
61+ await roleRef . update ( {
62+ isDeleted : true ,
63+ deletedAt : admin . firestore . Timestamp . fromDate ( new Date ( ) ) ,
64+ deletedBy : deletedBy ,
65+ } ) ;
66+
67+ return { isSuccess : true } ;
68+ } catch ( error ) {
69+ logger . error ( `Error in deleteGroupRole: ${ error } ` ) ;
70+ return { isSuccess : false } ;
71+ }
72+ } ;
73+
4974const removeMemberGroup = async ( roleId , discordId ) => {
5075 try {
5176 const backendResponse = await deleteRoleFromDatabase ( roleId , discordId ) ;
@@ -139,10 +164,13 @@ const updateGroupRole = async (roleData, docId) => {
139164
140165const isGroupRoleExists = async ( options = { } ) => {
141166 try {
142- const { rolename = null , roleid = null } = options ;
167+ const { groupId = null , rolename = null , roleid = null } = options ;
143168
144169 let existingRoles ;
145- if ( rolename && roleid ) {
170+ if ( groupId ) {
171+ existingRoles = await discordRoleModel . doc ( groupId ) . get ( ) ;
172+ return { roleExists : existingRoles . exists , existingRoles } ;
173+ } else if ( rolename && roleid ) {
146174 existingRoles = await discordRoleModel
147175 . where ( "rolename" , "==" , rolename )
148176 . where ( "roleid" , "==" , roleid )
@@ -153,9 +181,8 @@ const isGroupRoleExists = async (options = {}) => {
153181 } else if ( roleid ) {
154182 existingRoles = await discordRoleModel . where ( "roleid" , "==" , roleid ) . limit ( 1 ) . get ( ) ;
155183 } else {
156- throw Error ( "Either rolename or roleId is required" ) ;
184+ throw Error ( "Either rolename, roleId, or groupId is required" ) ;
157185 }
158-
159186 return { roleExists : ! existingRoles . empty , existingRoles } ;
160187 } catch ( err ) {
161188 logger . error ( "Error in getting all group-roles" , err ) ;
@@ -1075,4 +1102,5 @@ module.exports = {
10751102 getUserDiscordInvite,
10761103 addInviteToInviteModel,
10771104 groupUpdateLastJoinDate,
1105+ deleteGroupRole,
10781106} ;
0 commit comments