👔 feat: support user collection check on hero banner #30

Merged
vivy-agent merged 1 commits from fix-banner into main 2026-03-30 19:21:12 +07:00
4 changed files with 45 additions and 21 deletions
Showing only changes of commit 5cebd200c4 - Show all commits

View File

@ -1,17 +1,16 @@
import { Context } from "elysia";
import { Context, Static } from "elysia";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
import { getActiveHeroBannerService } from "../services/getActiveHeroBanner.service";
import { getActiveHeroBannerSchema } from "../schemas/getActiveHeroBanner.schema";
export const getActiveHeroBannerController = async (ctx: Context) => {
export const getActiveHeroBannerController = async (ctx: {
set: Context["set"];
header: Static<typeof getActiveHeroBannerSchema.headers>;
}) => {
try {
const response = await getActiveHeroBannerService();
return returnReadResponse(
ctx.set,
200,
"Active hero banners fetched successfully",
response,
);
const response = await getActiveHeroBannerService({ cookie: ctx.header?.cookie });
return returnReadResponse(ctx.set, 200, "Active hero banners fetched successfully", response);
} catch (error) {
return mainErrorHandler(ctx.set, error);
}

View File

@ -1,7 +1,7 @@
import { AppError } from "../../../../helpers/error/instances/app";
import { prisma } from "../../../../utils/databases/prisma/connection";
export const findAllActiveHeroBannerRepository = async () => {
export const findAllActiveHeroBannerRepository = async (userId?: string) => {
try {
return await prisma.heroBanner.findMany({
where: {
@ -36,6 +36,17 @@ export const findAllActiveHeroBannerRepository = async () => {
name: true,
},
},
_count: {
select: {
inCollections: {
where: {
collection: {
ownerId: userId,
},
},
},
},
},
},
},
},

View File

@ -0,0 +1,8 @@
import { t } from "elysia";
import { AppRouteSchema } from "../../../helpers/types/AppRouteSchema";
export const getActiveHeroBannerSchema = {
headers: t.Object({
cookie: t.Optional(t.String()),
}),
} satisfies AppRouteSchema;

View File

@ -1,22 +1,25 @@
import { redisKey } from "../../../config/redis/key";
import { AppError } from "../../../helpers/error/instances/app";
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
import { redis } from "../../../utils/databases/redis/connection";
import { tokenValidationService } from "../../auth/services/http/tokenValidation.service";
import { findSystemPreferenceService } from "../../systemPreference/services/internal/findSystemPreference.service";
import { findAllActiveHeroBannerRepository } from "../repositories/GET/findAllActiveHeroBanner.repository";
export const getActiveHeroBannerService = async () => {
export const getActiveHeroBannerService = async ({ cookie }: { cookie?: string }) => {
try {
const userId = cookie ? (await tokenValidationService(cookie)).user.id : undefined;
// Check if Hero Banner is enabled in system preferences
const isHeroBannerEnabled = await findSystemPreferenceService("HERO_BANNER_ENABLED", "boolean");
if (!isHeroBannerEnabled) throw new AppError(403, "Hero Banner is disabled");
// 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);
// Dont implement caching just yet; implement collection caching first, then implement banner caching.
// 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();
const activeBanners = await findAllActiveHeroBannerRepository(userId);
const constructedBanners = activeBanners.map((banner) => ({
id: banner.media.id,
title: banner.media.title,
@ -27,11 +30,14 @@ export const getActiveHeroBannerService = async () => {
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),
);
// await redis.set(
// `${redisKey.filter((key) => key.name === "HERO_BANNER")[0].key}`,
// JSON.stringify(constructedBanners),
// );
return constructedBanners;
} catch (error) {
ErrorForwarder(error);