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 { cacheUtils } = require('../config/redis');
const { sequelize } = require('../config/database');
const { Op, Sequelize } = require('sequelize');
/**
* Game Controller - Quản lý trò chơi giáo dục
@@ -410,11 +411,22 @@ class GameController {
});
}
// Get distinct types
const types = await Game.aggregate('type', 'DISTINCT', { plain: false });
// Get all games with only type field
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
const result = types.map(t => ({ type: t.type }));
// Get distinct types
const result = games.filter(g => g.type);
await cacheUtils.set(cacheKey, result, 3600);