@sourceloop/ctrl-plane-subscription-service
Version:
Subscription management microservice for SaaS control plane.
215 lines • 10.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BillingCustomerController = 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 billing_customer_model_1 = require("../models/billing-customer.model");
const customer_dto_model_1 = require("../models/dto/customer-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-customer';
let BillingCustomerController = class BillingCustomerController {
constructor(billingCustomerRepository, invoiceRepository, billingProvider) {
this.billingCustomerRepository = billingCustomerRepository;
this.invoiceRepository = invoiceRepository;
this.billingProvider = billingProvider;
}
async create(customerDto, tenantId) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
const customer = await this.billingProvider.createCustomer(customerDto);
await this.billingCustomerRepository.create(new billing_customer_model_1.BillingCustomer({
tenantId,
customerId: customer.id,
}));
return new customer_dto_model_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) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
const customers = await this.billingCustomerRepository.find(filter);
if (customers.length === 0) {
throw new Error('Customer is not present');
}
const customer = await this.billingProvider.getCustomers(customers[0].customerId);
return {
customerDetails: new customer_dto_model_1.CustomerDto({
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,
}),
}),
info: customers[0],
};
}
async updateById(tenantId, customerDto) {
const customers = await this.billingCustomerRepository.find({
where: { tenantId: tenantId },
});
if (customers.length === 0) {
throw new Error(`Customer with tenantId ${tenantId} is not present`);
}
await this.billingProvider.updateCustomerById(customers[0].customerId, customerDto);
}
async deleteById(tenantId) {
const customer = await this.billingCustomerRepository.find({
where: { tenantId: tenantId },
});
if (customer.length === 0) {
throw new Error(' Customer with tenantId is not present');
}
await this.billingProvider.deleteCustomer(customer[0].customerId);
await this.invoiceRepository.deleteAll({ billingCustomerId: customer[0].id });
await this.billingCustomerRepository.deleteById(customer[0].id);
}
};
exports.BillingCustomerController = BillingCustomerController;
tslib_1.__decorate([
(0, loopback4_authorization_1.authorize)({
permissions: [permissions_1.PermissionKey.CreateBillingCustomer],
}),
(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: 'BillingCustomer model instance',
content: {
'application/json': {
schema: (0, rest_1.getModelSchemaRef)(customer_dto_model_1.CustomerDto, {
title: 'NewBillingCustomer',
}),
},
},
},
},
}),
tslib_1.__param(0, (0, rest_1.requestBody)({
content: {
'application/json': {
schema: (0, rest_1.getModelSchemaRef)(customer_dto_model_1.CustomerDto, {
title: 'NewCustomer',
exclude: ['id'],
}),
},
},
})),
tslib_1.__param(1, rest_1.param.header.string('tenantId')),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [Object, String]),
tslib_1.__metadata("design:returntype", Promise)
], BillingCustomerController.prototype, "create", null);
tslib_1.__decorate([
(0, loopback4_authorization_1.authorize)({
permissions: [permissions_1.PermissionKey.GetBillingCustomer],
}),
(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: 'BillingCustomer model ',
content: { 'application/json': { schema: (0, rest_1.getModelSchemaRef)(customer_dto_model_1.CustomerDto) } },
},
},
}),
tslib_1.__param(0, rest_1.param.filter(billing_customer_model_1.BillingCustomer)),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [Object]),
tslib_1.__metadata("design:returntype", Promise)
], BillingCustomerController.prototype, "getCustomer", null);
tslib_1.__decorate([
(0, loopback4_authorization_1.authorize)({
permissions: [permissions_1.PermissionKey.UpdateBillingCustomer],
}),
(0, loopback4_authentication_1.authenticate)("bearer" /* STRATEGY.BEARER */, {
passReqToCallback: true,
}),
(0, rest_1.patch)(`${basePath}/{tenantId}`, {
security: core_2.OPERATION_SECURITY_SPEC,
responses: {
[204 /* STATUS_CODE.NO_CONTENT */]: {
description: 'BillingCustomer PATCH success',
},
},
}),
tslib_1.__param(0, rest_1.param.path.string('tenantId')),
tslib_1.__param(1, (0, rest_1.requestBody)({
content: {
'application/json': {
schema: (0, rest_1.getModelSchemaRef)(customer_dto_model_1.CustomerDto, { partial: true }),
},
},
})),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [String, Object]),
tslib_1.__metadata("design:returntype", Promise)
], BillingCustomerController.prototype, "updateById", null);
tslib_1.__decorate([
(0, loopback4_authorization_1.authorize)({
permissions: [permissions_1.PermissionKey.DeleteBillingCustomer],
}),
(0, loopback4_authentication_1.authenticate)("bearer" /* STRATEGY.BEARER */, {
passReqToCallback: true,
}),
(0, rest_1.del)(`${basePath}/{tenantId}`, {
security: core_2.OPERATION_SECURITY_SPEC,
responses: {
[204 /* STATUS_CODE.NO_CONTENT */]: {
description: 'BillingCustomer DELETE success',
},
},
}),
tslib_1.__param(0, rest_1.param.path.string('tenantId')),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [String]),
tslib_1.__metadata("design:returntype", Promise)
], BillingCustomerController.prototype, "deleteById", null);
exports.BillingCustomerController = BillingCustomerController = 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])
], BillingCustomerController);
//# sourceMappingURL=billing-customer.controller.js.map