115 lines
3.0 KiB
TypeScript
115 lines
3.0 KiB
TypeScript
/**
|
|
* Seed Mana Collection flagship store + STORE_ADMIN (and optional SUPER_ADMIN).
|
|
*
|
|
* Usage:
|
|
* npm run seed:mana-store -- \
|
|
* --phone 09120000000 --password secret123 \
|
|
* --first مانا --last ادمین \
|
|
* [--super-phone 09127004945 --super-password secret123]
|
|
*/
|
|
import { PrismaClient, StoreStatus, UserRole } from '@prisma/client';
|
|
import * as bcrypt from 'bcrypt';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
function arg(name: string, fallback?: string): string | undefined {
|
|
const idx = process.argv.indexOf(`--${name}`);
|
|
if (idx >= 0 && process.argv[idx + 1]) return process.argv[idx + 1];
|
|
return fallback;
|
|
}
|
|
|
|
async function main() {
|
|
const cellNumber = arg('phone', '09120000000')!;
|
|
const password = arg('password', 'mana1234')!;
|
|
const firstName = arg('first', 'مانا')!;
|
|
const lastName = arg('last', 'ادمین')!;
|
|
const superPhone = arg('super-phone');
|
|
const superPassword = arg('super-password', 'admin1234');
|
|
|
|
if (!/^09\d{9}$/.test(cellNumber)) {
|
|
throw new Error('phone must match 09xxxxxxxxx');
|
|
}
|
|
|
|
const passwordHash = await bcrypt.hash(password, 10);
|
|
|
|
const storeAdmin = await prisma.user.upsert({
|
|
where: { cellNumber },
|
|
update: {
|
|
firstName,
|
|
lastName,
|
|
passwordHash,
|
|
role: UserRole.STORE_ADMIN,
|
|
disabled: false,
|
|
},
|
|
create: {
|
|
firstName,
|
|
lastName,
|
|
cellNumber,
|
|
passwordHash,
|
|
role: UserRole.STORE_ADMIN,
|
|
},
|
|
});
|
|
|
|
const store = await prisma.store.upsert({
|
|
where: { slug: 'mana-collection' },
|
|
update: {
|
|
nameFa: 'مانا کالکشن',
|
|
nameEn: 'Mana Collection',
|
|
status: StoreStatus.active,
|
|
ownerId: storeAdmin.id,
|
|
rejectionReason: null,
|
|
},
|
|
create: {
|
|
slug: 'mana-collection',
|
|
nameFa: 'مانا کالکشن',
|
|
nameEn: 'Mana Collection',
|
|
description: 'فروشگاه اصلی مانا کالکشن',
|
|
status: StoreStatus.active,
|
|
ownerId: storeAdmin.id,
|
|
},
|
|
});
|
|
|
|
console.log('Seeded STORE_ADMIN + store:', {
|
|
userId: storeAdmin.id,
|
|
cellNumber: storeAdmin.cellNumber,
|
|
storeId: store.id,
|
|
slug: store.slug,
|
|
status: store.status,
|
|
});
|
|
|
|
if (superPhone) {
|
|
if (!/^09\d{9}$/.test(superPhone)) {
|
|
throw new Error('super-phone must match 09xxxxxxxxx');
|
|
}
|
|
const superHash = await bcrypt.hash(superPassword!, 10);
|
|
const superAdmin = await prisma.user.upsert({
|
|
where: { cellNumber: superPhone },
|
|
update: {
|
|
role: UserRole.SUPER_ADMIN,
|
|
passwordHash: superHash,
|
|
disabled: false,
|
|
},
|
|
create: {
|
|
firstName: 'سوپر',
|
|
lastName: 'ادمین',
|
|
cellNumber: superPhone,
|
|
passwordHash: superHash,
|
|
role: UserRole.SUPER_ADMIN,
|
|
},
|
|
});
|
|
console.log('Seeded SUPER_ADMIN:', {
|
|
id: superAdmin.id,
|
|
cellNumber: superAdmin.cellNumber,
|
|
});
|
|
}
|
|
}
|
|
|
|
main()
|
|
.catch((err) => {
|
|
console.error(err.message || err);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|