Skip to content

Commit

Permalink
rename redis functions for clarification
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasilva414 committed Jan 3, 2025
1 parent 2c33857 commit 1f1f46e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
8 changes: 4 additions & 4 deletions apps/web/src/actions/admin/modify-nav-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { z } from "zod";
import { adminAction } from "@/lib/safe-action";
import { sadd, hset, removeNavItem } from "@/lib/utils/server/redis";
import { redisSAdd, redisHSet, removeNavItem } from "@/lib/utils/server/redis";
import { revalidatePath } from "next/cache";

const metadataSchema = z.object({
Expand All @@ -16,8 +16,8 @@ const navAdminPage = "/admin/toggles/landing";
export const setItem = adminAction
.schema(metadataSchema)
.action(async ({ parsedInput: { name, url }, ctx: { user, userId } }) => {
await sadd("config:navitemslist", encodeURIComponent(name));
await hset(`config:navitems:${encodeURIComponent(name)}`, {
await redisSAdd("config:navitemslist", encodeURIComponent(name));
await redisHSet(`config:navitems:${encodeURIComponent(name)}`, {
url,
name,
enabled: true,
Expand All @@ -42,7 +42,7 @@ export const toggleItem = adminAction
parsedInput: { name, statusToSet },
ctx: { user, userId },
}) => {
await hset(`config:navitems:${encodeURIComponent(name)}`, {
await redisHSet(`config:navitems:${encodeURIComponent(name)}`, {
enabled: statusToSet,
});
revalidatePath(navAdminPage);
Expand Down
18 changes: 12 additions & 6 deletions apps/web/src/actions/admin/registration-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { z } from "zod";
import { adminAction } from "@/lib/safe-action";
import { set } from "@/lib/utils/server/redis";
import { redisSet } from "@/lib/utils/server/redis";
import { revalidatePath } from "next/cache";

const defaultRegistrationToggleSchema = z.object({
Expand All @@ -16,39 +16,45 @@ const defaultRSVPLimitSchema = z.object({
export const toggleRegistrationEnabled = adminAction
.schema(defaultRegistrationToggleSchema)
.action(async ({ parsedInput: { enabled }, ctx: { user, userId } }) => {
await set("config:registration:registrationEnabled", enabled);
await redisSet("config:registration:registrationEnabled", enabled);
revalidatePath("/admin/toggles/registration");
return { success: true, statusSet: enabled };
});

export const toggleRegistrationMessageEnabled = adminAction
.schema(defaultRegistrationToggleSchema)
.action(async ({ parsedInput: { enabled }, ctx: { user, userId } }) => {
await set("config:registration:registrationMessageEnabled", enabled);
await redisSet(
"config:registration:registrationMessageEnabled",
enabled,
);
revalidatePath("/admin/toggles/registration");
return { success: true, statusSet: enabled };
});

export const toggleSecretRegistrationEnabled = adminAction
.schema(defaultRegistrationToggleSchema)
.action(async ({ parsedInput: { enabled }, ctx: { user, userId } }) => {
await set("config:registration:secretRegistrationEnabled", enabled);
await redisSet(
"config:registration:secretRegistrationEnabled",
enabled,
);
revalidatePath("/admin/toggles/registration");
return { success: true, statusSet: enabled };
});

export const toggleRSVPs = adminAction
.schema(defaultRegistrationToggleSchema)
.action(async ({ parsedInput: { enabled }, ctx: { user, userId } }) => {
await set("config:registration:allowRSVPs", enabled);
await redisSet("config:registration:allowRSVPs", enabled);
revalidatePath("/admin/toggles/registration");
return { success: true, statusSet: enabled };
});

export const setRSVPLimit = adminAction
.schema(defaultRSVPLimitSchema)
.action(async ({ parsedInput: { rsvpLimit }, ctx: { user, userId } }) => {
await set("config:registration:maxRSVPs", rsvpLimit);
await redisSet("config:registration:maxRSVPs", rsvpLimit);
revalidatePath("/admin/toggles/registration");
return { success: true, statusSet: rsvpLimit };
});
4 changes: 2 additions & 2 deletions apps/web/src/app/admin/toggles/registration/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RegistrationToggles } from "@/components/admin/toggles/RegistrationSettings";
import { mget } from "@/lib/utils/server/redis";
import { redisMGet } from "@/lib/utils/server/redis";
import { parseRedisBoolean, parseRedisNumber } from "@/lib/utils/server/redis";
import c from "config";

Expand All @@ -9,7 +9,7 @@ export default async function Page() {
defaultSecretRegistrationEnabled,
defaultRSVPsEnabled,
defaultRSVPLimit,
]: (string | null)[] = await mget(
]: (string | null)[] = await redisMGet(
"config:registration:registrationEnabled",
"config:registration:secretRegistrationEnabled",
"config:registration:allowRSVPs",
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { auth, currentUser } from "@clerk/nextjs";
import { redirect } from "next/navigation";
import Navbar from "@/components/shared/Navbar";
import Link from "next/link";
import { mget } from "@/lib/utils/server/redis";
import { redisMGet } from "@/lib/utils/server/redis";
import { parseRedisBoolean } from "@/lib/utils/server/redis";
import { Button } from "@/components/shadcn/ui/button";
import { getUser } from "db/functions";
Expand All @@ -22,7 +22,7 @@ export default async function Page() {
const [defaultRegistrationEnabled, defaultSecretRegistrationEnabled]: (
| string
| null
)[] = await mget(
)[] = await redisMGet(
"config:registration:registrationEnabled",
"config:registration:secretRegistrationEnabled",
);
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/app/rsvp/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SignedOut, RedirectToSignIn } from "@clerk/nextjs";
import {
parseRedisBoolean,
parseRedisNumber,
get,
redisGet,
} from "@/lib/utils/server/redis";
import Link from "next/link";
import { Button } from "@/components/shadcn/ui/button";
Expand Down Expand Up @@ -44,7 +44,7 @@ export default async function RsvpPage({
}

const rsvpEnabled = parseRedisBoolean(
(await get("config:registration:allowRSVPs")) as
(await redisGet("config:registration:allowRSVPs")) as
| string
| boolean
| null
Expand All @@ -56,7 +56,7 @@ export default async function RsvpPage({

if (rsvpEnabled === true) {
const rsvpLimit = parseRedisNumber(
await get("config:registration:maxRSVPs"),
await redisGet("config:registration:maxRSVPs"),
c.rsvpDefaultLimit,
);

Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/sign-up/[[...sign-up]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SignUp } from "@clerk/nextjs";
import { mget } from "@/lib/utils/server/redis";
import { redisMGet } from "@/lib/utils/server/redis";
import { parseRedisBoolean } from "@/lib/utils/server/redis";
import c from "config";
import { Button } from "@/components/shadcn/ui/button";
Expand All @@ -9,7 +9,7 @@ export default async function Page() {
const [defaultRegistrationEnabled, defaultSecretRegistrationEnabled]: (
| string
| null
)[] = await mget(
)[] = await redisMGet(
"config:registration:registrationEnabled",
"config:registration:secretRegistrationEnabled",
);
Expand Down
10 changes: 5 additions & 5 deletions apps/web/src/lib/utils/server/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ export function includeEnvPrefix(key: string) {
return `${process.env.HK_ENV}_${key}`;
}

export async function sadd(key: string, value: string): Promise<number> {
export async function redisSAdd(key: string, value: string): Promise<number> {
return kv.sadd(includeEnvPrefix(key), value);
}

export async function hset<TData>(
export async function redisHSet<TData>(
key: string,
value: Record<string, TData>,
): Promise<number> {
return kv.hset(includeEnvPrefix(key), value);
}

export async function set<TData>(
export async function redisSet<TData>(
key: string,
value: TData,
): Promise<TData | "OK" | null> {
return kv.set<TData>(includeEnvPrefix(key), value);
}

export async function get<TData>(key: string): Promise<TData | null> {
export async function redisGet<TData>(key: string): Promise<TData | null> {
return kv.get<TData>(includeEnvPrefix(key));
}

export async function mget<TData>(...keys: string[]): Promise<TData[]> {
export async function redisMGet<TData>(...keys: string[]): Promise<TData[]> {
return kv.mget<TData[]>(keys.map(includeEnvPrefix));
}

Expand Down

0 comments on commit 1f1f46e

Please sign in to comment.