Skip to content

Commit 42e2482

Browse files
committed
fix: byok
1 parent ba99652 commit 42e2482

File tree

1 file changed

+76
-10
lines changed

1 file changed

+76
-10
lines changed

services/api/src/access-admin-db.ts

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,41 @@ export async function getLlmConfigsCollection(): Promise<Collection<LlmConfigDoc
298298
}
299299

300300
export async function findLlmConfigForOrg(orgId: any): Promise<LlmConfigDoc | null> {
301+
if (orgId == null) {
302+
return null;
303+
}
304+
301305
const col = await getLlmConfigsCollection();
302-
return col.findOne({ org_id: orgId });
306+
307+
let effectiveId: any = orgId;
308+
if (typeof orgId === 'string') {
309+
try {
310+
effectiveId = new ObjectId(orgId);
311+
} catch {
312+
return null;
313+
}
314+
}
315+
316+
return col.findOne({ org_id: effectiveId });
303317
}
304318

305319
export async function findLlmConfigForToken(tokenId: any): Promise<LlmConfigDoc | null> {
320+
if (tokenId == null) {
321+
return null;
322+
}
323+
306324
const col = await getLlmConfigsCollection();
307-
return col.findOne({ token_id: tokenId });
325+
326+
let effectiveId: any = tokenId;
327+
if (typeof tokenId === 'string') {
328+
try {
329+
effectiveId = new ObjectId(tokenId);
330+
} catch {
331+
return null;
332+
}
333+
}
334+
335+
return col.findOne({ token_id: effectiveId });
308336
}
309337

310338
export async function upsertLlmConfigForOrg(
@@ -320,8 +348,17 @@ export async function upsertLlmConfigForOrg(
320348
const col = await getLlmConfigsCollection();
321349
const now = new Date();
322350

351+
let effectiveId: any = orgId;
352+
if (typeof orgId === 'string') {
353+
try {
354+
effectiveId = new ObjectId(orgId);
355+
} catch {
356+
throw new Error('invalid_org_id');
357+
}
358+
}
359+
323360
const update: Partial<LlmConfigDoc> = {
324-
org_id: orgId,
361+
org_id: effectiveId,
325362
token_id: undefined,
326363
provider: patch.provider,
327364
api_base_url: patch.api_base_url,
@@ -332,7 +369,7 @@ export async function upsertLlmConfigForOrg(
332369
};
333370

334371
await col.updateOne(
335-
{ org_id: orgId },
372+
{ org_id: effectiveId },
336373
{
337374
$set: update,
338375
$setOnInsert: {
@@ -342,7 +379,7 @@ export async function upsertLlmConfigForOrg(
342379
{ upsert: true }
343380
);
344381

345-
const doc = await col.findOne({ org_id: orgId });
382+
const doc = await col.findOne({ org_id: effectiveId });
346383
if (!doc) {
347384
throw new Error('failed_to_load_llm_config_org');
348385
}
@@ -362,9 +399,18 @@ export async function upsertLlmConfigForToken(
362399
const col = await getLlmConfigsCollection();
363400
const now = new Date();
364401

402+
let effectiveId: any = tokenId;
403+
if (typeof tokenId === 'string') {
404+
try {
405+
effectiveId = new ObjectId(tokenId);
406+
} catch {
407+
throw new Error('invalid_token_id');
408+
}
409+
}
410+
365411
const update: Partial<LlmConfigDoc> = {
366412
org_id: undefined,
367-
token_id: tokenId,
413+
token_id: effectiveId,
368414
provider: patch.provider,
369415
api_base_url: patch.api_base_url,
370416
api_key: patch.api_key,
@@ -374,7 +420,7 @@ export async function upsertLlmConfigForToken(
374420
};
375421

376422
await col.updateOne(
377-
{ token_id: tokenId },
423+
{ token_id: effectiveId },
378424
{
379425
$set: update,
380426
$setOnInsert: {
@@ -384,7 +430,7 @@ export async function upsertLlmConfigForToken(
384430
{ upsert: true }
385431
);
386432

387-
const doc = await col.findOne({ token_id: tokenId });
433+
const doc = await col.findOne({ token_id: effectiveId });
388434
if (!doc) {
389435
throw new Error('failed_to_load_llm_config_token');
390436
}
@@ -393,12 +439,32 @@ export async function upsertLlmConfigForToken(
393439

394440
export async function deleteLlmConfigForOrg(orgId: any): Promise<void> {
395441
const col = await getLlmConfigsCollection();
396-
await col.deleteOne({ org_id: orgId });
442+
443+
let effectiveId: any = orgId;
444+
if (typeof orgId === 'string') {
445+
try {
446+
effectiveId = new ObjectId(orgId);
447+
} catch {
448+
throw new Error('invalid_org_id');
449+
}
450+
}
451+
452+
await col.deleteOne({ org_id: effectiveId });
397453
}
398454

399455
export async function deleteLlmConfigForToken(tokenId: any): Promise<void> {
400456
const col = await getLlmConfigsCollection();
401-
await col.deleteOne({ token_id: tokenId });
457+
458+
let effectiveId: any = tokenId;
459+
if (typeof tokenId === 'string') {
460+
try {
461+
effectiveId = new ObjectId(tokenId);
462+
} catch {
463+
throw new Error('invalid_token_id');
464+
}
465+
}
466+
467+
await col.deleteOne({ token_id: effectiveId });
402468
}
403469

404470
// --- Token utilities ---

0 commit comments

Comments
 (0)