tango-app-api-payment-subscription 3.1.12 → 3.1.14-alpha.1
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 +3 -3
- package/src/controllers/billing.controllers.js +278 -1
- package/src/controllers/invoice.controller.js +808 -261
- package/src/controllers/payment.controller.js +339 -0
- package/src/controllers/paymentSubscription.controllers.js +179 -48
- package/src/dtos/isUniqueEvent.js +16 -0
- package/src/dtos/validation.dtos.js +66 -7
- package/src/hbs/invoicePdf.hbs +7 -0
- package/src/hbs/invoicepaymentemail.hbs +925 -0
- package/src/routes/billing.routes.js +15 -2
- package/src/routes/invoice.routes.js +9 -3
- package/src/routes/payment.routes.js +41 -0
- package/src/routes/paymentSubscription.routes.js +2 -0
- package/src/services/billing.service.js +4 -0
- package/src/services/invoice.service.js +8 -0
- package/src/services/paymentAccount.service.js +14 -0
- package/src/services/transaction.service.js +10 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tango-app-api-payment-subscription",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.14-alpha.1",
|
|
4
4
|
"description": "paymentSubscription",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"mongodb": "^6.4.0",
|
|
26
26
|
"nodemon": "^3.1.0",
|
|
27
27
|
"swagger-ui-express": "^5.0.0",
|
|
28
|
-
"tango-api-schema": "^2.0.
|
|
29
|
-
"tango-app-api-middleware": "^3.1.
|
|
28
|
+
"tango-api-schema": "^2.0.139",
|
|
29
|
+
"tango-app-api-middleware": "^3.1.26",
|
|
30
30
|
"winston": "^3.12.0",
|
|
31
31
|
"winston-daily-rotate-file": "^5.0.0"
|
|
32
32
|
},
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { download, logger } from 'tango-app-api-middleware';
|
|
2
2
|
import { aggregate } from '../services/store.service.js';
|
|
3
|
-
import
|
|
3
|
+
import * as invoice from '../services/invoice.service.js';
|
|
4
|
+
import { aggregatebilling, countDocuments, create, deleteOne, find, findOne, updateMany, updateOne } from '../services/billing.service.js';
|
|
5
|
+
import * as invoiceService from '../services/invoice.service.js';
|
|
4
6
|
import mongoose from 'mongoose';
|
|
7
|
+
import dayjs from 'dayjs';
|
|
5
8
|
|
|
6
9
|
|
|
7
10
|
export const subscribedStoreList = async ( req, res ) => {
|
|
@@ -308,6 +311,8 @@ export const deleteBillingGroup = async ( req, res ) => {
|
|
|
308
311
|
|
|
309
312
|
export const getBillingGroups = async ( req, res ) => {
|
|
310
313
|
try {
|
|
314
|
+
const allGroups = await countDocuments( { clientId: req.body.clientId } );
|
|
315
|
+
|
|
311
316
|
const matchStage = {
|
|
312
317
|
$match: {
|
|
313
318
|
clientId: req.body.clientId,
|
|
@@ -434,6 +439,7 @@ export const getBillingGroups = async ( req, res ) => {
|
|
|
434
439
|
await download( exportResult, res );
|
|
435
440
|
return;
|
|
436
441
|
}
|
|
442
|
+
groupList[0].pageInfo['total'] = allGroups;
|
|
437
443
|
|
|
438
444
|
return res.sendSuccess( groupList[0] );
|
|
439
445
|
} catch ( error ) {
|
|
@@ -443,3 +449,274 @@ export const getBillingGroups = async ( req, res ) => {
|
|
|
443
449
|
};
|
|
444
450
|
|
|
445
451
|
|
|
452
|
+
export const getInvoices = async ( req, res ) => {
|
|
453
|
+
try {
|
|
454
|
+
const allInvoices = await invoice.count( { clientId: req.body.clientId } );
|
|
455
|
+
|
|
456
|
+
const matchStage = {
|
|
457
|
+
$match: {
|
|
458
|
+
clientId: req.body.clientId,
|
|
459
|
+
},
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
let filterStartDate = '';
|
|
463
|
+
let filterEndDate = '';
|
|
464
|
+
|
|
465
|
+
if ( req.body?.filter && req.body.filter == 'current' ) {
|
|
466
|
+
filterStartDate = new Date( dayjs().startOf( 'month' ).format( 'YYYY-MM-DD' ) );
|
|
467
|
+
filterEndDate = new Date( dayjs().endOf( 'month' ).format( 'YYYY-MM-DD' ) );
|
|
468
|
+
}
|
|
469
|
+
if ( req.body?.filter && req.body.filter == 'prev' ) {
|
|
470
|
+
filterStartDate = new Date( dayjs().subtract( 1, 'month' ).startOf( 'month' ).format( 'YYYY-MM-DD' ) );
|
|
471
|
+
filterEndDate = new Date( dayjs().subtract( 1, 'month' ).endOf( 'month' ).format( 'YYYY-MM-DD' ) );
|
|
472
|
+
}
|
|
473
|
+
if ( req.body?.filter && req.body.filter == 'last' ) {
|
|
474
|
+
filterStartDate = new Date( dayjs().subtract( 3, 'month' ).startOf( 'month' ).format( 'YYYY-MM-DD' ) );
|
|
475
|
+
filterEndDate = new Date( dayjs().endOf( 'month' ).format( 'YYYY-MM-DD' ) );
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if ( req.body?.filter && !req.body?.searchValue ) {
|
|
479
|
+
matchStage.$match['$and'] = [
|
|
480
|
+
{ billingDate: { $gte: filterStartDate } },
|
|
481
|
+
{ billingDate: { $lte: filterEndDate } },
|
|
482
|
+
];
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
if ( req.body.searchValue ) {
|
|
486
|
+
matchStage.$match.$or = [
|
|
487
|
+
{
|
|
488
|
+
invoice: {
|
|
489
|
+
$regex: req.body.searchValue,
|
|
490
|
+
$options: 'i',
|
|
491
|
+
},
|
|
492
|
+
},
|
|
493
|
+
];
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
const pipeline = [
|
|
498
|
+
matchStage,
|
|
499
|
+
{
|
|
500
|
+
$project: {
|
|
501
|
+
'_id': 1,
|
|
502
|
+
'invoice': 1,
|
|
503
|
+
'billingDate': 1,
|
|
504
|
+
'totalAmount': 1,
|
|
505
|
+
'stores': 1,
|
|
506
|
+
'paymentStatus': 1,
|
|
507
|
+
'paymentTerm': 1,
|
|
508
|
+
'currency': 1,
|
|
509
|
+
'products.productName': 1,
|
|
510
|
+
},
|
|
511
|
+
},
|
|
512
|
+
];
|
|
513
|
+
|
|
514
|
+
if ( req.body?.sortColumn && req.body?.sortBy ) {
|
|
515
|
+
pipeline.push(
|
|
516
|
+
{
|
|
517
|
+
$addFields: {
|
|
518
|
+
sortField: {
|
|
519
|
+
$toLower: `$${req.body.sortColumn}`,
|
|
520
|
+
},
|
|
521
|
+
},
|
|
522
|
+
},
|
|
523
|
+
{
|
|
524
|
+
$sort: {
|
|
525
|
+
[req.body.sortColumn]: req.body.sortBy,
|
|
526
|
+
},
|
|
527
|
+
},
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
pipeline.push(
|
|
532
|
+
{
|
|
533
|
+
$project: {
|
|
534
|
+
sortField: 0,
|
|
535
|
+
},
|
|
536
|
+
},
|
|
537
|
+
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
const facetStage = {
|
|
541
|
+
$facet: {
|
|
542
|
+
data: [
|
|
543
|
+
{
|
|
544
|
+
$skip: ( ( req.body.offset - 1 ) * req.body.limit ),
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
$limit: ( req.body.limit ),
|
|
548
|
+
},
|
|
549
|
+
],
|
|
550
|
+
pageInfo: [
|
|
551
|
+
{
|
|
552
|
+
$count: 'count',
|
|
553
|
+
},
|
|
554
|
+
],
|
|
555
|
+
},
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
if ( req.body?.isExport ) {
|
|
559
|
+
facetStage.$facet.data = [];
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
pipeline.push( facetStage );
|
|
563
|
+
|
|
564
|
+
pipeline.push( {
|
|
565
|
+
$unwind: {
|
|
566
|
+
path: '$pageInfo',
|
|
567
|
+
},
|
|
568
|
+
} );
|
|
569
|
+
|
|
570
|
+
const invoiceList = await invoice.aggregate( pipeline );
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
if ( !invoiceList[0] ) {
|
|
574
|
+
return res.sendError( 'No data found', 204 );
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
if ( req.body.isExport ) {
|
|
578
|
+
const exportResult = [];
|
|
579
|
+
for ( let invoice of invoiceList[0].data ) {
|
|
580
|
+
exportResult.push( {
|
|
581
|
+
'Invoice': invoice.invoice||'',
|
|
582
|
+
'Billing date': dayjs( invoice.billingDate ) ||'',
|
|
583
|
+
'Total amount': invoice.totalAmount||'',
|
|
584
|
+
'Store count': invoice.stores || '',
|
|
585
|
+
'Status': invoice.paymentStatus||'',
|
|
586
|
+
'Products': invoice.products.length ? invoice.products.map( ( val ) => val.productName ).join( ', ' ) : '',
|
|
587
|
+
} );
|
|
588
|
+
}
|
|
589
|
+
await download( exportResult, res );
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function transformData( data ) {
|
|
594
|
+
const result = [];
|
|
595
|
+
const groups = {};
|
|
596
|
+
|
|
597
|
+
data.forEach( ( invoiceData ) => {
|
|
598
|
+
if ( invoiceData.paymentStatus === 'unpaid' ) {
|
|
599
|
+
const currentDate = dayjs();
|
|
600
|
+
const givenDate = dayjs( invoiceData.billingDate );
|
|
601
|
+
const daysDifference = currentDate.diff( givenDate, 'day' );
|
|
602
|
+
if ( daysDifference <= invoiceData.paymentTerm && daysDifference >= 0 ) {
|
|
603
|
+
invoiceData.paymentStatus = 'due';
|
|
604
|
+
} else {
|
|
605
|
+
invoiceData.paymentStatus = 'unpaid';
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
invoiceData.products = invoiceData.products.map( ( product ) => product.productName );
|
|
609
|
+
const tempProduct = new Set();
|
|
610
|
+
invoiceData.products.forEach( ( product ) => {
|
|
611
|
+
tempProduct.add( product );
|
|
612
|
+
} );
|
|
613
|
+
invoiceData.products = Array.from( tempProduct );
|
|
614
|
+
|
|
615
|
+
const billingDate = invoiceData.billingDate;
|
|
616
|
+
|
|
617
|
+
if ( !groups[dayjs( billingDate ).format( 'MMMM' )] ) {
|
|
618
|
+
groups[dayjs( billingDate ).format( 'MMMM' )] = {
|
|
619
|
+
summary: `Summary - ${dayjs( billingDate ).format( 'MMM YYYY' )}`,
|
|
620
|
+
products: new Set(),
|
|
621
|
+
stores: 0,
|
|
622
|
+
totalAmount: 0,
|
|
623
|
+
billingDate: billingDate,
|
|
624
|
+
invoices: [],
|
|
625
|
+
paymentStatus: invoiceData.paymentStatus,
|
|
626
|
+
currency: invoiceData.currency,
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
groups[dayjs( billingDate ).format( 'MMMM' )].invoices.push( invoiceData );
|
|
631
|
+
groups[dayjs( billingDate ).format( 'MMMM' )].stores += invoiceData.stores;
|
|
632
|
+
groups[dayjs( billingDate ).format( 'MMMM' )].totalAmount += invoiceData.totalAmount;
|
|
633
|
+
|
|
634
|
+
if ( invoiceData.paymentStatus === 'unpaid' ) {
|
|
635
|
+
const currentDate = dayjs();
|
|
636
|
+
const givenDate = dayjs( invoiceData.billingDate );
|
|
637
|
+
const daysDifference = currentDate.diff( givenDate, 'day' );
|
|
638
|
+
if ( daysDifference <= invoiceData.paymentTerm && daysDifference >= 0 ) {
|
|
639
|
+
groups[dayjs( billingDate ).format( 'MMMM' )].paymentStatus = 'due';
|
|
640
|
+
} else {
|
|
641
|
+
groups[dayjs( billingDate ).format( 'MMMM' )].paymentStatus = 'unpaid';
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
invoiceData.products.forEach( ( product ) => {
|
|
646
|
+
groups[dayjs( billingDate ).format( 'MMMM' )].products.add( product );
|
|
647
|
+
} );
|
|
648
|
+
} );
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
// eslint-disable-next-line guard-for-in
|
|
652
|
+
for ( const billingDate in groups ) {
|
|
653
|
+
const group = groups[billingDate];
|
|
654
|
+
group.products = Array.from( group.products );
|
|
655
|
+
result.push( group );
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
return result;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
invoiceList[0].data = transformData( invoiceList[0].data );
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
invoiceList[0].pageInfo['total'] = allInvoices;
|
|
665
|
+
|
|
666
|
+
return res.sendSuccess( invoiceList[0] );
|
|
667
|
+
} catch ( error ) {
|
|
668
|
+
logger.error( { error: error, function: 'subscribedStoreList' } );
|
|
669
|
+
return res.sendError( error, 500 );
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
export const onetimePayment = async ( req, res ) => {
|
|
674
|
+
try {
|
|
675
|
+
const invoice = await invoiceService.findOne( { invoice: req.params.invoice } );
|
|
676
|
+
if ( !invoice ) {
|
|
677
|
+
return res.sendError( 'Invoice not found', 404 );
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
const onetimefeeObj = {
|
|
681
|
+
amount: req.body.oneTimeFee,
|
|
682
|
+
productName: 'installationFee',
|
|
683
|
+
description: 'One time setup & installation',
|
|
684
|
+
month: dayjs( invoice.billingDate ).format( 'MMM YYYY' ),
|
|
685
|
+
HsnNumber: '998314',
|
|
686
|
+
price: 0,
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
await invoiceService.invoiceUpdateOne( { invoice: invoice.invoice }, { $push: { products: onetimefeeObj } } );
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
if ( invoice.currency === 'inr' ) {
|
|
693
|
+
let totalTaxRate = 0;
|
|
694
|
+
|
|
695
|
+
if ( invoice.tax.length ) {
|
|
696
|
+
for ( let i = 0; i<invoice._doc.tax.length; i++ ) {
|
|
697
|
+
totalTaxRate += invoice._doc.tax[i].value;
|
|
698
|
+
invoice._doc.tax[i].taxAmount = Number( invoice._doc.tax[i].taxAmount ) + ( req.body.oneTimeFee * invoice._doc.tax[i].value ) / 100;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
invoice._doc.amount += req.body.oneTimeFee;
|
|
704
|
+
|
|
705
|
+
invoice._doc.totalAmount = ( ( invoice._doc.amount * totalTaxRate ) / 100 ) + invoice._doc.amount;
|
|
706
|
+
} else {
|
|
707
|
+
invoice._doc.amount += req.body.oneTimeFee;
|
|
708
|
+
invoice._doc.totalAmount += req.body.oneTimeFee;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
await invoiceService.invoiceUpdateOne( { invoice: invoice.invoice }, { $set: { 'tax': invoice._doc.tax, 'amount': invoice._doc.amount, 'totalAmount': invoice._doc.totalAmount } } );
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
return res.sendSuccess( 'Fee added successfully to invoice' );
|
|
716
|
+
} catch ( error ) {
|
|
717
|
+
logger.error( { error: error, function: 'onetimePayment' } );
|
|
718
|
+
return res.sendError( error, 500 );
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
|