Incluye utilidades de ops para CRON_SECRET, login customer Google Ads, sync de seguimiento BPS y probes de conexiones. Co-authored-by: Cursor <cursoragent@cursor.com>
97 lines
2.6 KiB
TypeScript
97 lines
2.6 KiB
TypeScript
/**
|
|
* Provision victor@4ventures.es as BPS client portal user.
|
|
* npx tsx scripts/provision_victor_bps.ts
|
|
*/
|
|
import { config } from 'dotenv';
|
|
import { resolve } from 'path';
|
|
import { writeFileSync } from 'fs';
|
|
import { createClient } from '@supabase/supabase-js';
|
|
import { randomBytes } from 'crypto';
|
|
|
|
config({ path: resolve(process.cwd(), '.env.local') });
|
|
|
|
const BPS = '8bec977a-6032-4759-aa7f-b5042081f450';
|
|
const EMAIL = 'victor@4ventures.es';
|
|
|
|
async function main() {
|
|
const url = process.env.NEXT_PUBLIC_SUPABASE_URL?.trim();
|
|
const key = process.env.SUPABASE_SERVICE_ROLE_KEY?.trim();
|
|
if (!url || !key) throw new Error('Faltan NEXT_PUBLIC_SUPABASE_URL / SUPABASE_SERVICE_ROLE_KEY');
|
|
|
|
const admin = createClient(url, key, {
|
|
auth: { persistSession: false, autoRefreshToken: false },
|
|
});
|
|
|
|
const password = `BpsVictor-${randomBytes(4).toString('hex')}!9`;
|
|
|
|
let found: { id: string; app_metadata?: Record<string, unknown>; user_metadata?: Record<string, unknown> } | null =
|
|
null;
|
|
for (let page = 1; page <= 30; page++) {
|
|
const { data, error } = await admin.auth.admin.listUsers({ page, perPage: 200 });
|
|
if (error) throw error;
|
|
const hit = data.users.find((u) => (u.email || '').toLowerCase() === EMAIL);
|
|
if (hit) {
|
|
found = hit;
|
|
break;
|
|
}
|
|
if (data.users.length < 200) break;
|
|
}
|
|
|
|
const app_metadata = {
|
|
...(found?.app_metadata || {}),
|
|
role: 'client',
|
|
tenant_id: BPS,
|
|
company: 'Blue Parrot School',
|
|
};
|
|
const user_metadata = {
|
|
...(found?.user_metadata || {}),
|
|
role: 'client',
|
|
tenant_id: BPS,
|
|
company: 'Blue Parrot School',
|
|
};
|
|
|
|
if (found) {
|
|
const { error } = await admin.auth.admin.updateUserById(found.id, {
|
|
password,
|
|
email_confirm: true,
|
|
app_metadata,
|
|
user_metadata,
|
|
});
|
|
if (error) throw error;
|
|
console.log('updated', found.id);
|
|
} else {
|
|
const { data, error } = await admin.auth.admin.createUser({
|
|
email: EMAIL,
|
|
password,
|
|
email_confirm: true,
|
|
app_metadata,
|
|
user_metadata,
|
|
});
|
|
if (error) throw error;
|
|
console.log('created', data.user?.id);
|
|
}
|
|
|
|
writeFileSync(
|
|
resolve(process.cwd(), '.ops/victor-bps-credentials.txt'),
|
|
[
|
|
`email=${EMAIL}`,
|
|
`password=${password}`,
|
|
`tenant=Blue Parrot School (${BPS})`,
|
|
`role=client`,
|
|
`updated_at=${new Date().toISOString()}`,
|
|
'',
|
|
].join('\n'),
|
|
{ mode: 0o600 }
|
|
);
|
|
|
|
console.log('---');
|
|
console.log('url https://app.4ventures.es/login');
|
|
console.log('email', EMAIL);
|
|
console.log('password', password);
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|