feat: Improve node identification logic to handle domain mismatches and update the node's domain during admin updates.
This commit is contained in:
@@ -15,6 +15,14 @@ export async function PATCH(req: NextRequest) {
|
||||
where: eq(nodes.domain, domain),
|
||||
});
|
||||
|
||||
// 2. Fallback: If not found, check if there is exactly ONE node in the system.
|
||||
if (!node) {
|
||||
const allNodes = await db.query.nodes.findMany({ limit: 2 });
|
||||
if (allNodes.length === 1) {
|
||||
node = allNodes[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (!node) {
|
||||
[node] = await db.insert(nodes).values({
|
||||
domain,
|
||||
@@ -44,6 +52,8 @@ export async function PATCH(req: NextRequest) {
|
||||
isNsfw: data.isNsfw ?? node.isNsfw,
|
||||
turnstileSiteKey: data.turnstileSiteKey !== undefined ? data.turnstileSiteKey : node.turnstileSiteKey,
|
||||
turnstileSecretKey: data.turnstileSecretKey !== undefined ? data.turnstileSecretKey : node.turnstileSecretKey,
|
||||
// Fix domain drift: ensure the DB matches the current environment
|
||||
domain: domain,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(nodes.id, node.id))
|
||||
|
||||
@@ -9,10 +9,21 @@ export async function GET() {
|
||||
if (!db) return NextResponse.json({});
|
||||
|
||||
const domain = process.env.NEXT_PUBLIC_NODE_DOMAIN || 'localhost:3000';
|
||||
const node = await db.query.nodes.findFirst({
|
||||
|
||||
// 1. Try exact match
|
||||
let node = await db.query.nodes.findFirst({
|
||||
where: eq(nodes.domain, domain),
|
||||
});
|
||||
|
||||
// 2. Fallback: If not found, check if there is exactly ONE node in the system.
|
||||
// This handles upgrades where the env var domain might differ from the DB domain (e.g. localhost vs production).
|
||||
if (!node) {
|
||||
const allNodes = await db.query.nodes.findMany({ limit: 2 });
|
||||
if (allNodes.length === 1) {
|
||||
node = allNodes[0];
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure we have a public key
|
||||
const publicKey = await getNodePublicKey();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user