40226-vm/backend/src/db/models/tournaments.js
2026-06-07 22:17:56 +00:00

173 lines
2.5 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 tournaments = sequelize.define(
'tournaments',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
code: {
type: DataTypes.TEXT,
},
host_country: {
type: DataTypes.TEXT,
},
starts_at: {
type: DataTypes.DATE,
},
ends_at: {
type: DataTypes.DATE,
},
group_stage_deadline_at: {
type: DataTypes.DATE,
},
knockout_stage_deadline_at: {
type: DataTypes.DATE,
},
is_published: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
tournaments.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.tournaments.hasMany(db.friend_groups, {
as: 'friend_groups_tournament',
foreignKey: {
name: 'tournamentId',
},
constraints: false,
});
db.tournaments.hasMany(db.groups, {
as: 'groups_tournament',
foreignKey: {
name: 'tournamentId',
},
constraints: false,
});
db.tournaments.hasMany(db.matches, {
as: 'matches_tournament',
foreignKey: {
name: 'tournamentId',
},
constraints: false,
});
db.tournaments.hasMany(db.knockout_slots, {
as: 'knockout_slots_tournament',
foreignKey: {
name: 'tournamentId',
},
constraints: false,
});
db.tournaments.hasMany(db.open_questions, {
as: 'open_questions_tournament',
foreignKey: {
name: 'tournamentId',
},
constraints: false,
});
//end loop
db.tournaments.belongsTo(db.users, {
as: 'createdBy',
});
db.tournaments.belongsTo(db.users, {
as: 'updatedBy',
});
};
return tournaments;
};