feat(bot): add session management and user tracking

- Implement Prisma storage adapter for Telegram session management
- Add middleware to upsert Telegram user data
- Extend TelegramUser model with additional fields
- Add db:push command to turbo.json and package.json
- Extend Prisma client with exists helper method
This commit is contained in:
Yuu Ottosoka
2025-07-09 20:36:07 +03:00
parent 84760349c0
commit 49f0385d00
8 changed files with 115 additions and 15 deletions

View File

@@ -0,0 +1,37 @@
import { prisma } from '@repo/db';
import { StorageAdapter } from 'grammy';
export class PrismaStorageAdapter<T extends object>
implements StorageAdapter<T>
{
async read(key: string): Promise<T | undefined> {
return prisma.telegramSession
.findUnique({
where: {
id: key,
},
})
.then((session) => (session?.data as T) || undefined);
}
async write(key: string, data: T): Promise<void> {
console.log('write', data);
await prisma.telegramSession.upsert({
where: {
id: key,
},
create: {
id: key,
data,
},
update: {
data,
},
});
}
async delete(key: string): Promise<void> {
await prisma.telegramSession.delete({ where: { id: key } });
}
async has(key: string): Promise<boolean> {
return prisma.telegramSession.exists({ id: key });
}
}