Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions app/database/database_seeder.ts
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
password: 'Admin123',
firstName: 'Admin',
lastName: 'Citium',
role: 'admin',
})
await admin.save()

const user = new User({
email: '[email protected]',
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()
}
}
19 changes: 19 additions & 0 deletions commands/seed_database.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion config/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down