@@ -46,6 +46,31 @@ const createNewRole = async (roleData) => {
46
46
}
47
47
} ;
48
48
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
+
49
74
const removeMemberGroup = async ( roleId , discordId ) => {
50
75
try {
51
76
const backendResponse = await deleteRoleFromDatabase ( roleId , discordId ) ;
@@ -139,10 +164,13 @@ const updateGroupRole = async (roleData, docId) => {
139
164
140
165
const isGroupRoleExists = async ( options = { } ) => {
141
166
try {
142
- const { rolename = null , roleid = null } = options ;
167
+ const { groupId = null , rolename = null , roleid = null } = options ;
143
168
144
169
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 ) {
146
174
existingRoles = await discordRoleModel
147
175
. where ( "rolename" , "==" , rolename )
148
176
. where ( "roleid" , "==" , roleid )
@@ -153,9 +181,8 @@ const isGroupRoleExists = async (options = {}) => {
153
181
} else if ( roleid ) {
154
182
existingRoles = await discordRoleModel . where ( "roleid" , "==" , roleid ) . limit ( 1 ) . get ( ) ;
155
183
} else {
156
- throw Error ( "Either rolename or roleId is required" ) ;
184
+ throw Error ( "Either rolename, roleId, or groupId is required" ) ;
157
185
}
158
-
159
186
return { roleExists : ! existingRoles . empty , existingRoles } ;
160
187
} catch ( err ) {
161
188
logger . error ( "Error in getting all group-roles" , err ) ;
@@ -1075,4 +1102,5 @@ module.exports = {
1075
1102
getUserDiscordInvite,
1076
1103
addInviteToInviteModel,
1077
1104
groupUpdateLastJoinDate,
1105
+ deleteGroupRole,
1078
1106
} ;
0 commit comments