시드 생성
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
const bcrypt = require('bcrypt');
|
||||
|
||||
module.exports = {
|
||||
async up (queryInterface, Sequelize) {
|
||||
// 환경변수 기반으로 관리자 계정 생성
|
||||
const adminEmail = process.env.ADMIN_EMAIL;
|
||||
const adminPassword = process.env.ADMIN_PASSWORD;
|
||||
const adminName = process.env.ADMIN_NAME;
|
||||
const adminUsername = process.env.ADMIN_USERNAME;
|
||||
|
||||
// 이미 존재하는지 체크
|
||||
const [results] = await queryInterface.sequelize.query(
|
||||
`SELECT id FROM Users WHERE email = :email LIMIT 1`,
|
||||
{ replacements: { email: adminEmail } }
|
||||
);
|
||||
if (results.length > 0) return;
|
||||
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
const hashedPassword = await bcrypt.hash(adminPassword, salt);
|
||||
await queryInterface.bulkInsert('Users', [{
|
||||
username: adminUsername,
|
||||
name: adminName,
|
||||
email: adminEmail,
|
||||
password: hashedPassword,
|
||||
role: 'admin',
|
||||
status: 'active',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
}], {});
|
||||
},
|
||||
async down (queryInterface, Sequelize) {
|
||||
const adminEmail = process.env.ADMIN_EMAIL;
|
||||
// 먼저 ActivityLogs에서 admin 유저의 로그를 삭제
|
||||
const [results] = await queryInterface.sequelize.query(
|
||||
`SELECT id FROM Users WHERE email = :email LIMIT 1`,
|
||||
{ replacements: { email: adminEmail } }
|
||||
);
|
||||
if (results.length > 0) {
|
||||
const adminId = results[0].id;
|
||||
await queryInterface.bulkDelete('ActivityLogs', { userId: adminId }, {});
|
||||
}
|
||||
await queryInterface.bulkDelete('Users', { email: adminEmail }, {});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user