32571/backend/src/db/seeders/20231127130745-sample-data.js
2025-07-02 03:55:27 +00:00

503 lines
10 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Activities = db.activities;
const Batches = db.batches;
const Locations = db.locations;
const Products = db.products;
const Sales = db.sales;
const Suppliers = db.suppliers;
const ActivitiesData = [
{
description: 'Checked inventory levels',
activity_date: new Date('2023-10-01T09:00:00Z'),
// type code here for "relation_one" field
},
{
description: 'Processed a sale',
activity_date: new Date('2023-10-02T10:30:00Z'),
// type code here for "relation_one" field
},
{
description: 'Updated product prices',
activity_date: new Date('2023-10-03T11:45:00Z'),
// type code here for "relation_one" field
},
{
description: 'Generated sales report',
activity_date: new Date('2023-10-04T13:20:00Z'),
// type code here for "relation_one" field
},
];
const BatchesData = [
{
batch_number: 'B001',
manufacture_date: new Date('2023-01-15T00:00:00Z'),
expiry_date: new Date('2024-01-15T00:00:00Z'),
// type code here for "relation_one" field
},
{
batch_number: 'B002',
manufacture_date: new Date('2023-02-20T00:00:00Z'),
expiry_date: new Date('2024-02-20T00:00:00Z'),
// type code here for "relation_one" field
},
{
batch_number: 'B003',
manufacture_date: new Date('2023-03-25T00:00:00Z'),
expiry_date: new Date('2024-03-25T00:00:00Z'),
// type code here for "relation_one" field
},
{
batch_number: 'B004',
manufacture_date: new Date('2023-04-30T00:00:00Z'),
expiry_date: new Date('2024-04-30T00:00:00Z'),
// type code here for "relation_one" field
},
];
const LocationsData = [
{
name: 'Downtown Store',
address: '123 Main St, Cityville',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
name: 'Uptown Branch',
address: '456 Elm St, Cityville',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
name: 'Suburban Outlet',
address: '789 Oak St, Suburbia',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
name: 'Eastside Shop',
address: '101 Pine St, Eastville',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
];
const ProductsData = [
{
name: 'Wireless Mouse',
sku: 'WM123',
price: 29.99,
stock_quantity: 150,
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
name: 'Bluetooth Keyboard',
sku: 'BK456',
price: 49.99,
stock_quantity: 100,
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
name: 'USB-C Hub',
sku: 'UCH789',
price: 19.99,
stock_quantity: 200,
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
name: 'Laptop Stand',
sku: 'LS101',
price: 39.99,
stock_quantity: 75,
// type code here for "relation_one" field
// type code here for "relation_many" field
},
];
const SalesData = [
{
sale_date: new Date('2023-10-01T10:00:00Z'),
total_amount: 150,
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
sale_date: new Date('2023-10-02T11:30:00Z'),
total_amount: 200,
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
sale_date: new Date('2023-10-03T14:45:00Z'),
total_amount: 75,
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
sale_date: new Date('2023-10-04T16:20:00Z'),
total_amount: 300,
// type code here for "relation_one" field
// type code here for "relation_many" field
},
];
const SuppliersData = [
{
name: 'Tech Supplies Co.',
contact_info: 'contact@techsupplies.com',
},
{
name: 'Office Essentials Ltd.',
contact_info: 'info@officeessentials.com',
},
{
name: 'Gadget World',
contact_info: 'support@gadgetworld.com',
},
{
name: 'Peripheral Partners',
contact_info: 'sales@peripheralpartners.com',
},
];
// Similar logic for "relation_many"
async function associateActivityWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Activity0 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Activity0?.setUser) {
await Activity0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Activity1 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Activity1?.setUser) {
await Activity1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Activity2 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Activity2?.setUser) {
await Activity2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Activity3 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Activity3?.setUser) {
await Activity3.setUser(relatedUser3);
}
}
async function associateBatchWithProduct() {
const relatedProduct0 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Batch0 = await Batches.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Batch0?.setProduct) {
await Batch0.setProduct(relatedProduct0);
}
const relatedProduct1 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Batch1 = await Batches.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Batch1?.setProduct) {
await Batch1.setProduct(relatedProduct1);
}
const relatedProduct2 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Batch2 = await Batches.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Batch2?.setProduct) {
await Batch2.setProduct(relatedProduct2);
}
const relatedProduct3 = await Products.findOne({
offset: Math.floor(Math.random() * (await Products.count())),
});
const Batch3 = await Batches.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Batch3?.setProduct) {
await Batch3.setProduct(relatedProduct3);
}
}
// Similar logic for "relation_many"
// Similar logic for "relation_many"
async function associateProductWithSupplier() {
const relatedSupplier0 = await Suppliers.findOne({
offset: Math.floor(Math.random() * (await Suppliers.count())),
});
const Product0 = await Products.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Product0?.setSupplier) {
await Product0.setSupplier(relatedSupplier0);
}
const relatedSupplier1 = await Suppliers.findOne({
offset: Math.floor(Math.random() * (await Suppliers.count())),
});
const Product1 = await Products.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Product1?.setSupplier) {
await Product1.setSupplier(relatedSupplier1);
}
const relatedSupplier2 = await Suppliers.findOne({
offset: Math.floor(Math.random() * (await Suppliers.count())),
});
const Product2 = await Products.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Product2?.setSupplier) {
await Product2.setSupplier(relatedSupplier2);
}
const relatedSupplier3 = await Suppliers.findOne({
offset: Math.floor(Math.random() * (await Suppliers.count())),
});
const Product3 = await Products.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Product3?.setSupplier) {
await Product3.setSupplier(relatedSupplier3);
}
}
// Similar logic for "relation_many"
async function associateSaleWithCashier() {
const relatedCashier0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Sale0 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Sale0?.setCashier) {
await Sale0.setCashier(relatedCashier0);
}
const relatedCashier1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Sale1 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Sale1?.setCashier) {
await Sale1.setCashier(relatedCashier1);
}
const relatedCashier2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Sale2 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Sale2?.setCashier) {
await Sale2.setCashier(relatedCashier2);
}
const relatedCashier3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Sale3 = await Sales.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Sale3?.setCashier) {
await Sale3.setCashier(relatedCashier3);
}
}
// Similar logic for "relation_many"
module.exports = {
up: async (queryInterface, Sequelize) => {
await Activities.bulkCreate(ActivitiesData);
await Batches.bulkCreate(BatchesData);
await Locations.bulkCreate(LocationsData);
await Products.bulkCreate(ProductsData);
await Sales.bulkCreate(SalesData);
await Suppliers.bulkCreate(SuppliersData);
await Promise.all([
// Similar logic for "relation_many"
await associateActivityWithUser(),
await associateBatchWithProduct(),
// Similar logic for "relation_many"
// Similar logic for "relation_many"
await associateProductWithSupplier(),
// Similar logic for "relation_many"
await associateSaleWithCashier(),
// Similar logic for "relation_many"
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('activities', null, {});
await queryInterface.bulkDelete('batches', null, {});
await queryInterface.bulkDelete('locations', null, {});
await queryInterface.bulkDelete('products', null, {});
await queryInterface.bulkDelete('sales', null, {});
await queryInterface.bulkDelete('suppliers', null, {});
},
};