♻️ refactor: update hero banner code for new schema
This commit is contained in:
@ -1,57 +0,0 @@
|
|||||||
import { AppError } from "../../../../helpers/error/instances/app";
|
|
||||||
import { prisma } from "../../../../utils/databases/prisma/connection";
|
|
||||||
|
|
||||||
export const findAllActiveHeroBannerRepository = async (userId?: string) => {
|
|
||||||
try {
|
|
||||||
return await prisma.heroBanner.findMany({
|
|
||||||
where: {
|
|
||||||
startDate: {
|
|
||||||
lte: new Date(),
|
|
||||||
},
|
|
||||||
endDate: {
|
|
||||||
gte: new Date(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
orderBy: [
|
|
||||||
{
|
|
||||||
orderPriority: "asc",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
startDate: "asc",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
select: {
|
|
||||||
orderPriority: true,
|
|
||||||
imageUrl: true,
|
|
||||||
media: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
title: true,
|
|
||||||
slug: true,
|
|
||||||
pictureLarge: true,
|
|
||||||
synopsis: true,
|
|
||||||
genres: {
|
|
||||||
select: {
|
|
||||||
slug: true,
|
|
||||||
name: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
_count: {
|
|
||||||
select: {
|
|
||||||
inCollections: {
|
|
||||||
where: {
|
|
||||||
collection: {
|
|
||||||
ownerId: userId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
throw new AppError(500, "Failed to fetch active hero banners", error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
import { AppError } from "../../../../helpers/error/instances/app";
|
||||||
|
import { prisma } from "../../../../utils/databases/prisma/connection";
|
||||||
|
|
||||||
|
export const showHeroBannerToHomePageRepository = async () => {
|
||||||
|
try {
|
||||||
|
return await prisma.homeMediaBanner.findMany({
|
||||||
|
where: {
|
||||||
|
start_show: {
|
||||||
|
lte: new Date(),
|
||||||
|
},
|
||||||
|
end_show: {
|
||||||
|
gte: new Date(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
priority: "asc",
|
||||||
|
created_at: "desc",
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
media: {
|
||||||
|
select: {
|
||||||
|
title: true,
|
||||||
|
synopsis: true,
|
||||||
|
large_image_url: true,
|
||||||
|
genres: {
|
||||||
|
select: {
|
||||||
|
genre: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
slug: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(500, "Error fetching hero banner data", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -1,44 +1,14 @@
|
|||||||
import { AppError } from "../../../helpers/error/instances/app";
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||||
import { tokenValidationService } from "../../auth/services/http/tokenValidation.service";
|
import { showHeroBannerToHomePageRepository } from "../repositories/READ/showHeroBannerToHomePage.repository";
|
||||||
import { findSystemPreferenceService } from "../../systemPreference/services/internal/findSystemPreference.service";
|
|
||||||
import { findAllActiveHeroBannerRepository } from "../repositories/GET/findAllActiveHeroBanner.repository";
|
|
||||||
|
|
||||||
export const getActiveHeroBannerService = async ({ cookie }: { cookie?: string }) => {
|
export const getActiveHeroBannerService = async ({ cookie }: { cookie?: string }) => {
|
||||||
try {
|
try {
|
||||||
const userId = cookie ? (await tokenValidationService(cookie)).user.id : undefined;
|
const isHeroBannerEnabled = process.env.ENABLE_HERO_BANNER === "true";
|
||||||
|
|
||||||
// Check if Hero Banner is enabled in system preferences
|
if (!isHeroBannerEnabled) throw new AppError(403, "Hero banner feature is disabled");
|
||||||
const isHeroBannerEnabled = await findSystemPreferenceService("HERO_BANNER_ENABLED", "boolean");
|
|
||||||
if (!isHeroBannerEnabled) throw new AppError(403, "Hero Banner is disabled");
|
|
||||||
|
|
||||||
// Don’t implement caching just yet; implement collection caching first, then implement banner caching.
|
return await showHeroBannerToHomePageRepository();
|
||||||
// Please note that currently, a database query is still required to check the collections.
|
|
||||||
// // Try to get active banners from Redis cache
|
|
||||||
// const cachedBanners = await redis.get(`${redisKey.filter((key) => key.name === "HERO_BANNER")[0].key}`);
|
|
||||||
// if (cachedBanners) return JSON.parse(cachedBanners);
|
|
||||||
|
|
||||||
// If not in cache, fetch from database and cache the result
|
|
||||||
const activeBanners = await findAllActiveHeroBannerRepository(userId);
|
|
||||||
const constructedBanners = activeBanners.map((banner) => ({
|
|
||||||
id: banner.media.id,
|
|
||||||
title: banner.media.title,
|
|
||||||
slug: banner.media.slug,
|
|
||||||
imageUrl: banner.imageUrl || banner.media.pictureLarge,
|
|
||||||
synopsis: banner.media.synopsis,
|
|
||||||
genres: banner.media.genres.map((genre) => ({
|
|
||||||
slug: genre.slug,
|
|
||||||
name: genre.name,
|
|
||||||
})),
|
|
||||||
isInCollection: banner.media._count.inCollections > 0,
|
|
||||||
}));
|
|
||||||
|
|
||||||
// await redis.set(
|
|
||||||
// `${redisKey.filter((key) => key.name === "HERO_BANNER")[0].key}`,
|
|
||||||
// JSON.stringify(constructedBanners),
|
|
||||||
// );
|
|
||||||
|
|
||||||
return constructedBanners;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ErrorForwarder(error);
|
ErrorForwarder(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +0,0 @@
|
|||||||
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",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
@ -1,47 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
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