/* eslint-disable no-await-in-loop */ import { Meteor } from 'meteor/meteor'; import { Accounts } from 'meteor/accounts-base'; import Documents from '../../api/Documents/Documents'; /* * Dev/staging fixtures. Replaces @cleverbeagle/seeder (sync Mongo, no Meteor 3 * support) with a small async seeder: idempotent (find-before-create), only * runs outside production. */ const USERS = [ { email: 'admin@admin.com', password: 'password', profile: { name: { first: 'Andy', last: 'Warhol' } }, roles: ['admin'] }, ...[ ['Frida', 'Kahlo'], ['JoaquĆ­n', 'Sorolla'], ['Maruja', 'Mallo'], ['Remedios', 'Varo'], ['Pablo', 'Picasso'] ].map(([first, last], i) => ({ email: `user+${i + 1}@test.com`, password: 'password', profile: { name: { first, last } }, roles: ['user'] })) ]; const DOCUMENTS_PER_USER = 5; async function ensureUser({ email, password, profile, roles }) { const existing = await Meteor.users.findOneAsync({ 'emails.address': email }); const userId = existing ? existing._id : await Accounts.createUserAsync({ email, password, profile }); if (!existing && roles) { await Meteor.users.updateAsync(userId, { $set: { roles } }); } return userId; } async function ensureDocuments(userId) { const count = await Documents.find({ owner: userId }).countAsync(); if (count > 0) return; for (let i = 0; i < DOCUMENTS_PER_USER; i += 1) { await Documents.insertAsync({ owner: userId, title: `Document #${i + 1}`, body: `This is the body of document #${i + 1}` }); } } Meteor.startup(async () => { // dev always; staging opts in with TCEF_SEED=1 (the old seeder's // 'staging' environment had no reliable detection either). if (!Meteor.isDevelopment && process.env.TCEF_SEED !== '1') return; try { for (const user of USERS) { const userId = await ensureUser(user); await ensureDocuments(userId); } console.log(`fixtures: ${USERS.length} dev users ensured`); } catch (e) { console.warn(`fixtures: seeding failed: ${e}`); } });