|
| 1 | +import { superjson } from "@/lib/superjson"; |
| 2 | +import { PrismaClient, User } from "@prisma/client"; |
| 3 | +import { Context } from "chat-toolkit"; |
| 4 | +import { RecordedEvent, TransactionT } from 'chat-toolkit/src/state/state'; |
| 5 | + |
| 6 | + |
| 7 | +type DefaultPrismaStateManagerImplementation = (prisma: PrismaClient) => (params: { currentUser: User; defaultState: string; }) => Context['manage'] |
| 8 | +type FindOrCreateUserPrisma = (prisma: PrismaClient) => (user_id: any, first_name?: any) => Promise<User> |
| 9 | + |
| 10 | +export const defaultPrismaStateManagerImplementation: DefaultPrismaStateManagerImplementation |
| 11 | + = prisma => params => { |
| 12 | + const { currentUser, defaultState } = params; |
| 13 | + |
| 14 | + const cachedCurrentState = { |
| 15 | + async set(state: string, args: any, session?: TransactionT) { |
| 16 | + console.warn("CREATE STATE") |
| 17 | + |
| 18 | + await prisma.state.create({ |
| 19 | + data: { |
| 20 | + state: state, |
| 21 | + arguments: superjson.stringify(args), |
| 22 | + created_at: new Date(), |
| 23 | + userId: currentUser.id |
| 24 | + } |
| 25 | + }); |
| 26 | + }, |
| 27 | + |
| 28 | + async get(it?: TransactionT) { |
| 29 | + console.log("Getting current state"); |
| 30 | + return await findCurrentState(it); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + const findCurrentState = (session?: TransactionT) => prisma.state.findFirst({ where: { userId: currentUser.id } }); |
| 35 | + |
| 36 | + const state: Context['manage']['state'] = { |
| 37 | + async save(state: string, args: any, params): Promise<void> { |
| 38 | + await cachedCurrentState.set(state, args, params?.session); |
| 39 | + }, |
| 40 | + |
| 41 | + default(): string { |
| 42 | + return defaultState; |
| 43 | + }, |
| 44 | + |
| 45 | + async current(): Promise<string | undefined> { |
| 46 | + const result = await cachedCurrentState.get(); |
| 47 | + return result?.state; |
| 48 | + }, |
| 49 | + |
| 50 | + async currentFull(): Promise<any | undefined> { |
| 51 | + return await cachedCurrentState.get(); |
| 52 | + }, |
| 53 | + |
| 54 | + async delete(params): Promise<void> { |
| 55 | + await prisma.state.delete({ |
| 56 | + where: { |
| 57 | + userId: currentUser.id |
| 58 | + } |
| 59 | + }); |
| 60 | + }, |
| 61 | + }; |
| 62 | + |
| 63 | + const events: Context['manage']['events'] = { |
| 64 | + async loadAll() { |
| 65 | + console.warn("load all events") |
| 66 | + |
| 67 | + const currenState = await cachedCurrentState.get(); |
| 68 | + |
| 69 | + const all = await prisma.event.findMany({ |
| 70 | + where: { |
| 71 | + stateId: currenState.id |
| 72 | + }, |
| 73 | + orderBy: { |
| 74 | + created_at: 'asc' |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + return all.map(({ eventName, data }) => ({ eventName, data: data ? superjson.parse(data) : undefined })) as RecordedEvent[]; |
| 79 | + }, |
| 80 | + |
| 81 | + async save(event) { |
| 82 | + console.warn("save event") |
| 83 | + |
| 84 | + const state = await cachedCurrentState.get(); |
| 85 | + |
| 86 | + await prisma.event.create({ |
| 87 | + data: { |
| 88 | + eventName: event.eventName, |
| 89 | + data: superjson.stringify(event.data), |
| 90 | + created_at: new Date(), |
| 91 | + stateId: state.id, |
| 92 | + } |
| 93 | + }); |
| 94 | + }, |
| 95 | + |
| 96 | + async deleteAll(params) { |
| 97 | + console.warn("DELETE ALL events") |
| 98 | + const state = await cachedCurrentState.get(); |
| 99 | + |
| 100 | + await prisma.event.deleteMany({ |
| 101 | + where: { stateId: state.id } |
| 102 | + }); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + return { |
| 107 | + state, |
| 108 | + events |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +export const findOrCreateUserPrisma: FindOrCreateUserPrisma |
| 114 | + = (prisma) => (user_id: any, first_name = '') => |
| 115 | + prisma.user.upsert({ |
| 116 | + create: { |
| 117 | + id: user_id, |
| 118 | + first_name, |
| 119 | + }, |
| 120 | + update: { |
| 121 | + first_name |
| 122 | + }, |
| 123 | + where: { |
| 124 | + id: user_id |
| 125 | + } |
| 126 | + }) |
| 127 | + |
0 commit comments