@@ -2,10 +2,11 @@ import { apiInstance } from '../../utils';
22import { FailCodes , FailRequest } from '../../../../src/billing/types' ;
33import { CardType , Currency , OperationType , ReasonCode , ReasonCodesTranscript } from '../../../../src/billing/types/enums' ;
44import { Collection , ObjectId , Db } from 'mongodb' ;
5- import { BusinessOperationDBScheme , BusinessOperationStatus , PlanDBScheme , BusinessOperationType , UserDBScheme , WorkspaceDBScheme , UserNotificationType , PlanProlongationPayload } from '@hawk.so/types' ;
5+ import { BusinessOperationDBScheme , BusinessOperationStatus , PlanDBScheme , BusinessOperationType , UserDBScheme , WorkspaceDBScheme , UserNotificationType } from '@hawk.so/types' ;
66import { WorkerPaths } from '../../../../src/rabbitmq' ;
77import { PaymentFailedNotificationTask , SenderWorkerTaskType } from '../../../../src/types/personalNotifications' ;
88import checksumService from '../../../../src/utils/checksumService' ;
9+ import jwt , { Secret } from 'jsonwebtoken' ;
910import type { Global } from '@jest/types' ;
1011
1112declare var global : Global . Global ;
@@ -51,6 +52,7 @@ const tariffPlan: PlanDBScheme = {
5152} ;
5253
5354const planProlongationPayload = {
55+ isCardLinkOperation : false ,
5456 userId : user . _id . toString ( ) ,
5557 workspaceId : workspace . _id . toString ( ) ,
5658 tariffPlanId : tariffPlan . _id . toString ( ) ,
@@ -215,6 +217,35 @@ describe('Fail webhook', () => {
215217 expect ( message && JSON . parse ( message . content . toString ( ) ) ) . toStrictEqual ( expectedLimiterTask ) ;
216218 expect ( apiResponse . data . code ) . toBe ( FailCodes . SUCCESS ) ;
217219 } ) ;
220+
221+ test ( 'Should change business operation status to rejected for card linking payload without tariff plan id' , async ( ) => {
222+ const apiResponse = await apiInstance . post ( '/billing/fail' , {
223+ ...validRequest ,
224+ Data : JSON . stringify ( {
225+ checksum : await checksumService . generateChecksum ( {
226+ isCardLinkOperation : true ,
227+ userId : user . _id . toString ( ) ,
228+ workspaceId : workspace . _id . toString ( ) ,
229+ nextPaymentDate : new Date ( ) . toString ( ) ,
230+ } ) ,
231+ cloudPayments : {
232+ recurrent : {
233+ interval : 'Month' ,
234+ period : 1 ,
235+ amount : 100 ,
236+ startDate : new Date ( ) . toISOString ( ) ,
237+ } ,
238+ } ,
239+ } ) ,
240+ } ) ;
241+
242+ const updatedBusinessOperation = await businessOperationsCollection . findOne ( {
243+ transactionId : transactionId . toString ( ) ,
244+ } ) ;
245+
246+ expect ( apiResponse . data . code ) . toBe ( FailCodes . SUCCESS ) ;
247+ expect ( updatedBusinessOperation ?. status ) . toBe ( BusinessOperationStatus . Rejected ) ;
248+ } ) ;
218249 } ) ;
219250
220251 describe ( 'With invalid request' , ( ) => {
@@ -236,6 +267,7 @@ describe('Fail webhook', () => {
236267 ...validRequest ,
237268 Data : JSON . stringify ( {
238269 checksum : await checksumService . generateChecksum ( {
270+ isCardLinkOperation : false ,
239271 userId : '' ,
240272 workspaceId : workspace . _id . toString ( ) ,
241273 tariffPlanId : tariffPlan . _id . toString ( ) ,
@@ -252,5 +284,33 @@ describe('Fail webhook', () => {
252284 expect ( apiResponse . data . code ) . toBe ( FailCodes . SUCCESS ) ;
253285 expect ( updatedBusinessOperation ?. status ) . toBe ( BusinessOperationStatus . Pending ) ;
254286 } ) ;
287+
288+ test ( 'Should not change business operation status for non-card-link payload without tariff plan id' , async ( ) => {
289+ const invalidChecksum = jwt . sign ( {
290+ isCardLinkOperation : false ,
291+ userId : user . _id . toString ( ) ,
292+ workspaceId : workspace . _id . toString ( ) ,
293+ shouldSaveCard : false ,
294+ nextPaymentDate : new Date ( ) . toString ( ) ,
295+ } , process . env . JWT_SECRET_BILLING_CHECKSUM as Secret , { expiresIn : '30m' } ) ;
296+
297+ const apiResponse = await apiInstance . post ( '/billing/fail' , {
298+ ...validRequest ,
299+ Data : JSON . stringify ( {
300+ checksum : invalidChecksum ,
301+ } ) ,
302+ } ) ;
303+
304+ const updatedBusinessOperation = await businessOperationsCollection . findOne ( {
305+ transactionId : transactionId . toString ( ) ,
306+ } ) ;
307+ const message = await global . rabbitChannel . get ( WorkerPaths . Email . queue , {
308+ noAck : true ,
309+ } ) ;
310+
311+ expect ( apiResponse . data . code ) . toBe ( FailCodes . SUCCESS ) ;
312+ expect ( message ) . toBeFalsy ( ) ;
313+ expect ( updatedBusinessOperation ?. status ) . toBe ( BusinessOperationStatus . Pending ) ;
314+ } ) ;
255315 } ) ;
256316} ) ;
0 commit comments