feat: Clean up payment links endpoints

This commit is contained in:
Ahmed Bouhuolia
2024-09-25 19:37:09 +02:00
parent 323b95de7b
commit 1cc71eb368
14 changed files with 183 additions and 132 deletions

View File

@@ -1,13 +1,13 @@
import { Inject, Service } from 'typedi';
import { Router, Request, Response, NextFunction } from 'express';
import { body, param } from 'express-validator';
import { param } from 'express-validator';
import BaseController from '@/api/controllers/BaseController';
import { GetInvoicePaymentLinkMetadata } from '@/services/Sales/Invoices/GetInvoicePaymentLinkMetadata';
import { PaymentLinksApplication } from '@/services/PaymentLinks/PaymentLinksApplication';
@Service()
export class PublicSharableLinkController extends BaseController {
@Inject()
private getSharableLinkMetaService: GetInvoicePaymentLinkMetadata;
private paymentLinkApp: PaymentLinksApplication;
/**
* Router constructor.
@@ -16,12 +16,18 @@ export class PublicSharableLinkController extends BaseController {
const router = Router();
router.get(
'/sharable-links/meta/invoice/:linkId',
[param('linkId').exists()],
'/:paymentLinkId/invoice',
[param('paymentLinkId').exists()],
this.validationResult,
this.getPaymentLinkPublicMeta.bind(this),
this.validationResult
);
router.post(
'/:paymentLinkId/stripe_checkout_session',
[param('paymentLinkId').exists()],
this.validationResult,
this.createInvoicePaymentLinkCheckoutSession.bind(this)
);
return router;
}
@@ -33,19 +39,45 @@ export class PublicSharableLinkController extends BaseController {
* @returns
*/
public async getPaymentLinkPublicMeta(
req: Request,
req: Request<{ paymentLinkId: string }>,
res: Response,
next: NextFunction
) {
const { linkId } = req.params;
const { paymentLinkId } = req.params;
try {
const data =
await this.getSharableLinkMetaService.getInvoicePaymentLinkMeta(linkId);
const data = await this.paymentLinkApp.getInvoicePaymentLink(
paymentLinkId
);
return res.status(200).send({ data });
} catch (error) {
next(error);
}
}
/**
* Creates a Stripe checkout session for the given payment link id.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
* @returns {Promise<Response|void>}
*/
public async createInvoicePaymentLinkCheckoutSession(
req: Request<{ paymentLinkId: string }>,
res: Response,
next: NextFunction
) {
const { paymentLinkId } = req.params;
try {
const session =
await this.paymentLinkApp.createInvoicePaymentCheckoutSession(
paymentLinkId
);
return res.status(200).send(session);
} catch (error) {
next(error);
}
}
}

View File

@@ -27,10 +27,6 @@ export class StripeIntegrationController extends BaseController {
this.validationResult,
asyncMiddleware(this.createAccountLink.bind(this))
);
router.post(
'/:linkId/create_checkout_session',
this.createCheckoutSession.bind(this)
);
return router;
}
@@ -75,33 +71,6 @@ export class StripeIntegrationController extends BaseController {
}
}
/**
* Creates a Stripe checkout session for the given payment link id.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
* @returns {Promise<Response|void>}
*/
public async createCheckoutSession(
req: Request<{ linkId: number }>,
res: Response,
next: NextFunction
) {
const { linkId } = req.params;
const { tenantId } = req;
try {
const session =
await this.stripePaymentApp.createSaleInvoiceCheckoutSession(
tenantId,
linkId
);
return res.status(200).send(session);
} catch (error) {
next(error);
}
}
/**
* Creates a new Stripe account.
* @param {Request} req - The Express request object.

View File

@@ -87,7 +87,10 @@ export default () => {
app.use('/account', Container.get(Account).router());
app.use('/webhooks', Container.get(Webhooks).router());
app.use('/demo', Container.get(OneClickDemoController).router());
app.use(Container.get(PublicSharableLinkController).router());
app.use(
'/payment-links',
Container.get(PublicSharableLinkController).router()
);
// - Dashboard routes.
// ---------------------------

View File

@@ -50,7 +50,8 @@ export const injectI18nUtils = (req) => {
export const initalizeTenantServices = async (tenantId: number) => {
const tenant = await Tenant.query()
.findById(tenantId)
.withGraphFetched('metadata');
.withGraphFetched('metadata')
.throwIfNotFound();
const tenantServices = Container.get(TenancyService);
const tenantsManager = Container.get(TenantsManagerService);