@@ -27,6 +27,7 @@ const {
2727 createTestBillingIntegration,
2828 updateTestBillingIntegrationOrganizationContext,
2929} = require ( '@condo/domains/billing/utils/testSchema' )
30+ const { createTestBillingIntegrationAccessRight } = require ( '@condo/domains/billing/utils/testSchema' )
3031const {
3132 TestUtils,
3233 BillingTestMixin,
@@ -68,6 +69,21 @@ function getOnlyCategoryReceiptTest (category){
6869 }
6970}
7071
72+ async function initBillingTestMixin ( utils ) {
73+ await utils . createEmployee ( 'billing' , {
74+ canManageIntegrations : true ,
75+ canReadBillingReceipts : true ,
76+ canReadPayments : true ,
77+ } )
78+ const [ billingIntegration ] = await createTestBillingIntegration ( utils . clients . admin )
79+ const [ billingContext ] = await createTestBillingIntegrationOrganizationContext ( utils . clients . admin , utils . organization , billingIntegration , {
80+ status : CONTEXT_FINISHED_STATUS ,
81+ } )
82+ await createTestBillingIntegrationAccessRight ( utils . clients . admin , billingIntegration , utils . clients . service . user )
83+ utils . billingIntegration = billingIntegration
84+ utils . billingContext = billingContext
85+ }
86+
7187describe ( 'FindOrganizationsByAddress' , ( ) => {
7288
7389 const utils = new TestUtils ( [ BillingTestMixin ] )
@@ -133,20 +149,35 @@ describe('FindOrganizationsByAddress', () => {
133149 } )
134150
135151 test ( 'Should return organization and receipt category' , async ( ) => {
136- const utils = new TestUtils ( [ BillingTestMixin , AcquiringTestMixin , ResidentTestMixin , MeterTestMixin ] )
152+ const utils = new TestUtils ( [ ResidentTestMixin , MeterTestMixin ] )
153+ const utilsWithAnotherBillingContext = new TestUtils ( [ ResidentTestMixin , MeterTestMixin ] )
137154 await utils . init ( )
155+ await utilsWithAnotherBillingContext . init ( )
156+ utilsWithAnotherBillingContext . organization = utils . organization
157+ await initBillingTestMixin ( utilsWithAnotherBillingContext )
138158 await utils . createReceipts ( [
139159 utils . createJSONReceipt ( {
140160 address : utils . property . address ,
141161 category : { id : HOUSING_CATEGORY_ID } ,
142162 } ) ,
143163 ] )
164+ await utilsWithAnotherBillingContext . createReceipts ( [
165+ utils . createJSONReceipt ( {
166+ address : utils . property . address ,
167+ category : { id : REPAIR_CATEGORY_ID } ,
168+ } ) ,
169+ ] )
144170 const [ foundOrganizations ] = await findOrganizationsByAddressByTestClient ( utils . clients . resident , {
145171 addressKey : utils . property . addressKey ,
146172 } )
147173 const found = foundOrganizations . find ( ( { id } ) => id === utils . organization . id )
174+ expect ( found ) . toBeDefined ( )
148175 expect ( found . meters ) . toHaveLength ( 0 )
149- expect ( found . receipts ) . toContainEqual ( getOnlyCategoryReceiptTest ( HOUSING_CATEGORY_ID ) )
176+ expect ( found . receipts ) . toHaveLength ( 2 )
177+ expect ( found . receipts ) . toEqual ( expect . arrayContaining ( [
178+ expect . objectContaining ( getOnlyCategoryReceiptTest ( HOUSING_CATEGORY_ID ) ) ,
179+ expect . objectContaining ( getOnlyCategoryReceiptTest ( REPAIR_CATEGORY_ID ) ) ,
180+ ] ) )
150181 expect ( found . id ) . toEqual ( utils . organization . id )
151182 expect ( found . name ) . toEqual ( utils . organization . name )
152183 expect ( found . tin ) . toEqual ( utils . organization . tin )
@@ -281,6 +312,67 @@ describe('FindOrganizationsByAddress', () => {
281312 } )
282313 } )
283314
315+ test ( 'Should return receipts from all billing contexts if unitName and unitType matches' , async ( ) => {
316+ const utils = new TestUtils ( [ ResidentTestMixin , MeterTestMixin ] )
317+ const utilsWithAnotherBillingContext = new TestUtils ( [ ResidentTestMixin , MeterTestMixin ] )
318+ await utils . init ( )
319+ await utilsWithAnotherBillingContext . init ( )
320+ utilsWithAnotherBillingContext . organization = utils . organization
321+ await initBillingTestMixin ( utilsWithAnotherBillingContext )
322+
323+ const unitName = faker . random . alphaNumeric ( 16 )
324+ const unitType = 'flat'
325+ const accountNumber = faker . random . alphaNumeric ( 16 )
326+ const anotherAccountNumber = faker . random . alphaNumeric ( 16 )
327+ const toPay = '1000'
328+
329+ // NOTE(YEgorLu): if receipts have same category, they will be squashed into one receipt
330+ await utils . createReceipts ( [
331+ utils . createJSONReceipt ( {
332+ address : utils . property . address ,
333+ accountNumber,
334+ addressMeta : { unitName, unitType } ,
335+ category : { id : HOUSING_CATEGORY_ID } ,
336+ toPay,
337+ } ) ,
338+ ] )
339+ await utilsWithAnotherBillingContext . createReceipts ( [
340+ utils . createJSONReceipt ( {
341+ address : utils . property . address ,
342+ accountNumber : anotherAccountNumber ,
343+ category : { id : REPAIR_CATEGORY_ID } ,
344+ addressMeta : { unitName, unitType } ,
345+ toPay,
346+ } ) ,
347+ ] )
348+ const [ foundOrganizations ] = await findOrganizationsByAddressByTestClient ( utils . clients . resident , {
349+ addressKey : utils . property . addressKey ,
350+ unitName,
351+ unitType,
352+ } )
353+ const found = foundOrganizations . find ( ( { id } ) => id === utils . organization . id )
354+ expect ( found ) . toBeDefined ( )
355+ expect ( found . receipts ) . toHaveLength ( 2 )
356+ expect ( found . receipts ) . toEqual ( expect . arrayContaining ( [
357+ expect . objectContaining ( {
358+ accountNumber : expect . stringMatching ( accountNumber ) ,
359+ category : expect . any ( String ) ,
360+ balance : expect . stringMatching ( Big ( toPay ) . toFixed ( 8 ) ) ,
361+ routingNumber : expect . any ( String ) ,
362+ bankAccount : expect . any ( String ) ,
363+ address : utils . property . address ,
364+ } ) ,
365+ expect . objectContaining ( {
366+ accountNumber : expect . stringMatching ( anotherAccountNumber ) ,
367+ category : expect . any ( String ) ,
368+ balance : expect . stringMatching ( Big ( toPay ) . toFixed ( 8 ) ) ,
369+ routingNumber : expect . any ( String ) ,
370+ bankAccount : expect . any ( String ) ,
371+ address : utils . property . address ,
372+ } ) ,
373+ ] ) )
374+ } )
375+
284376 test ( 'Should return meter if unitName and unitType matches' , async ( ) => {
285377 const utils = new TestUtils ( [ ResidentTestMixin , MeterTestMixin ] )
286378 await utils . init ( )
@@ -653,6 +745,102 @@ describe('FindOrganizationsByAddress', () => {
653745 expect ( found . tin ) . toEqual ( utils . organization . tin )
654746 expect ( found . type ) . toEqual ( utils . organization . type )
655747 } )
748+
749+ test ( 'Should return organization and two receipts from different billing contexts' , async ( ) => {
750+ const utils = new TestUtils ( [ ResidentTestMixin , MeterTestMixin ] )
751+ const utilsWithAnotherBillingContext = new TestUtils ( [ ResidentTestMixin , MeterTestMixin ] )
752+ await utils . init ( )
753+ await utilsWithAnotherBillingContext . init ( )
754+ utilsWithAnotherBillingContext . organization = utils . organization
755+ await initBillingTestMixin ( utilsWithAnotherBillingContext )
756+
757+ const accountNumber1 = faker . random . alphaNumeric ( 16 )
758+ const accountNumber2 = faker . random . alphaNumeric ( 16 )
759+ const accountNumber3 = faker . random . alphaNumeric ( 16 )
760+ const toPay1 = '1000'
761+ const toPay2 = '2000'
762+ const unitName = faker . random . alphaNumeric ( 16 )
763+ const unitType = 'flat'
764+ await utils . createReceipts ( [
765+ utils . createJSONReceipt ( {
766+ address : utils . property . address ,
767+ addressMeta : { unitName, unitType } ,
768+ category : { id : HOUSING_CATEGORY_ID } ,
769+ accountNumber : accountNumber1 ,
770+ toPay : toPay1 ,
771+ } ) ,
772+ utils . createJSONReceipt ( {
773+ address : utils . property . address ,
774+ addressMeta : { unitName, unitType } ,
775+ category : { id : HOUSING_CATEGORY_ID } ,
776+ accountNumber : accountNumber2 ,
777+ toPay : toPay1 ,
778+ } ) ,
779+ ] )
780+
781+ await utilsWithAnotherBillingContext . createReceipts ( [
782+ utils . createJSONReceipt ( {
783+ address : utils . property . address ,
784+ addressMeta : { unitName, unitType } ,
785+ category : { id : REPAIR_CATEGORY_ID } ,
786+ accountNumber : accountNumber1 ,
787+ toPay : toPay2 ,
788+ } ) ,
789+ utils . createJSONReceipt ( {
790+ address : utils . property . address ,
791+ addressMeta : { unitName, unitType } ,
792+ category : { id : HOUSING_CATEGORY_ID } ,
793+ accountNumber : accountNumber3 ,
794+ toPay : toPay1 ,
795+ } ) ,
796+ ] )
797+
798+ // Finding receipts with same accountNumber from different billing contexts
799+ const [ foundOrganizations ] = await findOrganizationsByAddressByTestClient ( utils . clients . resident , {
800+ addressKey : utils . property . addressKey ,
801+ accountNumber : accountNumber1 ,
802+ tin : utils . organization . tin ,
803+ } )
804+ const found = foundOrganizations . find ( ( { id } ) => id === utils . organization . id )
805+ expect ( found ) . toBeDefined ( )
806+ expect ( found . meters ) . toHaveLength ( 0 )
807+ expect ( found . receipts ) . toHaveLength ( 2 ) // should be "2"
808+ expect ( found . id ) . toEqual ( utils . organization . id )
809+ expect ( found . name ) . toEqual ( utils . organization . name )
810+ expect ( found . tin ) . toEqual ( utils . organization . tin )
811+ expect ( found . type ) . toEqual ( utils . organization . type )
812+
813+ // Finding receipt from older billing context (was default behaviour before)
814+ const [ foundOrganizationsForAccountNumber2 ] = await findOrganizationsByAddressByTestClient ( utils . clients . resident , {
815+ addressKey : utils . property . addressKey ,
816+ accountNumber : accountNumber2 ,
817+ tin : utils . organization . tin ,
818+ } )
819+ const found2 = foundOrganizationsForAccountNumber2 . find ( ( { id } ) => id === utils . organization . id )
820+ expect ( found2 ) . toBeDefined ( )
821+ expect ( found2 . meters ) . toHaveLength ( 0 )
822+ expect ( found2 . receipts ) . toHaveLength ( 1 )
823+ expect ( found2 . id ) . toEqual ( utils . organization . id )
824+ expect ( found2 . name ) . toEqual ( utils . organization . name )
825+ expect ( found2 . tin ) . toEqual ( utils . organization . tin )
826+ expect ( found2 . type ) . toEqual ( utils . organization . type )
827+
828+ // Finding receipt from newer billing context (was impossible before)
829+ const [ foundOrganizationsForAccountNumber3 ] = await findOrganizationsByAddressByTestClient ( utils . clients . resident , {
830+ addressKey : utils . property . addressKey ,
831+ accountNumber : accountNumber3 ,
832+ tin : utils . organization . tin ,
833+ } )
834+ const found3 = foundOrganizationsForAccountNumber3 . find ( ( { id } ) => id === utils . organization . id )
835+ expect ( found3 ) . toBeDefined ( )
836+ expect ( found3 . meters ) . toHaveLength ( 0 )
837+ expect ( found3 . receipts ) . toHaveLength ( 1 )
838+ expect ( found3 . id ) . toEqual ( utils . organization . id )
839+ expect ( found3 . name ) . toEqual ( utils . organization . name )
840+ expect ( found3 . tin ) . toEqual ( utils . organization . tin )
841+ expect ( found3 . type ) . toEqual ( utils . organization . type )
842+ } )
843+
656844 } )
657845 } )
658846
0 commit comments