Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion front/src/app/embalse-cuenca/[cuenca]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { EmbalsesCuencaPod, getEmbalsesPorCuenca } from "@/pods/embalse-cuenca";
import { cuencas } from "@/core/constants";
import { Metadata } from "next";
import { mapLookupListFromApiToViewModel } from "@/common/mappers";
import { Metadata } from "next";

export const revalidate = 300; // ISR: regenerar cada 5 minutos

interface Props {
params: Promise<{ cuenca: string }>;
Expand Down
2 changes: 2 additions & 0 deletions front/src/app/embalse-cuenca/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { EmbalseCuencaListPod } from "@/pods/embalse-cuenca-list";
import { getRiverBasins } from "@/pods/embalse-cuenca-list/embalse-cuenca-list.repository";
import { Metadata } from "next";

export const revalidate = 300; // ISR: regenerar cada 5 minutos

export const metadata: Metadata = {
title: "Embalses por cuencas",
};
Expand Down
8 changes: 6 additions & 2 deletions front/src/app/embalse-provincia/[provincia]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { PROVINCIAS } from "@/core/constants";
import { EmbalseProvinciaPod } from "@/pods/embalse-provincia";
import {
EmbalseProvinciaPod,
getEmbalsesByProvince,
} from "@/pods/embalse-provincia";
import { mapEmbalseListFromApiToLookup } from "@/pods/embalse-provincia/embalse-provincia.mapper";
import { getEmbalsesByProvince } from "@/pods/embalse-provincia/embalse-provincia.repository";
import { Metadata } from "next";

export const revalidate = 300; // ISR: regenerar cada 5 minutos

interface Props {
params: Promise<{ provincia: string }>;
}
Expand Down
11 changes: 7 additions & 4 deletions front/src/app/embalse/[embalse]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { EmbalsePod, getReservoirInfoBySlugCached } from "@/pods/embalse";
import { getEmbalseBySlug } from "@/pods/embalse/embalse.repository";
import {
EmbalsePod,
getReservoirInfoBySlugCached,
getEmbalseBySlugCached,
} from "@/pods/embalse";
import { mapEmbalseToReservoirData } from "@/pods/embalse/embalse.mapper";

export const revalidate = 300; // ISR: regenerar cada 5 minutos

export async function generateMetadata({ params }: Props): Promise<Metadata> {
const { embalse } = await params;
const embalseSlug = await getEmbalseBySlug(embalse);
const embalseSlug = await getEmbalseBySlugCached(embalse);

return {
title: embalseSlug.nombre,
Expand All @@ -25,7 +28,7 @@ export default async function EmbalseDetallePage({ params }: Props) {
Si no se encuentra el embalse, llamamos a notFound() que muestra la pagina 404 de Next.js
*/
const { embalse } = await params;
const embalseDoc = await getEmbalseBySlug(embalse);
const embalseDoc = await getEmbalseBySlugCached(embalse);
const embalseInfo = await getReservoirInfoBySlugCached(embalse);

if (!embalseDoc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@

import { LookupApi } from "@/common/models";
import { getDb } from "@/lib/mongodb";
import { unstable_cache } from "next/cache";

export async function getRiverBasins(): Promise<LookupApi[]> {
try {
const getCachedRiverBasins = unstable_cache(
async (): Promise<LookupApi[]> => {
const db = await getDb();
return await db
const result = await db
.collection<LookupApi>("cuencas")
.find({}, { projection: { _id: 1, nombre: 1 } })
.toArray();
if (result.length === 0) {
throw new Error("Empty cuencas - skip cache");
}
return result;
},
["cuencas-collection"],
{ revalidate: 300 },
);

export async function getRiverBasins(): Promise<LookupApi[]> {
try {
return await getCachedRiverBasins();
} catch (error) {
console.warn(
"getEmbalsesByRiverBasin: MongoDB not available (build time?), returning empty array.",
"getRiverBasins: MongoDB not available or empty, returning empty array.",
"Error:",
error instanceof Error ? error.message : error,
);
return [];
}
}
30 changes: 30 additions & 0 deletions front/src/pods/embalse-cuenca/api/embalse-cuenca.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "server-only";
import { unstable_cache } from "next/cache";
import type { LookupApi } from "@/common/models";
import { getEmbalsesPorCuenca as getEmbalsesPorCuencaFromDb } from "../embalse-cuenca.repository";

/**
* Cached version of getEmbalsesPorCuenca.
* Revalidates every 5 minutes.
*/
const getCachedEmbalsesPorCuenca = unstable_cache(
async (nombre: string): Promise<LookupApi[]> => {
const embalses = await getEmbalsesPorCuencaFromDb(nombre);
if (!embalses || embalses.length === 0) {
throw new Error("Empty embalses por cuenca - skip cache");
}
return embalses;
},
["embalses-por-cuenca"],
{ revalidate: 300 },
);

export const getEmbalsesPorCuenca = async (
nombre: string,
): Promise<LookupApi[]> => {
try {
return await getCachedEmbalsesPorCuenca(nombre);
} catch {
return [];
}
};
1 change: 1 addition & 0 deletions front/src/pods/embalse-cuenca/embalse-cuenca.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export const getEmbalsesPorCuenca = async (
"Error:",
error instanceof Error ? error.message : error,
);
return [];
}
};
2 changes: 1 addition & 1 deletion front/src/pods/embalse-cuenca/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./embalse-cuenca.pod";
export * from "./embalse-cuenca.repository";
export * from "./api/embalse-cuenca.api";
30 changes: 30 additions & 0 deletions front/src/pods/embalse-provincia/api/embalse-provincia.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "server-only";
import { unstable_cache } from "next/cache";
import type { EmbalseApi } from "../embalse-provincia.api-model";
import { getEmbalsesByProvince as getEmbalsesByProvinceFromDb } from "../embalse-provincia.repository";

/**
* Cached version of getEmbalsesByProvince.
* Revalidates every 5 minutes.
*/
const getCachedEmbalsesByProvince = unstable_cache(
async (provincia: string): Promise<EmbalseApi[]> => {
const embalses = await getEmbalsesByProvinceFromDb(provincia);
if (!embalses || embalses.length === 0) {
throw new Error("Empty embalses por provincia - skip cache");
}
return embalses;
},
["embalses-por-provincia"],
{ revalidate: 300 },
);

export const getEmbalsesByProvince = async (
provincia: string,
): Promise<EmbalseApi[]> => {
try {
return await getCachedEmbalsesByProvince(provincia);
} catch {
return [];
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export async function getEmbalsesByProvince(
)
.toArray();

console.log(docs);
return docs.map((doc) => ({
_id: doc.slug ?? String(doc._id),
name: doc.nombre ?? "",
Expand Down
1 change: 1 addition & 0 deletions front/src/pods/embalse-provincia/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./embalse-provincia.pod";
export * from "./api/embalse-provincia.api";
18 changes: 16 additions & 2 deletions front/src/pods/embalse/api/embalse.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import "server-only";
import { unstable_cache } from "next/cache";
import type { ReservoirInfo } from "./embalse.api-model";
import { contentIslandClient } from "@/lib";
import { getEmbalseBySlug } from "../embalse.repository";
import type { Embalse } from "db-model";

/**
* Cached version of getReservoirInfoBySlug.
Expand All @@ -24,11 +26,23 @@ export const getReservoirInfoBySlugCached = unstable_cache(
} catch (error) {
console.warn(
`Warning reservoir info for slug not available: ${slug}`,
error
error,
);
return null;
}
},
["reservoir-by-slug"],
{ revalidate: 60 }
{ revalidate: 60 },
);

/**
* Cached version of getEmbalseBySlug.
* Revalidates every 60 seconds.
*/
export const getEmbalseBySlugCached = unstable_cache(
async (slug: string): Promise<Embalse | null> => {
return getEmbalseBySlug(slug);
},
["embalse-by-slug"],
{ revalidate: 60 },
);