Implement database migration, notification logs, and one-click Mailcow setup
This commit is contained in:
@@ -1,56 +1,29 @@
|
||||
import { prisma } from "../lib/prisma";
|
||||
import { getUsers } from "../lib/users";
|
||||
import "dotenv/config";
|
||||
|
||||
async function main() {
|
||||
console.log("Seeding database...");
|
||||
|
||||
// 1. Migrate Users
|
||||
const users = await getUsers();
|
||||
for (const user of users) {
|
||||
console.log(`Migrating user: ${user.email}`);
|
||||
await prisma.user.upsert({
|
||||
where: { email: user.email },
|
||||
update: {
|
||||
name: user.name,
|
||||
password: user.password,
|
||||
role: user.role,
|
||||
domains: user.domains,
|
||||
telegramId: user.telegramId,
|
||||
},
|
||||
create: {
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
password: user.password,
|
||||
role: user.role,
|
||||
domains: user.domains,
|
||||
telegramId: user.telegramId,
|
||||
// Create a default super admin if none exists
|
||||
const adminEmail = "admin@ayris.tech";
|
||||
|
||||
const existingAdmin = await prisma.user.findUnique({
|
||||
where: { email: adminEmail }
|
||||
});
|
||||
|
||||
if (!existingAdmin) {
|
||||
console.log(`Creating default admin: ${adminEmail}`);
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
email: adminEmail,
|
||||
name: "System Admin",
|
||||
password: "admin123", // Should be changed immediately
|
||||
role: "SUPER_ADMIN",
|
||||
domains: ["*"],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Migrate Mailbox Mappings
|
||||
const mappingsRaw = process.env.MAIL_USER_MAPPINGS || "{}";
|
||||
try {
|
||||
const mappings = JSON.parse(mappingsRaw);
|
||||
for (const [email, userKey] of Object.entries(mappings)) {
|
||||
const userIndex = parseInt((userKey as string).replace("USER_", ""));
|
||||
const userEmail = process.env[`USER_${userIndex}_EMAIL`];
|
||||
|
||||
if (userEmail) {
|
||||
const dbUser = await prisma.user.findUnique({ where: { email: userEmail } });
|
||||
if (dbUser) {
|
||||
console.log(`Creating mapping: ${email} -> ${userEmail}`);
|
||||
await prisma.mailboxMapping.upsert({
|
||||
where: { email },
|
||||
update: { userId: dbUser.id },
|
||||
create: { email, userId: dbUser.id },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Mapping migration failed:", e);
|
||||
} else {
|
||||
console.log("Admin user already exists.");
|
||||
}
|
||||
|
||||
console.log("Seeding complete.");
|
||||
|
||||
Reference in New Issue
Block a user