@@ -17,7 +17,9 @@ import {
1717 ensureOpportunityPermissions ,
1818 OpportunityPermissions ,
1919} from '../../opportunity/accessControl' ;
20- import { updateSubscriptionFlags } from '../../utils' ;
20+ import { updateRecruiterSubscriptionFlags } from '../../utils' ;
21+ import { OpportunityState } from '@dailydotdev/schema' ;
22+ import { Organization } from '../../../entity/Organization' ;
2123
2224export const createOpportunitySubscription = async ( {
2325 event,
@@ -47,14 +49,33 @@ export const createOpportunitySubscription = async ({
4749 return false ;
4850 }
4951
50- const opportunity : Pick < OpportunityJob , 'id' > = await con
51- . getRepository ( OpportunityJob )
52- . findOneOrFail ( {
53- select : [ 'id' ] ,
54- where : {
55- id : opportunity_id ,
56- } ,
57- } ) ;
52+ const opportunity : Pick <
53+ OpportunityJob ,
54+ 'id' | 'organizationId' | 'organization'
55+ > = await con . getRepository ( OpportunityJob ) . findOneOrFail ( {
56+ select : [ 'id' , 'organizationId' , 'organization' ] ,
57+ where : {
58+ id : opportunity_id ,
59+ } ,
60+ relations : {
61+ organization : true ,
62+ } ,
63+ } ) ;
64+
65+ const organization = await opportunity . organization ;
66+
67+ if ( ! organization ) {
68+ throw new Error (
69+ 'Opportunity does not have organization during payment processing, can not assign subscription, manual fixup needed' ,
70+ ) ;
71+ }
72+
73+ if (
74+ organization . recruiterSubscriptionFlags ?. status ===
75+ SubscriptionStatus . Active
76+ ) {
77+ throw new Error ( 'Organization already has active recruiter subscription' ) ;
78+ }
5879
5980 await ensureOpportunityPermissions ( {
6081 con : con . manager ,
@@ -63,22 +84,38 @@ export const createOpportunitySubscription = async ({
6384 permission : OpportunityPermissions . Edit ,
6485 } ) ;
6586
66- await con . getRepository ( OpportunityJob ) . update (
67- {
68- id : opportunity . id ,
69- } ,
70- {
71- subscriptionFlags : updateSubscriptionFlags < OpportunityJob > ( {
72- cycle : subscriptionType ,
73- createdAt : data . startedAt ?? new Date ( ) ,
74- updatedAt : new Date ( ) ,
75- subscriptionId : data . id ,
76- priceId : data . items [ 0 ] . price . id ,
77- provider : SubscriptionProvider . Paddle ,
78- status : SubscriptionStatus . Active ,
79- } ) ,
80- } ,
81- ) ;
87+ await con . transaction ( async ( entityManager ) => {
88+ await entityManager . getRepository ( Organization ) . update (
89+ {
90+ id : opportunity . id ,
91+ } ,
92+ {
93+ recruiterSubscriptionFlags :
94+ updateRecruiterSubscriptionFlags < Organization > ( {
95+ cycle : subscriptionType ,
96+ createdAt : data . startedAt ?? new Date ( ) ,
97+ updatedAt : new Date ( ) ,
98+ subscriptionId : data . id ,
99+ provider : SubscriptionProvider . Paddle ,
100+ status : SubscriptionStatus . Active ,
101+ items : data . items . map ( ( item ) => {
102+ return {
103+ priceId : item . price . id ,
104+ } ;
105+ } ) ,
106+ } ) ,
107+ } ,
108+ ) ;
109+
110+ await entityManager . getRepository ( OpportunityJob ) . update (
111+ {
112+ id : opportunity . id ,
113+ } ,
114+ {
115+ state : OpportunityState . IN_REVIEW ,
116+ } ,
117+ ) ;
118+ } ) ;
82119} ;
83120
84121export const cancelRecruiterSubscription = async ( {
@@ -91,14 +128,18 @@ export const cancelRecruiterSubscription = async ({
91128 event . data . customData ,
92129 ) ;
93130
94- const opportunity : Pick < OpportunityJob , 'id' > = await con
95- . getRepository ( OpportunityJob )
96- . findOneOrFail ( {
97- select : [ 'id' ] ,
98- where : {
99- id : opportunity_id ,
100- } ,
101- } ) ;
131+ const opportunity : Pick <
132+ OpportunityJob ,
133+ 'id' | 'organizationId' | 'organization'
134+ > = await con . getRepository ( OpportunityJob ) . findOneOrFail ( {
135+ select : [ 'id' , 'organizationId' , 'organization' ] ,
136+ where : {
137+ id : opportunity_id ,
138+ } ,
139+ relations : {
140+ organization : true ,
141+ } ,
142+ } ) ;
102143
103144 await ensureOpportunityPermissions ( {
104145 con : con . manager ,
@@ -107,19 +148,28 @@ export const cancelRecruiterSubscription = async ({
107148 permission : OpportunityPermissions . Edit ,
108149 } ) ;
109150
110- const subscriptionFlags : OpportunityJob [ 'subscriptionFlags' ] = {
151+ const organization = await opportunity . organization ;
152+
153+ if ( ! organization ) {
154+ throw new Error (
155+ 'Opportunity does not have organization during payment processing, can not cancel subscription, manual fixup needed' ,
156+ ) ;
157+ }
158+
159+ const subscriptionFlags : Organization [ 'recruiterSubscriptionFlags' ] = {
111160 cycle : null ,
112- status : SubscriptionStatus . Expired ,
161+ status : SubscriptionStatus . Cancelled ,
113162 updatedAt : new Date ( ) ,
163+ items : [ ] ,
114164 } ;
115165
116- con . getRepository ( OpportunityJob ) . update (
166+ con . getRepository ( Organization ) . update (
117167 {
118168 id : opportunity . id ,
119169 } ,
120170 {
121- subscriptionFlags :
122- updateSubscriptionFlags < OpportunityJob > ( subscriptionFlags ) ,
171+ recruiterSubscriptionFlags :
172+ updateRecruiterSubscriptionFlags < Organization > ( subscriptionFlags ) ,
123173 } ,
124174 ) ;
125175} ;
0 commit comments