@sourceloop/ctrl-plane-subscription-service
Version:
Subscription management microservice for SaaS control plane.
97 lines • 6.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BillingInvoiceService = void 0;
const tslib_1 = require("tslib");
const context_1 = require("@loopback/context");
const models_1 = require("../models");
const loopback4_billing_1 = require("loopback4-billing");
const repository_1 = require("@loopback/repository");
const repositories_1 = require("../repositories");
let BillingInvoiceService = class BillingInvoiceService {
constructor(billingCustomerRepository, invoiceRepository, billingProvider) {
this.billingCustomerRepository = billingCustomerRepository;
this.invoiceRepository = invoiceRepository;
this.billingProvider = billingProvider;
}
async createInvoice(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 models_1.InvoiceDto({
id: invoiceInfo.id,
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 models_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 applyPayment(invoiceId, transactionDto) {
const invoiceInfo = await this.invoiceRepository.findById(invoiceId);
await this.billingProvider.applyPaymentSourceForInvoice(invoiceInfo.invoiceId, transactionDto);
}
async deleteInvoice(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.BillingInvoiceService = BillingInvoiceService;
exports.BillingInvoiceService = BillingInvoiceService = tslib_1.__decorate([
(0, context_1.injectable)({ scope: context_1.BindingScope.TRANSIENT }),
tslib_1.__param(0, (0, repository_1.repository)(repositories_1.BillingCustomerRepository)),
tslib_1.__param(1, (0, repository_1.repository)(repositories_1.InvoiceRepository)),
tslib_1.__param(2, (0, context_1.inject)(loopback4_billing_1.BillingComponentBindings.BillingProvider)),
tslib_1.__metadata("design:paramtypes", [repositories_1.BillingCustomerRepository,
repositories_1.InvoiceRepository, Object])
], BillingInvoiceService);
//# sourceMappingURL=billing-invoice.service.js.map