31742/backend/src/db/models/products.js
2025-05-23 18:14:58 +00:00

110 lines
2.1 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 products = sequelize.define(
'products',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
serial_number: {
type: DataTypes.INTEGER,
},
hardware_version: {
type: DataTypes.TEXT,
},
firmware: {
type: DataTypes.TEXT,
},
bootloader: {
type: DataTypes.TEXT,
},
production_date: {
type: DataTypes.DATE,
},
parameter_file: {
type: DataTypes.TEXT,
},
date_of_issue: {
type: DataTypes.DATE,
},
customer: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
products.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.products.hasMany(db.calibrations, {
as: 'calibrations_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.logs, {
as: 'logs_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
//end loop
db.products.belongsTo(db.product_types, {
as: 'product_type',
foreignKey: {
name: 'product_typeId',
},
constraints: false,
});
db.products.belongsTo(db.users, {
as: 'tested_by',
foreignKey: {
name: 'tested_byId',
},
constraints: false,
});
db.products.belongsTo(db.users, {
as: 'createdBy',
});
db.products.belongsTo(db.users, {
as: 'updatedBy',
});
};
return products;
};