✨ feat: add helper to detect system preference
This commit is contained in:
12
src/modules/systemPreference/index.tsx
Normal file
12
src/modules/systemPreference/index.tsx
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import Elysia, { Context } from "elysia";
|
||||||
|
import { returnWriteResponse } from "../../helpers/callback/httpResponse";
|
||||||
|
|
||||||
|
export const systemPreferenceModule = new Elysia({
|
||||||
|
prefix: "/system-preference",
|
||||||
|
}).get("/", (ctx: Context) => {
|
||||||
|
return returnWriteResponse(
|
||||||
|
ctx.set,
|
||||||
|
200,
|
||||||
|
"System Preference module is up and running",
|
||||||
|
);
|
||||||
|
});
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
import { AppError } from "../../../../helpers/error/instances/app";
|
||||||
|
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||||
|
import { redis } from "../../../../utils/databases/redis/connection";
|
||||||
|
import { findSystemPreferenceRepository } from "../repositories/findSystemPreference.repository";
|
||||||
|
|
||||||
|
export const findSystemPreferenceService = async (
|
||||||
|
key: string,
|
||||||
|
type: "boolean" | "string" | "number" = "string",
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
// First, check if the system preference is exists in redis cache
|
||||||
|
const cachedValue = await redis.get(
|
||||||
|
`${process.env.APP_NAME}:configs:${key}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!cachedValue) {
|
||||||
|
// If not exists in cache, fetch from database. If found, return the value and set it to cache, if not found, throw an error
|
||||||
|
const systemPreference = await findSystemPreferenceRepository(key);
|
||||||
|
if (!systemPreference)
|
||||||
|
throw new AppError(404, "System preference not found");
|
||||||
|
|
||||||
|
// and set it to cache for future requests
|
||||||
|
await redis.set(
|
||||||
|
`${process.env.APP_NAME}:configs:${key}`,
|
||||||
|
systemPreference.value,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Return the value from database
|
||||||
|
return parseValue(systemPreference.value, type);
|
||||||
|
} else {
|
||||||
|
return parseValue(cachedValue, type);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
ErrorForwarder(error, 500, "Failed to find system preference");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const parseValue = (value: string, type: "boolean" | "string" | "number") => {
|
||||||
|
switch (type) {
|
||||||
|
case "boolean":
|
||||||
|
return value === "true";
|
||||||
|
case "number":
|
||||||
|
return Number(value);
|
||||||
|
default:
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
import { AppError } from "../../../../helpers/error/instances/app";
|
||||||
|
import { prisma } from "../../../../utils/databases/prisma/connection";
|
||||||
|
|
||||||
|
export const findSystemPreferenceRepository = async (key: string) => {
|
||||||
|
try {
|
||||||
|
return await prisma.systemPreference.findUnique({
|
||||||
|
where: {
|
||||||
|
key,
|
||||||
|
deletedAt: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(500, "Failed to find system preference", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user