45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import * as bcrypt from 'bcrypt';
|
|
import crypto from 'crypto';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
const clientId = 'raylab-telegram-bot';
|
|
const clientSecret = crypto.randomBytes(32).toString('hex');
|
|
const saltRounds = parseInt(process.env.BCRYPT_SALT_ROUNDS ?? '10', 10);
|
|
const clientSecretHash = await bcrypt.hash(clientSecret, saltRounds);
|
|
|
|
// Create service account only if not exists
|
|
const existing = await prisma.serviceAccount.findUnique({ where: { client_id: clientId } });
|
|
|
|
if (!existing) {
|
|
const sa = await prisma.serviceAccount.create({
|
|
data: {
|
|
client_id: clientId,
|
|
client_secret_hash: clientSecretHash,
|
|
name: 'Telegram Bot',
|
|
role: 'BOT',
|
|
permissions: ['DEBT_CREATE', 'DEBT_READ', 'DEBT_SETTLE'],
|
|
status: 'ACTIVE',
|
|
},
|
|
});
|
|
|
|
console.log('Service account created for development/testing.');
|
|
console.log('Client ID:', clientId);
|
|
console.log('Client Secret (one-time):', clientSecret);
|
|
console.log('Please store the client secret securely.');
|
|
} else {
|
|
console.log('Service account already exists.');
|
|
}
|
|
}
|
|
|
|
main()
|
|
.catch(e => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|