31684/backend/src/db/seeders/20231127130745-sample-data.js
2025-05-21 14:14:53 +00:00

919 lines
22 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Bookings = db.bookings;
const Extras = db.extras;
const Messages = db.messages;
const Vehicles = db.vehicles;
const Organizations = db.organizations;
const BookingsData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_date: new Date('2023-11-01T10:00:00Z'),
end_date: new Date('2023-11-05T10:00:00Z'),
status: 'cancelled',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_date: new Date('2023-11-10T09:00:00Z'),
end_date: new Date('2023-11-12T09:00:00Z'),
status: 'completed',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_date: new Date('2023-11-15T08:00:00Z'),
end_date: new Date('2023-11-20T08:00:00Z'),
status: 'pending',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_date: new Date('2023-11-18T07:00:00Z'),
end_date: new Date('2023-11-22T07:00:00Z'),
status: 'pending',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_date: new Date('2023-11-25T06:00:00Z'),
end_date: new Date('2023-11-30T06:00:00Z'),
status: 'cancelled',
// type code here for "relation_one" field
},
];
const ExtrasData = [
{
name: 'Child Seat',
price: 5,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Bicycle Stand',
price: 7,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Luggage Box',
price: 10,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'GPS Navigation',
price: 8,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Wi-Fi Hotspot',
price: 6,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
];
const MessagesData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'Is the car available for the weekend?',
sent_at: new Date('2023-10-20T14:00:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'Yes, it is available.',
sent_at: new Date('2023-10-20T14:05:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'Can I get a discount for a longer rental?',
sent_at: new Date('2023-10-21T09:00:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'We offer discounts for rentals over a week.',
sent_at: new Date('2023-10-21T09:10:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'How do I add extras to my booking?',
sent_at: new Date('2023-10-22T11:00:00Z'),
// type code here for "relation_one" field
},
];
const VehiclesData = [
{
name: 'Toyota Corolla',
description: 'Reliable sedan with great fuel efficiency.',
price_per_day: 30.5,
availability: 5,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
name: 'Hyundai Tucson',
description: 'Spacious SUV perfect for family trips.',
price_per_day: 45,
availability: 3,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
name: 'Ford Mustang',
description: 'Sporty coupe with powerful engine.',
price_per_day: 60,
availability: 2,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
name: 'Chevrolet Spark',
description: 'Compact car ideal for city driving.',
price_per_day: 25,
availability: 4,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
name: 'Nissan Altima',
description: 'Comfortable sedan with advanced features.',
price_per_day: 35,
availability: 6,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const OrganizationsData = [
{
name: 'City Car Rentals',
},
{
name: 'Urban Drive',
},
{
name: 'Quick Wheels',
},
{
name: 'Family Cars',
},
{
name: 'Eco Rides',
},
];
// Similar logic for "relation_many"
async function associateUserWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setOrganization) {
await User0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setOrganization) {
await User1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setOrganization) {
await User2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User3 = await Users.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (User3?.setOrganization) {
await User3.setOrganization(relatedOrganization3);
}
const relatedOrganization4 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User4 = await Users.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (User4?.setOrganization) {
await User4.setOrganization(relatedOrganization4);
}
}
async function associateBookingWithVehicle() {
const relatedVehicle0 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const Booking0 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Booking0?.setVehicle) {
await Booking0.setVehicle(relatedVehicle0);
}
const relatedVehicle1 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const Booking1 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Booking1?.setVehicle) {
await Booking1.setVehicle(relatedVehicle1);
}
const relatedVehicle2 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const Booking2 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Booking2?.setVehicle) {
await Booking2.setVehicle(relatedVehicle2);
}
const relatedVehicle3 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const Booking3 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Booking3?.setVehicle) {
await Booking3.setVehicle(relatedVehicle3);
}
const relatedVehicle4 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const Booking4 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Booking4?.setVehicle) {
await Booking4.setVehicle(relatedVehicle4);
}
}
async function associateBookingWithRenter() {
const relatedRenter0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Booking0 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Booking0?.setRenter) {
await Booking0.setRenter(relatedRenter0);
}
const relatedRenter1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Booking1 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Booking1?.setRenter) {
await Booking1.setRenter(relatedRenter1);
}
const relatedRenter2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Booking2 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Booking2?.setRenter) {
await Booking2.setRenter(relatedRenter2);
}
const relatedRenter3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Booking3 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Booking3?.setRenter) {
await Booking3.setRenter(relatedRenter3);
}
const relatedRenter4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Booking4 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Booking4?.setRenter) {
await Booking4.setRenter(relatedRenter4);
}
}
async function associateBookingWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Booking0 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Booking0?.setOrganization) {
await Booking0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Booking1 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Booking1?.setOrganization) {
await Booking1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Booking2 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Booking2?.setOrganization) {
await Booking2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Booking3 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Booking3?.setOrganization) {
await Booking3.setOrganization(relatedOrganization3);
}
const relatedOrganization4 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Booking4 = await Bookings.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Booking4?.setOrganization) {
await Booking4.setOrganization(relatedOrganization4);
}
}
// Similar logic for "relation_many"
async function associateExtraWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Extra0 = await Extras.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Extra0?.setOrganization) {
await Extra0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Extra1 = await Extras.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Extra1?.setOrganization) {
await Extra1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Extra2 = await Extras.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Extra2?.setOrganization) {
await Extra2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Extra3 = await Extras.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Extra3?.setOrganization) {
await Extra3.setOrganization(relatedOrganization3);
}
const relatedOrganization4 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Extra4 = await Extras.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Extra4?.setOrganization) {
await Extra4.setOrganization(relatedOrganization4);
}
}
async function associateMessageWithSender() {
const relatedSender0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message0 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Message0?.setSender) {
await Message0.setSender(relatedSender0);
}
const relatedSender1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message1 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Message1?.setSender) {
await Message1.setSender(relatedSender1);
}
const relatedSender2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message2 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Message2?.setSender) {
await Message2.setSender(relatedSender2);
}
const relatedSender3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message3 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Message3?.setSender) {
await Message3.setSender(relatedSender3);
}
const relatedSender4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message4 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Message4?.setSender) {
await Message4.setSender(relatedSender4);
}
}
async function associateMessageWithReceiver() {
const relatedReceiver0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message0 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Message0?.setReceiver) {
await Message0.setReceiver(relatedReceiver0);
}
const relatedReceiver1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message1 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Message1?.setReceiver) {
await Message1.setReceiver(relatedReceiver1);
}
const relatedReceiver2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message2 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Message2?.setReceiver) {
await Message2.setReceiver(relatedReceiver2);
}
const relatedReceiver3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message3 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Message3?.setReceiver) {
await Message3.setReceiver(relatedReceiver3);
}
const relatedReceiver4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message4 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Message4?.setReceiver) {
await Message4.setReceiver(relatedReceiver4);
}
}
async function associateMessageWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Message0 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Message0?.setOrganization) {
await Message0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Message1 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Message1?.setOrganization) {
await Message1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Message2 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Message2?.setOrganization) {
await Message2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Message3 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Message3?.setOrganization) {
await Message3.setOrganization(relatedOrganization3);
}
const relatedOrganization4 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Message4 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Message4?.setOrganization) {
await Message4.setOrganization(relatedOrganization4);
}
}
async function associateVehicleWithOwner() {
const relatedOwner0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Vehicle0 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Vehicle0?.setOwner) {
await Vehicle0.setOwner(relatedOwner0);
}
const relatedOwner1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Vehicle1 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Vehicle1?.setOwner) {
await Vehicle1.setOwner(relatedOwner1);
}
const relatedOwner2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Vehicle2 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Vehicle2?.setOwner) {
await Vehicle2.setOwner(relatedOwner2);
}
const relatedOwner3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Vehicle3 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Vehicle3?.setOwner) {
await Vehicle3.setOwner(relatedOwner3);
}
const relatedOwner4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Vehicle4 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Vehicle4?.setOwner) {
await Vehicle4.setOwner(relatedOwner4);
}
}
async function associateVehicleWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Vehicle0 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Vehicle0?.setOrganization) {
await Vehicle0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Vehicle1 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Vehicle1?.setOrganization) {
await Vehicle1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Vehicle2 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Vehicle2?.setOrganization) {
await Vehicle2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Vehicle3 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Vehicle3?.setOrganization) {
await Vehicle3.setOrganization(relatedOrganization3);
}
const relatedOrganization4 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Vehicle4 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Vehicle4?.setOrganization) {
await Vehicle4.setOrganization(relatedOrganization4);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Bookings.bulkCreate(BookingsData);
await Extras.bulkCreate(ExtrasData);
await Messages.bulkCreate(MessagesData);
await Vehicles.bulkCreate(VehiclesData);
await Organizations.bulkCreate(OrganizationsData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithOrganization(),
await associateBookingWithVehicle(),
await associateBookingWithRenter(),
await associateBookingWithOrganization(),
// Similar logic for "relation_many"
await associateExtraWithOrganization(),
await associateMessageWithSender(),
await associateMessageWithReceiver(),
await associateMessageWithOrganization(),
await associateVehicleWithOwner(),
await associateVehicleWithOrganization(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('bookings', null, {});
await queryInterface.bulkDelete('extras', null, {});
await queryInterface.bulkDelete('messages', null, {});
await queryInterface.bulkDelete('vehicles', null, {});
await queryInterface.bulkDelete('organizations', null, {});
},
};