Skip to content

Commit

Permalink
🐛 fix: fix a bug with server agent config when user not exist (lobehu…
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Sep 20, 2024
1 parent 2206e0a commit f6a232b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/app/api/webhooks/clerk/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const POST = async (req: Request): Promise<NextResponse> => {
const userService = new UserService();
switch (type) {
case 'user.created': {
pino.info('creating user due to clerk webhook');
return userService.createUser(data.id, data);
}
case 'user.deleted': {
Expand Down
6 changes: 6 additions & 0 deletions src/server/routers/lambda/agent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { z } from 'zod';

import { INBOX_SESSION_ID } from '@/const/session';
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
import { AgentModel } from '@/database/server/models/agent';
import { FileModel } from '@/database/server/models/file';
import { KnowledgeBaseModel } from '@/database/server/models/knowledgeBase';
import { SessionModel } from '@/database/server/models/session';
import { UserModel } from '@/database/server/models/user';
import { pino } from '@/libs/logger';
import { authedProcedure, router } from '@/libs/trpc';
import { KnowledgeItem, KnowledgeType } from '@/types/knowledgeBase';
Expand Down Expand Up @@ -84,6 +86,10 @@ export const agentRouter = router({
const item = await ctx.sessionModel.findByIdOrSlug(INBOX_SESSION_ID);
// if there is no session for user, create one
if (!item) {
// if there is no user, return default config
const user = await UserModel.findById(ctx.userId);
if (!user) return DEFAULT_AGENT_CONFIG;

const res = await ctx.sessionModel.createInbox();
pino.info('create inbox session', res);
}
Expand Down
2 changes: 0 additions & 2 deletions src/server/services/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { pino } from '@/libs/logger';

export class UserService {
createUser = async (id: string, params: UserJSON) => {
pino.info('creating user due to clerk webhook');

// Check if user already exists
const res = await UserModel.findById(id);

Expand Down

0 comments on commit f6a232b

Please sign in to comment.