32098/backend/src/db/models/tickets.js
2025-06-08 15:06:04 +00:00

82 lines
1.6 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 tickets = sequelize.define(
'tickets',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
ticket_number: {
type: DataTypes.TEXT,
},
issue_date: {
type: DataTypes.DATE,
},
expiry_date: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
tickets.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.tickets.hasMany(db.payments, {
as: 'payments_ticket',
foreignKey: {
name: 'ticketId',
},
constraints: false,
});
//end loop
db.tickets.belongsTo(db.users, {
as: 'user',
foreignKey: {
name: 'userId',
},
constraints: false,
});
db.tickets.belongsTo(db.parking_spaces, {
as: 'parking_space',
foreignKey: {
name: 'parking_spaceId',
},
constraints: false,
});
db.tickets.belongsTo(db.users, {
as: 'createdBy',
});
db.tickets.belongsTo(db.users, {
as: 'updatedBy',
});
};
return tickets;
};