diff --git a/.dockerignore b/.dockerignore index c5aea8f..210cc6c 100755 --- a/.dockerignore +++ b/.dockerignore @@ -16,6 +16,5 @@ npm-debug.log example.env migrations -.eslintrc.json newsData \ No newline at end of file diff --git a/src/controllers/geek/geek.ctl.ts b/src/controllers/geek/geek.ctl.ts index e776242..68bcc8a 100755 --- a/src/controllers/geek/geek.ctl.ts +++ b/src/controllers/geek/geek.ctl.ts @@ -1,5 +1,5 @@ import { Body, Controller, Post, Query } from '@nestjs/common'; -import { hadaNewsStarValidator, hadaNewsUnStarValidator, hadaNewsValidator } from '@validators/hada.validator'; +import { geekNewsStarValidator, geekNewsUnStarValidator, geekNewsValidator } from '@validators/geek.validator'; import { SetErrorResponse, SetResponse } from 'dto/response.dto'; import { GeekProvider } from 'providers/news/geek/geek.pvd'; import { DailyHadaNewsRequest } from 'types/geek.type'; @@ -9,14 +9,14 @@ import { StarRequest } from 'types/request.type'; export class GeekController { constructor(private readonly geek: GeekProvider) {} - @Post('/news') + @Post('news') async geekGetLatestNewsController( @Body() request: DailyHadaNewsRequest, @Query('page') page: number, @Query('size') size: number, ) { try { - const { today } = await hadaNewsValidator(request); + const { today } = await geekNewsValidator(request); const { result, total } = await this.geek.getNews(today, page, size); @@ -26,10 +26,10 @@ export class GeekController { } } - @Post('/star') + @Post('star') async geekGiveStarController(@Body() request: StarRequest) { try { - const { uuid: postUuid, email } = await hadaNewsStarValidator(request); + const { uuid: postUuid, email } = await geekNewsStarValidator(request); await this.geek.giveStar(postUuid, email); @@ -39,10 +39,10 @@ export class GeekController { } } - @Post('/unstar') + @Post('unstar') async geekUnStarController(@Body() request: StarRequest) { try { - const { uuid: postUuid, email } = await hadaNewsUnStarValidator(request); + const { uuid: postUuid, email } = await geekNewsUnStarValidator(request); await this.geek.unStar(postUuid, email); diff --git a/src/controllers/hacker/hacker.ctl.ts b/src/controllers/hacker/hacker.ctl.ts index 7a158ed..dd0f381 100755 --- a/src/controllers/hacker/hacker.ctl.ts +++ b/src/controllers/hacker/hacker.ctl.ts @@ -9,7 +9,7 @@ import { StarRequest } from 'types/request.type'; export class HackerController { constructor(private readonly hacker: HackersNewsProvider) {} - @Post('/news') + @Post('news') async hackerGetLatestNewsController( @Body() request: DailyHackerNewsRequest, @Query('page') page: number, @@ -26,7 +26,7 @@ export class HackerController { } } - @Post('/star') + @Post('star') async hackerGiveStarController(@Body() request: StarRequest) { try { const { uuid: PostUuid, email } = await hackerNewsStarValidator(request); @@ -39,7 +39,7 @@ export class HackerController { } } - @Post('/unstar') + @Post('unstar') async hackerUnStarController(@Body() request: StarRequest) { try { const { uuid: postUuid, email } = await hackerNewsUnStarValidator(request); diff --git a/src/controllers/machine/machine.ctl.ts b/src/controllers/machine/machine.ctl.ts index c09e7de..e96514e 100644 --- a/src/controllers/machine/machine.ctl.ts +++ b/src/controllers/machine/machine.ctl.ts @@ -9,7 +9,7 @@ import { StarRequest } from 'types/request.type'; export class MachineLearningController { constructor(private readonly mlNews: MachineLearningProvider) {} - @Post('/latest') + @Post('news') async mlGetLatestNewsController( @Body() request: DailyMlNewsRequest, @Query('page') page: number, @@ -26,7 +26,7 @@ export class MachineLearningController { } } - @Post('/star') + @Post('star') async mlGiveStarController(@Body() request: StarRequest) { try { const { uuid: postUuid, email } = await mlNewsStarValidator(request); @@ -39,7 +39,7 @@ export class MachineLearningController { } } - @Post('/unstar') + @Post('unstar') async mlUnStarController(@Body() request: StarRequest) { try { const { uuid: postUuid, email } = await mlNewsUnStarValidator(request); diff --git a/src/dto/response.dto.ts b/src/dto/response.dto.ts index 9084da2..d106e84 100755 --- a/src/dto/response.dto.ts +++ b/src/dto/response.dto.ts @@ -1,7 +1,7 @@ import { AuthError } from '@errors/auth.error'; import { ClientError } from '@errors/client.error'; import { HackerError } from '@errors/hacker.error'; -import { HadaError } from '@errors/hada.error'; +import { HadaError } from '@errors/geek.error'; import { MachineLearningError } from '@errors/machine.error'; import { PrismaError } from '@errors/prisma.error'; import { ValidatorError } from '@errors/validator.error'; diff --git a/src/errors/hada.error.ts b/src/errors/geek.error.ts similarity index 68% rename from src/errors/hada.error.ts rename to src/errors/geek.error.ts index 4bc61fa..8890601 100755 --- a/src/errors/hada.error.ts +++ b/src/errors/geek.error.ts @@ -1,4 +1,4 @@ -export class HadaError extends Error { +export class GeekError extends Error { type: string; constructor(type: string, message: string, cause?: Error) { @@ -6,7 +6,7 @@ export class HadaError extends Error { this.type = type; - this.name = '[Hada Error]'; + this.name = '[Geek Error]'; this.cause = cause; } diff --git a/src/providers/news/geek/geek-prisma.lib.ts b/src/providers/news/geek/geek-prisma.lib.ts index 861be6d..4597e78 100644 --- a/src/providers/news/geek/geek-prisma.lib.ts +++ b/src/providers/news/geek/geek-prisma.lib.ts @@ -1,12 +1,11 @@ -import { HadaError } from '@errors/hada.error'; -import { PrismaError } from '@errors/prisma.error'; +import { GeekError } from '@errors/geek.error'; import { Injectable } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; import { NewsLogger } from '@utils/logger.util'; @Injectable() export class GeekPrismaLibrary extends PrismaClient { - async bringHadaNews(startDate: Date, endDate: Date, page: number, size: number) { + async bringGeekNews(startDate: Date, endDate: Date, page: number, size: number) { try { const result = await this.geek.findMany({ select: { uuid: true, post: true, link: true, descLink: true, founded: true, liked: true }, @@ -23,12 +22,12 @@ export class GeekPrismaLibrary extends PrismaClient { return result; } catch (error) { - NewsLogger.error('[HADA] Bring Geek News Error: %o', { + NewsLogger.error('[GEEK] Bring Geek News Error: %o', { error, }); - throw new PrismaError( - '[HADA] Bring Geek News', + throw new GeekError( + '[GEEK] Bring Geek News', 'Bring Geek News Error. Please Try Again.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); @@ -48,19 +47,19 @@ export class GeekPrismaLibrary extends PrismaClient { return Math.ceil(totalCounts / size); } catch (error) { - NewsLogger.error('[HADA] Bring Total Geek News Count Error: %o', { + NewsLogger.error('[GEEK] Bring Total Geek News Count Error: %o', { error, }); - throw new PrismaError( - '[HADA] Bring Total Geek News Count', + throw new GeekError( + '[GEEK] Bring Total Geek News Count', 'Bring Total Geek News Count Error. Please Try Again.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); } } - async checkHadaNewsIsLiked(postUuid: string, clientUuid: string) { + async checkGeekNewsIsLiked(postUuid: string, clientUuid: string) { try { const isStarred = await this.geek_Liked.findFirst({ select: { @@ -77,29 +76,29 @@ export class GeekPrismaLibrary extends PrismaClient { }, }); - if (isStarred === null) throw new HadaError('[Hada] Get Star Info', 'No Star Info Found.'); + if (isStarred === null) throw new GeekError('[GEEK] Get Star Info', 'No Star Info Found.'); - NewsLogger.debug('[Hada] Found Is Starred Info: %o', { + NewsLogger.debug('[GEEK] Found Is Starred Info: %o', { isLiked: isStarred.geek_news.liked, }); return { uuid: isStarred.uuid, liked: isStarred.geek_news.liked }; } catch (error) { - NewsLogger.error('[Hada] Check Hada News Liked Info Error: %o', { + NewsLogger.error('[GEEK] Check Hada News Liked Info Error: %o', { error, }); - throw new HadaError( - '[Hada] Check Hada News Liked Info', + throw new GeekError( + '[GEEK] Check Hada News Liked Info', 'Check Hada News Liked Info Error.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); } } - async updateHadaNewsLikedtoUnliked(likedUuid: string, postUuid: string, clientUuid: string) { + async updateGeekNewsLikedtoUnliked(likedUuid: string, postUuid: string, clientUuid: string) { try { - NewsLogger.debug('[HADA] Give Hada News unStar Request: %o', { + NewsLogger.debug('[GEEK] Give Hada News unStar Request: %o', { likedUuid, postUuid, clientUuid, @@ -120,25 +119,25 @@ export class GeekPrismaLibrary extends PrismaClient { }, }); - NewsLogger.info('[HADA] Unstarred Updated'); + NewsLogger.info('[GEEK] Unstarred Updated'); return 0; } catch (error) { - NewsLogger.error('[Hada] Update Liked to UnLiked Error: %o', { + NewsLogger.error('[GEEK] Update Liked to UnLiked Error: %o', { error, }); - throw new HadaError( - '[Hada] Update Liked to UnLiked', + throw new GeekError( + '[GEEK] Update Liked to UnLiked', 'Update Liked to UnLiked Error.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); } } - async updateHadaNewsLiked(likedUuid: string, postUuid: string, clientUuid: string) { + async updateGeekNewsLiked(likedUuid: string, postUuid: string, clientUuid: string) { try { - NewsLogger.debug('[HADA] Give Hacker News Star Request: %o', { + NewsLogger.debug('[GEEK] Give Hacker News Star Request: %o', { likedUuid, postUuid, clientUuid, @@ -159,62 +158,19 @@ export class GeekPrismaLibrary extends PrismaClient { }, }); - NewsLogger.info('[HADA] Starred Updated'); + NewsLogger.info('[GEEK] Starred Updated'); return 0; } catch (error) { - NewsLogger.error('[Hada] Update News Liked Error: %o', { + NewsLogger.error('[GEEK] Update News Liked Error: %o', { error, }); - throw new HadaError( - '[Hada] Update News Liked', + throw new GeekError( + '[GEEK] Update News Liked', 'Update News Liked Error.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); } } - - async getStarredHadaNewsPagination(page: number, size: number) { - try { - const totalPosts = await this.geek.count({ where: { liked: 1 } }); - - const starredNews = await this.geek.findMany({ - select: { - uuid: true, - post: true, - link: true, - founded: true, - }, - orderBy: { - founded: 'desc', - }, - where: { - liked: 1, - }, - take: size, - skip: (page - 1) * size, - }); - - NewsLogger.debug('[Hada] Founded Starred News: %o', { - totalPosts, - newsSize: starredNews.length, - }); - - return { - totalPosts, - starredNews, - }; - } catch (error) { - NewsLogger.info('[Hada] Get Starred News Error: %o', { - error, - }); - - throw new HadaError( - '[Hada] Get Starred News', - 'Get Starred News Error.', - error instanceof Error ? error : new Error(JSON.stringify(error)), - ); - } - } } diff --git a/src/providers/news/geek/geek.pvd.ts b/src/providers/news/geek/geek.pvd.ts index a706939..9f31548 100644 --- a/src/providers/news/geek/geek.pvd.ts +++ b/src/providers/news/geek/geek.pvd.ts @@ -1,4 +1,4 @@ -import { HadaError } from '@errors/hada.error'; +import { GeekError } from '@errors/geek.error'; import { Injectable } from '@nestjs/common'; import { NewsLogger } from '@utils/logger.util'; import { endOfDay, startOfDay } from 'date-fns'; @@ -32,7 +32,7 @@ export class GeekProvider { size, }); - const result = await this.prisma.bringHadaNews(startDate, endDate, page, size); + const result = await this.prisma.bringGeekNews(startDate, endDate, page, size); for (let i = 0; i <= result.length - 1; i += 1) { const isUrlUndefined = result[i].descLink.split('.io/')[1]; @@ -80,7 +80,7 @@ export class GeekProvider { error, }); - throw new HadaError( + throw new GeekError( '[GEEK] Bring news', 'Bring Hada News Error. Please Try Again.', error instanceof Error ? error : new Error(JSON.stringify(error)), @@ -92,20 +92,20 @@ export class GeekProvider { try { const isLogined = await this.account.getItem(email); - if (isLogined === null) throw new HadaError('[GEEK] Give Star on the Stars', 'No Logined User Found.'); + if (isLogined === null) throw new GeekError('[GEEK] Give Star on the Stars', 'No Logined User Found.'); const { uuid: clientUuid } = isLogined; - const { uuid: likedUuid, liked } = await this.prisma.checkHadaNewsIsLiked(postUuid, clientUuid); + const { uuid: likedUuid, liked } = await this.prisma.checkGeekNewsIsLiked(postUuid, clientUuid); if (!liked) { - await this.prisma.updateHadaNewsLiked(likedUuid, postUuid, clientUuid); + await this.prisma.updateGeekNewsLiked(likedUuid, postUuid, clientUuid); } } catch (error) { NewsLogger.error('[GEEK] Star Update Error: %o', { error, }); - throw new HadaError( + throw new GeekError( '[GEEK] Give Star on the news', 'Failed to vie star news', error instanceof Error ? error : new Error(JSON.stringify(error)), @@ -117,12 +117,12 @@ export class GeekProvider { try { const isLogined = await this.account.getItem(email); - if (isLogined === null) throw new HadaError('[GEEK] Give Star on the Stars', 'No Logined User Found.'); + if (isLogined === null) throw new GeekError('[GEEK] Give Star on the Stars', 'No Logined User Found.'); const { uuid: clientUuid } = isLogined; - const { uuid: likedUuid, liked } = await this.prisma.checkHadaNewsIsLiked(postUuid, clientUuid); + const { uuid: likedUuid, liked } = await this.prisma.checkGeekNewsIsLiked(postUuid, clientUuid); - if (liked) await this.prisma.updateHadaNewsLikedtoUnliked(likedUuid, postUuid, clientUuid); + if (liked) await this.prisma.updateGeekNewsLikedtoUnliked(likedUuid, postUuid, clientUuid); NewsLogger.info('[GEEK] Finished UnStar Geek News'); } catch (error) { @@ -130,7 +130,7 @@ export class GeekProvider { error, }); - throw new HadaError( + throw new GeekError( '[GEEK] Finished UnStar Geek News', 'Failed to Finished UnStar Geek News. Please Try Again.', error instanceof Error ? error : new Error(JSON.stringify(error)), diff --git a/src/providers/news/hacker/hacker-prisma.lib.ts b/src/providers/news/hacker/hacker-prisma.lib.ts index fdbf2eb..3d96bee 100644 --- a/src/providers/news/hacker/hacker-prisma.lib.ts +++ b/src/providers/news/hacker/hacker-prisma.lib.ts @@ -1,5 +1,4 @@ import { HackerError } from '@errors/hacker.error'; -import { PrismaError } from '@errors/prisma.error'; import { Injectable } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; import { NewsLogger } from '@utils/logger.util'; @@ -23,12 +22,12 @@ export class HackerPrismaLibrary extends PrismaClient { return result; } catch (error) { - NewsLogger.error('[ML] Bring Geek News Error: %o', { + NewsLogger.error('[HACKER] Bring Hacker News Error: %o', { error, }); - throw new PrismaError( - '[ML] Bring Geek News', + throw new HackerError( + '[HACKER] Bring Hacker News', 'Bring Geek News Error. Please Try Again.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); @@ -48,12 +47,12 @@ export class HackerPrismaLibrary extends PrismaClient { return Math.ceil(totalCounts / size); } catch (error) { - NewsLogger.error('[HADA] Bring Total Hackers News Count Error: %o', { + NewsLogger.error('[HACKER] Bring Total Hackers News Count Error: %o', { error, }); - throw new PrismaError( - '[HADA] Bring Total Hackers News Count', + throw new HackerError( + '[HACKER] Bring Total Hackers News Count', 'Bring Total Hackers News Count Error. Please Try Again.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); @@ -77,20 +76,20 @@ export class HackerPrismaLibrary extends PrismaClient { }, }); - if (isStarred === null) throw new HackerError('[Hacker] Get Star Info', 'No Star Info Found.'); + if (isStarred === null) throw new HackerError('[HACKER] Get Star Info', 'No Star Info Found.'); - NewsLogger.debug('[Hacker] Found Is Starred Info: %o', { + NewsLogger.debug('[HACKER] Found Is Starred Info: %o', { isLiked: isStarred.hacker_news.liked, }); return { uuid: isStarred.uuid, isLiked: isStarred.hacker_news.liked }; } catch (error) { - NewsLogger.error('[Hacker] Check Hada News Liked Info Error: %o', { + NewsLogger.error('[HACKER] Check Hacker News Liked Info Error: %o', { error, }); throw new HackerError( - '[Hacker] Check Hada News Liked Info', + '[HACKER] Check Hacker News Liked Info', 'Check Hada News Liked Info Error.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); @@ -99,7 +98,7 @@ export class HackerPrismaLibrary extends PrismaClient { async updateHackerNewsLikedtoUnliked(likedUuid: string, postUuid: string, clientUuid: string): Promise { try { - NewsLogger.debug('[Hacker] Give Hada News unStar Request: %o', { + NewsLogger.debug('[HACKER] Give Hacker News unStar Request: %o', { likedUuid, postUuid, clientUuid, @@ -119,14 +118,14 @@ export class HackerPrismaLibrary extends PrismaClient { userUuid: clientUuid, }, }); - NewsLogger.info('[Hacker] Unstarred Updated'); + NewsLogger.info('[HACKER] Unstarred Updated'); } catch (error) { - NewsLogger.error('[Hacker] Update Liked to UnLiked Error: %o', { + NewsLogger.error('[HACKER] Update Liked to UnLiked Error: %o', { error, }); throw new HackerError( - '[Hacker] Update Liked to UnLiked', + '[HACKER] Update Liked to UnLiked', 'Update Liked to UnLiked Error.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); @@ -135,7 +134,7 @@ export class HackerPrismaLibrary extends PrismaClient { async updateHackerNewsLiked(likedUuid: string, postUuid: string, clientUuid: string): Promise { try { - NewsLogger.debug('[Hacker] Give Hacker News Star Request: %o', { + NewsLogger.debug('[HACKER] Give Hacker News Star Request: %o', { likedUuid, postUuid, clientUuid, @@ -156,14 +155,14 @@ export class HackerPrismaLibrary extends PrismaClient { }, }); - NewsLogger.info('[Hacker] Starred Updated'); + NewsLogger.info('[HACKER] Starred Updated'); } catch (error) { - NewsLogger.error('[Hacker] Update News Liked Error: %o', { + NewsLogger.error('[HACKER] Update News Liked Error: %o', { error, }); throw new HackerError( - '[Hacker] Update News Liked', + '[HACKER] Update News Liked', 'Update News Liked Error.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); @@ -208,7 +207,7 @@ export class HackerPrismaLibrary extends PrismaClient { // skip: (page - 1) * size, // }); - NewsLogger.debug('[Hacker] Founded Starred News: %o', { + NewsLogger.debug('[HACKER] Founded Starred News: %o', { totalPosts, newsSize: starredNews.length, }); @@ -218,12 +217,12 @@ export class HackerPrismaLibrary extends PrismaClient { starredNews, }; } catch (error) { - NewsLogger.error('[Hacker] Get Starred News Error: %o', { + NewsLogger.error('[HACKER] Get Starred News Error: %o', { error, }); throw new HackerError( - '[Hacker] Get Starred News', + '[HACKER] Get Starred News', 'Get Starred News Error.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); diff --git a/src/validators/hada.validator.ts b/src/validators/geek.validator.ts similarity index 69% rename from src/validators/hada.validator.ts rename to src/validators/geek.validator.ts index 50fe6e1..318c419 100644 --- a/src/validators/hada.validator.ts +++ b/src/validators/geek.validator.ts @@ -4,7 +4,7 @@ import { DailyHadaNewsRequest } from 'types/geek.type'; import { StarRequest } from 'types/request.type'; import { z } from 'zod'; -export const hadaNewsValidator = async (request: DailyHadaNewsRequest) => { +export const geekNewsValidator = async (request: DailyHadaNewsRequest) => { try { const scheme = z.object({ today: z.string() }); @@ -12,19 +12,19 @@ export const hadaNewsValidator = async (request: DailyHadaNewsRequest) => { return validated; } catch (error) { - Logger.error('[Hada] Hada News Request Validation Error: %o', { + Logger.error('[GEEK] Geek News Request Validation Error: %o', { error, }); throw new ValidatorError( - '[Hada] Hada News Request Validator', - 'Hada News Request Validator Failed. Please Check the request', + '[GEEK] Geek News Request Validator', + 'Geek News Request Validator Failed. Please Check the request', error instanceof Error ? error : new Error(JSON.stringify(error)), ); } }; -export const hadaNewsStarValidator = async (request: StarRequest) => { +export const geekNewsStarValidator = async (request: StarRequest) => { try { const scheme = z.object({ uuid: z.string(), email: z.string() }); @@ -32,19 +32,19 @@ export const hadaNewsStarValidator = async (request: StarRequest) => { return validated; } catch (error) { - Logger.error('[Hada] Star Request Validator Error: %o', { + Logger.error('[GEEK] Star Request Validator Error: %o', { error, }); throw new ValidatorError( - '[Hada] Star Request Validator', + '[GEEK] Star Request Validator', 'Failed to Star. Please Check the request Body.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); } }; -export const hadaNewsUnStarValidator = async (request: StarRequest) => { +export const geekNewsUnStarValidator = async (request: StarRequest) => { try { const scheme = z.object({ uuid: z.string(), email: z.string() }); @@ -52,12 +52,12 @@ export const hadaNewsUnStarValidator = async (request: StarRequest) => { return validated; } catch (error) { - Logger.error('[Hada] Unstar Request Validator Error: %o', { + Logger.error('[GEEK] Unstar Request Validator Error: %o', { error, }); throw new ValidatorError( - '[Hada] Unstar Request Validator', + '[GEEK] Unstar Request Validator', 'Failed to Unstar. Please Check the request Body.', error instanceof Error ? error : new Error(JSON.stringify(error)), ); diff --git a/src/libraries/client/crypto.test.ts b/test/libraries/crypto.test.ts similarity index 100% rename from src/libraries/client/crypto.test.ts rename to test/libraries/crypto.test.ts diff --git a/tsconfig.json b/tsconfig.json index c7db09d..e4ca121 100755 --- a/tsconfig.json +++ b/tsconfig.json @@ -27,7 +27,7 @@ "@errors/*": ["errors/*"] } }, - "include": ["src/**/*", "jest.config.ts"], + "include": ["src/**/*", "jest.config.ts", "test/**/*"], "exclude": ["node_modules"], "ts-node": { "files": true