11import { BillingComponentBindings , IService } from 'loopback4-billing' ;
22import { inject } from '@loopback/core' ;
3- import { repository } from '@loopback/repository' ;
43import {
54 del ,
65 get ,
@@ -13,22 +12,18 @@ import {
1312import { OPERATION_SECURITY_SPEC , STATUS_CODE } from '@sourceloop/core' ;
1413import { authenticate , STRATEGY } from 'loopback4-authentication' ;
1514import { authorize } from 'loopback4-authorization' ;
16- import { AddressDto , ChargeDto } from '../models' ;
1715import { InvoiceDto } from '../models/dto/invoice-dto.model' ;
1816import { TransactionDto } from '../models/dto/transaction-dto.model' ;
1917import { PermissionKey } from '../permissions' ;
20- import { InvoiceRepository } from '../repositories' ;
21- import { BillingCustomerRepository } from '../repositories/billing-customer.repository' ;
18+ import { BillingInvoiceService } from '../services/billing-invoice.service' ;
2219
2320const basePath = '/billing-invoice' ;
2421export class BillingInvoiceController {
2522 constructor (
26- @repository ( BillingCustomerRepository )
27- public billingCustomerRepository : BillingCustomerRepository ,
28- @repository ( InvoiceRepository )
29- public invoiceRepository : InvoiceRepository ,
3023 @inject ( BillingComponentBindings . BillingProvider )
3124 private readonly billingProvider : IService ,
25+ @inject ( 'services.BillingInvoiceService' )
26+ private readonly billingInvoiceService : BillingInvoiceService ,
3227 ) { }
3328
3429 @authorize ( {
@@ -59,43 +54,7 @@ export class BillingInvoiceController {
5954 } )
6055 invoiceDto : Omit < InvoiceDto , 'id' | 'status' > ,
6156 ) : Promise < InvoiceDto > {
62- const customer = await this . billingCustomerRepository . find ( {
63- where : { customerId : invoiceDto . customerId } ,
64- } ) ;
65-
66- if ( customer . length === 0 ) {
67- throw new Error ( ' Customer with tenantId is not present' ) ;
68- }
69- const invoice = await this . billingProvider . createInvoice ( invoiceDto ) ;
70- const charges = invoice . charges ?. map (
71- charge =>
72- new ChargeDto ( { amount : charge . amount , description : charge . description } ) ,
73- ) ;
74-
75- const invoiceInfo = await this . invoiceRepository . create ( {
76- invoiceId : invoice . id ,
77- invoiceStatus : invoice . status ,
78- billingCustomerId : customer [ 0 ] . id ,
79- } ) ;
80- return new InvoiceDto ( {
81- id : invoiceInfo . id , // passed the id of invoice info created in our db, to setup relation between subscription and invoice
82- customerId : invoice . customerId ,
83- charges : charges ,
84- status : invoice . status ,
85- shippingAddress : new AddressDto ( {
86- firstName : invoice . shippingAddress ?. firstName ?? '' ,
87- lastName : invoice . shippingAddress ?. lastName ?? '' ,
88- email : invoice . shippingAddress ?. email ?? '' ,
89- company : invoice . shippingAddress ?. company ,
90- phone : invoice . shippingAddress ?. phone ,
91- city : invoice . shippingAddress ?. city ?? '' ,
92- state : invoice . shippingAddress ?. state ?? '' ,
93- zip : invoice . shippingAddress ?. zip ?? '' ,
94- country : invoice . shippingAddress ?. country ?? '' ,
95- } ) ,
96- options : invoice . options ,
97- currencyCode : invoice . currencyCode ,
98- } ) ;
57+ return this . billingInvoiceService . createInvoice ( invoiceDto ) ;
9958 }
10059
10160 @authorize ( {
@@ -116,28 +75,7 @@ export class BillingInvoiceController {
11675 async getInvoice (
11776 @param . path . string ( 'invoiceId' ) invoiceId : string ,
11877 ) : Promise < InvoiceDto > {
119- const invoice = await this . billingProvider . retrieveInvoice ( invoiceId ) ;
120- const charges = invoice . charges ?. map (
121- charge =>
122- new ChargeDto ( { amount : charge . amount , description : charge . description } ) ,
123- ) ;
124- return new InvoiceDto ( {
125- customerId : invoice . customerId ,
126- charges : charges ,
127- status : invoice . status ,
128- shippingAddress : new AddressDto ( {
129- firstName : invoice . shippingAddress ?. firstName ?? '' ,
130- lastName : invoice . shippingAddress ?. lastName ?? '' ,
131- email : invoice . shippingAddress ?. email ?? '' ,
132- company : invoice . shippingAddress ?. company ,
133- phone : invoice . shippingAddress ?. phone ,
134- city : invoice . shippingAddress ?. city ?? '' ,
135- state : invoice . shippingAddress ?. state ?? '' ,
136- zip : invoice . shippingAddress ?. zip ?? '' ,
137- country : invoice . shippingAddress ?. country ?? '' ,
138- } ) ,
139- options : invoice . options ,
140- } ) ;
78+ return this . billingInvoiceService . getInvoice ( invoiceId ) ;
14179 }
14280
14381 @authorize ( {
@@ -193,11 +131,7 @@ export class BillingInvoiceController {
193131 } )
194132 transactionDto : TransactionDto ,
195133 ) : Promise < void > {
196- const invoiceInfo = await this . invoiceRepository . findById ( invoiceId ) ;
197- await this . billingProvider . applyPaymentSourceForInvoice (
198- invoiceInfo . invoiceId ,
199- transactionDto ,
200- ) ;
134+ return this . billingInvoiceService . applyPayment ( invoiceId , transactionDto ) ;
201135 }
202136
203137 @authorize ( {
@@ -217,12 +151,6 @@ export class BillingInvoiceController {
217151 async deleteById (
218152 @param . path . string ( 'invoiceId' ) invoiceId : string ,
219153 ) : Promise < void > {
220- const invoice = await this . invoiceRepository . find ( {
221- where : { invoiceId : invoiceId } ,
222- } ) ;
223- if ( invoice . length === 0 )
224- throw new Error ( ' invoice with invoiceId is not present' ) ;
225- await this . billingProvider . deleteInvoice ( invoiceId ) ;
226- await this . invoiceRepository . deleteById ( invoice [ 0 ] . id ) ;
154+ return this . billingInvoiceService . deleteInvoice ( invoiceId ) ;
227155 }
228156}
0 commit comments