|
| 1 | +const User = require('../domains/user/schema'); |
| 2 | +const bcrypt = require('bcrypt'); |
| 3 | + |
| 4 | +async function runMigration() { |
| 5 | + console.log("Running migration: 004-update-demo-users"); |
| 6 | + const salt = await bcrypt.genSalt(10); |
| 7 | + |
| 8 | + const adminUser = await User.findOne({ email: '[email protected]' }); |
| 9 | + const visitorUser = await User.findOne({ email: '[email protected]' }); |
| 10 | + |
| 11 | + if (adminUser) { |
| 12 | + adminUser.local.password = await bcrypt.hash('password', salt); |
| 13 | + await adminUser.save(); |
| 14 | + } else { |
| 15 | + const adminUser = { |
| 16 | + |
| 17 | + displayName: 'Admin User', |
| 18 | + authType: 'local', |
| 19 | + local: { |
| 20 | + |
| 21 | + password: await bcrypt.hash('password', salt), |
| 22 | + }, |
| 23 | + isDemo: false, |
| 24 | + isVerified: true, |
| 25 | + isAdmin: true, |
| 26 | + role: 'admin' |
| 27 | + }; |
| 28 | + const result = await User.create(adminUser); |
| 29 | + console.log(`Inserted ${adminUser.displayName}: ${result._id}`); |
| 30 | + } |
| 31 | + |
| 32 | + if (visitorUser) { |
| 33 | + visitorUser.local.password = await bcrypt.hash('password', salt); |
| 34 | + await visitorUser.save(); |
| 35 | + } else { |
| 36 | + const visitorUser = { |
| 37 | + |
| 38 | + displayName: 'Visitor User', |
| 39 | + authType: 'local', |
| 40 | + local: { |
| 41 | + |
| 42 | + password: await bcrypt.hash('password', salt), |
| 43 | + }, |
| 44 | + isDemo: false, |
| 45 | + isVerified: true, |
| 46 | + isAdmin: false, |
| 47 | + role: 'visitor' |
| 48 | + }; |
| 49 | + |
| 50 | + const result = await User.create(visitorUser); |
| 51 | + console.log(`Inserted ${visitorUser.displayName}: ${result._id}`); |
| 52 | + } |
| 53 | + |
| 54 | + console.log("Successfully completed migration 004"); |
| 55 | +} |
| 56 | + |
| 57 | +module.exports = { |
| 58 | + runMigration |
| 59 | +}; |
0 commit comments