104 lines
3.8 KiB
TypeScript
104 lines
3.8 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import * as argon2 from 'argon2';
|
|
import { randomBytes } from 'crypto';
|
|
import { createTransport } from 'nodemailer';
|
|
|
|
/**
|
|
* Crée le magasin de démonstration pour App Review (Apple) : actif d'emblée, formule Pro,
|
|
* sans Stripe ni lien de paiement, abonnement valable 3 mois, dirigeant avec mot de passe aléatoire.
|
|
* Le mot de passe est envoyé par e-mail à NOTIFY_EMAIL (affiché ici si SMTP n'est pas configuré).
|
|
*
|
|
* Usage : npx ts-node scripts/create-demo-store.ts
|
|
* (le hachage argon2 ne peut pas être produit en SQL pur, d'où ce script plutôt qu'un .sql)
|
|
*/
|
|
const STAFF_EMAIL = 'demo.apple@gmail.com';
|
|
const NOTIFY_EMAIL = 'tony.prive35@gmail.com';
|
|
const SERIAL_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
|
const token = () => randomBytes(24).toString('base64url');
|
|
const serial = () => 'MEP' + [...randomBytes(8)].map((b) => SERIAL_ALPHABET[b % SERIAL_ALPHABET.length]).join('');
|
|
const password = () => [...randomBytes(16)].map((b) => SERIAL_ALPHABET[b % SERIAL_ALPHABET.length]).join('').replace(/(.{4})(?=.)/g, '$1-');
|
|
|
|
async function main() {
|
|
try {
|
|
process.loadEnvFile();
|
|
} catch {
|
|
/* variables déjà dans l'environnement */
|
|
}
|
|
const prisma = new PrismaClient();
|
|
if (await prisma.staffUser.findUnique({ where: { email: STAFF_EMAIL } })) {
|
|
throw new Error(`${STAFF_EMAIL} existe déjà : rien créé.`);
|
|
}
|
|
const pwd = password();
|
|
const periodEnd = new Date();
|
|
periodEnd.setMonth(periodEnd.getMonth() + 3);
|
|
|
|
const store = await prisma.store.create({
|
|
data: {
|
|
slug: 'cafe-demo-' + randomBytes(2).toString('hex'),
|
|
name: 'Café Démo',
|
|
addressLine: '1 rue de la Démo',
|
|
postalCode: '35000',
|
|
city: 'Rennes',
|
|
siret: '35600000000048',
|
|
contactName: 'Tony Averty',
|
|
contactEmail: STAFF_EMAIL,
|
|
contactPhone: '0612345678',
|
|
status: 'ACTIVE',
|
|
maxAmountCents: 50000,
|
|
minScanIntervalSec: 300,
|
|
subscription: {
|
|
create: { status: 'ACTIVE', plan: 'PRO', interval: 'MONTH', currentPeriodEnd: periodEnd },
|
|
},
|
|
cardDesign: {
|
|
create: {
|
|
programName: 'Carte Café Démo',
|
|
pointsMode: 'STAMP',
|
|
backgroundColor: '#0E7A66',
|
|
rewards: { create: [{ label: 'Un café offert', cost: 10 }] },
|
|
},
|
|
},
|
|
staff: {
|
|
create: {
|
|
email: STAFF_EMAIL,
|
|
displayName: 'Tony Averty',
|
|
role: 'MANAGER',
|
|
passwordHash: await argon2.hash(pwd),
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const customer = await prisma.customer.create({
|
|
data: { firstName: 'Camille', email: 'camille.demo@example.com', consentAt: new Date() },
|
|
});
|
|
const card = await prisma.loyaltyCard.create({
|
|
data: { storeId: store.id, customerId: customer.id, serial: serial(), authToken: token(), accessKey: token() },
|
|
});
|
|
|
|
const summary = [
|
|
`Magasin : ${store.name} (${store.id}), actif, formule Pro, valable jusqu'au ${periodEnd.toLocaleDateString('fr-FR')}`,
|
|
`Connexion app : ${STAFF_EMAIL}`,
|
|
`Client de démo : Camille — carte ${card.serial.replace(/(.{4})/g, '$1 ').trim()}`,
|
|
`Mot de passe : ${pwd}`,
|
|
].join('\n');
|
|
|
|
if (process.env.SMTP_HOST) {
|
|
const transport = createTransport({
|
|
host: process.env.SMTP_HOST,
|
|
port: Number(process.env.SMTP_PORT ?? 587),
|
|
secure: Number(process.env.SMTP_PORT) === 465,
|
|
auth: process.env.SMTP_USER ? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS } : undefined,
|
|
});
|
|
await transport.sendMail({
|
|
from: process.env.MAIL_FROM,
|
|
to: NOTIFY_EMAIL,
|
|
subject: 'Compte de démonstration App Review',
|
|
text: summary,
|
|
});
|
|
console.log(summary.replace(/Mot de passe : .*/, 'Mot de passe : envoyé par e-mail à ' + NOTIFY_EMAIL));
|
|
} else {
|
|
console.log('SMTP non configuré — voici le récapitulatif (à noter) :\n' + summary);
|
|
}
|
|
await prisma.$disconnect();
|
|
}
|
|
main();
|