@sourceloop/ctrl-plane-subscription-service
Version:
Subscription management microservice for SaaS control plane.
88 lines • 4.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BillingCustomerService = void 0;
const tslib_1 = require("tslib");
const repository_1 = require("@loopback/repository");
const models_1 = require("../models");
const billing_customer_model_1 = require("../models/billing-customer.model");
const repositories_1 = require("../repositories");
const context_1 = require("@loopback/context");
const loopback4_billing_1 = require("loopback4-billing");
let BillingCustomerService = class BillingCustomerService {
constructor(billingCustomerRepo, invoiceRepo, billingProvider) {
this.billingCustomerRepo = billingCustomerRepo;
this.invoiceRepo = invoiceRepo;
this.billingProvider = billingProvider;
}
async createCustomer(customerDto, tenantId) {
const customer = await this.billingProvider.createCustomer(customerDto);
await this.billingCustomerRepo.create(new billing_customer_model_1.BillingCustomer({
tenantId,
customerId: customer.id,
}));
return this.mapToCustomerDto(customer);
}
mapToCustomerDto(customer) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
return new models_1.CustomerDto({
id: customer.id,
firstName: customer.firstName,
lastName: customer.lastName,
email: customer.email,
company: customer.company,
phone: customer.phone,
billingAddress: new models_1.AddressDto({
firstName: (_a = customer.billingAddress) === null || _a === void 0 ? void 0 : _a.firstName,
lastName: (_b = customer.billingAddress) === null || _b === void 0 ? void 0 : _b.lastName,
email: (_c = customer.billingAddress) === null || _c === void 0 ? void 0 : _c.email,
company: (_d = customer.billingAddress) === null || _d === void 0 ? void 0 : _d.company,
phone: (_e = customer.billingAddress) === null || _e === void 0 ? void 0 : _e.phone,
city: (_f = customer.billingAddress) === null || _f === void 0 ? void 0 : _f.city,
state: (_g = customer.billingAddress) === null || _g === void 0 ? void 0 : _g.state,
zip: (_h = customer.billingAddress) === null || _h === void 0 ? void 0 : _h.zip,
country: (_j = customer.billingAddress) === null || _j === void 0 ? void 0 : _j.country,
}),
});
}
async getCustomer(filter) {
const customers = await this.billingCustomerRepo.find(filter);
if (customers.length === 0) {
throw new Error('Customer is not present');
}
const customer = await this.billingProvider.getCustomers(customers[0].customerId);
return {
customerDetails: this.mapToCustomerDto(customer),
info: customers[0],
};
}
async updateCustomerByTenantId(tenantId, customerDto) {
const customers = await this.billingCustomerRepo.find({
where: { tenantId },
});
if (customers.length === 0) {
throw new Error(`Customer with tenantId ${tenantId} is not present`);
}
await this.billingProvider.updateCustomerById(customers[0].customerId, customerDto);
}
async deleteCustomerByTenantId(tenantId) {
const customer = await this.billingCustomerRepo.find({
where: { tenantId },
});
if (customer.length === 0) {
throw new Error('Customer with tenantId is not present');
}
await this.billingProvider.deleteCustomer(customer[0].customerId);
await this.invoiceRepo.deleteAll({ billingCustomerId: customer[0].id });
await this.billingCustomerRepo.deleteById(customer[0].id);
}
};
exports.BillingCustomerService = BillingCustomerService;
exports.BillingCustomerService = BillingCustomerService = 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])
], BillingCustomerService);
//# sourceMappingURL=billing-customer.service.js.map