Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions app/models/integration.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,61 @@ export async function isValidServiceKey(serviceKey: string | null): Promise<bool

return false;
}

export async function deleteDeviceIntegrations(deviceId: string) {
const integrations = await drizzleClient.query.integration.findMany()

const results: Array<{
slug: string
ok: boolean
status?: number
error?: string
}> = []

for (const intg of integrations) {
const serviceKey = process.env[intg.serviceKey]

if (!serviceKey) {
results.push({
slug: intg.slug,
ok: false,
error: `Service key '${intg.serviceKey}' not configured`,
})
continue
}

try {
const response = await fetch(`${intg.serviceUrl}/integrations/${deviceId}`, {
method: 'DELETE',
headers: {
'x-service-key': serviceKey,
},
})

// 404 is fine: no integration existed for this device
if (response.ok || response.status === 404) {
results.push({
slug: intg.slug,
ok: true,
status: response.status,
})
} else {
const text = await response.text()
results.push({
slug: intg.slug,
ok: false,
status: response.status,
error: text,
})
}
} catch (error) {
results.push({
slug: intg.slug,
ok: false,
error: error instanceof Error ? error.message : String(error),
})
}
}

return results
}
11 changes: 11 additions & 0 deletions app/routes/device.$deviceId.edit.general.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { MarkdownContent } from '~/components/markdown-content'
import { Button } from '~/components/ui/button'
import { updateDevice, deleteDevice } from '~/lib/devices-service.server'
import { getDevice, getDeviceWithoutSensors } from '~/models/device.server'
import { deleteDeviceIntegrations } from '~/models/integration.server'
import { verifyLogin } from '~/models/user.server'
import { type Device } from '~/schema'
import {
Expand Down Expand Up @@ -241,6 +242,16 @@ export async function action({ request, params }: ActionFunctionArgs) {
}
}

const integrationDeletionResults = await deleteDeviceIntegrations(deviceID)

const failedDeletions = integrationDeletionResults.filter((result) => !result.ok)
if (failedDeletions.length > 0) {
console.error('Failed to delete one or more integrations before device deletion', {
deviceID,
failedDeletions,
})
}
Comment on lines +248 to +253
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this inevitably produce problems when we still delete the device but not the integration?
As long as we won't proactively monitor such logs, we would be producing dead integrations..

I am not sure whats the right way to go forward it, just want to discuss. Should be break the device deletion process here as well? That way we probably get to know about a problem faster?


await deleteDevice(user, device, passwordDelete)

return redirect('/profile/me')
Expand Down
Loading