@@ -182,6 +182,15 @@ function isPlanApplicable(benefit: PromoCodeBenefit, plan: PlanModel): boolean {
182182 return benefit . applicablePlanIds . some ( ( planId ) : boolean => planId . toString ( ) === plan . _id . toString ( ) ) ;
183183}
184184
185+ /**
186+ * Returns whether discount promo can affect plan price.
187+ *
188+ * @param plan - tariff plan
189+ */
190+ function isDiscountablePlan ( plan : PlanModel ) : boolean {
191+ return plan . monthlyCharge > 0 && isPlanAvailable ( plan ) ;
192+ }
193+
185194/**
186195 * Calculates discounted price for one plan.
187196 *
@@ -190,7 +199,9 @@ function isPlanApplicable(benefit: PromoCodeBenefit, plan: PlanModel): boolean {
190199 */
191200export function calculatePromoCodePlanPrice ( benefit : PromoCodeBenefit , plan : PlanModel ) : PromoCodePlanPrice {
192201 const originalAmount = plan . monthlyCharge ;
193- const isApplicable = benefit . type !== 'grant_plan' && isPlanAvailable ( plan ) && isPlanApplicable ( benefit , plan ) ;
202+ const isApplicable = benefit . type !== 'grant_plan' &&
203+ isDiscountablePlan ( plan ) &&
204+ isPlanApplicable ( benefit , plan ) ;
194205
195206 if ( ! isApplicable ) {
196207 return {
@@ -208,6 +219,16 @@ export function calculatePromoCodePlanPrice(benefit: PromoCodeBenefit, plan: Pla
208219 const discountAmount = Math . floor ( originalAmount * benefit . percent / 100 ) ;
209220 const finalAmount = Math . max ( originalAmount - discountAmount , minFinalPrice ) ;
210221
222+ if ( finalAmount >= originalAmount ) {
223+ return {
224+ planId : plan . _id . toString ( ) ,
225+ isApplicable : false ,
226+ originalAmount,
227+ finalAmount : originalAmount ,
228+ discountAmount : 0 ,
229+ } ;
230+ }
231+
211232 return {
212233 planId : plan . _id . toString ( ) ,
213234 isApplicable : true ,
@@ -221,6 +242,16 @@ export function calculatePromoCodePlanPrice(benefit: PromoCodeBenefit, plan: Pla
221242 const minFinalPrice = benefit . minFinalPrice ?? DEFAULT_MIN_FINAL_PRICE ;
222243 const finalAmount = Math . max ( originalAmount - benefit . amount , minFinalPrice ) ;
223244
245+ if ( finalAmount >= originalAmount ) {
246+ return {
247+ planId : plan . _id . toString ( ) ,
248+ isApplicable : false ,
249+ originalAmount,
250+ finalAmount : originalAmount ,
251+ discountAmount : 0 ,
252+ } ;
253+ }
254+
224255 return {
225256 planId : plan . _id . toString ( ) ,
226257 isApplicable : true ,
@@ -231,12 +262,22 @@ export function calculatePromoCodePlanPrice(benefit: PromoCodeBenefit, plan: Pla
231262 }
232263
233264 case 'fixed_price' :
265+ if ( benefit . amount >= originalAmount ) {
266+ return {
267+ planId : plan . _id . toString ( ) ,
268+ isApplicable : false ,
269+ originalAmount,
270+ finalAmount : originalAmount ,
271+ discountAmount : 0 ,
272+ } ;
273+ }
274+
234275 return {
235276 planId : plan . _id . toString ( ) ,
236277 isApplicable : true ,
237278 originalAmount,
238279 finalAmount : benefit . amount ,
239- discountAmount : Math . max ( originalAmount - benefit . amount , 0 ) ,
280+ discountAmount : originalAmount - benefit . amount ,
240281 } ;
241282
242283 default :
0 commit comments