From 07341ee6baca8bd8bf7314cc72c69ff6ecf524b6 Mon Sep 17 00:00:00 2001 From: juliaam Date: Mon, 2 Sep 2024 15:06:15 -0300 Subject: [PATCH 1/2] feat: init of clean arc and zod errors handler --- .../twitter/controllers/twitter-controller.ts | 17 +++++++++- src/features/twitter/models/twitter-models.ts | 5 +++ .../routes/twitter-controller.factory.ts | 11 +++++- src/features/twitter/routes/twitter-routes.ts | 1 + .../services/post-twitter-service.test.ts | 0 .../twitter/services/post-twitter-service.ts | 34 +++++++++++++++++++ 6 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/features/twitter/services/post-twitter-service.test.ts create mode 100644 src/features/twitter/services/post-twitter-service.ts diff --git a/src/features/twitter/controllers/twitter-controller.ts b/src/features/twitter/controllers/twitter-controller.ts index 0ac6461c..eb6b1b70 100644 --- a/src/features/twitter/controllers/twitter-controller.ts +++ b/src/features/twitter/controllers/twitter-controller.ts @@ -3,9 +3,11 @@ import jwt from 'jsonwebtoken'; import type { TokenPayload } from '@/shared/infra/jwt/jwt'; import type { Controller } from '@/shared/protocols/controller'; import type { AsyncRequestHandler } from '@/shared/protocols/handlers'; +import { HttpStatusCode } from '@/shared/protocols/http-client'; import { generateAuthURL } from '../helpers/generate-auth-url'; import type { AuthorizeTwitterService } from '../services/authorize-twitter-service'; +import type { PostTwitterService } from '../services/post-twitter-service'; export class TwitterController implements Controller { callback: AsyncRequestHandler = async (req, res) => { @@ -35,5 +37,18 @@ export class TwitterController implements Controller { return res.json(url); }; - constructor(private readonly authorizeTwitter: AuthorizeTwitterService) {} + tweet: AsyncRequestHandler = (req, res) => { + const authorization = req.headers.authorization; // token bearer user + + const { file, text } = req.body; // talvez tenha a localização também + + const tweet = this.postTwitterService.execute({ file, text }); + + return res.status(HttpStatusCode.ok).json(); + }; + + constructor( + private readonly authorizeTwitter: AuthorizeTwitterService, + private readonly postTwitterService: PostTwitterService + ) {} } diff --git a/src/features/twitter/models/twitter-models.ts b/src/features/twitter/models/twitter-models.ts index bfefcff2..92774f1d 100644 --- a/src/features/twitter/models/twitter-models.ts +++ b/src/features/twitter/models/twitter-models.ts @@ -5,6 +5,11 @@ export type TwitterTokenResponse = { token_type: 'bearer'; }; +export type TwitterPostResponse = { + file?: File; + text?: string; +}; + export type TwitterUser = { id: string; name: string; diff --git a/src/features/twitter/routes/twitter-controller.factory.ts b/src/features/twitter/routes/twitter-controller.factory.ts index e96f7dc1..e33211bc 100644 --- a/src/features/twitter/routes/twitter-controller.factory.ts +++ b/src/features/twitter/routes/twitter-controller.factory.ts @@ -6,6 +6,7 @@ import { Logger } from '@/shared/infra/logger/logger'; import { TwitterController } from '../controllers/twitter-controller'; import { AuthorizeTwitterService } from '../services/authorize-twitter-service'; +import { PostTwitterService } from '../services/post-twitter-service'; import { TwitterService } from '../services/twitter-service'; export function twitterControllerFactory() { @@ -28,7 +29,15 @@ export function twitterControllerFactory() { tokenRepository ); - const twitterController = new TwitterController(authorizeTwitterService); + const postTwitterService = new PostTwitterService( + new Logger({ service: 'postTwitterService' }), + axiosAdapter + ); + + const twitterController = new TwitterController( + authorizeTwitterService, + postTwitterService + ); return { twitterController, diff --git a/src/features/twitter/routes/twitter-routes.ts b/src/features/twitter/routes/twitter-routes.ts index 693433ea..c26bf8fb 100644 --- a/src/features/twitter/routes/twitter-routes.ts +++ b/src/features/twitter/routes/twitter-routes.ts @@ -9,6 +9,7 @@ const { twitterController } = twitterControllerFactory(); router.get('/login', twitterController.login); router.get('/callback', twitterController.callback); +router.post('/tweets', twitterController.tweet); export default { prefix: 'twitter', diff --git a/src/features/twitter/services/post-twitter-service.test.ts b/src/features/twitter/services/post-twitter-service.test.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/features/twitter/services/post-twitter-service.ts b/src/features/twitter/services/post-twitter-service.ts new file mode 100644 index 00000000..e250456e --- /dev/null +++ b/src/features/twitter/services/post-twitter-service.ts @@ -0,0 +1,34 @@ +import type { HttpAdapter } from '@/shared/infra/http/http-adapter'; +import type { Logger } from '@/shared/infra/logger/logger'; +import type { Service } from '@/shared/protocols/service'; + +import type { TwitterPostResponse } from '../models/twitter-models'; + +type Input = { + file?: File; + text?: string; +}; + +export class PostTwitterService implements Service { + constructor( + private readonly logger: Logger, + private readonly http: HttpAdapter + ) {} + + async execute({ file, text }) { + try { + const { data } = await this.http.post({ + data: { + file, + text, + }, + url: '/2/tweets', + }); + + return data.text; + } catch (err) { + this.logger.error(`Error on postTwitter in twitter service -${err}`); + throw err; + } + } +} From f5eb6ab97890afd976cf3d7ca0419fb0bd70309a Mon Sep 17 00:00:00 2001 From: juliaam Date: Mon, 2 Sep 2024 15:07:10 -0300 Subject: [PATCH 2/2] fix: remove test --- src/features/twitter/services/post-twitter-service.test.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/features/twitter/services/post-twitter-service.test.ts diff --git a/src/features/twitter/services/post-twitter-service.test.ts b/src/features/twitter/services/post-twitter-service.test.ts deleted file mode 100644 index e69de29b..00000000