eletrotupi / tcc/ api/scripts/seed-dev.mjs27bca510c14b3745f6fa52ae54fac66653498781
2.6 KB | 95 lines Raw
1
import pg from 'pg'
2
import bcrypt from 'bcryptjs'
3

4
const { Client } = pg
5

6
const dbUrl = process.env.DATABASE_URL
7
if (!dbUrl) {
8
  console.error('DATABASE_URL is not set')
9
  process.exit(1)
10
}
11

12
const DEFAULT_EMAIL = process.env.SEED_EMAIL ?? 'e2e@example.com'
13
const DEFAULT_PASSWORD = process.env.SEED_PASSWORD ?? 'e2e-password'
14

15
// One user per lifecycle state, so the manual verification checklist can jump
16
// straight to a state instead of re-running signup -> email -> code every time.
17
const users = [
18
  {
19
    firstName: 'E2E',
20
    lastName: 'User',
21
    email: DEFAULT_EMAIL,
22
    password: DEFAULT_PASSWORD,
23
    active: true,
24
    onboardingSeen: true,
25
    activationCode: null,
26
  },
27
  {
28
    firstName: 'Inactive',
29
    lastName: 'User',
30
    email: 'inactive@example.com',
31
    password: 'inactive-password',
32
    active: false,
33
    onboardingSeen: false,
34
    activationCode: '123456',
35
  },
36
  {
37
    firstName: 'Onboarding',
38
    lastName: 'User',
39
    email: 'onboarding@example.com',
40
    password: 'onboarding-password',
41
    active: true,
42
    onboardingSeen: false,
43
    activationCode: null,
44
  },
45
]
46

47
const db = new Client({ connectionString: dbUrl })
48
await db.connect()
49

50
for (const user of users) {
51
  const hash = bcrypt.hashSync(user.password, 10)
52

53
  // The seeded activation code stays valid for a year so the inactive fixture
54
  // does not silently rot between runs.
55
  const codeExpiresAt = user.activationCode
56
    ? new Date(Date.now() + 365 * 24 * 60 * 60 * 1000)
57
    : null
58

59
  const { rows } = await db.query(
60
    `INSERT INTO users (
61
       first_name, last_name, email, encrypted_password, active,
62
       activation_code, activation_code_expires_at,
63
       onboarding_seen_at, terms_accepted_at, terms_version, updated_at
64
     )
65
     VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW(), '1', NOW())
66
     ON CONFLICT (email) DO UPDATE
67
       SET encrypted_password = EXCLUDED.encrypted_password,
68
           active = EXCLUDED.active,
69
           activation_code = EXCLUDED.activation_code,
70
           activation_code_expires_at = EXCLUDED.activation_code_expires_at,
71
           onboarding_seen_at = EXCLUDED.onboarding_seen_at,
72
           terms_accepted_at = NOW(),
73
           terms_version = '1',
74
           updated_at = NOW()
75
     RETURNING id, email`,
76
    [
77
      user.firstName,
78
      user.lastName,
79
      user.email,
80
      hash,
81
      user.active,
82
      user.activationCode,
83
      codeExpiresAt,
84
      user.onboardingSeen ? new Date() : null,
85
    ],
86
  )
87

88
  const suffix = user.activationCode ? ` code=${user.activationCode}` : ''
89
  console.log(
90
    `Seeded ${rows[0].email} (id ${rows[0].id}) ` +
91
      `active=${user.active} onboardingSeen=${user.onboardingSeen}${suffix}`,
92
  )
93
}
94

95
await db.end()