1+ const { getRandomId, getProductDetails } = require ( "./utils" ) ;
2+ const { manageProducts, manageUsers } = require ( "./db" ) ;
3+ const { API_KEY } = require ( './config' ) ;
4+ const express = require ( 'express' ) ;
5+ const bot = require ( './bot' ) ;
6+
7+ //Globals
8+ const port = process . env . PORT || 3000 ;
9+ const app = express ( ) ;
10+
11+ app . use ( express . json ( ) ) ;
12+
13+ app . get ( '/' , async ( req , res ) => {
14+ const userId = Number ( req . query . userId ) ;
15+ if ( userId ) {
16+ try {
17+ const products = await manageProducts ( { userId } , 'read' ) ;
18+ res . status ( 200 ) . send ( JSON . stringify ( products ) ) ;
19+ } catch ( e ) {
20+ res . status ( 500 ) . send ( JSON . stringify ( { error : e . message } ) ) ;
21+ }
22+ } else {
23+ res . status ( 500 ) . send ( JSON . stringify ( { error : 'Please pass a valid user id.' } ) )
24+ }
25+ } )
26+
27+ app . post ( '/' , async ( req , res ) => {
28+ const { url, userId, email} = req . body ;
29+ const merchant = url . replace ( 'www.' , '' ) . split ( '//' ) [ 1 ] . split ( '.' ) [ 0 ] ;
30+ if ( userId && merchant . match ( / a m a z o n | f l i p k a r t / gi) ) {
31+ try {
32+ const details = await getProductDetails ( url , merchant ) ;
33+ if ( details . ok ) {
34+ const tracking_id = getRandomId ( ) ;
35+ await manageProducts ( { tracking_id, email, userId, merchant, title : details . title , link : details . link , initPrice : details . price , price : details . price } , 'update' ) ;
36+ res . status ( 200 ) . send ( JSON . stringify ( { ok : true , tracking_id} ) ) ;
37+ } else {
38+ res . status ( 500 ) . send ( JSON . stringify ( { error : 'Failed to add product to tracking list' } ) ) ;
39+ }
40+ } catch ( e ) {
41+ res . status ( 500 ) . send ( JSON . stringify ( { error : e . message } ) ) ;
42+ }
43+ } else {
44+ res . status ( 503 ) . send ( JSON . stringify ( { error : 'Either url or userId is missing/incorrect' } ) )
45+ }
46+ } ) ;
47+
48+ app . delete ( '/' , async ( req , res ) => {
49+ const { tracking_id, userId} = req . body ;
50+ if ( tracking_id , userId ) {
51+ try {
52+ await manageProducts ( { tracking_id, userId} , 'delete' ) ;
53+ res . status ( 200 ) . send ( JSON . stringify ( { ok : true } ) )
54+ } catch ( e ) {
55+ res . status ( 500 ) . send ( JSON . stringify ( { error : e . message } ) )
56+ }
57+ } else {
58+ res . status ( 503 ) . send ( JSON . stringify ( { error : `Either tracking_id or userId is incorrect/missing.` } ) )
59+ }
60+ } )
61+
62+ app . get ( '/stats' , async ( req , res ) => {
63+ try {
64+ const users = await manageUsers ( { } , 'read' ) ;
65+ const products = await manageProducts ( { } , 'read' ) ;
66+ res . status ( 200 ) . send ( JSON . stringify ( { users : users ?. result ?. length , products : products ?. result ?. length } ) )
67+ } catch ( e ) {
68+
69+ }
70+ } )
71+
72+ app . get ( '/info' , async ( req , res ) => {
73+ const { authorization} = req . headers ;
74+ console . log ( authorization ) ;
75+ if ( authorization && authorization . replace ( 'Bearer ' , '' ) == API_KEY ) {
76+ let users = ( await manageUsers ( { } , 'read' ) ) . result ;
77+ res . status ( 200 ) . send ( JSON . stringify ( users . map ( u => ( { id : u . id , name : u . name , mail : u ?. mail ?. trim ( ) } ) ) ) )
78+ }
79+ res . send ( { } )
80+ } )
81+
82+ app . listen ( port , async ( ) => console . log ( 'listening to port ' + port ) ) ;
83+ bot . start ( ) . then ( ( ) => console . log ( 'Bot launched!' ) ) ;
0 commit comments