Skip to content

Commit

Permalink
fix: issue with cached standup channels
Browse files Browse the repository at this point in the history
  • Loading branch information
en3sis committed Sep 2, 2024
1 parent 70bef00 commit 90ad60e
Showing 2 changed files with 45 additions and 21 deletions.
16 changes: 14 additions & 2 deletions src/controllers/bot/plugins.controller.ts
Original file line number Diff line number Diff line change
@@ -157,15 +157,27 @@ export const toggleGuildPlugin = async (

export const updateMetadataGuildPlugin = async (metadata: any, name: string, guildId: string) => {
try {
const { error } = await supabase
console.log('Updating metadata:', JSON.stringify(metadata, null, 2))

const { data, error } = await supabase
.from('guilds_plugins')
.update({ metadata })
.eq('name', name)
.eq('owner', guildId)
.select()

if (error) throw error

if (!data || data.length === 0) {
throw new Error('No rows were updated')
}

console.log('Update successful. Updated data:', JSON.stringify(data, null, 2))

return data[0]
} catch (error) {
console.log('❌ ERROR: updateMetadataGuildPlugin(): ', error)
console.error('❌ ERROR: updateMetadataGuildPlugin(): ', error)
throw error // Re-throw the error so the calling function knows the update failed
}
}

50 changes: 31 additions & 19 deletions src/controllers/plugins/standup.controller.ts
Original file line number Diff line number Diff line change
@@ -5,21 +5,26 @@ import { updateMetadataGuildPlugin } from '../bot/plugins.controller'
import supabase from '../../libs/supabase'
import { scheduledTasks } from '../tasks/cron-jobs'
import { Hans } from '../..'
import { deleteFromCache } from '../../libs/node-cache'

export const standupPluginController = async (
interaction: CommandInteraction,
newSchedule: StandupScheduleMetadata,
) => {
try {
// Fetch current plugin data
const { data: guildPlugin } = await supabase
.from('guilds_plugins')
.select('metadata')
.eq('owner', interaction.guildId)
.eq('name', 'standup')
.single()

let currentSchedules: StandupScheduleMetadata[] = []

// Force to update the cache from the database
deleteFromCache(`guildPlugins:${interaction.guildId}:standup`)
const guildPlugin = await Hans.guildPluginSettings(interaction.guildId, 'standup')
const currentSchedules: StandupScheduleMetadata[] = guildPlugin?.metadata || []
if (guildPlugin?.metadata && Array.isArray(guildPlugin.metadata)) {
currentSchedules = guildPlugin.metadata as StandupScheduleMetadata[]
}

// Check if a schedule for this channel already exists
// Check if the schedule already exists
const existingScheduleIndex = currentSchedules.findIndex(
(schedule) => schedule.channelId === newSchedule.channelId,
)
@@ -33,7 +38,7 @@ export const standupPluginController = async (
}

const updatedMetadata = currentSchedules.map((schedule) => {
const { channelId, expression, role } = schedule
const { expression } = schedule
const _expression = expression.startsWith('0 ') ? expression : `0 ${expression} * * 1-5`
const isValidExpression = RegExp(/^0 [0-9,]+ \* \* 1-5$/).test(_expression)

@@ -46,20 +51,27 @@ export const standupPluginController = async (
return { ...schedule, expression: _expression }
})

await updateMetadataGuildPlugin(updatedMetadata, 'standup', interaction.guildId)
console.log('Updated metadata before saving:', JSON.stringify(updatedMetadata, null, 2))

await registerStandupSchedules(interaction.guildId, updatedMetadata)
try {
await updateMetadataGuildPlugin(updatedMetadata, 'standup', interaction.guildId)

const scheduleInfo = updatedMetadata
.map(
(schedule) =>
`<#${schedule.channelId}> at **${schedule.expression.split(' ')[1]}h** mentioning ${schedule.role || 'no role'}`,
)
.join('\n')
await registerStandupSchedules(interaction.guildId, updatedMetadata)

await interaction.editReply({
content: `Updated Standup Notifications:\n${scheduleInfo}\n\nYou can disable it by running **/plugins toggle standup false**`,
})
const scheduleInfo = updatedMetadata
.map(
(schedule) =>
`<#${schedule.channelId}> at **${schedule.expression.split(' ')[1]}h** mentioning ${schedule.role || 'no role'}`,
)
.join('\n')

await interaction.editReply({
content: `Updated Standup Notifications:\n${scheduleInfo}\n\nYou can disable it by running /plugins toggle standup false`,
})
} catch (updateError) {
console.error('Error updating metadata:', updateError)
throw new Error('Failed to update standup schedules. Please try again.')
}
} catch (error) {
console.error('❌ ERROR: standupPluginController(): ', error)
await interaction.editReply({

0 comments on commit 90ad60e

Please sign in to comment.