@@ -1034,6 +1034,60 @@ describe('Organization', () => {
10341034 expect ( org . subscription . activeSubscriptionContextId ) . toBeDefined ( )
10351035 } )
10361036
1037+ test ( 'activeSubscriptionEndAt chains consecutive non-trial contexts of the same plan' , async ( ) => {
1038+ const [ organization ] = await createTestOrganization ( admin )
1039+ const endAt1 = dayjs ( ) . add ( 30 , 'days' ) . format ( 'YYYY-MM-DD' )
1040+ const endAt2 = dayjs ( ) . add ( 60 , 'days' ) . format ( 'YYYY-MM-DD' )
1041+ const endAt2WithBuffer = dayjs ( ) . add ( 60 + SUBSCRIPTION_PAYMENT_BUFFER_DAYS , 'days' ) . format ( 'YYYY-MM-DD' )
1042+ const [ plan ] = await createTestSubscriptionPlan ( admin , {
1043+ name : faker . commerce . productName ( ) ,
1044+ organizationType : MANAGING_COMPANY_TYPE ,
1045+ payments : true ,
1046+ } )
1047+
1048+ await createTestSubscriptionContext ( admin , organization , plan , {
1049+ startAt : dayjs ( ) . subtract ( 1 , 'day' ) . format ( 'YYYY-MM-DD' ) ,
1050+ endAt : endAt1 ,
1051+ isTrial : false ,
1052+ } )
1053+ await createTestSubscriptionContext ( admin , organization , plan , {
1054+ startAt : endAt1 ,
1055+ endAt : endAt2 ,
1056+ isTrial : false ,
1057+ } )
1058+
1059+ const org = await Organization . getOne ( admin , { id : organization . id } )
1060+
1061+ expect ( org . subscription ) . not . toBeNull ( )
1062+ expect ( org . subscription . activeSubscriptionEndAt ) . toBe ( endAt2WithBuffer )
1063+ } )
1064+
1065+ test ( 'non-trial context in buffer period is still treated as active' , async ( ) => {
1066+ const [ organization ] = await createTestOrganization ( admin )
1067+ // endAt is 2 days ago, but buffered endAt is still in the future
1068+ const endAt = dayjs ( ) . subtract ( 2 , 'days' ) . format ( 'YYYY-MM-DD' )
1069+ const endAtWithBuffer = dayjs ( endAt ) . add ( SUBSCRIPTION_PAYMENT_BUFFER_DAYS , 'days' ) . format ( 'YYYY-MM-DD' )
1070+ const [ plan ] = await createTestSubscriptionPlan ( admin , {
1071+ name : faker . commerce . productName ( ) ,
1072+ organizationType : MANAGING_COMPANY_TYPE ,
1073+ payments : true ,
1074+ tickets : true ,
1075+ } )
1076+ const [ context ] = await createTestSubscriptionContext ( admin , organization , plan , {
1077+ startAt : dayjs ( ) . subtract ( 30 , 'days' ) . format ( 'YYYY-MM-DD' ) ,
1078+ endAt,
1079+ isTrial : false ,
1080+ } )
1081+
1082+ const org = await Organization . getOne ( admin , { id : organization . id } )
1083+
1084+ expect ( org . subscription ) . not . toBeNull ( )
1085+ expect ( org . subscription . activeSubscriptionContextId ) . toBe ( context . id )
1086+ expect ( org . subscription . activeSubscriptionEndAt ) . toBe ( endAtWithBuffer )
1087+ expect ( org . subscription . paymentsEndAt ) . toBe ( endAtWithBuffer )
1088+ expect ( org . subscription . ticketsEndAt ) . toBe ( endAtWithBuffer )
1089+ } )
1090+
10371091 test ( 'only selects activeSubscriptionContextId from service subscriptions' , async ( ) => {
10381092 const [ organization ] = await createTestOrganization ( admin )
10391093 const endAt = dayjs ( ) . add ( 30 , 'days' ) . format ( 'YYYY-MM-DD' )
0 commit comments