fix(api): apply feedback

This commit is contained in:
yassinedorbozgithub 2025-05-29 09:12:51 +01:00
parent 64a8729be3
commit 772236d220

View File

@ -30,13 +30,12 @@ const getAdminUser = async () => {
}; };
const migrateBlockOptionsContentLimit = async (services: MigrationServices) => { const migrateBlockOptionsContentLimit = async (services: MigrationServices) => {
const BlockModel = mongoose.model<Block>(Block.name, blockSchema); const BlockModel = mongoose.model<Block>(Block.name, blockSchema).collection;
const adminUser = await getAdminUser(); const adminUser = await getAdminUser();
if (!adminUser) { if (!adminUser) {
services.logger.warn('Unable to process block, no admin user found'); services.logger.warn('Unable to process block, no admin user found');
return;
} }
try { try {
@ -52,13 +51,16 @@ const migrateBlockOptionsContentLimit = async (services: MigrationServices) => {
); );
} catch (error) { } catch (error) {
services.logger.error(`Failed to update limit : ${error.message}`); services.logger.error(`Failed to update limit : ${error.message}`);
return false;
} }
return true;
}; };
const migrateBlockOptionsContentButtonsUrl = async ( const migrateBlockOptionsContentButtonsUrl = async (
services: MigrationServices, services: MigrationServices,
) => { ) => {
const BlockModel = mongoose.model<Block>(Block.name, blockSchema); const BlockModel = mongoose.model<Block>(Block.name, blockSchema).collection;
try { try {
await BlockModel.updateMany( await BlockModel.updateMany(
@ -76,15 +78,18 @@ const migrateBlockOptionsContentButtonsUrl = async (
); );
} catch (error) { } catch (error) {
services.logger.error(`Failed to update button url : ${error.message}`); services.logger.error(`Failed to update button url : ${error.message}`);
return false;
} }
return true;
}; };
const migrateBlockOptionsFallback = async (services: MigrationServices) => { const migrateBlockOptionsFallback = async (services: MigrationServices) => {
const BlockModel = mongoose.model<Block>(Block.name, blockSchema); const BlockModel = mongoose.model<Block>(Block.name, blockSchema).collection;
try { try {
await BlockModel.updateMany( await BlockModel.updateMany(
{ 'options.fallback.max_attempts': { $exists: true } }, { 'options.fallback.max_attempts': { $exists: true, type: 'string' } },
[ [
{ {
$set: { $set: {
@ -97,6 +102,7 @@ const migrateBlockOptionsFallback = async (services: MigrationServices) => {
); );
} catch (error) { } catch (error) {
services.logger.error(`Failed to update max_attempts : ${error.message}`); services.logger.error(`Failed to update max_attempts : ${error.message}`);
return false;
} }
try { try {
@ -112,15 +118,18 @@ const migrateBlockOptionsFallback = async (services: MigrationServices) => {
services.logger.error( services.logger.error(
`Failed to update max_attempts, active : ${error.message}`, `Failed to update max_attempts, active : ${error.message}`,
); );
return false;
} }
return true;
}; };
module.exports = { module.exports = {
async up(services: MigrationServices) { async up(services: MigrationServices) {
await migrateBlockOptionsContentLimit(services); const migration1 = await migrateBlockOptionsContentLimit(services);
await migrateBlockOptionsContentButtonsUrl(services); const migration2 = await migrateBlockOptionsContentButtonsUrl(services);
await migrateBlockOptionsFallback(services); const migration3 = await migrateBlockOptionsFallback(services);
return true;
return migration1 && migration2 && migration3;
}, },
async down(_services: MigrationServices) { async down(_services: MigrationServices) {
return true; return true;