File size: 2,157 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { RedisCache } from "./redis-client";

// Redis-based cache for chat data shared across all server instances
const userContextCache = new RedisCache("chat:user", 30 * 60); // 30 minutes TTL
const teamContextCache = new RedisCache("chat:team", 5 * 60); // 5 minutes TTL

// Disable caching in development
const isDevelopment = process.env.NODE_ENV === "development";

export interface ChatTeamContext {
  teamId: string;
  hasBankAccounts?: boolean;
}

export interface ChatUserContext {
  userId: string;
  teamId: string;
  teamName?: string | null;
  fullName?: string | null;
  avatarUrl?: string | null;
  baseCurrency?: string | null;
  countryCode?: string | null;
  dateFormat?: string | null;
  locale?: string | null;
  country?: string | null;
  city?: string | null;
  region?: string | null;
  timezone?: string | null;
  fiscalYearStartMonth?: number | null;
  hasBankAccounts?: boolean;
}

export const chatCache = {
  getUserContext: (
    userId: string,
    teamId: string,
  ): Promise<ChatUserContext | undefined> => {
    if (isDevelopment) return Promise.resolve(undefined);
    return userContextCache.get<ChatUserContext>(`${userId}:${teamId}`);
  },

  setUserContext: (
    userId: string,
    teamId: string,
    context: ChatUserContext,
  ): Promise<void> => {
    if (isDevelopment) return Promise.resolve();
    return userContextCache.set(`${userId}:${teamId}`, context);
  },

  invalidateUserContext: (userId: string, teamId: string): Promise<void> => {
    if (isDevelopment) return Promise.resolve();
    return userContextCache.delete(`${userId}:${teamId}`);
  },

  getTeamContext: (teamId: string): Promise<ChatTeamContext | undefined> => {
    if (isDevelopment) return Promise.resolve(undefined);
    return teamContextCache.get<ChatTeamContext>(teamId);
  },

  setTeamContext: (teamId: string, context: ChatTeamContext): Promise<void> => {
    if (isDevelopment) return Promise.resolve();
    return teamContextCache.set(teamId, context);
  },

  invalidateTeamContext: (teamId: string): Promise<void> => {
    if (isDevelopment) return Promise.resolve();
    return teamContextCache.delete(teamId);
  },
};