feat: base pages and news fetching with cron job

This commit is contained in:
Riccardo
2023-11-25 09:20:38 +01:00
parent aa34263bdf
commit 74ec709155
31 changed files with 10641 additions and 123 deletions

68
app/api/cron/route.ts Normal file
View File

@@ -0,0 +1,68 @@
import { NextResponse } from 'next/server';
import { z } from 'zod';
import prisma from '../../../prisma/prisma';
import { NewsSchema } from '../../utils/types';
import { singleNews, topNews } from '../../utils/urls';
export async function GET(request: Request) {
if (
request.headers.get('Authorization') !== `Bearer ${process.env.CRON_SECRET}`
) {
return new Response('Unauthorized', { status: 401 });
}
const response = await hackernewsApi();
return new NextResponse(JSON.stringify(response), {
status: 200,
});
}
async function hackernewsApi() {
const topstories: number[] = await fetch(topNews).then((res) => res.json());
console.log('topstories', topstories);
const newsPromises = topstories
.splice(0, Number(process.env.NEWS_LIMIT))
.map(async (id) => {
console.log('id', id);
const sourceNews: z.infer<typeof NewsSchema> = await fetch(
singleNews(id)
).then((res) => res.json());
console.log('sourceNews', sourceNews);
return await prisma.news.upsert({
create: {
id,
title: sourceNews.title,
text: sourceNews.text,
type: sourceNews.type,
by: sourceNews.by,
time: sourceNews.time,
url: sourceNews.url,
score: sourceNews.score,
},
update: {
title: sourceNews.title,
text: sourceNews.text,
type: sourceNews.type,
by: sourceNews.by,
time: sourceNews.time,
url: sourceNews.url,
score: sourceNews.score,
},
where: {
id,
},
select: {
id: true,
},
});
});
const newsIds = await Promise.all(newsPromises);
return newsIds.map((news) => news.id);
}

View File

@@ -0,0 +1,32 @@
import { z } from 'zod';
import prisma from '../../../prisma/prisma';
import { ResponseSchema, SubscribeFormSchema } from '../../utils/types';
export const dynamic = 'force-dynamic'; // defaults to force-static
export async function POST(request: Request) {
const body = await request.json();
const validation = SubscribeFormSchema.safeParse(body);
if (!validation.success) {
return new Response('Bad request', { status: 400 });
}
const { email, targetingAllowed } = validation.data;
await prisma.user.upsert({
create: {
email,
targetingAllowed,
},
update: {
targetingAllowed,
},
where: {
email,
},
});
const message: z.infer<typeof ResponseSchema> = {
message: `${email} subscribed!`,
};
return new Response(JSON.stringify(message), { status: 200 });
}

View File

@@ -0,0 +1,29 @@
import { z } from 'zod';
import prisma from '../../../prisma/prisma';
import { ResponseSchema, UnsubscribeFormSchema } from '../../utils/types';
export const dynamic = 'force-dynamic'; // defaults to force-static
export async function POST(request: Request) {
const body = await request.json();
const validation = UnsubscribeFormSchema.safeParse(body);
if (!validation.success) {
return new Response('Bad request', { status: 400 });
}
const { email } = validation.data;
try {
await prisma.user.delete({
where: {
email,
},
});
} catch (err) {
console.log(err);
}
const message: z.infer<typeof ResponseSchema> = {
message: `${email} unsubscribe!`,
};
return new Response(JSON.stringify(message), { status: 200 });
}