11import { getPlanFromId } from "@data/PREMIUM" ;
2- import StripeService from '~/server/services/StripeService' ;
32import { PREMIUM_PLAN } from "../../../../shared/data/PREMIUM" ;
4- import { checkAppsumoCode , useAppsumoCode } from "~/server/services/AppsumoService" ;
5-
6-
3+ import { canTryAppsumoCode , checkAppsumoCode , useAppsumoCode , useTryAppsumoCode } from "~/server/services/AppsumoService" ;
4+ import StripeService from '~/server/services/StripeService' ;
75
86function getPlanToActivate ( current_plan_id : number ) {
97 if ( current_plan_id === PREMIUM_PLAN . FREE . ID ) {
@@ -28,24 +26,26 @@ export default defineEventHandler(async event => {
2826 const data = await getRequestData ( event , { requireSchema : false , allowGuests : false , allowLitlyx : false } ) ;
2927 if ( ! data ) return ;
3028
31- const { project, pid } = data ;
29+ const { project, pid, user } = data ;
3230
3331 const body = await readBody ( event ) ;
3432
3533 const { code } = body ;
3634
37- const valid = await checkAppsumoCode ( code ) ;
35+ const canTry = await canTryAppsumoCode ( pid ) ;
36+ if ( ! canTry ) return setResponseStatus ( event , 400 , 'You tried too much codes. Please contact support.' ) ;
37+ await useTryAppsumoCode ( pid , code ) ;
3838
39- if ( ! valid ) return setResponseStatus ( event , 400 , 'Current plan not found' ) ;
39+ const valid = await checkAppsumoCode ( code ) ;
40+ if ( ! valid ) return setResponseStatus ( event , 400 , 'Code not valid' ) ;
4041
4142 const currentPlan = getPlanFromId ( project . premium_type ) ;
4243 if ( ! currentPlan ) return setResponseStatus ( event , 400 , 'Current plan not found' ) ;
4344 const planToActivate = getPlanToActivate ( currentPlan . ID ) ;
4445 if ( ! planToActivate ) return setResponseStatus ( event , 400 , 'Cannot use code on current plan' ) ;
4546
46- await StripeService . deleteSubscription ( project . subscription_id ) ;
4747 await StripeService . createSubscription ( project . customer_id , planToActivate . ID ) ;
4848
49- await useAppsumoCode ( code ) ;
49+ await useAppsumoCode ( pid , code ) ;
5050
5151} ) ;
0 commit comments