@sourceloop/ctrl-plane-subscription-service
Version:
Subscription management microservice for SaaS control plane.
232 lines • 12.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BillingInvoiceController = 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 models_1 = require("../models");
const invoice_dto_model_1 = require("../models/dto/invoice-dto.model");
const transaction_dto_model_1 = require("../models/dto/transaction-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-invoice';
let BillingInvoiceController = class BillingInvoiceController {
constructor(billingCustomerRepository, invoiceRepository, billingProvider) {
this.billingCustomerRepository = billingCustomerRepository;
this.invoiceRepository = invoiceRepository;
this.billingProvider = billingProvider;
}
async create(invoiceDto) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
const customer = await this.billingCustomerRepository.find({
where: { customerId: invoiceDto.customerId },
});
if (customer.length === 0) {
throw new Error(' Customer with tenantId is not present');
}
const invoice = await this.billingProvider.createInvoice(invoiceDto);
const charges = (_a = invoice.charges) === null || _a === void 0 ? void 0 : _a.map(charge => new models_1.ChargeDto({ amount: charge.amount, description: charge.description }));
const invoiceInfo = await this.invoiceRepository.create({
invoiceId: invoice.id,
invoiceStatus: invoice.status,
billingCustomerId: customer[0].id,
});
return new invoice_dto_model_1.InvoiceDto({
id: invoiceInfo.id, // passed the id of invoice info created in our db, to setup relation between subscription and invoice
customerId: invoice.customerId,
charges: charges,
status: invoice.status,
shippingAddress: new models_1.AddressDto({
firstName: (_c = (_b = invoice.shippingAddress) === null || _b === void 0 ? void 0 : _b.firstName) !== null && _c !== void 0 ? _c : '',
lastName: (_e = (_d = invoice.shippingAddress) === null || _d === void 0 ? void 0 : _d.lastName) !== null && _e !== void 0 ? _e : '',
email: (_g = (_f = invoice.shippingAddress) === null || _f === void 0 ? void 0 : _f.email) !== null && _g !== void 0 ? _g : '',
company: (_h = invoice.shippingAddress) === null || _h === void 0 ? void 0 : _h.company,
phone: (_j = invoice.shippingAddress) === null || _j === void 0 ? void 0 : _j.phone,
city: (_l = (_k = invoice.shippingAddress) === null || _k === void 0 ? void 0 : _k.city) !== null && _l !== void 0 ? _l : '',
state: (_o = (_m = invoice.shippingAddress) === null || _m === void 0 ? void 0 : _m.state) !== null && _o !== void 0 ? _o : '',
zip: (_q = (_p = invoice.shippingAddress) === null || _p === void 0 ? void 0 : _p.zip) !== null && _q !== void 0 ? _q : '',
country: (_s = (_r = invoice.shippingAddress) === null || _r === void 0 ? void 0 : _r.country) !== null && _s !== void 0 ? _s : '',
}),
options: invoice.options,
currencyCode: invoice.currencyCode,
});
}
async getInvoice(invoiceId) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
const invoice = await this.billingProvider.retrieveInvoice(invoiceId);
const charges = (_a = invoice.charges) === null || _a === void 0 ? void 0 : _a.map(charge => new models_1.ChargeDto({ amount: charge.amount, description: charge.description }));
return new invoice_dto_model_1.InvoiceDto({
customerId: invoice.customerId,
charges: charges,
status: invoice.status,
shippingAddress: new models_1.AddressDto({
firstName: (_c = (_b = invoice.shippingAddress) === null || _b === void 0 ? void 0 : _b.firstName) !== null && _c !== void 0 ? _c : '',
lastName: (_e = (_d = invoice.shippingAddress) === null || _d === void 0 ? void 0 : _d.lastName) !== null && _e !== void 0 ? _e : '',
email: (_g = (_f = invoice.shippingAddress) === null || _f === void 0 ? void 0 : _f.email) !== null && _g !== void 0 ? _g : '',
company: (_h = invoice.shippingAddress) === null || _h === void 0 ? void 0 : _h.company,
phone: (_j = invoice.shippingAddress) === null || _j === void 0 ? void 0 : _j.phone,
city: (_l = (_k = invoice.shippingAddress) === null || _k === void 0 ? void 0 : _k.city) !== null && _l !== void 0 ? _l : '',
state: (_o = (_m = invoice.shippingAddress) === null || _m === void 0 ? void 0 : _m.state) !== null && _o !== void 0 ? _o : '',
zip: (_q = (_p = invoice.shippingAddress) === null || _p === void 0 ? void 0 : _p.zip) !== null && _q !== void 0 ? _q : '',
country: (_s = (_r = invoice.shippingAddress) === null || _r === void 0 ? void 0 : _r.country) !== null && _s !== void 0 ? _s : '',
}),
options: invoice.options,
});
}
async updateById(invoiceId, invoiceDto) {
await this.billingProvider.updateInvoice(invoiceId, invoiceDto);
}
async applyPaymentForInvoice(invoiceId, transactionDto) {
const invoiceInfo = await this.invoiceRepository.findById(invoiceId);
await this.billingProvider.applyPaymentSourceForInvoice(invoiceInfo.invoiceId, transactionDto);
}
async deleteById(invoiceId) {
const invoice = await this.invoiceRepository.find({
where: { invoiceId: invoiceId },
});
if (invoice.length === 0)
throw new Error(' invoice with invoiceId is not present');
await this.billingProvider.deleteInvoice(invoiceId);
await this.invoiceRepository.deleteById(invoice[0].id);
}
};
exports.BillingInvoiceController = BillingInvoiceController;
tslib_1.__decorate([
(0, loopback4_authorization_1.authorize)({
permissions: [permissions_1.PermissionKey.CreateBillingInvoice],
}),
(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: 'invoice model instance',
content: { 'application/json': { schema: (0, rest_1.getModelSchemaRef)(invoice_dto_model_1.InvoiceDto) } },
},
},
}),
tslib_1.__param(0, (0, rest_1.requestBody)({
content: {
'application/json': {
schema: (0, rest_1.getModelSchemaRef)(invoice_dto_model_1.InvoiceDto, {
title: 'newInvoice',
exclude: ['id', 'status'],
}),
},
},
})),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [Object]),
tslib_1.__metadata("design:returntype", Promise)
], BillingInvoiceController.prototype, "create", null);
tslib_1.__decorate([
(0, loopback4_authorization_1.authorize)({
permissions: [permissions_1.PermissionKey.GetBillingInvoice],
}),
(0, loopback4_authentication_1.authenticate)("bearer" /* STRATEGY.BEARER */, {
passReqToCallback: true,
}),
(0, rest_1.get)(`${basePath}/{invoiceId}`, {
security: core_2.OPERATION_SECURITY_SPEC,
responses: {
[200 /* STATUS_CODE.OK */]: {
description: 'get invoice',
content: { 'application/json': { schema: (0, rest_1.getModelSchemaRef)(invoice_dto_model_1.InvoiceDto) } },
},
},
}),
tslib_1.__param(0, rest_1.param.path.string('invoiceId')),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [String]),
tslib_1.__metadata("design:returntype", Promise)
], BillingInvoiceController.prototype, "getInvoice", null);
tslib_1.__decorate([
(0, loopback4_authorization_1.authorize)({
permissions: [permissions_1.PermissionKey.UpdateBillingInvoice],
}),
(0, loopback4_authentication_1.authenticate)("bearer" /* STRATEGY.BEARER */, {
passReqToCallback: true,
}),
(0, rest_1.patch)(`${basePath}/{invoiceId}`, {
security: core_2.OPERATION_SECURITY_SPEC,
responses: {
[204 /* STATUS_CODE.NO_CONTENT */]: {
description: 'Billing Invoice PATCH success',
},
},
}),
tslib_1.__param(0, rest_1.param.path.string('invoiceId')),
tslib_1.__param(1, (0, rest_1.requestBody)({
content: {
'application/json': {
schema: (0, rest_1.getModelSchemaRef)(invoice_dto_model_1.InvoiceDto, { partial: true }),
},
},
})),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [String, Object]),
tslib_1.__metadata("design:returntype", Promise)
], BillingInvoiceController.prototype, "updateById", null);
tslib_1.__decorate([
(0, loopback4_authorization_1.authorize)({
permissions: [permissions_1.PermissionKey.CreateBillingInvoice],
}),
(0, loopback4_authentication_1.authenticate)("bearer" /* STRATEGY.BEARER */, {
passReqToCallback: true,
}),
(0, rest_1.post)(`${basePath}/{invoiceId}/payments`, {
security: core_2.OPERATION_SECURITY_SPEC,
responses: {
[200 /* STATUS_CODE.OK */]: {
description: 'invoice model instance',
},
},
}),
tslib_1.__param(0, rest_1.param.path.string('invoiceId')),
tslib_1.__param(1, (0, rest_1.requestBody)({
content: {
'application/json': {
schema: (0, rest_1.getModelSchemaRef)(transaction_dto_model_1.TransactionDto, { partial: true }),
},
},
})),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [String, transaction_dto_model_1.TransactionDto]),
tslib_1.__metadata("design:returntype", Promise)
], BillingInvoiceController.prototype, "applyPaymentForInvoice", 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}/{invoiceId}`, {
security: core_2.OPERATION_SECURITY_SPEC,
responses: {
[204 /* STATUS_CODE.NO_CONTENT */]: {
description: 'Billing Invoice DELETE success',
},
},
}),
tslib_1.__param(0, rest_1.param.path.string('invoiceId')),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [String]),
tslib_1.__metadata("design:returntype", Promise)
], BillingInvoiceController.prototype, "deleteById", null);
exports.BillingInvoiceController = BillingInvoiceController = 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])
], BillingInvoiceController);
//# sourceMappingURL=billing-invoice.controller.js.map