mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -0,0 +1,51 @@
|
||||
/***********************************************************************************************************************************************************************
|
||||
* File Name: accountStatus.service.js
|
||||
* Type of Program: Service
|
||||
* Description: Shared active/ban-status check + auto-lift for expired temporary
|
||||
* bans. Used by every entry point that authenticates a user
|
||||
* (login, Google OAuth callback, refresh token, JWT middleware)
|
||||
* so the ban logic lives in exactly one place.
|
||||
* Author: Kenneth Obsequio (@lash0000)
|
||||
* Date Created: Jul. 4, 2026
|
||||
***********************************************************************************************************************************************************************
|
||||
* HOW TO USE:
|
||||
* const { checkAccountStatus } = require('../services/accountStatus.service');
|
||||
* const status = await checkAccountStatus(user);
|
||||
* if (!status.ok) { ... map status.code ('deactivated' | 'banned') to a response ... }
|
||||
***********************************************************************************************************************************************************************/
|
||||
const mdl_UserBans = require('../models/users/user_bans.mdl');
|
||||
|
||||
/**
|
||||
* Checks whether a user is allowed to authenticate right now.
|
||||
* Auto-lifts an expired temporary ban as a side effect.
|
||||
*
|
||||
* @param {import('../models/users/users.mdl')} user
|
||||
* @returns {Promise<{ok: true} | {ok: false, code: 'deactivated'|'banned', reason?: string|null, ban_type?: string|null, ban_expires_at?: Date|null}>}
|
||||
*/
|
||||
const checkAccountStatus = async (user) => {
|
||||
if (!user.is_active) return { ok: false, code: 'deactivated' };
|
||||
|
||||
if (user.is_banned) {
|
||||
const stillBanned = !user.ban_expires_at || new Date() < new Date(user.ban_expires_at);
|
||||
if (stillBanned) {
|
||||
const activeBan = await mdl_UserBans.findOne({
|
||||
where: { user_id: user.user_id, is_lifted: false },
|
||||
order: [['banned_at', 'DESC']],
|
||||
attributes: ['reason', 'ban_type', 'expires_at'],
|
||||
});
|
||||
return {
|
||||
ok: false,
|
||||
code: 'banned',
|
||||
reason: activeBan?.reason ?? null,
|
||||
ban_type: activeBan?.ban_type ?? null,
|
||||
ban_expires_at: activeBan?.expires_at ?? null,
|
||||
};
|
||||
}
|
||||
// Expired temporary ban — auto-lift
|
||||
await user.update({ is_banned: false, ban_expires_at: null });
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
};
|
||||
|
||||
module.exports = { checkAccountStatus };
|
||||
Reference in New Issue
Block a user