213 lines
6.2 KiB
TypeScript
213 lines
6.2 KiB
TypeScript
|
|
import { PrismaClient, UserRole, UserStatus } from '@prisma/client';
|
||
|
|
import * as argon2 from 'argon2';
|
||
|
|
import * as dotenv from 'dotenv';
|
||
|
|
import * as path from 'path';
|
||
|
|
|
||
|
|
dotenv.config({ path: path.resolve(__dirname, '../.env') });
|
||
|
|
|
||
|
|
const prisma = new PrismaClient();
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
console.log('--- Starting Database Seeding ---');
|
||
|
|
|
||
|
|
// 1. Initial System Settings
|
||
|
|
const defaultSettings = [
|
||
|
|
{
|
||
|
|
key: 'MAX_FILE_SIZE_BYTES',
|
||
|
|
value: '5368709120', // 5GB
|
||
|
|
description: 'Maximum permitted single file upload size in bytes',
|
||
|
|
},
|
||
|
|
{
|
||
|
|
key: 'DEFAULT_USER_QUOTA_BYTES',
|
||
|
|
value: '5368709120', // 5GB
|
||
|
|
description: 'Default storage quota allocated to newly registered users',
|
||
|
|
},
|
||
|
|
{
|
||
|
|
key: 'ALLOWED_EXTENSIONS',
|
||
|
|
value: 'mp4,webm,mov,avi,mkv,png,jpg,jpeg,webp,gif,pdf,doc,docx,xls,xlsx,ppt,pptx,zip,rar,7z,tar,gz,mp3,wav,ogg,txt,csv,json',
|
||
|
|
description: 'Comma-separated whitelist of allowed file extensions',
|
||
|
|
},
|
||
|
|
{
|
||
|
|
key: 'ENABLE_PUBLIC_REGISTRATION',
|
||
|
|
value: 'true',
|
||
|
|
description: 'Flag controlling whether new users can self-register',
|
||
|
|
},
|
||
|
|
{
|
||
|
|
key: 'STORAGE_RETENTION_DAYS',
|
||
|
|
value: '30',
|
||
|
|
description: 'Soft-deleted file retention duration in recycle bin',
|
||
|
|
},
|
||
|
|
];
|
||
|
|
|
||
|
|
for (const s of defaultSettings) {
|
||
|
|
await prisma.systemSetting.upsert({
|
||
|
|
where: { key: s.key },
|
||
|
|
update: { value: s.value, description: s.description },
|
||
|
|
create: s,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
console.log('✓ System settings initialized');
|
||
|
|
|
||
|
|
// 2. Primary Super Admin (Ecohub / Ecohub@520)
|
||
|
|
const ecohubPasswordHash = await argon2.hash('Ecohub@520', { type: argon2.argon2id });
|
||
|
|
const ecohubAdmin = await prisma.user.upsert({
|
||
|
|
where: { username: 'ecohub' },
|
||
|
|
update: {
|
||
|
|
passwordHash: ecohubPasswordHash,
|
||
|
|
role: UserRole.SUPER_ADMIN,
|
||
|
|
status: UserStatus.ACTIVE,
|
||
|
|
},
|
||
|
|
create: {
|
||
|
|
email: 'ecohub@fileshare.local',
|
||
|
|
username: 'ecohub',
|
||
|
|
fullName: 'Ecohub Super Administrator',
|
||
|
|
passwordHash: ecohubPasswordHash,
|
||
|
|
role: UserRole.SUPER_ADMIN,
|
||
|
|
status: UserStatus.ACTIVE,
|
||
|
|
storageQuotaBytes: BigInt(107374182400), // 100GB
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const ecohubRoot = await prisma.folder.findFirst({
|
||
|
|
where: { ownerId: ecohubAdmin.id, parentId: null, name: 'Root' },
|
||
|
|
});
|
||
|
|
if (!ecohubRoot) {
|
||
|
|
await prisma.folder.create({
|
||
|
|
data: {
|
||
|
|
ownerId: ecohubAdmin.id,
|
||
|
|
parentId: null,
|
||
|
|
name: 'Root',
|
||
|
|
logicalPath: '/',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
console.log(`✓ Primary Super Admin created: Ecohub (email: ${ecohubAdmin.email})`);
|
||
|
|
|
||
|
|
// 3. Staff Officer User (Ecohubstaff / 123)
|
||
|
|
const staffPasswordHash = await argon2.hash('123', { type: argon2.argon2id });
|
||
|
|
const staffUser = await prisma.user.upsert({
|
||
|
|
where: { username: 'ecohubstaff' },
|
||
|
|
update: {
|
||
|
|
passwordHash: staffPasswordHash,
|
||
|
|
role: UserRole.ADMIN,
|
||
|
|
status: UserStatus.ACTIVE,
|
||
|
|
},
|
||
|
|
create: {
|
||
|
|
email: 'ecohubstaff@fileshare.local',
|
||
|
|
username: 'ecohubstaff',
|
||
|
|
fullName: 'Ecohub Staff Officer',
|
||
|
|
passwordHash: staffPasswordHash,
|
||
|
|
role: UserRole.ADMIN,
|
||
|
|
status: UserStatus.ACTIVE,
|
||
|
|
storageQuotaBytes: BigInt(53687091200), // 50GB
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const staffRoot = await prisma.folder.findFirst({
|
||
|
|
where: { ownerId: staffUser.id, parentId: null, name: 'Root' },
|
||
|
|
});
|
||
|
|
if (!staffRoot) {
|
||
|
|
await prisma.folder.create({
|
||
|
|
data: {
|
||
|
|
ownerId: staffUser.id,
|
||
|
|
parentId: null,
|
||
|
|
name: 'Root',
|
||
|
|
logicalPath: '/',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
console.log(`✓ Staff User created: Ecohubstaff (email: ${staffUser.email})`);
|
||
|
|
|
||
|
|
// 4. Normal User (ecouser / 123)
|
||
|
|
const normalUserPasswordHash = await argon2.hash('123', { type: argon2.argon2id });
|
||
|
|
const normalUser = await prisma.user.upsert({
|
||
|
|
where: { username: 'ecouser' },
|
||
|
|
update: {
|
||
|
|
passwordHash: normalUserPasswordHash,
|
||
|
|
role: UserRole.USER,
|
||
|
|
status: UserStatus.ACTIVE,
|
||
|
|
},
|
||
|
|
create: {
|
||
|
|
email: 'ecouser@fileshare.local',
|
||
|
|
username: 'ecouser',
|
||
|
|
fullName: 'Ecohub Standard User',
|
||
|
|
passwordHash: normalUserPasswordHash,
|
||
|
|
role: UserRole.USER,
|
||
|
|
status: UserStatus.ACTIVE,
|
||
|
|
storageQuotaBytes: BigInt(10737418240), // 10GB
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const normalUserRoot = await prisma.folder.findFirst({
|
||
|
|
where: { ownerId: normalUser.id, parentId: null, name: 'Root' },
|
||
|
|
});
|
||
|
|
if (!normalUserRoot) {
|
||
|
|
await prisma.folder.create({
|
||
|
|
data: {
|
||
|
|
ownerId: normalUser.id,
|
||
|
|
parentId: null,
|
||
|
|
name: 'Root',
|
||
|
|
logicalPath: '/',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
console.log(`✓ Normal User created: ecouser (email: ${normalUser.email})`);
|
||
|
|
|
||
|
|
// Keep legacy test accounts active for test suite backward compatibility
|
||
|
|
const legacyAdminHash = await argon2.hash('SuperAdmin@Secure2026!', { type: argon2.argon2id });
|
||
|
|
await prisma.user.upsert({
|
||
|
|
where: { email: 'superadmin@fileshare.local' },
|
||
|
|
update: {},
|
||
|
|
create: {
|
||
|
|
email: 'superadmin@fileshare.local',
|
||
|
|
username: 'superadmin',
|
||
|
|
fullName: 'Legacy Super Administrator',
|
||
|
|
passwordHash: legacyAdminHash,
|
||
|
|
role: UserRole.SUPER_ADMIN,
|
||
|
|
status: UserStatus.ACTIVE,
|
||
|
|
storageQuotaBytes: BigInt(53687091200),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const userAHash = await argon2.hash('UserA@Secure2026!', { type: argon2.argon2id });
|
||
|
|
await prisma.user.upsert({
|
||
|
|
where: { email: 'usera@fileshare.local' },
|
||
|
|
update: {},
|
||
|
|
create: {
|
||
|
|
email: 'usera@fileshare.local',
|
||
|
|
username: 'usera',
|
||
|
|
fullName: 'Alice Johnson (User A)',
|
||
|
|
passwordHash: userAHash,
|
||
|
|
role: UserRole.USER,
|
||
|
|
status: UserStatus.ACTIVE,
|
||
|
|
storageQuotaBytes: BigInt(5368709120),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const userBHash = await argon2.hash('UserB@Secure2026!', { type: argon2.argon2id });
|
||
|
|
await prisma.user.upsert({
|
||
|
|
where: { email: 'userb@fileshare.local' },
|
||
|
|
update: {},
|
||
|
|
create: {
|
||
|
|
email: 'userb@fileshare.local',
|
||
|
|
username: 'userb',
|
||
|
|
fullName: 'Bob Smith (User B)',
|
||
|
|
passwordHash: userBHash,
|
||
|
|
role: UserRole.USER,
|
||
|
|
status: UserStatus.ACTIVE,
|
||
|
|
storageQuotaBytes: BigInt(5368709120),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('--- Database Seeding Completed Successfully ---');
|
||
|
|
}
|
||
|
|
|
||
|
|
main()
|
||
|
|
.catch((e) => {
|
||
|
|
console.error('Seeding error:', e);
|
||
|
|
process.exit(1);
|
||
|
|
})
|
||
|
|
.finally(async () => {
|
||
|
|
await prisma.$disconnect();
|
||
|
|
});
|