From 2855fac3681a02107c2abb886a8c754c760f4277 Mon Sep 17 00:00:00 2001 From: Ashu11-A Date: Mon, 19 Feb 2024 23:07:10 -0400 Subject: [PATCH] =?UTF-8?q?Revert=20"=F0=9F=97=91=EF=B8=8F=20Delete=20unus?= =?UTF-8?q?ed=20Routers"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 4d8773940aed40b51c70327cab28d69a4dee1e38. --- src/express/routes/ctrlpanel/index.ts | 44 +++++++++++ .../routes/ctrlpanel/voucher/create.ts | 73 +++++++++++++++++ .../routes/ctrlpanel/voucher/delete.ts | 54 +++++++++++++ src/express/routes/payment/create/card.ts | 79 +++++++++++++++++++ src/express/routes/payment/create/pix.ts | 58 ++++++++++++++ 5 files changed, 308 insertions(+) create mode 100644 src/express/routes/ctrlpanel/index.ts create mode 100644 src/express/routes/ctrlpanel/voucher/create.ts create mode 100644 src/express/routes/ctrlpanel/voucher/delete.ts create mode 100644 src/express/routes/payment/create/card.ts create mode 100644 src/express/routes/payment/create/pix.ts diff --git a/src/express/routes/ctrlpanel/index.ts b/src/express/routes/ctrlpanel/index.ts new file mode 100644 index 00000000..462d81cd --- /dev/null +++ b/src/express/routes/ctrlpanel/index.ts @@ -0,0 +1,44 @@ +import { client, db } from '@/app' +import { numerosParaLetras } from '@/functions' +import { type Request, type Response } from 'express' + +export class CtrlPanel { + /** + * Mostra o total de usuários atuais registrados no dash + */ + public async get (req: Request, res: Response): Promise>> { + try { + const guilds = client.guilds.cache + const dataCtrlPanel: Record = {} + + for (const guild of guilds.values()) { + const { id } = guild + const ctrlUsers = await db.ctrlPanel.table(`${numerosParaLetras(id)}_users`).get('metadata') + const ctrlServer = await db.ctrlPanel.table(`${numerosParaLetras(id)}_servers`).get('metadata') + + dataCtrlPanel[id] = { + ...ctrlUsers, + ...ctrlServer + } + } + + if (dataCtrlPanel !== undefined) { + return res.status(200).json({ + status: 200, + ...dataCtrlPanel + }) + } else { + return res.status(404).json({ + status: 404, + message: 'Nenhuma informação foi encontrada' + }) + } + } catch (error) { + return res.status(500).json({ + status: 500, + error: 'Internal Server Error' + }) + } + } +} +export const Root = new CtrlPanel() diff --git a/src/express/routes/ctrlpanel/voucher/create.ts b/src/express/routes/ctrlpanel/voucher/create.ts new file mode 100644 index 00000000..4aca596e --- /dev/null +++ b/src/express/routes/ctrlpanel/voucher/create.ts @@ -0,0 +1,73 @@ +import axios from 'axios' +import { type Request, type Response } from 'express' +import randomstring from 'randomstring' + +interface createCtrlPanelVoucher { + user: { id: string, name: string } + guild: { id: string, name: string } + credits: number + price: number + name: string + token: string + url: string +} +class CreateVoucher { + /** + * Recebe solicitações para a criação de vouchers + */ + public async post (req: Request, res: Response): Promise> | undefined> { + const { user, credits, price, name, url, token } = req.body as createCtrlPanelVoucher + const pass = randomstring.generate({ length: 36 }) + const code = pass.toString() + console.log(req.body) + + try { + if ( + typeof user !== 'object' || + typeof credits !== 'number' || + typeof price !== 'number' || + typeof name !== 'string' || + typeof url !== 'string' || + typeof token !== 'string' + ) { + console.log('Missing required fields') + return res.status(400).json({ + error: 'Bad Request: Missing required fields', + status: 400 + }) + } + const postData = { + memo: `${user.name} (ID: ${user.id}) comprou créditos no valor de R$${price}`, + code, + uses: 1, + credits + } + + const { data, status } = await axios.post(url + '/api/vouchers', postData, { + headers: { + Accept: 'application/json', + Authorization: `Bearer ${token}` + } + }) + if (status === 201 && data.status === 'VALID') { + res.status(201).json({ + code, + id: data.id + }) + } else { + return res.status(500).json({ + error: 'A função createVoucher não retornou corretamente o code e id', + status: 500 + }) + } + } catch (err) { + console.log(err) + res.status(500).json({ + status: 500, + message: 'Houve um erro na requisição' + }) + } + } +} + +export const Root = new CreateVoucher() diff --git a/src/express/routes/ctrlpanel/voucher/delete.ts b/src/express/routes/ctrlpanel/voucher/delete.ts new file mode 100644 index 00000000..0a675176 --- /dev/null +++ b/src/express/routes/ctrlpanel/voucher/delete.ts @@ -0,0 +1,54 @@ +import axios from 'axios' +import { type Request, type Response } from 'express' + +interface deleteCtrlPanelVoucher { + id: string + token: string + url: string +} + +class Voucher { + /** + * Recebe solicitação para deletar vouchers + */ + public async post (req: Request, res: Response): Promise { + const { id, url, token } = req.body as deleteCtrlPanelVoucher + + if ( + id === undefined || + typeof url !== 'string' || + typeof token !== 'string' + ) { + console.log('Missing required fields') + return res.status(400).json({ + error: 'Bad Request: Missing required fields', + status: 400 + }) + } + + try { + const response = await axios.delete(`${url}/api/vouchers/${id}`, { + headers: { + Accept: 'application/json', + Authorization: `Bearer ${token}` + } + }) + + const { data, status } = response + + if (status === 200 && data.status === 'VALID') { + res.status(200).json({ + status: 200 + }) + } + } catch (err) { + console.log(err) + res.status(500).json({ + status: 500, + message: 'Houve um problema' + }) + } + } +} + +export const Root = new Voucher() diff --git a/src/express/routes/payment/create/card.ts b/src/express/routes/payment/create/card.ts new file mode 100644 index 00000000..8f0557e4 --- /dev/null +++ b/src/express/routes/payment/create/card.ts @@ -0,0 +1,79 @@ +import { type infoPayment } from '@/interfaces' +import { type Request, type Response } from 'express' +import { MercadoPagoConfig, Payment } from 'mercadopago' + +class CreatePayment { + /** + * post + */ + public async post (req: Request, res: Response): Promise> | undefined> { + const infoPayment = req.body as infoPayment + const { userName, userId, mpToken, price, method, ipn } = infoPayment + + if ( + userName === undefined || + userId === undefined || + mpToken === undefined || + price === undefined || + method === undefined || + ipn === undefined + ) { + return res.status(400).json({ + error: 'Bad Request: Missing required fields', + status: 400 + }) + } + + try { + const client = new MercadoPagoConfig({ accessToken: mpToken }) + + const date = new Date() + date.setDate(date.getDate() + 3) + const isoDate = date.toISOString() + + const payment = await new Payment(client).create({ + body: { + payer: { + first_name: userName, + last_name: userId, + email: `${userId}@gmail.com` + }, + additional_info: { + items: [ + { + id: userId, + title: 'Pagamento Via Discord', + description: `${userName} | R$${price.toFixed(2)}`, + unit_price: price, + quantity: 1, + currency_id: 'BRL' + } + ] + }, + payment_method_id: method, + installments: 1, + notification_url: ipn ?? undefined, + metadata: { + ...infoPayment, + price: Math.round(price * 100) / 100 + }, + date_of_expiration: isoDate + } + }) + const dateStr = (payment.date_of_expiration ?? isoDate) + const expirationDate = new Date(dateStr) + expirationDate.setMinutes(expirationDate.getMinutes()) + const unixTimestamp = Math.floor(expirationDate.getTime() / 1000) + + return res.status(200).json({ unixTimestamp, payment }) + } catch (err) { + console.log(err) + res.status(500).json({ + code: 500, + message: 'Houve um erro na solicitação.' + }) + } + } +} + +export const Root = new CreatePayment() diff --git a/src/express/routes/payment/create/pix.ts b/src/express/routes/payment/create/pix.ts new file mode 100644 index 00000000..75b8c025 --- /dev/null +++ b/src/express/routes/payment/create/pix.ts @@ -0,0 +1,58 @@ +import { type infoPayment } from '@/interfaces' +import { type Request, type Response } from 'express' +import { MercadoPagoConfig, Payment } from 'mercadopago' + +class CreatePixPayment { + /** + * Cria um pedido de pagamento para o Mercado Pago + */ + public async post (req: Request, res: Response): Promise> | undefined> { + const { userName, userId, mpToken, price } = req.body as infoPayment + + if ( + userName === undefined || + userId === undefined || + mpToken === undefined || + price === undefined + ) { + return res.status(400).json({ + error: 'Bad Request: Missing required fields', + status: 400 + }) + } + + try { + const date = new Date() + date.setDate(date.getDate() + 1) + const isoDate = date.toISOString() + const client = new MercadoPagoConfig({ accessToken: mpToken }) + const paymentData = await new Payment(client).create({ + body: { + payer: { + first_name: userName, + last_name: userId, + email: `${userId}@gmail.com` + }, + description: `Pagamento Via Discord | ${userName} | R$${(price).toFixed(2)}`, + transaction_amount: Math.round(price * 100) / 100, + payment_method_id: 'pix', + installments: 0 + } + }) + const dateStr = paymentData?.date_of_expiration ?? isoDate + const expirationDate = new Date(dateStr) + expirationDate.setMinutes(expirationDate.getMinutes()) + const unixTimestamp = Math.floor(expirationDate.getTime() / 1000) + + return res.status(200).json({ unixTimestamp, paymentData }) + } catch (err) { + console.log(err) + res.status(500).json({ + code: 500, + message: 'Houve um erro na solicitação.' + }) + } + } +} + +export const Root = new CreatePixPayment()