fix: improve getGameTypes with DISTINCT query and null filtering
All checks were successful
Deploy to Production / deploy (push) Successful in 21s

This commit is contained in:
vuongps38770
2026-01-28 10:32:05 +07:00
parent c8af2e268d
commit 57c45d27a3

View File

@@ -1,6 +1,7 @@
const { Game } = require('../models'); const { Game } = require('../models');
const { cacheUtils } = require('../config/redis'); const { cacheUtils } = require('../config/redis');
const { sequelize } = require('../config/database'); const { sequelize } = require('../config/database');
const { Op, Sequelize } = require('sequelize');
/** /**
* Game Controller - Quản lý trò chơi giáo dục * Game Controller - Quản lý trò chơi giáo dục
@@ -410,11 +411,22 @@ class GameController {
}); });
} }
// Get distinct types // Get all games with only type field
const types = await Game.aggregate('type', 'DISTINCT', { plain: false }); const games = await Game.findAll({
attributes: [[Sequelize.fn('DISTINCT', Sequelize.col('type')), 'type']],
where: {
type: {
[Op.and]: [
{ [Op.ne]: null },
{ [Op.notIn]: ['', ' '] }
]
}
},
raw: true,
});
// Format response as array of objects // Get distinct types
const result = types.map(t => ({ type: t.type })); const result = games.filter(g => g.type);
await cacheUtils.set(cacheKey, result, 3600); await cacheUtils.set(cacheKey, result, 3600);