From 57c45d27a3f31f0d5c65792a1545419b6455674b Mon Sep 17 00:00:00 2001 From: vuongps38770 <166083538+vuongps38770@users.noreply.github.com> Date: Wed, 28 Jan 2026 10:32:05 +0700 Subject: [PATCH] fix: improve getGameTypes with DISTINCT query and null filtering --- controllers/gameController.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/controllers/gameController.js b/controllers/gameController.js index e261cd7..cca3bef 100644 --- a/controllers/gameController.js +++ b/controllers/gameController.js @@ -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);