@@ -12,7 +12,7 @@ import { UserInputError } from 'apollo-server-express';
1212import cloudPaymentsApi , { CloudPaymentsJsonData } from '../utils/cloudPaymentsApi' ;
1313import * as telegram from '../utils/telegram' ;
1414import { TelegramBotURLs } from '../utils/telegram' ;
15- import PromoCodeService , { PromoCodeError , PromoCodeErrorCode , PromoCodePreviewResult } from '../utils/promoCodeService' ;
15+ import PromoCodeService , { PromoCodeError , PromoCodeErrorCode , PromoCodePreviewResult , buildPaymentPromoData } from '../utils/promoCodeService' ;
1616import { publish } from '../rabbitmq' ;
1717import type { Utm } from '@hawk.so/types' ;
1818import { validateUtmParams } from '../utils/utm/utm' ;
@@ -117,10 +117,13 @@ export default {
117117 checksum : string ;
118118 nextPaymentDate : Date ;
119119 cloudPaymentsPublicId : string ;
120- promoCode ?: string ;
121- originalAmount ?: number ;
122- finalAmount ?: number ;
123- discountAmount ?: number ;
120+ promo ?: {
121+ id : string ;
122+ benefitType : 'percent_discount' | 'amount_discount' | 'fixed_price' ;
123+ originalAmount : number ;
124+ finalAmount : number ;
125+ discountAmount : number ;
126+ } ;
124127 } > {
125128 const { workspaceId, tariffPlanId, shouldSaveCard, promoCode } = input ;
126129 const promoUtm = validateUtmParams ( input . promoUtm ) ;
@@ -162,23 +165,15 @@ export default {
162165 }
163166
164167 let paymentAmount = plan . monthlyCharge ;
165- let promoPaymentData ;
168+ let paymentPromo ;
166169
167170 if ( promoCode && ! isCardLinkOperation ) {
168171 try {
169172 const promoCodeService = new PromoCodeService ( factories ) ;
170173 const pricing = await promoCodeService . getPricingForPlan ( promoCode , user . id , workspace . _id . toString ( ) , plan ) ;
171174
172175 paymentAmount = pricing . finalAmount ;
173- promoPaymentData = {
174- promoCodeId : pricing . promoCode . _id . toString ( ) ,
175- promoCodeValue : pricing . promoCode . value ,
176- benefitType : pricing . benefitType ,
177- originalAmount : pricing . originalAmount ,
178- finalAmount : pricing . finalAmount ,
179- discountAmount : pricing . discountAmount ,
180- ...( promoUtm && Object . keys ( promoUtm ) . length > 0 ? { promoUtm } : { } ) ,
181- } ;
176+ paymentPromo = buildPaymentPromoData ( pricing , promoUtm ) ;
182177 } catch ( error ) {
183178 throwPromoCodeGraphQLError ( error ) ;
184179 }
@@ -207,7 +202,7 @@ export default {
207202 tariffPlanId : plan . _id . toString ( ) ,
208203 shouldSaveCard : Boolean ( shouldSaveCard ) ,
209204 nextPaymentDate : nextPaymentDate . toISOString ( ) ,
210- ...promoPaymentData ,
205+ ...( paymentPromo ? { promo : paymentPromo } : { } ) ,
211206 } ;
212207
213208 const checksum = await checksumService . generateChecksum ( checksumData ) ;
@@ -239,10 +234,7 @@ debug: ${Boolean(workspace.isDebug)}`
239234 checksum,
240235 nextPaymentDate,
241236 cloudPaymentsPublicId : process . env . CLOUDPAYMENTS_PUBLIC_ID || '' ,
242- promoCode : promoPaymentData ?. promoCodeValue ,
243- originalAmount : promoPaymentData ?. originalAmount ,
244- finalAmount : promoPaymentData ?. finalAmount ,
245- discountAmount : promoPaymentData ?. discountAmount ,
237+ promo : paymentPromo ,
246238 } ;
247239 } ,
248240 } ,
@@ -394,7 +386,7 @@ debug: ${Boolean(workspace.isDebug)}`
394386 throw new UserInputError ( 'Wrong checksum data' ) ;
395387 }
396388
397- const planPaymentAmount = paymentData . finalAmount ?? plan . monthlyCharge ;
389+ const planPaymentAmount = paymentData . promo ?. finalAmount ?? plan . monthlyCharge ;
398390
399391 const token = fullUserInfo . bankCards ?. find ( card => card . id === args . input . cardId ) ?. token ;
400392
0 commit comments