33225/backend/src/db/seeders/20231127130745-sample-data.js
2025-08-05 01:31:16 +00:00

791 lines
16 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Categories = db.categories;
const Customers = db.customers;
const Invoices = db.invoices;
const Orders = db.orders;
const Products = db.products;
const Purchases = db.purchases;
const Services = db.services;
const Suppliers = db.suppliers;
const Units = db.units;
const Chat = db.chat;
const CategoriesData = [
{
name: 'Batteries',
},
{
name: 'Screen Protectors',
},
{
name: 'Chargers',
},
{
name: 'Cooling Pads',
},
];
const CustomersData = [
{
name: 'Alice Johnson',
contact: '1234567890',
address: '123 Main St, Anytown',
},
{
name: 'Bob Smith',
contact: '0987654321',
address: '456 Elm St, Othertown',
},
{
name: 'Charlie Brown',
contact: '1122334455',
address: '789 Oak St, Sometown',
},
{
name: 'Diana Prince',
contact: '2233445566',
address: '321 Pine St, Yourtown',
},
];
const InvoicesData = [
{
// type code here for "relation_one" field
invoice_date: new Date('2023-10-01T10:30:00Z'),
amount_due: 100,
status: 'Paid',
},
{
// type code here for "relation_one" field
invoice_date: new Date('2023-10-02T12:00:00Z'),
amount_due: 150,
status: 'Paid',
},
{
// type code here for "relation_one" field
invoice_date: new Date('2023-10-03T14:30:00Z'),
amount_due: 200,
status: 'Paid',
},
{
// type code here for "relation_one" field
invoice_date: new Date('2023-10-04T09:30:00Z'),
amount_due: 250,
status: 'Unpaid',
},
];
const OrdersData = [
{
// type code here for "relation_one" field
order_date: new Date('2023-10-01T10:00:00Z'),
total_amount: 100,
status: 'Cancelled',
},
{
// type code here for "relation_one" field
order_date: new Date('2023-10-02T11:30:00Z'),
total_amount: 150,
status: 'Completed',
},
{
// type code here for "relation_one" field
order_date: new Date('2023-10-03T14:00:00Z'),
total_amount: 200,
status: 'Completed',
},
{
// type code here for "relation_one" field
order_date: new Date('2023-10-04T09:00:00Z'),
total_amount: 250,
status: 'Cancelled',
},
];
const ProductsData = [
{
name: 'iPhone Battery',
description: 'High capacity replacement battery for iPhone.',
purchase_price: 20.5,
sale_price: 35,
stock: 50,
// type code here for "relation_one" field
// type code here for "relation_one" field
sku: 'IPB001',
sn_supported: true,
},
{
name: 'Samsung Screen Protector',
description: 'Tempered glass screen protector for Samsung Galaxy.',
purchase_price: 5,
sale_price: 10,
stock: 100,
// type code here for "relation_one" field
// type code here for "relation_one" field
sku: 'SSP002',
sn_supported: true,
},
{
name: 'USB-C Charger',
description: 'Fast charging USB-C charger for multiple devices.',
purchase_price: 10,
sale_price: 20,
stock: 75,
// type code here for "relation_one" field
// type code here for "relation_one" field
sku: 'UCC003',
sn_supported: false,
},
{
name: 'Laptop Cooling Pad',
description: 'Ergonomic cooling pad for laptops.',
purchase_price: 15,
sale_price: 25,
stock: 30,
// type code here for "relation_one" field
// type code here for "relation_one" field
sku: 'LCP004',
sn_supported: true,
},
];
const PurchasesData = [
{
// type code here for "relation_one" field
purchase_date: new Date('2023-09-01T10:00:00Z'),
total_amount: 500,
},
{
// type code here for "relation_one" field
purchase_date: new Date('2023-09-02T11:30:00Z'),
total_amount: 750,
},
{
// type code here for "relation_one" field
purchase_date: new Date('2023-09-03T14:00:00Z'),
total_amount: 1000,
},
{
// type code here for "relation_one" field
purchase_date: new Date('2023-09-04T09:00:00Z'),
total_amount: 1250,
},
];
const ServicesData = [
{
// type code here for "relation_one" field
device_type: 'Smartphone',
brand: 'Apple',
model: 'iPhone 12',
serial_number: 'SN123456',
issue_description: 'Battery not charging',
status: 'InProgress',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
device_type: 'Tablet',
brand: 'Samsung',
model: 'Galaxy Tab S7',
serial_number: 'SN234567',
issue_description: 'Screen cracked',
status: 'Received',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
device_type: 'Laptop',
brand: 'Dell',
model: 'XPS 13',
serial_number: 'SN345678',
issue_description: 'Overheating issue',
status: 'Received',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
device_type: 'Smartwatch',
brand: 'Fitbit',
model: 'Versa 3',
serial_number: 'SN456789',
issue_description: 'Syncing problems',
status: 'Completed',
// type code here for "relation_one" field
},
];
const SuppliersData = [
{
name: 'Tech Supplies Co.',
contact: '9876543210',
address: '101 Tech Park, Silicon Valley',
},
{
name: 'Gadget World',
contact: '8765432109',
address: '202 Innovation Dr, Tech City',
},
{
name: 'Parts Hub',
contact: '7654321098',
address: '303 Component Ave, Gear Town',
},
{
name: 'Device Depot',
contact: '6543210987',
address: '404 Gadget St, Widget City',
},
];
const UnitsData = [
{
name: 'Piece',
},
{
name: 'Set',
},
{
name: 'Box',
},
{
name: 'Pack',
},
];
const ChatData = [{}, {}, {}, {}];
// Similar logic for "relation_many"
async function associateInvoiceWithOrder() {
const relatedOrder0 = await Orders.findOne({
offset: Math.floor(Math.random() * (await Orders.count())),
});
const Invoice0 = await Invoices.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Invoice0?.setOrder) {
await Invoice0.setOrder(relatedOrder0);
}
const relatedOrder1 = await Orders.findOne({
offset: Math.floor(Math.random() * (await Orders.count())),
});
const Invoice1 = await Invoices.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Invoice1?.setOrder) {
await Invoice1.setOrder(relatedOrder1);
}
const relatedOrder2 = await Orders.findOne({
offset: Math.floor(Math.random() * (await Orders.count())),
});
const Invoice2 = await Invoices.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Invoice2?.setOrder) {
await Invoice2.setOrder(relatedOrder2);
}
const relatedOrder3 = await Orders.findOne({
offset: Math.floor(Math.random() * (await Orders.count())),
});
const Invoice3 = await Invoices.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Invoice3?.setOrder) {
await Invoice3.setOrder(relatedOrder3);
}
}
async function associateOrderWithCustomer() {
const relatedCustomer0 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Order0 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Order0?.setCustomer) {
await Order0.setCustomer(relatedCustomer0);
}
const relatedCustomer1 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Order1 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Order1?.setCustomer) {
await Order1.setCustomer(relatedCustomer1);
}
const relatedCustomer2 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Order2 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Order2?.setCustomer) {
await Order2.setCustomer(relatedCustomer2);
}
const relatedCustomer3 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Order3 = await Orders.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Order3?.setCustomer) {
await Order3.setCustomer(relatedCustomer3);
}
}
async function associateProductWithCategory() {
const relatedCategory0 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const Product0 = await Products.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Product0?.setCategory) {
await Product0.setCategory(relatedCategory0);
}
const relatedCategory1 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const Product1 = await Products.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Product1?.setCategory) {
await Product1.setCategory(relatedCategory1);
}
const relatedCategory2 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const Product2 = await Products.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Product2?.setCategory) {
await Product2.setCategory(relatedCategory2);
}
const relatedCategory3 = await Categories.findOne({
offset: Math.floor(Math.random() * (await Categories.count())),
});
const Product3 = await Products.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Product3?.setCategory) {
await Product3.setCategory(relatedCategory3);
}
}
async function associateProductWithUnit() {
const relatedUnit0 = await Units.findOne({
offset: Math.floor(Math.random() * (await Units.count())),
});
const Product0 = await Products.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Product0?.setUnit) {
await Product0.setUnit(relatedUnit0);
}
const relatedUnit1 = await Units.findOne({
offset: Math.floor(Math.random() * (await Units.count())),
});
const Product1 = await Products.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Product1?.setUnit) {
await Product1.setUnit(relatedUnit1);
}
const relatedUnit2 = await Units.findOne({
offset: Math.floor(Math.random() * (await Units.count())),
});
const Product2 = await Products.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Product2?.setUnit) {
await Product2.setUnit(relatedUnit2);
}
const relatedUnit3 = await Units.findOne({
offset: Math.floor(Math.random() * (await Units.count())),
});
const Product3 = await Products.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Product3?.setUnit) {
await Product3.setUnit(relatedUnit3);
}
}
async function associatePurchaseWithSupplier() {
const relatedSupplier0 = await Suppliers.findOne({
offset: Math.floor(Math.random() * (await Suppliers.count())),
});
const Purchase0 = await Purchases.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Purchase0?.setSupplier) {
await Purchase0.setSupplier(relatedSupplier0);
}
const relatedSupplier1 = await Suppliers.findOne({
offset: Math.floor(Math.random() * (await Suppliers.count())),
});
const Purchase1 = await Purchases.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Purchase1?.setSupplier) {
await Purchase1.setSupplier(relatedSupplier1);
}
const relatedSupplier2 = await Suppliers.findOne({
offset: Math.floor(Math.random() * (await Suppliers.count())),
});
const Purchase2 = await Purchases.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Purchase2?.setSupplier) {
await Purchase2.setSupplier(relatedSupplier2);
}
const relatedSupplier3 = await Suppliers.findOne({
offset: Math.floor(Math.random() * (await Suppliers.count())),
});
const Purchase3 = await Purchases.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Purchase3?.setSupplier) {
await Purchase3.setSupplier(relatedSupplier3);
}
}
async function associateServiceWithCustomer() {
const relatedCustomer0 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Service0 = await Services.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Service0?.setCustomer) {
await Service0.setCustomer(relatedCustomer0);
}
const relatedCustomer1 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Service1 = await Services.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Service1?.setCustomer) {
await Service1.setCustomer(relatedCustomer1);
}
const relatedCustomer2 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Service2 = await Services.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Service2?.setCustomer) {
await Service2.setCustomer(relatedCustomer2);
}
const relatedCustomer3 = await Customers.findOne({
offset: Math.floor(Math.random() * (await Customers.count())),
});
const Service3 = await Services.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Service3?.setCustomer) {
await Service3.setCustomer(relatedCustomer3);
}
}
async function associateServiceWithTechnician() {
const relatedTechnician0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Service0 = await Services.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Service0?.setTechnician) {
await Service0.setTechnician(relatedTechnician0);
}
const relatedTechnician1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Service1 = await Services.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Service1?.setTechnician) {
await Service1.setTechnician(relatedTechnician1);
}
const relatedTechnician2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Service2 = await Services.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Service2?.setTechnician) {
await Service2.setTechnician(relatedTechnician2);
}
const relatedTechnician3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Service3 = await Services.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Service3?.setTechnician) {
await Service3.setTechnician(relatedTechnician3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Categories.bulkCreate(CategoriesData);
await Customers.bulkCreate(CustomersData);
await Invoices.bulkCreate(InvoicesData);
await Orders.bulkCreate(OrdersData);
await Products.bulkCreate(ProductsData);
await Purchases.bulkCreate(PurchasesData);
await Services.bulkCreate(ServicesData);
await Suppliers.bulkCreate(SuppliersData);
await Units.bulkCreate(UnitsData);
await Chat.bulkCreate(ChatData);
await Promise.all([
// Similar logic for "relation_many"
await associateInvoiceWithOrder(),
await associateOrderWithCustomer(),
await associateProductWithCategory(),
await associateProductWithUnit(),
await associatePurchaseWithSupplier(),
await associateServiceWithCustomer(),
await associateServiceWithTechnician(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('categories', null, {});
await queryInterface.bulkDelete('customers', null, {});
await queryInterface.bulkDelete('invoices', null, {});
await queryInterface.bulkDelete('orders', null, {});
await queryInterface.bulkDelete('products', null, {});
await queryInterface.bulkDelete('purchases', null, {});
await queryInterface.bulkDelete('services', null, {});
await queryInterface.bulkDelete('suppliers', null, {});
await queryInterface.bulkDelete('units', null, {});
await queryInterface.bulkDelete('chat', null, {});
},
};