Bugs in settings fixed, profile added

This commit is contained in:
Torsten Schulz
2024-09-21 00:25:42 +02:00
parent c5a72d57d8
commit e494fe41db
65 changed files with 3121 additions and 7478 deletions

View File

@@ -8,6 +8,8 @@ import UserParamValue from './type/user_param_value.js';
import InterestType from './type/interest.js';
import InterestTranslationType from './type/interest_translation.js';
import Interest from './community/interest.js';
import UserParamVisibilityType from './type/user_param_visibility.js';
import UserParamVisibility from './community/user_param_visibility.js';
export default function setupAssociations() {
SettingsType.hasMany(UserParamType, { foreignKey: 'settingsId', as: 'user_param_types' });
@@ -16,11 +18,12 @@ export default function setupAssociations() {
UserParamType.hasMany(UserParam, { foreignKey: 'paramTypeId', as: 'user_params' });
UserParam.belongsTo(UserParamType, { foreignKey: 'paramTypeId', as: 'paramType' });
User.hasMany(UserParam, { foreignKey: 'userId', as: 'user_params' });
UserParam.belongsTo(User, { foreignKey: 'userId', as: 'user' });
UserRight.belongsTo(User, { foreignKey: 'userId' });
UserRight.belongsTo(User, { foreignKey: 'userId', as: 'user_with_rights' });
UserRight.belongsTo(UserRightType, { foreignKey: 'rightTypeId', as: 'rightType' });
UserRightType.hasMany(UserRight, { foreignKey: 'rightTypeId', as: 'rightType' });
UserRightType.hasMany(UserRight, { foreignKey: 'rightTypeId', as: 'user_rights' });
UserParamType.hasMany(UserParamValue, { foreignKey: 'userParamTypeId', as: 'user_param_values' });
UserParamValue.belongsTo(UserParamType, { foreignKey: 'userParamTypeId', as: 'user_param_type' });
@@ -28,15 +31,16 @@ export default function setupAssociations() {
InterestType.hasMany(InterestTranslationType, { foreignKey: 'interestsId', as: 'interest_translations' });
InterestTranslationType.belongsTo(InterestType, { foreignKey: 'interestsId', as: 'interest_translations' });
InterestType.hasMany(Interest, { foreignKey: 'userinterestId', as: 'user_interest_type'} );
User.hasMany(Interest, { foreignKey: 'userId', as: 'user_interest' });
Interest.belongsTo(InterestType, { foreignKey: 'userinterestId', as: 'user_interest_type' });
Interest.belongsTo(User, { foreignKey: 'userId', as: 'user_interest' });
InterestType.hasMany(Interest, { foreignKey: 'userinterestId', as: 'user_interest_type' });
User.hasMany(Interest, { foreignKey: 'userId', as: 'user_interests' });
Interest.belongsTo(InterestType, { foreignKey: 'userinterestId', as: 'interest_type' });
Interest.belongsTo(User, { foreignKey: 'userId', as: 'interest_owner' });
InterestTranslationType.belongsTo(UserParamValue, {
foreignKey: 'language',
targetKey: 'id',
as: 'user_param_value'
});
InterestTranslationType.belongsTo(UserParamValue, { foreignKey: 'language', targetKey: 'id', as: 'user_param_value' });
UserParam.hasMany(UserParamVisibility, { foreignKey: 'param_id', as: 'param_visibilities' });
UserParamVisibility.belongsTo(UserParam, { foreignKey: 'param_id', as: 'param' });
UserParamVisibility.belongsTo(UserParamVisibilityType, { foreignKey: 'visibility', as: 'visibility_type' });
UserParamVisibilityType.hasMany(UserParamVisibility, { foreignKey: 'visibility', as: 'user_param_visibilities' });
}

View File

@@ -5,7 +5,7 @@ import crypto from 'crypto';
const User = sequelize.define('user', {
email: {
type: DataTypes.BLOB, // Verwende BLOB, um die E-Mail als bytea zu speichern
type: DataTypes.BLOB,
allowNull: false,
unique: true,
set(value) {

View File

@@ -61,22 +61,4 @@ const UserParam = sequelize.define('user_param', {
]
});
UserParam.upsertParam = async function (userId, paramTypeId, value) {
try {
const val = value !== null && value !== undefined ? value.toString() : '';
const [userParam, created] = await UserParam.findOrCreate({
where: { userId, paramTypeId },
defaults: { value: val }
});
if (!created) {
userParam.value = value !== null && value !== undefined ? value.toString() : '';
await userParam.save();
}
} catch (error) {
console.error('Error in upsertParam:', error);
throw error;
}
};
export default UserParam;

View File

@@ -0,0 +1,26 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const UserParamVisibility = sequelize.define('user_param_visibility', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
param_id: {
type: DataTypes.INTEGER,
allowNull: false
},
visibility: {
type: DataTypes.INTEGER,
allowNull: false
}
}, {
tableName: 'user_param_visibility',
timestamps: false,
underscored: true,
schema: 'community'
});
export default UserParamVisibility;

View File

@@ -10,6 +10,8 @@ import InterestType from './type/interest.js';
import InterestTranslationType from './type/interest_translation.js';
import Interest from './community/interest.js';
import ContactMessage from './service/contactmessage.js';
import UserParamVisibilityType from './type/user_param_visibility.js';
import UserParamVisibility from './community/user_param_visibility.js';
const models = {
SettingsType,
@@ -24,6 +26,8 @@ const models = {
InterestTranslationType,
Interest,
ContactMessage,
UserParamVisibilityType,
UserParamVisibility,
};
export default models;

View File

@@ -59,6 +59,32 @@ const ContactMessage = sequelize.define('contact_message', {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
// Neue Felder für die Antwort
answer: {
type: DataTypes.TEXT,
allowNull: true,
set(value) {
if (value) {
const encryptedValue = encrypt(value);
this.setDataValue('answer', encryptedValue.toString('hex'));
}
},
get() {
const value = this.getDataValue('answer');
if (value) {
return decrypt(Buffer.from(value, 'hex'));
}
}
},
answeredAt: {
type: DataTypes.DATE,
allowNull: true
},
isAnswered: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, {
tableName: 'contact_message',
@@ -68,4 +94,3 @@ const ContactMessage = sequelize.define('contact_message', {
});
export default ContactMessage;

47
backend/models/trigger.js Normal file
View File

@@ -0,0 +1,47 @@
import { sequelize } from '../utils/sequelize.js';
export async function createTriggers() {
const createTriggerFunction = `
CREATE OR REPLACE FUNCTION create_user_param_visibility_trigger()
RETURNS TRIGGER AS $$
BEGIN
-- Check if UserParamVisibility already exists for this UserParam
IF NOT EXISTS (
SELECT 1 FROM community.user_param_visibility
WHERE param_id = NEW.id
) THEN
-- Insert the default visibility (Invisible)
INSERT INTO community.user_param_visibility (param_id, visibility)
VALUES (NEW.id, (
SELECT id FROM type.user_param_visibility WHERE description = 'Invisible'
));
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
`;
const createInsertTrigger = `
CREATE TRIGGER trigger_create_user_param_visibility
AFTER INSERT ON community.user_param
FOR EACH ROW
EXECUTE FUNCTION create_user_param_visibility_trigger();
`;
const createUpdateTrigger = `
CREATE TRIGGER trigger_update_user_param_visibility
AFTER UPDATE ON community.user_param
FOR EACH ROW
EXECUTE FUNCTION create_user_param_visibility_trigger();
`;
try {
await sequelize.query(createTriggerFunction);
await sequelize.query(createInsertTrigger);
await sequelize.query(createUpdateTrigger);
console.log('Triggers created successfully');
} catch (error) {
console.error('Error creating triggers:', error);
}
}

View File

@@ -26,6 +26,15 @@ const UserParamType = sequelize.define('user_param_type', {
model: 'settings',
key: 'id'
}
},
orderId: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
},
unit: {
type: DataTypes.STRING,
allowNull: true
}
}, {
tableName: 'user_param',

View File

@@ -10,6 +10,11 @@ const UserParamValue = sequelize.define('user_param_value', {
value: {
type: DataTypes.STRING,
allowNull: false
},
orderId: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
}
},
{

View File

@@ -0,0 +1,22 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const UserParamVisibilityType = sequelize.define('user_param_visibility_type', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
description: {
type: DataTypes.STRING,
allowNull: false
}
}, {
tableName: 'user_param_visibility_type',
timestamps: false,
underscored: true,
schema: 'type'
});
export default UserParamVisibilityType;