From fec184b81979ab6b0ebadd0d4b199a4d78c628d5 Mon Sep 17 00:00:00 2001 From: Claudemir Todo Bom Date: Fri, 12 Jul 2024 16:29:11 -0300 Subject: [PATCH] PaymentGatewayServices.ts: prettier --- .../PaymentGatewayServices.ts | 95 +++++++++++-------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/backend/src/services/PaymentGatewayServices/PaymentGatewayServices.ts b/backend/src/services/PaymentGatewayServices/PaymentGatewayServices.ts index 23d36a0b..aa78a23b 100644 --- a/backend/src/services/PaymentGatewayServices/PaymentGatewayServices.ts +++ b/backend/src/services/PaymentGatewayServices/PaymentGatewayServices.ts @@ -1,57 +1,75 @@ +import { Request, Response } from "express"; +import { Op } from "sequelize"; import AppError from "../../errors/AppError"; import GetSuperSettingService from "../SettingServices/GetSuperSettingService"; -import { efiCheckStatus, efiCreateSubscription, efiInitialize, efiWebhook } from "./EfiServices"; -import { Request, Response } from "express"; +import { + efiCheckStatus, + efiCreateSubscription, + efiInitialize, + efiWebhook +} from "./EfiServices"; import { owenCreateSubscription, owenWebhook } from "./OwenServices"; import Invoices from "../../models/Invoices"; import { getIO } from "../../libs/socket"; -import { Op } from "sequelize"; import Company from "../../models/Company"; export const payGatewayInitialize = async () => { - const paymentGateway = await GetSuperSettingService({ key: "_paymentGateway" }); + const paymentGateway = await GetSuperSettingService({ + key: "_paymentGateway" + }); if (paymentGateway === "efi") { return efiInitialize(); } throw new AppError("Unsupported payment gateway", 400); -} +}; -export const payGatewayCreateSubscription = async (req: Request, res: Response): Promise => { - const paymentGateway = await GetSuperSettingService({ key: "_paymentGateway" }); +export const payGatewayCreateSubscription = async ( + req: Request, + res: Response +): Promise => { + const paymentGateway = await GetSuperSettingService({ + key: "_paymentGateway" + }); switch (paymentGateway) { case "efi": { return efiCreateSubscription(req, res); - } + } case "owen": { return owenCreateSubscription(req, res); - } + } default: { throw new AppError("Unsupported payment gateway", 400); } - } -} + } +}; -export const payGatewayReceiveWebhook = async (req: Request, res: Response): Promise => { - const paymentGateway = await GetSuperSettingService({ key: "_paymentGateway" }); +export const payGatewayReceiveWebhook = async ( + req: Request, + res: Response +): Promise => { + const paymentGateway = await GetSuperSettingService({ + key: "_paymentGateway" + }); switch (paymentGateway) { case "efi": { return efiWebhook(req, res); - } + } case "owen": { return owenWebhook(req, res); - } + } default: { throw new AppError("Unsupported payment gateway", 400); } } -} +}; export const processInvoicePaid = async (invoice: Invoices) => { - const company = invoice.company || await Company.findByPk(invoice.companyId); - + const company = + invoice.company || (await Company.findByPk(invoice.companyId)); + if (company) { const expiresAt = new Date(company.dueDate); expiresAt.setDate(expiresAt.getDate() + 30); @@ -69,54 +87,51 @@ export const processInvoicePaid = async (invoice: Invoices) => { io.to(`company-${invoice.companyId}-mainchannel`) .to("super") .emit(`company-${invoice.companyId}-payment`, { - action: "CONCLUIDA", - company, - invoiceId: invoice.id, - }); + action: "CONCLUIDA", + company, + invoiceId: invoice.id + }); } -} +}; export const processInvoiceExpired = async (invoice: Invoices) => { const io = getIO(); - + await invoice.update({ txId: null, payGw: null, - payGwData: null, + payGwData: null }); - + await invoice.reload(); - + io.to(`company-${invoice.companyId}-mainchannel`) .to("super") .emit(`company-${invoice.companyId}-payment`, { - action: "EXPIRADA", - company: invoice.company || await Invoices.findByPk(invoice.companyId) , - invoiceId: invoice.id, - }); -} + action: "EXPIRADA", + company: invoice.company || (await Invoices.findByPk(invoice.companyId)), + invoiceId: invoice.id + }); +}; export const checkInvoicePayment = async (invoice: Invoices) => { if (invoice.payGw === "efi") { efiCheckStatus(invoice); } -} +}; export const checkOpenInvoices = async () => { const invoices = await Invoices.findAll({ where: { status: "open", txId: { - [Op.or]: [ - { [Op.not]: "" }, - { [Op.not]: null } - ], + [Op.or]: [{ [Op.not]: "" }, { [Op.not]: null }] } }, include: { model: Company, as: "company" } }); - - invoices.forEach( (invoice) => { + + invoices.forEach(invoice => { checkInvoicePayment(invoice); }); -} \ No newline at end of file +};