fix: improve getGameTypes with DISTINCT query and null filtering
All checks were successful
Deploy to Production / deploy (push) Successful in 21s
All checks were successful
Deploy to Production / deploy (push) Successful in 21s
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user