scan for deleted tickets

This commit is contained in:
indifferentketchup
2026-04-07 10:15:58 -05:00
parent 7da082275f
commit 03794ceb25
3 changed files with 42 additions and 4 deletions

View File

@@ -584,6 +584,39 @@ async function checkAutoUnclaim(client) {
logAutomation('Auto-unclaim run', null, `checked: ${checked}, unclaimed: ${unclaimed}`).catch(() => {});
}
async function reconcileDeletedTicketChannels(client) {
const guild = client.guilds.cache.get(CONFIG.DISCORD_GUILD_ID) || client.guilds.cache.first();
if (!guild) return { checked: 0, reconciled: 0 };
const openTickets = await Ticket.find({
status: 'open',
discordThreadId: { $ne: null }
}).lean();
let checked = 0, reconciled = 0;
for (const ticket of openTickets) {
checked++;
try {
let channel = guild.channels.cache.get(ticket.discordThreadId);
if (!channel) {
channel = await guild.channels.fetch(ticket.discordThreadId).catch(() => null);
}
if (!channel) {
await Ticket.updateOne(
{ gmailThreadId: ticket.gmailThreadId },
{ $set: { status: 'closed', discordThreadId: null } }
);
logAutomation('Reconcile: channel deleted', ticket.discordThreadId, `ticket #${ticket.ticketNumber}`).catch(() => {});
reconciled++;
}
} catch (err) {
console.error(`reconcileDeletedTicketChannels error for ${ticket.gmailThreadId}:`, err);
}
}
logAutomation('Reconcile run', null, `checked: ${checked}, reconciled: ${reconciled}`).catch(() => {});
return { checked, reconciled };
}
module.exports = {
getNextTicketNumber,
pickTicketCategoryId,
@@ -606,5 +639,6 @@ module.exports = {
updateTicketActivity,
checkAutoClose,
checkReminders,
checkAutoUnclaim
checkAutoUnclaim,
reconcileDeletedTicketChannels
};