32315/backend/src/db/seeders/20231127130745-sample-data.js
2025-06-18 16:32:59 +00:00

378 lines
7.1 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Admins = db.admins;
const CustomerSupportReps = db.customer_support_reps;
const DeliveryStaff = db.delivery_staff;
const Inventory = db.inventory;
const OrderItems = db.order_items;
const Orders = db.orders;
const Products = db.products;
const AdminsData = [
{
username: 'admin1',
email: 'admin1@example.com',
password: 'adminpass1',
},
{
username: 'admin2',
email: 'admin2@example.com',
password: 'adminpass2',
},
{
username: 'admin3',
email: 'admin3@example.com',
password: 'adminpass3',
},
];
const CustomerSupportRepsData = [
{
name: 'Alice Johnson',
email: 'alice.johnson@example.com',
phone: '4455667788',
},
{
name: 'Bob Williams',
email: 'bob.williams@example.com',
phone: '5566778899',
},
{
name: 'Charlie Brown',
email: 'charlie.brown@example.com',
phone: '6677889900',
},
];
const DeliveryStaffData = [
{
name: 'Tom Hardy',
email: 'tom.hardy@example.com',
phone: '1234567890',
},
{
name: 'Emma Stone',
email: 'emma.stone@example.com',
phone: '0987654321',
},
{
name: 'Chris Evans',
email: 'chris.evans@example.com',
phone: '1122334455',
},
];
const InventoryData = [
{
// type code here for "relation_one" field
quantity: 150,
},
{
// type code here for "relation_one" field
quantity: 50,
},
{
// type code here for "relation_one" field
quantity: 200,
},
];
const OrderItemsData = [
{
// type code here for "relation_one" field
quantity: 3,
price: 11.97,
},
{
// type code here for "relation_one" field
quantity: 2,
price: 4.98,
},
{
// type code here for "relation_one" field
quantity: 1,
price: 12.99,
},
];
const OrdersData = [
{
// type code here for "relation_one" field
// type code here for "relation_many" field
order_date: new Date('2023-10-01T10:00:00Z'),
status: 'pending',
total_amount: 16.95,
},
{
// type code here for "relation_one" field
// type code here for "relation_many" field
order_date: new Date('2023-10-02T12:30:00Z'),
status: 'delivered',
total_amount: 19.44,
},
{
// type code here for "relation_one" field
// type code here for "relation_many" field
order_date: new Date('2023-10-03T15:45:00Z'),
status: 'shipped',
total_amount: 26.94,
},
];
const ProductsData = [
{
name: 'Organic Apples',
description: 'Fresh organic apples from local farms.',
price: 3.99,
stock: 150,
category: 'fish',
// type code here for "images" field
},
{
name: 'Wild Salmon',
description: 'Freshly caught wild salmon fillets.',
price: 12.99,
stock: 50,
category: 'vegetables',
// type code here for "images" field
},
{
name: 'Chicken Breast',
description: 'Free-range chicken breast, skinless and boneless.',
price: 5.49,
stock: 200,
category: 'vegetables',
// type code here for "images" field
},
];
// Similar logic for "relation_many"
async function associateInventoryWithProduct() {
const relatedProduct0 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Inventory0 = await Inventory.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Inventory0?.setProduct) {
await Inventory0.setProduct(relatedProduct0);
}
const relatedProduct1 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Inventory1 = await Inventory.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Inventory1?.setProduct) {
await Inventory1.setProduct(relatedProduct1);
}
const relatedProduct2 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Inventory2 = await Inventory.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Inventory2?.setProduct) {
await Inventory2.setProduct(relatedProduct2);
}
}
async function associateOrderItemWithProduct() {
const relatedProduct0 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const OrderItem0 = await OrderItems.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (OrderItem0?.setProduct) {
await OrderItem0.setProduct(relatedProduct0);
}
const relatedProduct1 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const OrderItem1 = await OrderItems.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (OrderItem1?.setProduct) {
await OrderItem1.setProduct(relatedProduct1);
}
const relatedProduct2 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const OrderItem2 = await OrderItems.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (OrderItem2?.setProduct) {
await OrderItem2.setProduct(relatedProduct2);
}
}
async function associateOrderWithShopper() {
const relatedShopper0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Order0 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Order0?.setShopper) {
await Order0.setShopper(relatedShopper0);
}
const relatedShopper1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Order1 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Order1?.setShopper) {
await Order1.setShopper(relatedShopper1);
}
const relatedShopper2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Order2 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Order2?.setShopper) {
await Order2.setShopper(relatedShopper2);
}
}
// Similar logic for "relation_many"
module.exports = {
up: async (queryInterface, Sequelize) => {
await Admins.bulkCreate(AdminsData);
await CustomerSupportReps.bulkCreate(CustomerSupportRepsData);
await DeliveryStaff.bulkCreate(DeliveryStaffData);
await Inventory.bulkCreate(InventoryData);
await OrderItems.bulkCreate(OrderItemsData);
await Orders.bulkCreate(OrdersData);
await Products.bulkCreate(ProductsData);
await Promise.all([
// Similar logic for "relation_many"
await associateInventoryWithProduct(),
await associateOrderItemWithProduct(),
await associateOrderWithShopper(),
// Similar logic for "relation_many"
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('admins', null, {});
await queryInterface.bulkDelete('customer_support_reps', null, {});
await queryInterface.bulkDelete('delivery_staff', null, {});
await queryInterface.bulkDelete('inventory', null, {});
await queryInterface.bulkDelete('order_items', null, {});
await queryInterface.bulkDelete('orders', null, {});
await queryInterface.bulkDelete('products', null, {});
},
};