refactor: renames, rewrite some file, function names and texts

This commit is contained in:
Riccardo
2024-06-18 12:03:56 +02:00
parent acc10bf5fd
commit e0b89237fb
22 changed files with 55 additions and 84 deletions

View File

@@ -6,7 +6,7 @@ import {
STATUS_OK,
STATUS_UNAUTHORIZED
} from '@utils/statusCodes';
import { singleNews, topNews } from '@utils/urls';
import { getSingleNews, getTopNews } from '@utils/urls';
import { NewsDatabaseSchema, NewsDatabaseType } from '@utils/validationSchemas';
import { Resend } from 'resend';
@@ -18,7 +18,7 @@ export async function GET(request: Request) {
}
try {
const topStories: number[] = await fetch(topNews, {
const topStories: number[] = await fetch(getTopNews, {
cache: 'no-store'
}).then(res => res.json());
@@ -26,31 +26,33 @@ export async function GET(request: Request) {
const newsPromises = topStories
.slice(0, Number(process.env.NEWS_LIMIT))
.map(id => fetch(singleNews(id)).then(res => res.json()));
.map(id => fetch(getSingleNews(id)).then(res => res.json()));
const news: NewsDatabaseType[] = await Promise.all(newsPromises);
const upsertPromises = news.map(async singleNews => {
const validation = NewsDatabaseSchema.safeParse(singleNews);
const upsertPromises = news.map(async getSingleNews => {
const validation = NewsDatabaseSchema.safeParse(getSingleNews);
if (validation.success) {
console.info(
`Validated news N° ${singleNews.id} - ${singleNews.title}`
`Validated news N° ${getSingleNews.id} - ${getSingleNews.title}`
);
const result = await prisma.news.upsert({
create: {
...validation.data,
id: singleNews.id
id: getSingleNews.id
},
update: {
...validation.data
},
where: {
id: singleNews.id
id: getSingleNews.id
}
});
console.info(`Imported N° ${singleNews.id} - ${singleNews.title}`);
console.info(
`Imported N° ${getSingleNews.id} - ${getSingleNews.title}`
);
return result;
} else {
@@ -69,7 +71,7 @@ export async function GET(request: Request) {
from: process.env.RESEND_FROM,
to: [process.env.ADMIN_EMAIL],
subject: 'Newsletter: import cron job',
text: `Found these ids ${topStories.join(', ')} and imported ${result.length} of them.`
text: `Imported ${result.length} news out of these ids: ${topStories.join(', ')}.`
});
}

View File

@@ -72,7 +72,7 @@ export async function GET(request: Request) {
from: process.env.RESEND_FROM,
to: [process.env.ADMIN_EMAIL],
subject: 'Newsletter: mailing cron job',
text: `Found ${users.length} users and ${news.length} news to send.`
text: `Found ${users.length} users and ${news.length} news to send for ${new Date().toISOString()}.`
});
}

View File

@@ -13,8 +13,6 @@ import { ResponseType, SubscribeFormSchema } from '@utils/validationSchemas';
import * as crypto from 'crypto';
import { Resend } from 'resend';
export const dynamic = 'force-dynamic'; // defaults to force-static
export async function POST(request: Request) {
try {
if (!process.env.RESEND_KEY || !process.env.RESEND_AUDIENCE) {

View File

@@ -1,5 +1,5 @@
import Tiles from '@components/tiles/Tiles';
import { cn } from '@utils/ui';
import { cn } from '@utils/cn';
import { Analytics } from '@vercel/analytics/react';
import type { Metadata } from 'next';
import { Inter as FontSans } from 'next/font/google';

View File

@@ -4,7 +4,7 @@ export default function manifest(): MetadataRoute.Manifest {
return {
name: 'Newsletter',
short_name: 'Newsletter',
description: 'Newsletter with Hackernews top stories',
description: 'Newsletter with top stories from the Hacker News forum',
start_url: '/',
display: 'standalone',
background_color: '#fff',