Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1#!/usr/bin/env node
2/* eslint-disable no-console */
3/**
4 * Script to get invite token for a user
5 *
6 * Usage:
7 * npm run get-invite -- --email "user@example.com"
8 */
9
10import yargs from 'yargs';
11import { hideBin } from 'yargs/helpers';
12
13import getBottle from '../iocContainer/index.js';
14
15const argv = await yargs(hideBin(process.argv))
16 .options({
17 email: {
18 type: 'string',
19 demandOption: true,
20 description: 'Email address of the invited user',
21 },
22 })
23 .help()
24 .parse();
25
26async function getInviteToken() {
27 const bottle = await getBottle();
28 const container = bottle.container;
29
30 try {
31 const result = await container.KyselyPg
32 .selectFrom('public.invite_user_tokens')
33 .selectAll()
34 .where('email', '=', argv.email)
35 .orderBy('created_at', 'desc')
36 .limit(1)
37 .execute();
38
39 if (result.length === 0) {
40 console.log(`\n❌ No invite token found for email: ${argv.email}\n`);
41 process.exit(1);
42 }
43
44 const invite = result[0];
45 const uiUrl = process.env.UI_URL ?? 'http://localhost:3000';
46 const signupUrl = `${uiUrl}/signup/${invite.token}`;
47
48 console.log('\n✅ Invite Token Found!\n');
49 console.log('═'.repeat(60));
50 console.log('Invite Details:');
51 console.log('═'.repeat(60));
52 console.log(`Email: ${invite.email}`);
53 console.log(`Role: ${invite.role}`);
54 console.log(`Organization: ${invite.org_id}`);
55 console.log(`Created At: ${invite.created_at}`);
56 console.log('\n' + '═'.repeat(60));
57 console.log('🔗 Signup URL:');
58 console.log('═'.repeat(60));
59 console.log(`\n${signupUrl}\n`);
60 console.log('Copy this URL and paste it in your browser to sign up.');
61 console.log('═'.repeat(60) + '\n');
62
63 await container.closeSharedResourcesForShutdown();
64 process.exit(0);
65 } catch (error: unknown) {
66 console.error('\n❌ Error retrieving invite token:\n');
67 console.error(error);
68
69 try {
70 await container.closeSharedResourcesForShutdown();
71 } catch (shutdownError) {
72 console.error('Error during shutdown:', shutdownError);
73 }
74
75 process.exit(1);
76 }
77}
78
79getInviteToken().catch((error) => {
80 console.error('Unhandled error:', error);
81 process.exit(1);
82});
83