retropilot-server/src/server/controllers/admin.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-01-12 08:02:30 -07:00
import authentication from './authentication';
2022-03-21 17:38:56 -06:00
import { Accounts } from '../../models';
async function isCurrentUserAdmin(hardFail, req) {
2022-01-07 18:35:55 -07:00
const account = await authentication.getAuthenticatedAccount(req);
2022-03-22 09:14:08 -06:00
if (!account) {
return { isAdmin: false, account };
}
2022-01-07 18:35:55 -07:00
if (account.admin !== 1) {
return { isAdmin: false, account };
}
2021-08-14 16:11:32 -06:00
2022-01-07 18:35:55 -07:00
return { isAdmin: true, account };
2021-08-14 16:11:32 -06:00
}
async function banAccount(ban, userId) {
2022-01-08 17:35:40 -07:00
if (!userId || !ban) {
return { success: false, status: 400, data: { bad_data: true } };
}
2022-01-07 18:35:55 -07:00
let cleanBan;
if (ban === 'true' || ban === 'false') {
cleanBan = ban === 'true';
2022-01-08 13:43:57 -07:00
} else {
2022-01-07 18:35:55 -07:00
return { success: false, status: 400, data: { bad_data: true } };
}
2022-03-21 17:38:56 -06:00
await Accounts.update(
2022-01-07 18:35:55 -07:00
{ banned: cleanBan ? 1 : 0 },
2022-01-08 13:43:57 -07:00
{ where: { id: userId } },
2022-01-07 18:35:55 -07:00
);
2022-03-21 17:38:56 -06:00
const verify = await Accounts.findOne({ where: { id: userId } });
2022-01-07 18:35:55 -07:00
if (verify.dataValues && verify.dataValues.banned === cleanBan ? 1 : 0) {
return { success: true, status: 200, data: { banned: ban } };
}
return { success: false, status: 500, data: { banned: false } };
2021-08-14 16:11:32 -06:00
}
2022-01-12 08:02:30 -07:00
export default {
2022-01-07 18:35:55 -07:00
banAccount,
2022-01-08 13:43:57 -07:00
isCurrentUserAdmin,
2022-01-07 18:35:55 -07:00
};