Handle delete correctly

This commit is contained in:
Owen
2026-04-26 20:26:03 -07:00
parent 8ca72a39da
commit 467cd70b72

View File

@@ -52,16 +52,6 @@ export async function deleteResource(
.from(targets) .from(targets)
.where(eq(targets.resourceId, resourceId)); .where(eq(targets.resourceId, resourceId));
const targetHealthChecksToBeRemoved = await db
.select()
.from(targetHealthCheck)
.where(
inArray(
targetHealthCheck.targetId,
targetsToBeRemoved.map((t) => t.targetId)
)
);
const [deletedResource] = await db const [deletedResource] = await db
.delete(resources) .delete(resources)
.where(eq(resources.resourceId, resourceId)) .where(eq(resources.resourceId, resourceId))
@@ -76,38 +66,45 @@ export async function deleteResource(
); );
} }
const [site] = await db for (const target of targetsToBeRemoved) {
.select() const [site] = await db
.from(sites) .select()
.where(eq(sites.siteId, targets.siteId)) .from(sites)
.limit(1); .where(eq(sites.siteId, target.siteId))
.limit(1);
if (!site) { if (!site) {
return next( return next(
createHttpError( createHttpError(
HttpCode.NOT_FOUND, HttpCode.NOT_FOUND,
`Site with ID ${targets.siteId} not found` `Site with ID ${target.siteId} not found`
) )
);
}
if (site.pubKey) {
if (site.type == "newt") {
// get the newt on the site by querying the newt table for siteId
const [newt] = await db
.select()
.from(newts)
.where(eq(newts.siteId, site.siteId))
.limit(1);
await removeTargets(
newt.newtId,
targetsToBeRemoved,
targetHealthChecksToBeRemoved,
deletedResource.protocol,
newt.version
); );
} }
if (site.pubKey) {
if (site.type == "newt") {
// get the newt on the site by querying the newt table for siteId
const [newt] = await db
.select()
.from(newts)
.where(eq(newts.siteId, site.siteId))
.limit(1);
const [healthCheck] = await db
.select()
.from(targetHealthCheck)
.where(eq(targetHealthCheck.targetId, target.targetId));
await removeTargets(
newt.newtId,
[target],
[healthCheck],
deletedResource.protocol,
newt.version
);
}
}
} }
return response(res, { return response(res, {