File size: 1,373 Bytes
37fb9ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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);