39504-vm/backend/src/db/models/stations.js
2026-04-06 15:16:18 +00:00

188 lines
2.4 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 stations = sequelize.define(
'stations',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
external_source: {
type: DataTypes.TEXT,
},
external_station_key: {
type: DataTypes.TEXT,
},
name: {
type: DataTypes.TEXT,
},
brand: {
type: DataTypes.TEXT,
},
address: {
type: DataTypes.TEXT,
},
suburb: {
type: DataTypes.TEXT,
},
state: {
type: DataTypes.TEXT,
},
postcode: {
type: DataTypes.TEXT,
},
latitude: {
type: DataTypes.DECIMAL,
},
longitude: {
type: DataTypes.DECIMAL,
},
is_active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
price_lock_region: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
stations.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.stations.hasMany(db.prices, {
as: 'prices_station',
foreignKey: {
name: 'stationId',
},
constraints: false,
});
db.stations.hasMany(db.price_submissions, {
as: 'price_submissions_station',
foreignKey: {
name: 'stationId',
},
constraints: false,
});
db.stations.hasMany(db.trip_recommendations, {
as: 'trip_recommendations_recommended_station',
foreignKey: {
name: 'recommended_stationId',
},
constraints: false,
});
//end loop
db.stations.belongsTo(db.users, {
as: 'createdBy',
});
db.stations.belongsTo(db.users, {
as: 'updatedBy',
});
};
return stations;
};