2026-01-14 09:31:29 +00:00

236 lines
2.7 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const orders = sequelize.define(
'orders',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
order_number: {
type: DataTypes.TEXT,
},
order_date: {
type: DataTypes.DATE,
},
delivery_date: {
type: DataTypes.DATE,
},
status: {
type: DataTypes.ENUM,
values: [
"Draft",
"Pending",
"Processing",
"Shipped",
"Delivered",
"Cancelled",
"Returned",
"RefundPending",
"Refunded"
],
},
subtotal: {
type: DataTypes.DECIMAL,
},
tax_amount: {
type: DataTypes.DECIMAL,
},
shipping_fee: {
type: DataTypes.DECIMAL,
},
total_amount: {
type: DataTypes.DECIMAL,
},
billing_address: {
type: DataTypes.TEXT,
},
shipping_address: {
type: DataTypes.TEXT,
},
payment_method: {
type: DataTypes.ENUM,
values: [
"Card",
"PayPal",
"BankTransfer",
"Cash",
"GiftCard"
],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
orders.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.orders.hasMany(db.order_items, {
as: 'order_items_order',
foreignKey: {
name: 'orderId',
},
constraints: false,
});
db.orders.hasMany(db.payments, {
as: 'payments_order',
foreignKey: {
name: 'orderId',
},
constraints: false,
});
//end loop
db.orders.belongsTo(db.customers, {
as: 'customer',
foreignKey: {
name: 'customerId',
},
constraints: false,
});
db.orders.belongsTo(db.users, {
as: 'handled_by',
foreignKey: {
name: 'handled_byId',
},
constraints: false,
});
db.orders.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.orders.belongsTo(db.users, {
as: 'createdBy',
});
db.orders.belongsTo(db.users, {
as: 'updatedBy',
});
};
return orders;
};