Skip to content

Commit

Permalink
PaymentGatewayServices.ts: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jul 12, 2024
1 parent 723fa29 commit fec184b
Showing 1 changed file with 55 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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<Response> => {
const paymentGateway = await GetSuperSettingService({ key: "_paymentGateway" });
export const payGatewayCreateSubscription = async (
req: Request,
res: Response
): Promise<Response> => {
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<Response> => {
const paymentGateway = await GetSuperSettingService({ key: "_paymentGateway" });
export const payGatewayReceiveWebhook = async (
req: Request,
res: Response
): Promise<Response> => {
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);
Expand All @@ -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);
});
}
};

0 comments on commit fec184b

Please sign in to comment.