diff --git a/lib/cache.ts b/lib/cache.ts index 2aad7ed8..528dd012 100644 --- a/lib/cache.ts +++ b/lib/cache.ts @@ -2,35 +2,7 @@ import { User, Website } from '@prisma/client'; import redis from '@umami/redis-client'; import { getSession, getUser, getWebsite } from '../queries'; -const DELETED = 'DELETED'; - -async function fetchObject(key, query) { - const obj = await redis.get(key); - - if (obj === DELETED) { - return null; - } - - if (!obj) { - return query().then(async data => { - if (data) { - await redis.set(key, data); - } - - return data; - }); - } - - return obj; -} - -async function storeObject(key, data) { - return redis.set(key, data); -} - -async function deleteObject(key, soft = false) { - return soft ? redis.set(key, DELETED) : redis.del(key); -} +const { fetchObject, storeObject, deleteObject } = redis; async function fetchWebsite(id): Promise { return fetchObject(`website:${id}`, () => getWebsite({ id })); diff --git a/package.json b/package.json index 736ec659..0db07f49 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "@prisma/client": "4.13.0", "@tanstack/react-query": "^4.16.1", "@umami/prisma-client": "^0.2.0", - "@umami/redis-client": "^0.2.0", + "@umami/redis-client": "^0.5.0", "chalk": "^4.1.1", "chart.js": "^4.2.1", "chartjs-adapter-date-fns": "^3.0.0", diff --git a/pages/api/users/[id]/index.ts b/pages/api/users/[id]/index.ts index 8219c4a7..de4642cb 100644 --- a/pages/api/users/[id]/index.ts +++ b/pages/api/users/[id]/index.ts @@ -51,11 +51,11 @@ export default async ( data.password = hashPassword(password); } + // Only admin can change these fields if (role && isAdmin) { data.role = role; } - // Only admin can change these fields if (username && isAdmin) { data.username = username; } diff --git a/pages/api/websites/[id]/index.ts b/pages/api/websites/[id]/index.ts index c1907fce..3f660a91 100644 --- a/pages/api/websites/[id]/index.ts +++ b/pages/api/websites/[id]/index.ts @@ -1,8 +1,8 @@ +import { NextApiResponse } from 'next'; +import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics'; import { Website, NextApiRequestQueryBody } from 'lib/types'; import { canViewWebsite, canUpdateWebsite, canDeleteWebsite } from 'lib/auth'; import { useAuth, useCors } from 'lib/middleware'; -import { NextApiResponse } from 'next'; -import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics'; import { deleteWebsite, getWebsite, updateWebsite } from 'queries'; export interface WebsiteRequestQuery { diff --git a/queries/admin/user.ts b/queries/admin/user.ts index 412c7785..a81a76ef 100644 --- a/queries/admin/user.ts +++ b/queries/admin/user.ts @@ -1,4 +1,5 @@ import { Prisma, Team, TeamUser } from '@prisma/client'; +import { getRandomChars } from 'next-basics'; import cache from 'lib/cache'; import { ROLES } from 'lib/constants'; import prisma from 'lib/prisma'; @@ -222,6 +223,7 @@ export async function deleteUser( cloudMode ? client.user.update({ data: { + username: getRandomChars(32), deletedAt: new Date(), }, where: { diff --git a/yarn.lock b/yarn.lock index d98ada11..41cca434 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3272,10 +3272,10 @@ dependencies: debug "^4.3.4" -"@umami/redis-client@^0.2.0": - version "0.2.0" - resolved "https://registry.yarnpkg.com/@umami/redis-client/-/redis-client-0.2.0.tgz#bdb1cd8b5c99afc5230621f19296c6d3559d68af" - integrity sha512-TONWhkuC//K2hRo3Psk7FHsuvu3XkQIYMY62/CERPtlIJz4Ac7DqsmYw4jO9/RkljA9XLl/5u+OggD4ARhMV8A== +"@umami/redis-client@^0.5.0": + version "0.5.0" + resolved "https://registry.yarnpkg.com/@umami/redis-client/-/redis-client-0.5.0.tgz#09b15458001bc172fc856d65316efbe5ff749461" + integrity sha512-x7wx/pMjyg3AAYzgjGOw031bNhyZ81h6tRMAl60RQQI9xlJaJEA1r0TEUrWfFi21gHAvdBLJGYCsvHzpix4LKQ== dependencies: debug "^4.3.4" redis "^4.5.1"