@sourceloop/ctrl-plane-subscription-service
Version:
Subscription management microservice for SaaS control plane.
143 lines (137 loc) • 4.21 kB
text/typescript
import {inject} from '@loopback/core';
import {Filter} from '@loopback/repository';
import {del, get, param, patch, post, requestBody} from '@loopback/rest';
import {
OPERATION_SECURITY_SPEC,
STATUS_CODE,
getModelSchemaRefSF,
} from '@sourceloop/core';
import {authenticate, STRATEGY} from 'loopback4-authentication';
import {authorize} from 'loopback4-authorization';
import {BillingCustomer} from '../models/billing-customer.model';
import {CustomerDto} from '../models/dto/customer-dto.model';
import {PermissionKey} from '../permissions';
import {BillingCustomerService} from '../services/billing-customer.service';
const basePath = '/billing-customer';
export class BillingCustomerController {
/**
* This TypeScript constructor injects the BillingCustomerService dependency using the @inject
* decorator.
* @param {BillingCustomerService} billingCustomerService - The `billingCustomerService` parameter is
* an instance of the `BillingCustomerService` class that is being injected into the constructor
* using dependency injection. This allows the class to have access to the functionality provided by
* the `BillingCustomerService` class within its methods.
*/
constructor(
('services.BillingCustomerService')
private readonly billingCustomerService: BillingCustomerService,
) {}
({
permissions: [PermissionKey.CreateBillingCustomer],
})
(STRATEGY.BEARER, {
passReqToCallback: true,
})
(basePath, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'BillingCustomer model instance',
content: {
'application/json': {
schema: getModelSchemaRefSF(CustomerDto, {
title: 'NewBillingCustomer',
}),
},
},
},
},
})
async create(
({
content: {
'application/json': {
schema: getModelSchemaRefSF(CustomerDto, {
title: 'NewCustomer',
exclude: ['id'],
}),
},
},
})
customerDto: Omit<CustomerDto, 'id'>,
.header.string('tenantId') tenantId: string,
): Promise<CustomerDto> {
return this.billingCustomerService.createCustomer(customerDto, tenantId);
}
({
permissions: [PermissionKey.GetBillingCustomer],
})
(STRATEGY.BEARER, {
passReqToCallback: true,
})
(basePath, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'BillingCustomer model ',
content: {
'application/json': {schema: getModelSchemaRefSF(CustomerDto)},
},
},
},
})
async getCustomer(
.filter(BillingCustomer) filter?: Filter<BillingCustomer>,
): Promise<{customerDetails: CustomerDto; info: BillingCustomer}> {
return this.billingCustomerService.getCustomer(filter);
}
({
permissions: [PermissionKey.UpdateBillingCustomer],
})
(STRATEGY.BEARER, {
passReqToCallback: true,
})
(`${basePath}/{tenantId}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'BillingCustomer PATCH success',
},
},
})
async updateById(
.path.string('tenantId') tenantId: string,
({
content: {
'application/json': {
schema: getModelSchemaRefSF(CustomerDto, {partial: true}),
},
},
})
customerDto: Partial<CustomerDto>,
): Promise<void> {
return this.billingCustomerService.updateCustomerByTenantId(
tenantId,
customerDto,
);
}
({
permissions: [PermissionKey.DeleteBillingCustomer],
})
(STRATEGY.BEARER, {
passReqToCallback: true,
})
(`${basePath}/{tenantId}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'BillingCustomer DELETE success',
},
},
})
async deleteById(
.path.string('tenantId') tenantId: string,
): Promise<void> {
return this.billingCustomerService.deleteCustomerByTenantId(tenantId);
}
}