edtech / scripts /db-ops /fix_db_token.ts
CognxSafeTrack
refactor(worker): Sprint 2 β€” dead code removal and queue consolidation
37fb9ce
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);