228 lines
5.2 KiB
JavaScript
228 lines
5.2 KiB
JavaScript
const { addMinutes } = require("date-fns");
|
|
const { v4: uuid } = require("uuid");
|
|
|
|
const { ROLES } = require("../consts");
|
|
const utils = require("../utils");
|
|
const redis = require("../redis");
|
|
const knex = require("../knex");
|
|
const env = require("../env");
|
|
|
|
async function find(match) {
|
|
if ((match.email || match.apikey) && env.REDIS_ENABLED) {
|
|
const key = redis.key.user(match.email || match.apikey);
|
|
const cachedUser = await redis.client.get(key);
|
|
if (cachedUser) return JSON.parse(cachedUser);
|
|
}
|
|
|
|
const query = knex("users");
|
|
Object.entries(match).forEach(([key, value]) => {
|
|
query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
|
|
});
|
|
|
|
const user = await query.first();
|
|
|
|
if (user && env.REDIS_ENABLED) {
|
|
const emailKey = redis.key.user(user.email);
|
|
redis.client.set(emailKey, JSON.stringify(user), "EX", 60 * 15);
|
|
|
|
if (user.apikey) {
|
|
const apikeyKey = redis.key.user(user.apikey);
|
|
redis.client.set(apikeyKey, JSON.stringify(user), "EX", 60 * 15);
|
|
}
|
|
}
|
|
|
|
return user;
|
|
}
|
|
|
|
async function add(params, user) {
|
|
const data = {
|
|
email: params.email,
|
|
password: params.password,
|
|
verification_token: uuid(),
|
|
verification_expires: utils.dateToUTC(addMinutes(new Date(), 60))
|
|
};
|
|
|
|
if (user) {
|
|
await knex("users")
|
|
.where("id", user.id)
|
|
.update({ ...data, updated_at: utils.dateToUTC(new Date()) });
|
|
} else {
|
|
await knex("users").insert(data);
|
|
}
|
|
|
|
if (env.REDIS_ENABLED) {
|
|
redis.remove.user(user);
|
|
}
|
|
|
|
return {
|
|
...user,
|
|
...data
|
|
};
|
|
}
|
|
|
|
async function update(match, update, methods) {
|
|
const query = knex("users");
|
|
|
|
Object.entries(match).forEach(([key, value]) => {
|
|
query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
|
|
});
|
|
|
|
const updateQuery = query.clone();
|
|
if (methods?.increments) {
|
|
methods.increments.forEach(columnName => {
|
|
updateQuery.increment(columnName);
|
|
});
|
|
}
|
|
|
|
await updateQuery.update({ ...update, updated_at: utils.dateToUTC(new Date()) });
|
|
|
|
const users = await query.select("*");
|
|
|
|
if (env.REDIS_ENABLED) {
|
|
users.forEach(redis.remove.user);
|
|
}
|
|
|
|
return users;
|
|
}
|
|
|
|
async function remove(user) {
|
|
const deletedUser = await knex("users").where("id", user.id).delete();
|
|
|
|
if (env.REDIS_ENABLED) {
|
|
redis.remove.user(user);
|
|
}
|
|
|
|
return !!deletedUser;
|
|
}
|
|
|
|
const selectable_admin = [
|
|
"users.id",
|
|
"users.email",
|
|
"users.verified",
|
|
"users.role",
|
|
"users.banned",
|
|
"users.banned_by_id",
|
|
"users.created_at",
|
|
"users.updated_at"
|
|
];
|
|
|
|
function normalizeMatch(match) {
|
|
const newMatch = { ...match }
|
|
|
|
if (newMatch.banned !== undefined) {
|
|
newMatch["users.banned"] = newMatch.banned;
|
|
delete newMatch.banned;
|
|
}
|
|
|
|
return newMatch;
|
|
}
|
|
|
|
async function getAdmin(match, params) {
|
|
const query = knex("users")
|
|
.select(...selectable_admin)
|
|
.select("l.links_count")
|
|
.select("d.domains")
|
|
.fromRaw("users")
|
|
.where(normalizeMatch(match))
|
|
.offset(params.skip)
|
|
.limit(params.limit)
|
|
.orderBy("users.id", "desc")
|
|
.groupBy(1)
|
|
.groupBy("l.links_count")
|
|
.groupBy("d.domains");
|
|
|
|
if (params?.search) {
|
|
const id = parseInt(params?.search);
|
|
if (Number.isNaN(id)) {
|
|
query.andWhereILike("users.email", "%" + params?.search + "%");
|
|
} else {
|
|
query.andWhere("users.id", params?.search);
|
|
}
|
|
}
|
|
|
|
if (params?.domains !== undefined) {
|
|
query.andWhere("d.domains", params?.domains ? "is not" : "is", null);
|
|
}
|
|
|
|
if (params?.links !== undefined) {
|
|
query.andWhere("links_count", params?.links ? "is not" : "is", null);
|
|
}
|
|
|
|
query.leftJoin(
|
|
knex("domains")
|
|
.select("user_id", knex.raw("string_agg(address, ', ') AS domains"))
|
|
.groupBy("user_id").as("d"),
|
|
"users.id",
|
|
"d.user_id"
|
|
)
|
|
query.leftJoin(
|
|
knex("links").select("user_id").count("id as links_count").groupBy("user_id").as("l"),
|
|
"users.id",
|
|
"l.user_id"
|
|
);
|
|
|
|
return query;
|
|
}
|
|
|
|
async function totalAdmin(match, params) {
|
|
const query = knex("users")
|
|
.count("users.id")
|
|
.fromRaw('users')
|
|
.where(normalizeMatch(match));
|
|
|
|
if (params?.search) {
|
|
const id = parseInt(params?.search);
|
|
if (Number.isNaN(id)) {
|
|
query.andWhereILike("users.email", "%" + params?.search + "%");
|
|
} else {
|
|
query.andWhere("users.id", params?.search);
|
|
}
|
|
}
|
|
|
|
if (params?.domains !== undefined) {
|
|
query.andWhere("domains", params?.domains ? "is not" : "is", null);
|
|
query.leftJoin(
|
|
knex("domains")
|
|
.select("user_id", knex.raw("string_agg(address, ', ') AS domains"))
|
|
.groupBy("user_id").as("d"),
|
|
"users.id",
|
|
"d.user_id"
|
|
);
|
|
}
|
|
|
|
if (params?.links !== undefined) {
|
|
query.andWhere("links", params?.links ? "is not" : "is", null);
|
|
query.leftJoin(
|
|
knex("links").select("user_id").count("id as links").groupBy("user_id").as("l"),
|
|
"users.id",
|
|
"l.user_id"
|
|
);
|
|
}
|
|
|
|
const [{count}] = await query;
|
|
|
|
return typeof count === "number" ? count : parseInt(count);
|
|
}
|
|
|
|
async function create(params) {
|
|
const [user] = await knex("users").insert({
|
|
email: params.email,
|
|
password: params.password,
|
|
role: params.role ?? ROLES.USER,
|
|
verified: params.verified ?? false,
|
|
banned: params.banned ?? false,
|
|
}, "*");
|
|
|
|
return user;
|
|
}
|
|
|
|
module.exports = {
|
|
add,
|
|
create,
|
|
find,
|
|
getAdmin,
|
|
remove,
|
|
totalAdmin,
|
|
update,
|
|
}
|