| import 'dotenv/config'; | |
| import path from 'path'; | |
| import dotenv from 'dotenv'; | |
| dotenv.config({ path: path.join(__dirname, '../../../../.env') }); | |
| import { PrismaClient } from '@prisma/client'; | |
| import { encrypt } from '../../../packages/shared-types/src/crypto'; | |
| const prisma = new PrismaClient(); | |
| async function fixToken() { | |
| const orgId = 'default-org-id'; | |
| const globalToken = process.env.WHATSAPP_ACCESS_TOKEN; | |
| const encryptionSecret = process.env.ENCRYPTION_SECRET; | |
| if (!globalToken || !encryptionSecret) { | |
| console.error('β Missing WHATSAPP_ACCESS_TOKEN or ENCRYPTION_SECRET in .env'); | |
| return; | |
| } | |
| const org = await prisma.organization.findUnique({ | |
| where: { id: orgId } | |
| }); | |
| if (!org) { | |
| console.error(`β Organization ${orgId} NOT FOUND.`); | |
| return; | |
| } | |
| // Encrypt the global token using the CURRENT secret | |
| const newlyEncryptedToken = encrypt(globalToken, encryptionSecret); | |
| await prisma.organization.update({ | |
| where: { id: orgId }, | |
| data: { | |
| systemUserToken: newlyEncryptedToken | |
| } | |
| }); | |
| console.log(`β Successfully updated systemUserToken for ${org.name} (${org.id}).`); | |
| console.log(`New encrypted token starts with: ${newlyEncryptedToken.substring(0, 20)}...`); | |
| await prisma.$disconnect(); | |
| } | |
| fixToken().catch(console.error); | |