(client, guildId)
| 1083 | } |
| 1084 | |
| 1085 | export async function cleanupExpiredApplications(client, guildId) { |
| 1086 | try { |
| 1087 | if (!client.db || typeof client.db.list !== 'function') { |
| 1088 | return { removed: 0, scanned: 0 }; |
| 1089 | } |
| 1090 | |
| 1091 | const settings = await getApplicationSettings(client, guildId); |
| 1092 | const retentionDays = getApplicationRetentionDays(settings); |
| 1093 | const prefix = `guild:${guildId}:applications:`; |
| 1094 | let keys = await client.db.list(prefix); |
| 1095 | |
| 1096 | if (!Array.isArray(keys)) { |
| 1097 | if (typeof keys === 'object' && keys !== null) { |
| 1098 | keys = Object.keys(keys).filter(key => key.startsWith(prefix)); |
| 1099 | } else { |
| 1100 | return { removed: 0, scanned: 0 }; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | const applicationKeyPattern = new RegExp(`^guild:${guildId}:applications:[^:]+$`); |
| 1105 | const applicationKeys = keys.filter(key => applicationKeyPattern.test(key)); |
| 1106 | |
| 1107 | const now = Date.now(); |
| 1108 | let removed = 0; |
| 1109 | |
| 1110 | for (const key of applicationKeys) { |
| 1111 | const app = unwrapReplitData(await client.db.get(key, null)); |
| 1112 | if (!app) { |
| 1113 | continue; |
| 1114 | } |
| 1115 | |
| 1116 | if (isApplicationExpired(app, retentionDays, now)) { |
| 1117 | const deleted = await deleteApplication(client, guildId, app.id, app.userId); |
| 1118 | if (deleted) { |
| 1119 | removed += 1; |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | return { removed, scanned: applicationKeys.length }; |
| 1125 | } catch (error) { |
| 1126 | logger.error(`Error cleaning expired applications for guild ${guildId}:`, error); |
| 1127 | return { removed: 0, scanned: 0 }; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | export async function saveApplicationSettings(client, guildId, settings) { |
| 1132 | const key = getApplicationSettingsKey(guildId); |
no test coverage detected