1
+ import TransactionsModel from "../../Database/Models/Transactions.model" ;
2
+ import Logger from "../../Lib/Logger" ;
3
+ import inquirer from 'inquirer' ;
4
+ import CustomerModel from "../../Database/Models/Customers/Customer.model" ;
5
+ import InvoiceModel from "../../Database/Models/Invoices.model" ;
6
+ import { A_CC_Payments } from "../../Types/PaymentMethod" ;
7
+ import { currencyCodes } from "../../Lib/Currencies" ;
8
+ import dateFormat from "date-and-time" ;
9
+ import mainEvent from "../../Events/Main.event" ;
10
+
11
+ export default
12
+ {
13
+ name : 'Transactions' ,
14
+ description : 'Get all transactions jobs' ,
15
+ args : [
16
+ {
17
+ name : 'action' ,
18
+ type : "list" ,
19
+ message : "Select the transaction job you want to run" ,
20
+ choices : [
21
+ {
22
+ name : 'Show transactions' ,
23
+ value : 'show_transactions' ,
24
+ } ,
25
+ {
26
+ name : 'Add transaction' ,
27
+ value : 'add_transaction' ,
28
+ } ,
29
+ {
30
+ name : 'Delete transaction' ,
31
+ value : 'delete_transaction' ,
32
+ }
33
+ ] ,
34
+ }
35
+ ] ,
36
+ method : async ( { action} : { action : string } ) =>
37
+ {
38
+ switch ( action )
39
+ {
40
+ case 'show_transactions' :
41
+ {
42
+ const transaction = await TransactionsModel . find ( ) ;
43
+ Logger . info ( `Transactions:` , transaction ) ;
44
+ break ;
45
+ }
46
+ case 'add_transaction' :
47
+ {
48
+ const action1 = [
49
+ {
50
+ name : "statement" ,
51
+ type : "list" ,
52
+ message : "Select the transaction statement" ,
53
+ choices : [
54
+ {
55
+ name : "Income" ,
56
+ value : "income" ,
57
+ } ,
58
+ {
59
+ name : "Expense" ,
60
+ value : "expense" ,
61
+ }
62
+ ] ,
63
+ }
64
+ ]
65
+
66
+ const { statement} = await inquirer . prompt ( action1 ) ;
67
+ const income_action = [
68
+ {
69
+ name : "customer_uid" ,
70
+ type : 'search-list' ,
71
+ message : 'Customer' ,
72
+ choices : ( await CustomerModel . find ( ) ) . map ( e =>
73
+ {
74
+ return {
75
+ name : `${ e . personal . first_name } ${ e . personal . last_name } (${ e . id } )` ,
76
+ value : e . id ,
77
+ }
78
+ } )
79
+ } ,
80
+ {
81
+ name : "invoice_uid" ,
82
+ type : 'search-list' ,
83
+ message : 'Invoice' ,
84
+ choices : ( await InvoiceModel . find ( ) ) . map ( e =>
85
+ {
86
+ return {
87
+ name : `#${ e . id } (${ e . amount } ${ e . currency } )` ,
88
+ value : e . id ,
89
+ }
90
+ } )
91
+ } ,
92
+ {
93
+ name : "amount" ,
94
+ type : 'number' ,
95
+ message : 'Amount' ,
96
+
97
+ } ,
98
+ {
99
+ name : "fees" ,
100
+ type : 'number' ,
101
+ message : 'Fees' ,
102
+ } ,
103
+ {
104
+ name : "payment_method" ,
105
+ type : 'search-list' ,
106
+ message : 'Payment method' ,
107
+ choices : A_CC_Payments
108
+ } ,
109
+ {
110
+ name : 'currency' ,
111
+ type : 'search-list' ,
112
+ message : 'Enter the currency' ,
113
+ choices : currencyCodes
114
+ } ,
115
+ {
116
+ name : 'date' ,
117
+ type : 'input' ,
118
+ message : 'Enter the date' ,
119
+ default : dateFormat . format ( new Date ( ) , 'YYYY-MM-DD' ) ,
120
+ }
121
+ ] ;
122
+ const expense_action = [
123
+ {
124
+ name : "invoice_id" ,
125
+ type : 'input' ,
126
+ message : 'Invoice ID' ,
127
+ } ,
128
+ {
129
+ name : "company" ,
130
+ type : 'input' ,
131
+ message : 'Company' ,
132
+ } ,
133
+ {
134
+ name : "amount" ,
135
+ type : 'number' ,
136
+ message : 'Amount' ,
137
+
138
+ } ,
139
+ {
140
+ name : "fees" ,
141
+ type : 'number' ,
142
+ message : 'Fees' ,
143
+ } ,
144
+ {
145
+ name : "payment_method" ,
146
+ type : 'search-list' ,
147
+ message : 'Payment method' ,
148
+ choices : A_CC_Payments
149
+ } ,
150
+ {
151
+ name : 'currency' ,
152
+ type : 'search-list' ,
153
+ message : 'Enter the currency' ,
154
+ choices : currencyCodes
155
+ } ,
156
+ {
157
+ name : "description" ,
158
+ type : 'input' ,
159
+ message : 'Description' ,
160
+ } ,
161
+ {
162
+ name : "notes" ,
163
+ type : 'input' ,
164
+ message : 'Notes' ,
165
+ } ,
166
+ {
167
+ name : 'date' ,
168
+ type : 'input' ,
169
+ message : 'Enter the date' ,
170
+ default : dateFormat . format ( new Date ( ) , 'YYYY-MM-DD' ) ,
171
+ } ,
172
+ ]
173
+
174
+ const picked_action = statement === 'income' ? income_action : expense_action ;
175
+
176
+ const result = await inquirer . prompt ( picked_action ) ;
177
+
178
+ switch ( statement )
179
+ {
180
+ case 'income' :
181
+ {
182
+ const { customer_uid, invoice_uid, amount, fees, payment_method, currency, date} = result ;
183
+ const transaction = await ( new TransactionsModel ( {
184
+ customer_uid,
185
+ invoice_uid,
186
+ amount,
187
+ fees,
188
+ payment_method,
189
+ currency,
190
+ date,
191
+ statement : 'income' ,
192
+ } ) ) . save ( ) ;
193
+ Logger . info ( `Transaction:` , transaction ) ;
194
+ mainEvent . emit ( 'transaction_created' , transaction ) ;
195
+ break ;
196
+ }
197
+ case 'expense' :
198
+ {
199
+ const { invoice_id, company, amount, fees, payment_method, currency, date, description, notes} = result ;
200
+ const transaction = await ( new TransactionsModel ( {
201
+ statement : 'expense' ,
202
+ expense_information : {
203
+ invoice_id,
204
+ company,
205
+ description,
206
+ notes,
207
+ } ,
208
+ amount,
209
+ fees,
210
+ payment_method,
211
+ currency,
212
+ date,
213
+ } ) ) . save ( ) ;
214
+ Logger . info ( `Transaction:` , transaction ) ;
215
+ mainEvent . emit ( 'transaction_created' , transaction ) ;
216
+ break ;
217
+ }
218
+ }
219
+ break ;
220
+ }
221
+ case 'delete_transaction' :
222
+ {
223
+ const transaction = await TransactionsModel . find ( ) ;
224
+ const action1 = [
225
+ {
226
+ name : "transaction_id" ,
227
+ type : "search-list" ,
228
+ message : "Select the transaction you want to delete" ,
229
+ choices : transaction . map ( e =>
230
+ {
231
+ return {
232
+ name : `#${ e . id } (${ e . amount } ${ e . currency } )` ,
233
+ value : e . id ,
234
+ }
235
+ }
236
+ )
237
+ }
238
+ ]
239
+ const { transaction_id} = await inquirer . prompt ( action1 ) ;
240
+ const transaction_to_delete = await TransactionsModel . findOne ( { id : transaction_id } ) ;
241
+ if ( ! transaction_to_delete )
242
+ return Logger . error ( `Transaction with id ${ transaction_id } not found` ) ;
243
+
244
+ await transaction_to_delete . remove ( ) ;
245
+ Logger . info ( `Transaction deleted:` , transaction_to_delete ) ;
246
+ mainEvent . emit ( 'transaction_deleted' , transaction_to_delete ) ;
247
+
248
+ break ;
249
+ }
250
+ }
251
+ return true ;
252
+ }
253
+ }
0 commit comments