39856-vm/backend/src/db/models/achievements.js
2026-05-01 13:00:34 +00:00

176 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 achievements = sequelize.define(
'achievements',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
category: {
type: DataTypes.ENUM,
values: [
"comida",
"exploracion",
"social",
"eventos"
],
},
threshold_value: {
type: DataTypes.INTEGER,
},
threshold_metric: {
type: DataTypes.ENUM,
values: [
"chinchorros_visited",
"routes_completed",
"events_attended",
"favorites_added",
"reviews_written"
],
},
active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
achievements.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.achievements.hasMany(db.user_achievements, {
as: 'user_achievements_achievement',
foreignKey: {
name: 'achievementId',
},
constraints: false,
});
//end loop
db.achievements.hasMany(db.file, {
as: 'badge_images',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.achievements.getTableName(),
belongsToColumn: 'badge_images',
},
});
db.achievements.belongsTo(db.users, {
as: 'createdBy',
});
db.achievements.belongsTo(db.users, {
as: 'updatedBy',
});
};
return achievements;
};