UNPKG

@sourceloop/ctrl-plane-subscription-service

Version:

Subscription management microservice for SaaS control plane.

144 lines 6.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BillingPaymentSourceController = void 0; const tslib_1 = require("tslib"); const loopback4_billing_1 = require("loopback4-billing"); const core_1 = require("@loopback/core"); const repository_1 = require("@loopback/repository"); const rest_1 = require("@loopback/rest"); const core_2 = require("@sourceloop/core"); const loopback4_authentication_1 = require("loopback4-authentication"); const loopback4_authorization_1 = require("loopback4-authorization"); const payment_dto_model_1 = require("../models/dto/payment-dto.model"); const permissions_1 = require("../permissions"); const repositories_1 = require("../repositories"); const billing_customer_repository_1 = require("../repositories/billing-customer.repository"); const basePath = '/billing-payment-source'; let BillingPaymentSourceController = class BillingPaymentSourceController { constructor(billingCustomerRepository, invoiceRepository, billingProvider) { this.billingCustomerRepository = billingCustomerRepository; this.invoiceRepository = invoiceRepository; this.billingProvider = billingProvider; } async create(paymentSourceDto) { const customer = await this.billingCustomerRepository.find({ where: { customerId: paymentSourceDto.customerId }, }); if (customer.length === 0) { throw new Error(' Customer with tenantId is not present'); } const paymentSource = await this.billingProvider.createPaymentSource(paymentSourceDto); await this.billingCustomerRepository.updateById(customer[0].id, { paymentSourceId: paymentSource.id, }); return new payment_dto_model_1.PaymentSourceDto({ id: paymentSource.id, customerId: paymentSource.customerId, card: paymentSource.card, }); } async getPaymentSource(paymentSourceId) { const paymentSource = await this.billingProvider.retrievePaymentSource(paymentSourceId); return new payment_dto_model_1.PaymentSourceDto({ id: paymentSource.id, customerId: paymentSource.customerId, card: paymentSource.card, }); } async deleteById(paymentSourceId) { const customer = await this.billingCustomerRepository.find({ where: { paymentSourceId: paymentSourceId }, }); if (customer.length === 0) { throw new Error(' Customer with tenantId is not present'); } await this.billingProvider.deletePaymentSource(paymentSourceId); await this.billingCustomerRepository.updateById(customer[0].id, { paymentSourceId: undefined, }); } }; exports.BillingPaymentSourceController = BillingPaymentSourceController; tslib_1.__decorate([ (0, loopback4_authorization_1.authorize)({ permissions: [permissions_1.PermissionKey.CreateBillingPaymentSource], }), (0, loopback4_authentication_1.authenticate)("bearer" /* STRATEGY.BEARER */, { passReqToCallback: true, }), (0, rest_1.post)(basePath, { security: core_2.OPERATION_SECURITY_SPEC, responses: { [200 /* STATUS_CODE.OK */]: { description: 'Payment model instance', content: { 'application/json': { schema: (0, rest_1.getModelSchemaRef)(payment_dto_model_1.PaymentSourceDto) }, }, }, }, }), tslib_1.__param(0, (0, rest_1.requestBody)({ content: { 'application/json': { schema: (0, rest_1.getModelSchemaRef)(payment_dto_model_1.PaymentSourceDto, { title: 'NewPaymentSource', exclude: ['id'], }), }, }, })), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [payment_dto_model_1.PaymentSourceDto]), tslib_1.__metadata("design:returntype", Promise) ], BillingPaymentSourceController.prototype, "create", null); tslib_1.__decorate([ (0, loopback4_authorization_1.authorize)({ permissions: [permissions_1.PermissionKey.GetBillingPaymentSource], }), (0, loopback4_authentication_1.authenticate)("bearer" /* STRATEGY.BEARER */, { passReqToCallback: true, }), (0, rest_1.get)(basePath, { security: core_2.OPERATION_SECURITY_SPEC, responses: { [200 /* STATUS_CODE.OK */]: { description: 'get payment source', content: { 'application/json': { schema: (0, rest_1.getModelSchemaRef)(payment_dto_model_1.PaymentSourceDto) }, }, }, }, }), tslib_1.__param(0, rest_1.param.path.string('paymentSourceId')), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String]), tslib_1.__metadata("design:returntype", Promise) ], BillingPaymentSourceController.prototype, "getPaymentSource", null); tslib_1.__decorate([ (0, loopback4_authorization_1.authorize)({ permissions: [permissions_1.PermissionKey.DeleteBillingPaymentSource], }), (0, loopback4_authentication_1.authenticate)("bearer" /* STRATEGY.BEARER */, { passReqToCallback: true, }), (0, rest_1.del)(`${basePath}/{paymentSourceId}`, { security: core_2.OPERATION_SECURITY_SPEC, responses: { [204 /* STATUS_CODE.NO_CONTENT */]: { description: 'Billing Payment Source DELETE success', }, }, }), tslib_1.__param(0, rest_1.param.path.string('paymentSourceId')), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String]), tslib_1.__metadata("design:returntype", Promise) ], BillingPaymentSourceController.prototype, "deleteById", null); exports.BillingPaymentSourceController = BillingPaymentSourceController = tslib_1.__decorate([ tslib_1.__param(0, (0, repository_1.repository)(billing_customer_repository_1.BillingCustomerRepository)), tslib_1.__param(1, (0, repository_1.repository)(repositories_1.InvoiceRepository)), tslib_1.__param(2, (0, core_1.inject)(loopback4_billing_1.BillingComponentBindings.BillingProvider)), tslib_1.__metadata("design:paramtypes", [billing_customer_repository_1.BillingCustomerRepository, repositories_1.InvoiceRepository, Object]) ], BillingPaymentSourceController); //# sourceMappingURL=billing-payment-source.controller.js.map