fix(search): removing server search engine causes loading error (#3252)
This commit is contained in:
@@ -2,9 +2,10 @@ import { TRPCError } from "@trpc/server";
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { asc, createId, eq, like } from "@homarr/db";
|
import { asc, createId, eq, like } from "@homarr/db";
|
||||||
import { getServerSettingByKeyAsync } from "@homarr/db/queries";
|
import { getServerSettingByKeyAsync, updateServerSettingByKeyAsync } from "@homarr/db/queries";
|
||||||
import { searchEngines, users } from "@homarr/db/schema";
|
import { searchEngines, users } from "@homarr/db/schema";
|
||||||
import { createIntegrationAsync } from "@homarr/integrations";
|
import { createIntegrationAsync } from "@homarr/integrations";
|
||||||
|
import { logger } from "@homarr/log";
|
||||||
import { byIdSchema, paginatedSchema, searchSchema } from "@homarr/validation/common";
|
import { byIdSchema, paginatedSchema, searchSchema } from "@homarr/validation/common";
|
||||||
import { searchEngineEditSchema, searchEngineManageSchema } from "@homarr/validation/search-engine";
|
import { searchEngineEditSchema, searchEngineManageSchema } from "@homarr/validation/search-engine";
|
||||||
import { mediaRequestOptionsSchema, mediaRequestRequestSchema } from "@homarr/validation/widgets/media-request";
|
import { mediaRequestOptionsSchema, mediaRequestRequestSchema } from "@homarr/validation/widgets/media-request";
|
||||||
@@ -96,23 +97,35 @@ export const searchEngineRouter = createTRPCRouter({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const serverDefaultId = await getServerSettingByKeyAsync(ctx.db, "search").then(
|
const searchSettings = await getServerSettingByKeyAsync(ctx.db, "search");
|
||||||
(setting) => setting.defaultSearchEngineId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (serverDefaultId) {
|
if (!searchSettings.defaultSearchEngineId) return null;
|
||||||
return await ctx.db.query.searchEngines.findFirst({
|
|
||||||
where: eq(searchEngines.id, serverDefaultId),
|
const serverDefault = await ctx.db.query.searchEngines.findFirst({
|
||||||
with: {
|
where: eq(searchEngines.id, searchSettings.defaultSearchEngineId),
|
||||||
integration: {
|
with: {
|
||||||
columns: {
|
integration: {
|
||||||
kind: true,
|
columns: {
|
||||||
url: true,
|
kind: true,
|
||||||
id: true,
|
url: true,
|
||||||
},
|
id: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (serverDefault) return serverDefault;
|
||||||
|
|
||||||
|
// Remove the default search engine ID from settings if it does not longer exist
|
||||||
|
try {
|
||||||
|
await updateServerSettingByKeyAsync(ctx.db, "search", {
|
||||||
|
...searchSettings,
|
||||||
|
defaultSearchEngineId: null,
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn(
|
||||||
|
new Error("Failed to update search settings after default search engine not found", { cause: error }),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user