Skip to content

Commit

Permalink
Revert "🗑️ Delete unused Routers"
Browse files Browse the repository at this point in the history
This reverts commit 4d87739.
  • Loading branch information
Ashu11-A committed Feb 20, 2024
1 parent cf894af commit 2855fac
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/express/routes/ctrlpanel/index.ts
Original file line number Diff line number Diff line change
@@ -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<Response<any, Record<string, any>>> {
try {
const guilds = client.guilds.cache
const dataCtrlPanel: Record<string, any> = {}

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()
73 changes: 73 additions & 0 deletions src/express/routes/ctrlpanel/voucher/create.ts
Original file line number Diff line number Diff line change
@@ -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<Response<any, Record<string, any>> | 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()
54 changes: 54 additions & 0 deletions src/express/routes/ctrlpanel/voucher/delete.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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()
79 changes: 79 additions & 0 deletions src/express/routes/payment/create/card.ts
Original file line number Diff line number Diff line change
@@ -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<Response<any, Record<string, any>> | 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()
58 changes: 58 additions & 0 deletions src/express/routes/payment/create/pix.ts
Original file line number Diff line number Diff line change
@@ -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<Response<any, Record<string, any>> | 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()

0 comments on commit 2855fac

Please sign in to comment.