| import type { Database } from "@db/client"; |
| import { type ChatUserContext, chatCache } from "@midday/cache/chat-cache"; |
| import { getBankAccounts, getTeamById, getUserById } from "@midday/db/queries"; |
| import { logger } from "@midday/logger"; |
| import { HTTPException } from "hono/http-exception"; |
|
|
| interface GetUserContextParams { |
| db: Database; |
| userId: string; |
| teamId: string; |
| country?: string; |
| city?: string; |
| timezone?: string; |
| } |
|
|
| |
| |
| |
| |
| export async function getUserContext({ |
| db, |
| userId, |
| teamId, |
| country, |
| city, |
| timezone, |
| }: GetUserContextParams): Promise<ChatUserContext> { |
| |
| const cached = await chatCache.getUserContext(userId, teamId); |
|
|
| |
| let teamContext = await chatCache.getTeamContext(teamId); |
|
|
| |
| if (!teamContext) { |
| const bankAccounts = await getBankAccounts(db, { |
| teamId, |
| enabled: true, |
| }); |
| const hasBankAccounts = bankAccounts.length > 0; |
|
|
| teamContext = { |
| teamId, |
| hasBankAccounts, |
| }; |
|
|
| |
| chatCache.setTeamContext(teamId, teamContext).catch((err) => { |
| logger.warn("Failed to cache team context", { |
| teamId, |
| error: err.message, |
| }); |
| }); |
| } |
|
|
| |
| if (cached) { |
| return { |
| ...cached, |
| hasBankAccounts: teamContext.hasBankAccounts, |
| }; |
| } |
|
|
| |
| const [team, user] = await Promise.all([ |
| getTeamById(db, teamId), |
| getUserById(db, userId), |
| ]); |
|
|
| if (!team || !user) { |
| throw new HTTPException(404, { |
| message: "User or team not found", |
| }); |
| } |
|
|
| const context: ChatUserContext = { |
| userId, |
| teamId, |
| teamName: team.name, |
| fullName: user.fullName, |
| fiscalYearStartMonth: team.fiscalYearStartMonth, |
| baseCurrency: team.baseCurrency, |
| locale: user.locale ?? "en-US", |
| dateFormat: user.dateFormat, |
| country, |
| city, |
| timezone, |
| hasBankAccounts: teamContext.hasBankAccounts, |
| }; |
|
|
| |
| chatCache.setUserContext(userId, teamId, context).catch((err) => { |
| logger.warn("Failed to cache user context", { |
| userId, |
| teamId, |
| error: err.message, |
| }); |
| }); |
|
|
| return context; |
| } |
|
|