tango-app-api-payment-subscription 3.0.20-dev → 3.0.22-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-payment-subscription",
3
- "version": "3.0.20-dev",
3
+ "version": "3.0.22-dev",
4
4
  "description": "paymentSubscription",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -1,3 +1,4 @@
1
+
1
2
  /* eslint-disable new-cap */
2
3
  import { logger, download, sendEmailWithSES, appConfig } from 'tango-app-api-middleware';
3
4
  import * as paymentService from '../services/clientPayment.services.js';
@@ -31,9 +32,19 @@ export const addBilling = async ( req, res ) => {
31
32
  billingDetails.nextBillingDate = '--';
32
33
  resultData.billingDetails = billingDetails;
33
34
  logger.info( 'Billing Details Added Successfully' );
35
+ // const logObj = {
36
+ // clientId: req.body.clientId,
37
+ // userName: req.user?.userName,
38
+ // email: req.user?.email,
39
+ // date: new Date(),
40
+ // logType: 'billing',
41
+ // logSubType: 'billingInfo',
42
+ // changes: [ 'billingAddress', 'gstNumber' ],
43
+ // eventType: 'update',
44
+ // };
34
45
  return res.sendSuccess( { message: 'Billing Details Added Successfully', data: resultData } );
35
46
  } else {
36
- logger.error( 'Error Occurs WHile updating billing Detaisls' );
47
+ logger.error( 'Error Occurs WHile updating billing Details' );
37
48
  return res.sendError( 'Something Went Wrong', 500 );
38
49
  }
39
50
  } catch ( e ) {
@@ -130,7 +141,7 @@ export const clientBillingSubscriptionInfo = async ( req, res, next ) => {
130
141
  if ( element.status == 'trial' ) {
131
142
  let differenceInDays = 14;
132
143
  if ( element?.trialEndDate ) {
133
- differenceInDays = dateDifference( element?.trialEndDate, currentDate );
144
+ differenceInDays = dateDifference( new Date( dayjs( element?.trialEndDate ).add( 1, 'days' ).startOf( 'day' ) ), new Date( dayjs( currentDate ).startOf( 'day' ) ) );
134
145
  if ( element.trialEndDate < currentDate ) {
135
146
  expiredProducts.push( { 'productName': element.productName, 'aliseProductName': element.aliseProductName, 'toolTip': 'Trial Expired' } );
136
147
  element.toolTip = 'Trial Expired';
@@ -373,6 +384,10 @@ export const updateSubscription = async ( req, res ) => {
373
384
  if ( !requestBody?.products?.length ) {
374
385
  return res.sendError( 'product is required', 400 );
375
386
  }
387
+ let premiumType = requestBody.client.planDetails.subscriptionType;
388
+ if ( requestBody.client.planDetails.subscriptionType == 'free' ) {
389
+ premiumType = 'premium';
390
+ }
376
391
 
377
392
  let clientProducts = requestBody.client.planDetails.product;
378
393
  for ( let item of requestBody.products ) {
@@ -386,7 +401,10 @@ export const updateSubscription = async ( req, res ) => {
386
401
  category: 'Trial',
387
402
  status: 'pending',
388
403
  };
389
- await clientRequestService.insert( params );
404
+ let productExists = await clientRequestService.findOne( { clientId: requestBody.clientId, name: item.name, category: 'Trial', status: 'pending' } );
405
+ if ( !productExists ) {
406
+ await clientRequestService.insert( params );
407
+ }
390
408
  }
391
409
  if ( item.type == 'subscription' ) {
392
410
  if ( existsIndex == -1 ) {
@@ -403,18 +421,25 @@ export const updateSubscription = async ( req, res ) => {
403
421
  }
404
422
 
405
423
  let details = {
406
- subscriptionType: requestBody.subscriptionType,
424
+ subscriptionType: premiumType,
407
425
  subscriptionPeriod: requestBody.subscriptionPeriod,
408
- storeCount: requestBody.storeCount,
409
426
  totalCamera: requestBody.totalCamera,
410
427
  totalStores: requestBody.totalStores,
411
428
  storeSize: requestBody.storeSize,
412
429
  product: clientProducts,
413
430
  };
414
431
 
432
+ req.body = {
433
+ 'camaraPerSqft': req.body.storeSize,
434
+ 'storesCount': req.body.totalStores,
435
+ 'planName': requestBody.subscriptionPeriod,
436
+ 'products': clientProducts.map( ( item ) => item.productName ),
437
+ 'currencyType': 'rupees',
438
+ };
439
+ let pricingDetails = await calculatePricing( req, res );
415
440
  let data = {
416
441
  planDetails: details,
417
- price: requestBody.price,
442
+ price: pricingDetails.price,
418
443
  priceType: requestBody.priceType,
419
444
  };
420
445
 
@@ -471,7 +496,7 @@ export const trialProductList = async ( req, res ) => {
471
496
  } );
472
497
  return res.sendSuccess( products );
473
498
  } catch ( e ) {
474
- logger.error( { error: e, function: 'trialRequest' } );
499
+ logger.error( { error: e, function: 'trialProductList' } );
475
500
  return res.sendError( e, 500 );
476
501
  }
477
502
  };
@@ -541,7 +566,7 @@ export const trialRequest = async ( req, res ) => {
541
566
 
542
567
  return res.sendSuccess( 'Request Send Successfully' );
543
568
  } catch ( e ) {
544
- logger.error( { error: e, function: 'trialExtendRequest' } );
569
+ logger.error( { error: e, function: 'trialRequest' } );
545
570
  return res.sendError( e, 500 );
546
571
  }
547
572
  };
@@ -588,7 +613,7 @@ export const updateInvoiceDetails = async ( req, res ) => {
588
613
  clientInvoiceDetails.save().then( () => {
589
614
  return res.sendSuccess( 'Invoice Updated Successfully' );
590
615
  } ).catch( ( e ) => {
591
- return res.sendError( e );
616
+ return res.sendError( e, 500 );
592
617
  } );
593
618
  } catch ( e ) {
594
619
  logger.error( { error: e, function: 'invoiceDetails' } );
@@ -684,6 +709,9 @@ export const trialApproval = async ( req, res ) => {
684
709
  requestData.save().then( async () => {
685
710
  if ( req.body.type == 'approve' ) {
686
711
  let clientProducts = await paymentService.findOne( { clientId: requestData.clientId, status: 'active' }, { planDetails: 1 } );
712
+ if ( clientProducts?.planDetails.subscriptionType == 'free' ) {
713
+ clientProducts.planDetails.subscriptionType = 'premium';
714
+ }
687
715
  if ( !clientProducts ) {
688
716
  return res.sendError( 'no data found', 204 );
689
717
  }
@@ -759,7 +787,7 @@ export const trialExtendRequestApproval = async ( req, res ) => {
759
787
  userName: userDetails.userName,
760
788
  product: firstWord +' '+secondWord,
761
789
  days: req.body.days,
762
- date: trialDate,
790
+ date: dayjs( trialDate ).format( 'YYYY-MM-DD' ),
763
791
  };
764
792
  const templateHtml = fs.readFileSync( path.resolve( path.dirname( '' ) ) + '/src/hbs/trialExtentionEmail.hbs', 'utf8' );
765
793
  const template = Handlebars.compile( templateHtml );
@@ -839,6 +867,18 @@ export const productSubscribe = async ( req, res ) => {
839
867
  }
840
868
  }
841
869
  product = product.filter( ( item ) => !removeProducts.includes( item.productName ) );
870
+ req.body = {
871
+ 'camaraPerSqft': clientInfo.planDetails.storeSize,
872
+ 'storesCount': clientInfo.planDetails.totalStores,
873
+ 'planName': clientInfo.planDetails.subscriptionPeriod,
874
+ 'products': product.map( ( item ) => item.productName ),
875
+ 'currencyType': 'rupees',
876
+ };
877
+ let pricingDetails = await calculatePricing( req, res );
878
+ clientInfo.price = pricingDetails.price;
879
+ if ( product.length > 1 ) {
880
+ clientInfo.planDetails.subscriptionType = 'premium';
881
+ }
842
882
  clientInfo.planDetails.product = product;
843
883
  clientInfo.save().then( async () => {
844
884
  let userDetails= await userService.findOne( { clientId: clientInfo.clientId, role: 'superadmin' } );
@@ -1379,11 +1419,11 @@ export const priceList = async ( req, res ) => {
1379
1419
  item.storeCount = item.storeCount - diff;
1380
1420
  }
1381
1421
  }
1382
- let discountPrice = product.basePrice * ( product.discountPercentage / 100 );
1383
- let price = product.basePrice - discountPrice;
1422
+ // let discountPrice = product.basePrice * ( product.discountPercentage / 100 );
1423
+ // let price = product.basePrice - discountPrice;
1384
1424
  totalProductPrice = totalProductPrice + product.basePrice;
1385
1425
  totalnegotiatePrice = totalnegotiatePrice + product.negotiatePrice;
1386
- product.price = price * product.storeCount;
1426
+ product.price = product.negotiatePrice * product.storeCount;
1387
1427
  temp.push( product );
1388
1428
  let originalPrice = product.basePrice * product.storeCount;
1389
1429
  product.originalPrice = originalPrice;
@@ -1392,9 +1432,10 @@ export const priceList = async ( req, res ) => {
1392
1432
  } );
1393
1433
  } );
1394
1434
  data = temp;
1395
- let discountPrice = originalTotalPrice - discountTotalPrice;
1396
- let discountPercentage = ( discountTotalPrice / originalTotalPrice ) * 100;
1397
- let finalValue = discountTotalPrice * ( 18 / 100 );
1435
+ let discountPrice = totalProductPrice - totalnegotiatePrice;
1436
+ let discountPercentage = totalnegotiatePrice > 0 ? ( totalnegotiatePrice / totalProductPrice ) * 100 : 0;
1437
+ let gstAmount = discountTotalPrice * ( 18 / 100 );
1438
+ let finalValue = parseFloat( discountTotalPrice ) + gstAmount;
1398
1439
  let result = {
1399
1440
  product: data,
1400
1441
  totalActualPrice: originalTotalPrice,
@@ -1402,7 +1443,7 @@ export const priceList = async ( req, res ) => {
1402
1443
  actualPrice: totalProductPrice,
1403
1444
  negotiatePrice: totalnegotiatePrice,
1404
1445
  discountPrice: discountPrice,
1405
- discountPercentage: discountPercentage.toFixed(),
1446
+ discountPercentage: discountPercentage.toFixed( 2 ),
1406
1447
  finalValue: finalValue.toFixed( 2 ),
1407
1448
  };
1408
1449
  return res.sendSuccess( result );
@@ -1432,7 +1473,7 @@ export const pricingListUpdate = async ( req, res ) => {
1432
1473
  productName: product,
1433
1474
  discountPercentage: baseDetails.discoutPercentage,
1434
1475
  basePrice: baseDetails.basePrice,
1435
- negotiatePrice: discountPrice,
1476
+ negotiatePrice: baseDetails.basePrice - discountPrice,
1436
1477
  },
1437
1478
  );
1438
1479
  stepList.push(
@@ -1440,7 +1481,7 @@ export const pricingListUpdate = async ( req, res ) => {
1440
1481
  productName: product,
1441
1482
  discountPercentage: baseDetails.discoutPercentage,
1442
1483
  basePrice: baseDetails.basePrice,
1443
- negotiatePrice: discountPrice,
1484
+ negotiatePrice: baseDetails.basePrice - discountPrice,
1444
1485
  storeRange: '1-100',
1445
1486
  },
1446
1487
  );
@@ -1506,7 +1547,22 @@ export const pricingListUpdate = async ( req, res ) => {
1506
1547
  } else {
1507
1548
  getPriceInfo.step = req.body.products;
1508
1549
  }
1509
- getPriceInfo.save().then( () => {
1550
+ getPriceInfo.save().then( async () => {
1551
+ let userDetails= await userService.findOne( { clientId: invoiceDetails.clientId, role: 'superadmin' } );
1552
+ if ( userDetails ) {
1553
+ const templateHtml = fs.readFileSync( path.resolve( path.dirname( '' ) ) + '/src/hbs/revisedPriceEmail.hbs', 'utf8' );
1554
+ const template = Handlebars.compile( templateHtml );
1555
+ const html = template( { data: '' } );
1556
+ let params = {
1557
+ toEmail: userDetails.email,
1558
+ mailSubject: 'test',
1559
+ htmlBody: html,
1560
+ attachment: '',
1561
+ sourceEmail: appConfig.cloud.aws.ses.adminEmail,
1562
+ };
1563
+ sendEmailWithSES( params.toEmail, params.mailSubject, params.htmlBody, params.attachment, params.sourceEmail );
1564
+ }
1565
+
1510
1566
  return res.sendSuccess( 'Pricig Updated Successfully' );
1511
1567
  } );
1512
1568
  } catch ( e ) {
@@ -1531,7 +1587,7 @@ export const updatedRevisedPrice = async ( req, res ) => {
1531
1587
  const template = Handlebars.compile( templateHtml );
1532
1588
  const html = template( { data: '' } );
1533
1589
  let params = {
1534
- toEmail: 'sudha@tangotech.co.in',
1590
+ toEmail: userDetails.email,
1535
1591
  mailSubject: 'test',
1536
1592
  htmlBody: html,
1537
1593
  attachment: '',
@@ -152,7 +152,6 @@ export const validateUpdateSubscriptionSchema = {
152
152
  priceType: joi.string().required(),
153
153
  subscriptionType: joi.string().required(),
154
154
  subscriptionPeriod: joi.string().required(),
155
- storeCount: joi.number().required(),
156
155
  totalCamera: joi.number().required(),
157
156
  totalStores: joi.string().required(),
158
157
  storeSize: joi.string().required(),