38966-vm/backend/src/db/models/restaurants.js
2026-03-04 11:17:20 +00:00

315 lines
4.8 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 restaurants = sequelize.define(
'restaurants',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
brand_name: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
phone: {
type: DataTypes.TEXT,
},
email: {
type: DataTypes.TEXT,
},
website_url: {
type: DataTypes.TEXT,
},
address_line1: {
type: DataTypes.TEXT,
},
postal_code: {
type: DataTypes.TEXT,
},
city: {
type: DataTypes.TEXT,
},
country: {
type: DataTypes.TEXT,
},
latitude: {
type: DataTypes.DECIMAL,
},
longitude: {
type: DataTypes.DECIMAL,
},
instagram_url: {
type: DataTypes.TEXT,
},
facebook_url: {
type: DataTypes.TEXT,
},
is_published: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
restaurants.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.restaurants.hasMany(db.opening_hours, {
as: 'opening_hours_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.menu_categories, {
as: 'menu_categories_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.menu_items, {
as: 'menu_items_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.menu_tags, {
as: 'menu_tags_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.modifier_groups, {
as: 'modifier_groups_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.discounts, {
as: 'discounts_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.orders, {
as: 'orders_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.reservations, {
as: 'reservations_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.tables, {
as: 'tables_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.reviews, {
as: 'reviews_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.pages, {
as: 'pages_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.contact_messages, {
as: 'contact_messages_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.delivery_zones, {
as: 'delivery_zones_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
//end loop
db.restaurants.hasMany(db.file, {
as: 'logo_images',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.restaurants.getTableName(),
belongsToColumn: 'logo_images',
},
});
db.restaurants.hasMany(db.file, {
as: 'cover_images',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.restaurants.getTableName(),
belongsToColumn: 'cover_images',
},
});
db.restaurants.belongsTo(db.users, {
as: 'createdBy',
});
db.restaurants.belongsTo(db.users, {
as: 'updatedBy',
});
};
return restaurants;
};