feat: update match request embed format and notification handling

This commit is contained in:
VinceC
2024-11-29 04:27:09 -06:00
parent 03c612b351
commit 3651537c1f
2 changed files with 41 additions and 3 deletions

View File

@@ -49,6 +49,8 @@ class NotificationService {
return res.status(400).json({ error: 'Invalid match data' }); return res.status(400).json({ error: 'Invalid match data' });
} }
matchData.match_date = `${matchData.match_date}T${matchData.match_time}Z`;
const { data: subscriptions, error } = await this.supabase const { data: subscriptions, error } = await this.supabase
.from('active_subscriptions') .from('active_subscriptions')
.select('*') .select('*')
@@ -59,9 +61,22 @@ class NotificationService {
return res.status(500).json({ error: 'Failed to fetch subscriptions' }); return res.status(500).json({ error: 'Failed to fetch subscriptions' });
} }
const results = [];
for (const subscription of (subscriptions || [])) { for (const subscription of (subscriptions || [])) {
try { try {
const channel = await this.bot.client.channels.fetch(subscription.notification_channel_id); const channel = await this.bot.client.channels.fetch(subscription.notification_channel_id);
// Check if bot has permissions to send messages in this channel
if (!channel.permissionsFor(this.bot.client.user).has(['SendMessages', 'ViewChannel'])) {
console.warn(`Missing permissions for channel ${subscription.notification_channel_id} in server ${channel.guild.name}`);
results.push({
channelId: subscription.notification_channel_id,
status: 'failed',
error: 'Missing permissions'
});
continue;
}
const embed = createMatchRequestEmbed(matchData); const embed = createMatchRequestEmbed(matchData);
const actionRow = createMatchActionRow(matchData.game_name); const actionRow = createMatchActionRow(matchData.game_name);
@@ -69,12 +84,32 @@ class NotificationService {
embeds: [embed], embeds: [embed],
components: [actionRow] components: [actionRow]
}); });
results.push({
channelId: subscription.notification_channel_id,
status: 'success'
});
} catch (error) { } catch (error) {
console.error(`Error sending notification to channel ${subscription.notification_channel_id}:`, error); console.error(`Error sending notification to channel ${subscription.notification_channel_id}:`, error);
results.push({
channelId: subscription.notification_channel_id,
status: 'failed',
error: error.message
});
} }
} }
res.json({ success: true }); res.json({
success: true,
results,
summary: {
total: results.length,
successful: results.filter(r => r.status === 'success').length,
failed: results.filter(r => r.status === 'failed').length
}
});
} catch (error) { } catch (error) {
console.error('Error handling match notification:', error); console.error('Error handling match notification:', error);
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
@@ -89,12 +124,14 @@ class NotificationService {
const requiredFields = [ const requiredFields = [
'game_name', 'game_name',
'game_id', 'game_id',
'created_by_user_id',
'status',
'team_size', 'team_size',
'match_type', 'match_type',
'region', 'region',
'match_date', 'match_date',
'match_class', 'match_time',
'status' 'match_class'
]; ];
return requiredFields.every(field => matchData[field] !== undefined); return requiredFields.every(field => matchData[field] !== undefined);

View File

@@ -147,6 +147,7 @@ class EmbedBuilders {
} }
static createMatchRequestEmbed(matchData) { static createMatchRequestEmbed(matchData) {
// Parse the match date from the provided format
const matchDateTime = new Date(matchData.match_date); const matchDateTime = new Date(matchData.match_date);
const formattedDateTime = matchDateTime.toLocaleString(); const formattedDateTime = matchDateTime.toLocaleString();