2026-01-08 14:18:21 +00:00

203 lines
2.7 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 items = sequelize.define(
'items',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
sku: {
type: DataTypes.TEXT,
},
serial_number: {
type: DataTypes.TEXT,
},
purchase_date: {
type: DataTypes.DATE,
},
warranty_expiry: {
type: DataTypes.DATE,
},
value: {
type: DataTypes.DECIMAL,
},
condition: {
type: DataTypes.ENUM,
values: [
"New",
"Good",
"Fair",
"Poor"
],
},
description: {
type: DataTypes.TEXT,
},
quantity: {
type: DataTypes.INTEGER,
},
barcode: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
items.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.items.belongsTo(db.categories, {
as: 'category',
foreignKey: {
name: 'categoryId',
},
constraints: false,
});
db.items.belongsTo(db.locations, {
as: 'location',
foreignKey: {
name: 'locationId',
},
constraints: false,
});
db.items.belongsTo(db.statuses, {
as: 'status',
foreignKey: {
name: 'statusId',
},
constraints: false,
});
db.items.belongsTo(db.users, {
as: 'owner',
foreignKey: {
name: 'ownerId',
},
constraints: false,
});
db.items.hasMany(db.file, {
as: 'photos',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.items.getTableName(),
belongsToColumn: 'photos',
},
});
db.items.hasMany(db.file, {
as: 'documents',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.items.getTableName(),
belongsToColumn: 'documents',
},
});
db.items.belongsTo(db.users, {
as: 'createdBy',
});
db.items.belongsTo(db.users, {
as: 'updatedBy',
});
};
return items;
};