diff --git a/app/database/database_seeder.ts b/app/database/database_seeder.ts new file mode 100644 index 0000000..3c99d93 --- /dev/null +++ b/app/database/database_seeder.ts @@ -0,0 +1,55 @@ +import User from '#models/user_model' +import ConstructionSite from '#models/construction_site_model' +import mongoose from 'mongoose' +import dotenv from 'dotenv' +import { Logger } from '@adonisjs/core/logger' + +dotenv.config() + +mongoose.connect(process.env.MONGO_URI || '') as mongoose.ConnectOptions + +// Seed the database with some data +export default class DatabaseSeeder { + public async run() { + // Create users + const admin = new User({ + email: 'admin@citium.it', + password: 'Admin123', + firstName: 'Admin', + lastName: 'Citium', + role: 'admin', + }) + await admin.save() + + const user = new User({ + email: 'mariorossi@mail.com', + password: 'Password123', + firstName: 'Mario', + lastName: 'Rossi', + }) + await user.save() + // Create construction sites + for (let i = 0; i < 10; i++) { + const constructionSite = new ConstructionSite({ + name: `Cantiere ${i}`, + street: `Via Cantiere`, + number: `${i}`, + description: `Costruzione di un nuovo cantiere ${i}`, + impacts_road: Math.random() >= 0.5, + impacts_cycling_lane: Math.random() >= 0.5, + impacts_public_transport: Math.random() >= 0.5, + impacts_sidewalk: Math.random() >= 0.5, + initial_budget: Math.floor(Math.random() * 1000000), + size: 1, + // Start date is random between 1 month before and 1 month after the current date + start_date: new Date(new Date().setMonth(new Date().getMonth() - 1 + Math.random() * 2)), + // End date is random between 1 week before and 6 months after now + end_date: new Date(new Date().setDate(new Date().getDate() - 7 + Math.random() * 180)), + }) + await constructionSite.save() + } + + // Close the connection + mongoose.connection.close() + } +} diff --git a/commands/seed_database.ts b/commands/seed_database.ts new file mode 100644 index 0000000..ce0e8b4 --- /dev/null +++ b/commands/seed_database.ts @@ -0,0 +1,19 @@ +import { BaseCommand } from '@adonisjs/core/ace' +import type { CommandOptions } from '@adonisjs/core/types/ace' +import DatabaseSeeder from '../app/database/database_seeder.js' + +export default class SeedDatabase extends BaseCommand { + static commandName = 'seed:database' + static description = 'seeds the database with initial data as admin and construction sites' + + static options: CommandOptions = {} + + async run() { + this.logger.info('Seeding the database...') + const seeder = new DatabaseSeeder() + await seeder.run() + this.logger.success('Database seeded successfully') + + process.exit(0) + } +} diff --git a/config/cors.ts b/config/cors.ts index 1c94b04..a6d34ea 100644 --- a/config/cors.ts +++ b/config/cors.ts @@ -8,7 +8,7 @@ import { defineConfig } from '@adonisjs/cors' */ const corsConfig = defineConfig({ enabled: true, - origin: ['citium.fiatlinux.it', 'http://localhost:5173'], + origin: ['https://citium.fiatlinux.it', 'http://localhost:5173'], methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'], headers: true, exposeHeaders: [],