Updated redis client. Soft deletes reset username.

This commit is contained in:
Mike Cao 2023-05-05 21:16:13 -07:00
parent 37b94e5b96
commit e92d958b24
6 changed files with 11 additions and 37 deletions

View File

@ -2,35 +2,7 @@ import { User, Website } from '@prisma/client';
import redis from '@umami/redis-client'; import redis from '@umami/redis-client';
import { getSession, getUser, getWebsite } from '../queries'; import { getSession, getUser, getWebsite } from '../queries';
const DELETED = 'DELETED'; const { fetchObject, storeObject, deleteObject } = redis;
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);
}
async function fetchWebsite(id): Promise<Website> { async function fetchWebsite(id): Promise<Website> {
return fetchObject(`website:${id}`, () => getWebsite({ id })); return fetchObject(`website:${id}`, () => getWebsite({ id }));

View File

@ -62,7 +62,7 @@
"@prisma/client": "4.13.0", "@prisma/client": "4.13.0",
"@tanstack/react-query": "^4.16.1", "@tanstack/react-query": "^4.16.1",
"@umami/prisma-client": "^0.2.0", "@umami/prisma-client": "^0.2.0",
"@umami/redis-client": "^0.2.0", "@umami/redis-client": "^0.5.0",
"chalk": "^4.1.1", "chalk": "^4.1.1",
"chart.js": "^4.2.1", "chart.js": "^4.2.1",
"chartjs-adapter-date-fns": "^3.0.0", "chartjs-adapter-date-fns": "^3.0.0",

View File

@ -51,11 +51,11 @@ export default async (
data.password = hashPassword(password); data.password = hashPassword(password);
} }
// Only admin can change these fields
if (role && isAdmin) { if (role && isAdmin) {
data.role = role; data.role = role;
} }
// Only admin can change these fields
if (username && isAdmin) { if (username && isAdmin) {
data.username = username; data.username = username;
} }

View File

@ -1,8 +1,8 @@
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
import { Website, NextApiRequestQueryBody } from 'lib/types'; import { Website, NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite, canUpdateWebsite, canDeleteWebsite } from 'lib/auth'; import { canViewWebsite, canUpdateWebsite, canDeleteWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
import { deleteWebsite, getWebsite, updateWebsite } from 'queries'; import { deleteWebsite, getWebsite, updateWebsite } from 'queries';
export interface WebsiteRequestQuery { export interface WebsiteRequestQuery {

View File

@ -1,4 +1,5 @@
import { Prisma, Team, TeamUser } from '@prisma/client'; import { Prisma, Team, TeamUser } from '@prisma/client';
import { getRandomChars } from 'next-basics';
import cache from 'lib/cache'; import cache from 'lib/cache';
import { ROLES } from 'lib/constants'; import { ROLES } from 'lib/constants';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
@ -222,6 +223,7 @@ export async function deleteUser(
cloudMode cloudMode
? client.user.update({ ? client.user.update({
data: { data: {
username: getRandomChars(32),
deletedAt: new Date(), deletedAt: new Date(),
}, },
where: { where: {

View File

@ -3272,10 +3272,10 @@
dependencies: dependencies:
debug "^4.3.4" debug "^4.3.4"
"@umami/redis-client@^0.2.0": "@umami/redis-client@^0.5.0":
version "0.2.0" version "0.5.0"
resolved "https://registry.yarnpkg.com/@umami/redis-client/-/redis-client-0.2.0.tgz#bdb1cd8b5c99afc5230621f19296c6d3559d68af" resolved "https://registry.yarnpkg.com/@umami/redis-client/-/redis-client-0.5.0.tgz#09b15458001bc172fc856d65316efbe5ff749461"
integrity sha512-TONWhkuC//K2hRo3Psk7FHsuvu3XkQIYMY62/CERPtlIJz4Ac7DqsmYw4jO9/RkljA9XLl/5u+OggD4ARhMV8A== integrity sha512-x7wx/pMjyg3AAYzgjGOw031bNhyZ81h6tRMAl60RQQI9xlJaJEA1r0TEUrWfFi21gHAvdBLJGYCsvHzpix4LKQ==
dependencies: dependencies:
debug "^4.3.4" debug "^4.3.4"
redis "^4.5.1" redis "^4.5.1"