1
1
import * as admin from "firebase-admin" ;
2
2
import * as functions from 'firebase-functions' ;
3
+ import * as requester from "request" ;
3
4
import { QuickBooks } from './libs/qbmain' ;
4
5
import { CompanySync } from "./models/Cloud/CompanySync" ;
5
6
import { OrderCreate } from './models/Cloud/OrderCreate' ;
6
- import { DaudiCustomer , emptyDaudiCustomer } from './models/Daudi/customer/Customer' ;
7
+ import { EmptyQboConfig } from "./models/Cloud/QboEnvironment" ;
8
+ import { emptyDaudiCustomer } from './models/Daudi/customer/Customer' ;
9
+ import { Order } from "./models/Daudi/order/Order" ;
7
10
import { SMS } from './models/Daudi/sms/sms' ;
8
11
import { MyTimestamp } from './models/firestore/firestoreTypes' ;
12
+ import { QboOrder } from "./models/Qbo/QboOrder" ;
13
+ import { toObject } from "./models/utils/SnapshotUtils" ;
9
14
import { creteOrder , updateOrder } from './tasks/crud/daudi/Order' ;
15
+ import { ReadAndInstantiate , readQboConfig } from "./tasks/crud/daudi/QboConfig" ;
10
16
import { updateCustomer } from './tasks/crud/qbo/customer/update' ;
11
17
import { createQboOrder } from './tasks/crud/qbo/Order/create' ;
12
- import { createQbo } from './tasks/sharedqb' ;
13
18
import { sendsms } from './tasks/sms/sms' ;
14
19
import { ordersms } from './tasks/sms/smscompose' ;
15
20
import { processSync } from './tasks/syncdb/processSync' ;
16
21
import { validorderupdate } from './validators/orderupdate' ;
17
- import { readQboConfig , ReadAndInstantiate } from "./tasks/crud/daudi/QboConfig" ;
18
- import { toArray , toObject } from "./models/utils/SnapshotUtils" ;
19
- import { EmptyQboConfig , QboCofig } from "./models/Cloud/QboEnvironment" ;
20
- import { QboOrder } from "./models/Qbo/QboOrder" ;
21
- import { Order } from "./models/Daudi/order/Order" ;
22
- import * as requester from "request" ;
22
+ import { createQbo } from "./tasks/sharedqb" ;
23
+ import { EquityBulk } from "./models/ipn/EquityBulk" ;
24
+ import { paymentFcm } from "./tasks/FCM/paymentFcm" ;
25
+ import { resolveIpn } from "./tasks/resolvepayment" ;
23
26
24
27
admin . initializeApp ( functions . config ( ) . firebase ) ;
25
28
admin . firestore ( ) . settings ( { timestampsInSnapshots : true } ) ;
@@ -39,10 +42,11 @@ function markAsRunning(eventID: string) {
39
42
*/
40
43
exports . createEstimate = functions . https . onCall ( ( data : OrderCreate , context ) => {
41
44
console . log ( data )
45
+ // return creteOrder(data.order, data.omcId)
46
+
42
47
return ReadAndInstantiate ( data . omcId ) . then ( ( result ) => {
43
48
console . log ( result . qbo )
44
- const est = new createQboOrder ( data . order , result . config )
45
- return result . qbo . createEstimate ( est . formulate ( ) ) . then ( ( createResult ) => {
49
+ return result . qbo . createEstimate ( new createQboOrder ( data . order , result . config ) . QboOrder ) . then ( ( createResult ) => {
46
50
/**
47
51
* Only send sn SMS when estimate creation is complete
48
52
* Make the two processes run parallel so that none is blocking
@@ -60,8 +64,7 @@ exports.createInvoice = functions.https.onCall((data: OrderCreate, context) => {
60
64
console . log ( data )
61
65
return ReadAndInstantiate ( data . omcId ) . then ( ( result ) => {
62
66
console . log ( result . qbo )
63
- const inv = new createQboOrder ( data . order , result . config )
64
- return result . qbo . createInvoice ( inv . formulate ( ) ) . then ( ( createResult ) => {
67
+ return result . qbo . createInvoice ( new createQboOrder ( data . order , result . config ) . QboOrder ) . then ( ( createResult ) => {
65
68
/**
66
69
* Only send sn SMS when invoice creation is complete
67
70
* Make the two processes run parallel so that none is blocking
@@ -258,3 +261,79 @@ exports.onUserStatusChanged = functions.database
258
261
} ) ;
259
262
}
260
263
} ) ;
264
+
265
+ exports . ipnsandbox_db = functions . firestore
266
+ . document ( "/sandboxpayments/{ipnid}" )
267
+ . onCreate ( ( snap , context ) => {
268
+ // console.log(snap);
269
+ // A new customer has been updated
270
+ const eventID = context . eventId ;
271
+ if ( isAlreadyRunning ( eventID ) ) {
272
+ console . log (
273
+ "Ignore it because it is already running (eventId):" ,
274
+ eventID
275
+ ) ;
276
+ return true ;
277
+ }
278
+ markAsRunning ( eventID ) ;
279
+ const ipn = snap . data ( ) as EquityBulk ;
280
+ return resolveIpn ( ipn , context . params . ipnid ) . then ( ( ) => {
281
+ ipn . daudiFields . status = 2 ;
282
+ return paymentFcm ( ipn ) ;
283
+ } ) ;
284
+ } ) ;
285
+ exports . ipnprod_db = functions . firestore
286
+ . document ( "/prodpayments/{ipnid}" )
287
+ . onCreate ( ( snap , context ) => {
288
+ // console.log(snap);
289
+ // A new customer has been updated
290
+ const eventID = context . eventId ;
291
+ if ( isAlreadyRunning ( eventID ) ) {
292
+ console . log (
293
+ "Ignore it because it is already running (eventId):" ,
294
+ eventID
295
+ ) ;
296
+ return true ;
297
+ } else {
298
+ markAsRunning ( eventID ) ;
299
+ const ipn = snap . data ( ) as EquityBulk ;
300
+ return resolveIpn ( ipn , context . params . ipnid ) . then ( ( ) => {
301
+ ipn . daudiFields . status = 2 ;
302
+ return paymentFcm ( ipn ) ;
303
+ } ) ;
304
+ }
305
+ } ) ;
306
+
307
+ /**
308
+ * a way for the client app to execute a cloud function directly
309
+ */
310
+
311
+ exports . ipnsandboxcallable = functions . https . onCall ( ( data , context ) => {
312
+ const ipn = data as EquityBulk ;
313
+ console . log ( data ) ;
314
+ return resolveIpn ( ipn , ipn . billNumber . toString ( ) ) . then ( ( ) => {
315
+ /**
316
+ * force the fcm to send a payment received msg, because otherwise the new ipn data wont be known
317
+ * unless we make a db read before sending, which at this point is unneccessary
318
+ */
319
+ ipn . daudiFields . status = 2 ;
320
+ return paymentFcm ( ipn ) ;
321
+ } ) ;
322
+ } ) ;
323
+
324
+ /**
325
+ * a way for the client app to execute a cloud function directly
326
+ */
327
+
328
+ exports . ipnprodcallable = functions . https . onCall ( ( data , context ) => {
329
+ const ipn = data as EquityBulk ;
330
+ console . log ( data ) ;
331
+ return resolveIpn ( ipn , ipn . billNumber . toString ( ) ) . then ( ( ) => {
332
+ /**
333
+ * force the fcm to send a payment received msg, because otherwise the new ipn data wont be known
334
+ * unless we make a db read before sending, which at this point is unneccessary
335
+ */
336
+ ipn . daudiFields . status = 2 ;
337
+ return paymentFcm ( ipn ) ;
338
+ } ) ;
339
+ } ) ;
0 commit comments