93 lines
2.4 KiB
JavaScript
93 lines
2.4 KiB
JavaScript
import { sequelize } from '../utils/sequelize.js';
|
|
import { encrypt } from '../utils/encryption.js';
|
|
import UserParamType from '../models/type/user_param.js';
|
|
import UserParamValue from '../models/type/user_param_value.js';
|
|
|
|
const DEFAULT_SETTINGS = [
|
|
{ description: 'falukantPortraitStyle', optionValue: 'classic' },
|
|
{ description: 'falukantFigureMode', optionValue: 'both' },
|
|
];
|
|
|
|
async function resolveParamDefinition(description, optionValue) {
|
|
const paramType = await UserParamType.findOne({
|
|
where: { description },
|
|
raw: true,
|
|
});
|
|
|
|
if (!paramType) {
|
|
throw new Error(
|
|
`Parametertyp '${description}' fehlt. Zuerst backend/sql/add_falukant_visual_settings.sql ausfuehren.`
|
|
);
|
|
}
|
|
|
|
const option = await UserParamValue.findOne({
|
|
where: {
|
|
userParamTypeId: paramType.id,
|
|
value: optionValue,
|
|
},
|
|
raw: true,
|
|
});
|
|
|
|
if (!option) {
|
|
throw new Error(
|
|
`Option '${optionValue}' fuer '${description}' fehlt. Zuerst backend/sql/add_falukant_visual_settings.sql ausfuehren.`
|
|
);
|
|
}
|
|
|
|
return {
|
|
paramTypeId: paramType.id,
|
|
encryptedOptionId: encrypt(String(option.id)),
|
|
};
|
|
}
|
|
|
|
async function insertMissingDefaults({ description, optionValue }) {
|
|
const { paramTypeId, encryptedOptionId } = await resolveParamDefinition(description, optionValue);
|
|
|
|
const [rows] = await sequelize.query(
|
|
`
|
|
INSERT INTO community.user_param (user_id, param_type_id, value)
|
|
SELECT
|
|
u.id,
|
|
:paramTypeId,
|
|
:encryptedOptionId
|
|
FROM community."user" u
|
|
LEFT JOIN community.user_param existing
|
|
ON existing.user_id = u.id
|
|
AND existing.param_type_id = :paramTypeId
|
|
WHERE existing.id IS NULL
|
|
RETURNING user_id
|
|
`,
|
|
{
|
|
replacements: {
|
|
paramTypeId,
|
|
encryptedOptionId,
|
|
},
|
|
}
|
|
);
|
|
|
|
console.log(
|
|
`✅ ${description}: ${rows.length} fehlende Default-Eintraege mit '${optionValue}' angelegt`
|
|
);
|
|
}
|
|
|
|
async function main() {
|
|
try {
|
|
await sequelize.authenticate();
|
|
console.log('🔌 Datenbankverbindung hergestellt');
|
|
|
|
for (const setting of DEFAULT_SETTINGS) {
|
|
await insertMissingDefaults(setting);
|
|
}
|
|
|
|
console.log('🏁 Falukant-Visual-Settings-Defaults abgeschlossen');
|
|
await sequelize.close();
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error('❌ Backfill fehlgeschlagen:', error);
|
|
await sequelize.close();
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|