@sourceloop/ctrl-plane-subscription-service
Version:
Subscription management microservice for SaaS control plane.
139 lines (132 loc) • 4.13 kB
text/typescript
import {BillingComponentBindings, IService} from 'loopback4-billing';
import {inject} from '@loopback/core';
import {repository} from '@loopback/repository';
import {
del,
get,
getModelSchemaRef,
param,
post,
requestBody,
} from '@loopback/rest';
import {OPERATION_SECURITY_SPEC, STATUS_CODE} from '@sourceloop/core';
import {authenticate, STRATEGY} from 'loopback4-authentication';
import {authorize} from 'loopback4-authorization';
import {PaymentSourceDto} from '../models/dto/payment-dto.model';
import {PermissionKey} from '../permissions';
import {InvoiceRepository} from '../repositories';
import {BillingCustomerRepository} from '../repositories/billing-customer.repository';
const basePath = '/billing-payment-source';
export class BillingPaymentSourceController {
constructor(
(BillingCustomerRepository)
public billingCustomerRepository: BillingCustomerRepository,
(InvoiceRepository)
public invoiceRepository: InvoiceRepository,
(BillingComponentBindings.BillingProvider)
private readonly billingProvider: IService,
) {}
({
permissions: [PermissionKey.CreateBillingPaymentSource],
})
(STRATEGY.BEARER, {
passReqToCallback: true,
})
(basePath, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'Payment model instance',
content: {
'application/json': {schema: getModelSchemaRef(PaymentSourceDto)},
},
},
},
})
async create(
({
content: {
'application/json': {
schema: getModelSchemaRef(PaymentSourceDto, {
title: 'NewPaymentSource',
exclude: ['id'],
}),
},
},
})
paymentSourceDto: PaymentSourceDto,
): Promise<PaymentSourceDto> {
const customer = await this.billingCustomerRepository.find({
where: {customerId: paymentSourceDto.customerId},
});
if (customer.length === 0) {
throw new Error(' Customer with tenantId is not present');
}
const paymentSource =
await this.billingProvider.createPaymentSource(paymentSourceDto);
await this.billingCustomerRepository.updateById(customer[0].id, {
paymentSourceId: paymentSource.id,
});
return new PaymentSourceDto({
id: paymentSource.id,
customerId: paymentSource.customerId,
card: paymentSource.card,
});
}
({
permissions: [PermissionKey.GetBillingPaymentSource],
})
(STRATEGY.BEARER, {
passReqToCallback: true,
})
(basePath, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'get payment source',
content: {
'application/json': {schema: getModelSchemaRef(PaymentSourceDto)},
},
},
},
})
async getPaymentSource(
.path.string('paymentSourceId') paymentSourceId: string,
): Promise<PaymentSourceDto> {
const paymentSource =
await this.billingProvider.retrievePaymentSource(paymentSourceId);
return new PaymentSourceDto({
id: paymentSource.id,
customerId: paymentSource.customerId,
card: paymentSource.card,
});
}
({
permissions: [PermissionKey.DeleteBillingPaymentSource],
})
(STRATEGY.BEARER, {
passReqToCallback: true,
})
(`${basePath}/{paymentSourceId}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'Billing Payment Source DELETE success',
},
},
})
async deleteById(
.path.string('paymentSourceId') paymentSourceId: string,
): Promise<void> {
const customer = await this.billingCustomerRepository.find({
where: {paymentSourceId: paymentSourceId},
});
if (customer.length === 0) {
throw new Error(' Customer with tenantId is not present');
}
await this.billingProvider.deletePaymentSource(paymentSourceId);
await this.billingCustomerRepository.updateById(customer[0].id, {
paymentSourceId: undefined,
});
}
}