feat(falukant): implement backfill for falukant visual settings and add SQL scripts for default values
All checks were successful
Deploy to production / deploy (push) Successful in 2m6s
All checks were successful
Deploy to production / deploy (push) Successful in 2m6s
This commit is contained in:
94
backend/scripts/backfill-falukant-visual-settings.js
Normal file
94
backend/scripts/backfill-falukant-visual-settings.js
Normal file
@@ -0,0 +1,94 @@
|
||||
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, created_at, updated_at)
|
||||
SELECT
|
||||
u.id,
|
||||
:paramTypeId,
|
||||
:encryptedOptionId,
|
||||
NOW(),
|
||||
NOW()
|
||||
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();
|
||||
Reference in New Issue
Block a user