2021-12-31 09:33:23 +00:00
var express = require ( 'express' ) ;
var bodyparser = require ( 'body-parser' ) ;
var path = require ( 'path' ) ;
const async = require ( 'async' ) ;
const http = require ( "https" ) ;
var app = express ( ) ;
var db = require ( '../lib/db' ) ;
const multer = require ( 'multer' ) ;
const { monitorEventLoopDelay } = require ( 'perf_hooks' ) ;
app . use ( bodyparser . json ( ) ) ;
2021-12-31 14:08:02 +00:00
//1.getotp
2021-12-31 09:33:23 +00:00
app . post ( '/getOtp' , function ( requ , res ) {
var mob = requ . body . mobile ;
var ccode = requ . body . countryCode ;
var mobwithccode = ccode + mob ;
let sql = "SELECT * FROM register WHERE mobile=" + requ . body . mobile ;
let date _ob = new Date ( ) ;
let date = ( "0" + date _ob . getDate ( ) ) . slice ( - 2 ) ;
let month = ( "0" + ( date _ob . getMonth ( ) + 1 ) ) . slice ( - 2 ) ;
let year = date _ob . getFullYear ( ) ;
let hours = date _ob . getHours ( ) ;
let minutes = date _ob . getMinutes ( ) ;
let seconds = date _ob . getSeconds ( ) ;
var currentdatetime = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
const options = {
"method" : "GET" ,
"hostname" : "api.msg91.com" ,
"port" : null ,
"path" : "/api/v5/otp?template_id=619c52d5e986f307e67dec97&mobile=" + mobwithccode + "&authkey=319029A6b14aygCL5e4cd2a2P1&otp_length=6" ,
"headers" : {
"Content-Type" : "application/json"
}
} ;
const req = http . request ( options , function ( res ) {
const chunks = [ ] ;
res . on ( "data" , function ( chunk ) {
chunks . push ( chunk ) ;
} ) ;
res . on ( "end" , function ( ) {
const body = Buffer . concat ( chunks ) ;
console . log ( body . toString ( ) ) ;
} ) ;
} ) ;
req . write ( "{\"Value1\":\"Param1\",\"Value2\":\"Param2\",\"Value3\":\"Param3\"}" ) ;
req . end ( ) ;
}
else
{
let data =
{
mobile : requ . body . mobile ,
country _code : requ . body . countryCode ,
created : currentdatetime
}
let sqlin = "INSERT INTO register SET ?" ;
let query = db . query ( sqlin , data , ( err , results ) => {
if ( err ) throw err
const options = {
"method" : "GET" ,
"hostname" : "api.msg91.com" ,
"port" : null ,
"path" : "/api/v5/otp?template_id=619c52d5e986f307e67dec97&mobile=" + mobwithccode + "&authkey=319029A6b14aygCL5e4cd2a2P1&otp_length=6" ,
"headers" : {
"Content-Type" : "application/json"
}
} ;
const req = http . request ( options , function ( res ) {
const chunks = [ ] ;
res . on ( "data" , function ( chunk ) {
chunks . push ( chunk ) ;
} ) ;
res . on ( "end" , function ( ) {
const body = Buffer . concat ( chunks ) ;
console . log ( body . toString ( ) ) ;
} ) ;
} ) ;
req . write ( "{\"Value1\":\"Param1\",\"Value2\":\"Param2\",\"Value3\":\"Param3\"}" ) ;
req . end ( ) ;
} ) ;
}
var responseMsg = "OTP Sent to your Mobile Number" ;
var response _code = "1" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : responseMsg } ) ) ;
} ) ;
} ) ;
//=================================
2021-12-31 14:08:02 +00:00
//2.verifyotp
2021-12-31 09:33:23 +00:00
app . post ( '/verifyOtp' , function ( requ , rese ) {
let bufferObjmob = Buffer . from ( requ . body . mobile , "base64" ) ;
let mobile = bufferObjmob . toString ( "utf8" ) ;
let bufferObjotp = Buffer . from ( requ . body . otp , "base64" ) ;
let otp = bufferObjotp . toString ( "utf8" ) ;
let sql = "SELECT * FROM register WHERE mobile=" + mobile ;
let date _ob = new Date ( ) ;
let date = ( "0" + date _ob . getDate ( ) ) . slice ( - 2 ) ;
let month = ( "0" + ( date _ob . getMonth ( ) + 1 ) ) . slice ( - 2 ) ;
let year = date _ob . getFullYear ( ) ;
let hours = date _ob . getHours ( ) ;
let minutes = date _ob . getMinutes ( ) ;
let seconds = date _ob . getSeconds ( ) ;
var currentdatetime = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 )
{
var userId = results [ 0 ] [ "id" ] ;
var ccode = results [ 0 ] [ "country_code" ] ;
var mobwithccode = ccode + mobile ;
const options = {
"method" : "GET" ,
"hostname" : "api.msg91.com" ,
"port" : null ,
"path" : "/api/v5/otp/verify?authkey=319029A6b14aygCL5e4cd2a2P1&mobile=" + mobwithccode + "&otp=" + otp + "" ,
"headers" : { }
} ;
const req = http . request ( options , function ( res ) {
const chunks = [ ] ;
res . on ( "data" , function ( chunk ) {
chunks . push ( chunk ) ;
} ) ;
res . on ( "end" , function ( ) {
const body = Buffer . concat ( chunks ) ;
var finalresult = body . toString ( ) ;
var jsonParsedArray = JSON . parse ( finalresult ) ;
var alertinfo = jsonParsedArray [ "message" ] ;
var alerttype = jsonParsedArray [ "type" ] ;
if ( alerttype == "success" ) {
var sql = "UPDATE register SET mobile_verification =" + otp + " ,mobile_verify_status =1 ,device_type=" + requ . body . deviceType + " ,device_token=" + requ . body . deviceToken + ",device_unique_id='" + requ . body . deviceId + "' WHERE id=" + userId ;
var response _code = "2" ;
}
else
{
var sql = "UPDATE register SET mobile_verification =" + otp + " WHERE id=" + userId ;
var response _code = "3" ;
}
let query = db . query ( sql , ( err , result ) => {
if ( err ) throw err
rese . send ( JSON . stringify ( { "code" : response _code , "message" : alertinfo , "userId" : userId } ) ) ;
} ) ;
} ) ;
} ) ;
req . write ( "{\"Value1\":\"Param1\",\"Value2\":\"Param2\",\"Value3\":\"Param3\"}" ) ;
req . end ( ) ;
}
} ) ;
} ) ;
//=========================
2021-12-31 14:08:02 +00:00
//3.resend
2021-12-31 09:33:23 +00:00
app . post ( '/resendOtp' , function ( requ , rese ) {
let sql = "SELECT * FROM register WHERE mobile=" + requ . body . mobile ;
let date _ob = new Date ( ) ;
let date = ( "0" + date _ob . getDate ( ) ) . slice ( - 2 ) ;
let month = ( "0" + ( date _ob . getMonth ( ) + 1 ) ) . slice ( - 2 ) ;
let year = date _ob . getFullYear ( ) ;
let hours = date _ob . getHours ( ) ;
let minutes = date _ob . getMinutes ( ) ;
let seconds = date _ob . getSeconds ( ) ;
var currentdatetime = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
var userId = results [ 0 ] [ "id" ] ;
var ccode = results [ 0 ] [ "country_code" ] ;
var mobwithccode = ccode + requ . body . mobile ;
const options = {
"method" : "GET" ,
"hostname" : "api.msg91.com" ,
"port" : null ,
"path" : "/api/v5/otp/retry?authkey=319029A6b14aygCL5e4cd2a2P1&retrytype=text&mobile=" + mobwithccode + "" ,
"headers" : { }
} ;
const req = http . request ( options , function ( res ) {
const chunks = [ ] ;
res . on ( "data" , function ( chunk ) {
chunks . push ( chunk ) ;
} ) ;
res . on ( "end" , function ( ) {
const body = Buffer . concat ( chunks ) ;
var finalresult = body . toString ( ) ;
var jsonParsedArray = JSON . parse ( finalresult ) ;
var alertinfo = jsonParsedArray [ "message" ] ;
var alerttype = jsonParsedArray [ "type" ] ;
var response _code = "4" ;
rese . send ( JSON . stringify ( { "code" : response _code , "message" : alertinfo } ) ) ;
} ) ;
} ) ;
req . write ( "{\"Value1\":\"Param1\",\"Value2\":\"Param2\",\"Value3\":\"Param3\"}" ) ;
req . end ( ) ;
}
} ) ;
} ) ;
//=================================
2021-12-31 14:08:02 +00:00
//4. List Plans
2021-12-31 09:33:23 +00:00
app . get ( '/listPlans' , function ( req , res ) {
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
var trialused = results [ 0 ] [ "trial_status" ] ;
if ( trialused == "0" )
{
let sqlp = "SELECT * FROM plan WHERE status=1" ;
let query = db . query ( sqlp , ( err , results ) => {
var response _code = "1" ;
var response _msg = "Success" ;
if ( err ) throw err
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : results } ) ) ;
} ) ;
}
else
{
let sqlp = "SELECT * FROM plan WHERE status=1 and user_type!=1" ; //user_type 1->free,0->payment
let query = db . query ( sqlp , ( err , results ) => {
var response _code = "1" ;
var response _msg = "Success" ;
if ( err ) throw err
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : results } ) ) ;
} ) ;
}
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
} ) ;
} ) ;
//========================
2021-12-31 14:08:02 +00:00
//5.subscribe plans
2021-12-31 09:33:23 +00:00
app . post ( '/subscribePlans' , function ( req , res ) {
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
let date _ob = new Date ( ) ;
let date = ( "0" + date _ob . getDate ( ) ) . slice ( - 2 ) ;
let month = ( "0" + ( date _ob . getMonth ( ) + 1 ) ) . slice ( - 2 ) ;
let year = date _ob . getFullYear ( ) ;
let hours = date _ob . getHours ( ) ;
let minutes = date _ob . getMinutes ( ) ;
let seconds = date _ob . getSeconds ( ) ;
let sql1 = "SELECT * FROM plan WHERE status=1" ;
let query = db . query ( sql1 , ( err , resultss ) => {
if ( err ) throw err
if ( resultss . length > 0 ) {
for ( let n in resultss )
{
var numrow = resultss [ n ] ;
var days = numrow . days ;
var planId = numrow . id ;
var planName = numrow . name ;
var userType = numrow . user _type ; //1-freeplan,2-payment
var last = new Date ( date _ob . getTime ( ) + ( days * 24 * 60 * 60 * 1000 ) ) ;
var eday = last . getDate ( ) ;
var emonth = last . getMonth ( ) + 1 ;
var eyear = last . getFullYear ( ) ;
//plan start & end
var currentdatetime = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds ;
var plan _end _date = eyear + "-" + emonth + "-" + eday + " " + hours + ":" + minutes + ":" + seconds ;
var plid = req . body . planType ;
if ( plid == planId )
{
if ( userType == "1" ) {
var sql = "UPDATE register SET status=2, trial_status=1,plan_type =" + plid + " ,plan_start ='" + currentdatetime + "' , plan_end ='" + plan _end _date + "' WHERE mobile=" + req . body . mobile ;
}
else
{
var sql = "UPDATE register SET status=2,plan_type =" + plid + " ,plan_start ='" + currentdatetime + "' , plan_end ='" + plan _end _date + "' WHERE mobile=" + req . body . mobile ;
}
var response _code = "1" ;
var response _msg = "" + planName + " Subscribed Successfully" ;
let querys = db . query ( sql , ( err , resultss ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "planId" : plid } ) ) ;
} ) ;
}
}
}
} ) ;
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
} ) ;
} ) ;
//==================
2021-12-31 14:08:02 +00:00
//6.News
2021-12-31 09:33:23 +00:00
app . get ( '/listNews' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
let sql = "SELECT * FROM news WHERE status=1 ORDER BY id DESC" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
if ( results . length > 0 ) {
var response _code = "1" ;
var response _msg = "Success" ;
2021-12-31 14:08:02 +00:00
2021-12-31 09:33:23 +00:00
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : results } ) ) ;
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
} ) ;
2021-12-31 09:33:23 +00:00
} ) ;
//=======================
2021-12-31 14:08:02 +00:00
//7.categories
2021-12-31 09:33:23 +00:00
app . get ( '/listCategories' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
2021-12-31 09:33:23 +00:00
let sql = "SELECT * FROM category WHERE status=1" ;
let query = db . query ( sql , ( err , results ) => {
var process = results ;
let finalresult = [ ] ;
async . eachSeries (
process ,
( resulta , callback ) => {
var CID = resulta . id ;
let sql1 = "SELECT id FROM product WHERE category_id=" + CID ;
db . query ( sql1 , ( err , res ) => { if ( err ) throw err ;
let sql2 = "SELECT DISTINCT mp.market_id as marketCount FROM product as p left join market_product as mp on mp.product_id=p.id WHERE category_id=" + CID ;
db . query ( sql2 , ( err , marCount ) => { if ( err ) throw err ;
resulta . marketsCounts = marCount . length ;
} ) ;
resulta . varietiesCounts = res . length ;
let resultupdated = resulta ;
finalresult . push ( resultupdated ) ;
callback ( null , resultupdated ) ;
} ) ;
} ,
( err ) => {
if ( err ) throw err ;
2021-12-31 14:08:02 +00:00
var response _code = "1" ;
var response _msg = "success" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : finalresult } ) ) ;
2021-12-31 09:33:23 +00:00
} ,
) ;
2021-12-31 14:08:02 +00:00
} ) ;
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//=================================
//8.markets
2021-12-31 09:33:23 +00:00
app . get ( '/listMarkets' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
2021-12-31 09:33:23 +00:00
let sql = "SELECT m.id,m.location,m.city,m.state,m.country,m.product_id,m.category_id,m.created,m.updated,m.status,m.created_by,m.name as marketName,ca.name as categoryName,c.name as cityName,cn.name as countryName,s.name as stateName FROM market as m LEFT JOIN cities as c ON c.id=m.city LEFT JOIN countries as cn ON cn.id=m.country LEFT JOIN states as s ON s.id=m.state LEFT JOIN category as ca on ca.id=m.category_id WHERE m.status=1" ;
let query = db . query ( sql , ( err , results ) => {
if ( results . length > 0 ) {
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : results } ) ) ;
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
} ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
//====================
//9.Detailed category info
2021-12-31 09:33:23 +00:00
app . get ( '/categoryDetail/:categoryId' , ( req , res ) => {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
let sql = "SELECT * FROM product WHERE category_id=" + req . params . categoryId + " status=1"
let query = db . query ( sql , ( err , resultss ) => {
2021-12-31 09:33:23 +00:00
if ( err ) throw err ;
2021-12-31 14:08:02 +00:00
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
2021-12-31 14:08:02 +00:00
var process = resultss ;
2021-12-31 09:33:23 +00:00
let finalresult = [ ] ;
async . eachSeries (
process ,
( result , callback ) => {
var PID = result . id ;
let sql1 = "SELECT DISTINCT market_id as marketCount FROM market_product WHERE product_id=" + PID ;
db . query ( sql1 , ( err , marCount ) => {
var marketCounts = marCount . length ;
result . marketCounts = marketCounts ;
let resultupdated = result ;
finalresult . push ( resultupdated ) ;
callback ( null , resultupdated ) ;
} ) ;
} ,
( err ) => {
if ( err ) throw err ;
2021-12-31 14:08:02 +00:00
var response _code = "1" ;
var response _msg = "success" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : finalresult } ) ) ;
2021-12-31 09:33:23 +00:00
} ,
) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "0" ;
var response _msg = "No Data" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : finalresult } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//==========================
//10.Total items from market
2021-12-31 09:33:23 +00:00
app . get ( '/totalItemFromMarket' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
let sql = "SELECT count(id)as marketItemCount FROM product_price WHERE created_date > DATE_SUB(NOW(), INTERVAL 1 WEEK) and status=1" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
if ( results . length > 0 ) {
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : results } ) ) ;
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//==========================
2021-12-31 09:33:23 +00:00
//8. Recent News
app . get ( '/recentNewsCount' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
let sql = "SELECT count(id)as recentNews FROM news WHERE created_date > DATE_SUB(NOW(), INTERVAL 1 WEEK) and status=1" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
if ( results . length > 0 ) {
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : results } ) ) ;
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//==========================
//11. statewise product with price using product id
2021-12-31 09:33:23 +00:00
app . get ( '/statewiseProduct/:productId' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
2021-12-31 09:33:23 +00:00
let sql = "SELECT DISTINCT s.name as stateName, s.id FROM market as m inner join states as s on s.id=m.state where m.status=1" ;
2021-12-31 14:08:02 +00:00
let query = db . query ( sql , ( err , resultss ) => {
2021-12-31 09:33:23 +00:00
if ( err ) throw err ;
2021-12-31 14:08:02 +00:00
var process = resultss ;
2021-12-31 09:33:23 +00:00
let finalresult = [ ] ;
async . eachSeries (
process ,
( result , callback ) => {
var SID = result . id ;
2021-12-31 14:08:02 +00:00
let sql1 = "SELECT pp.created_date,m.name as marketName,pp.price,pp.units,me.name as measurement from product_price as pp left join market as m on m.id=pp.market_id left join product as p on p.id=pp.product_id left join measurement as me on me.id=pp.measurement WHERE m.state=" + SID + " and pp.status=1 and pp.product_id=" + req . params . productId ;
2021-12-31 09:33:23 +00:00
db . query ( sql1 , ( err , priceinfo ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err ;
2021-12-31 09:33:23 +00:00
result . priceinfo = priceinfo ;
let resultupdated = result ;
finalresult . push ( resultupdated ) ;
callback ( null , resultupdated ) ;
} ) ;
} ,
( err ) => {
if ( err ) throw err ;
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : "1" , "message" : "success" , "data" : finalresult } ) ) ;
2021-12-31 09:33:23 +00:00
} ,
) ;
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//==============================
//12.News category
2021-12-31 09:33:23 +00:00
app . get ( '/listNewsCategory' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err
2021-12-31 09:33:23 +00:00
if ( results . length > 0 ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM news_category WHERE status=1 ORDER BY id DESC" ;
let query = db . query ( sql , ( err , resultss ) => {
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : resultss } ) ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
} ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
//==========================
//13.This week filter
2021-12-31 09:33:23 +00:00
app . get ( '/thisweekNews' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err
2021-12-31 09:33:23 +00:00
if ( results . length > 0 ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM news WHERE created_date > DATE_SUB(NOW(), INTERVAL 1 WEEK) and status=1" ;
let query = db . query ( sql , ( err , resultss ) => {
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : resultss } ) ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
} ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
//==================================
//14.next week filter
2021-12-31 09:33:23 +00:00
app . get ( '/nextweekNews' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err
2021-12-31 09:33:23 +00:00
if ( results . length > 0 ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM news WHERE created_date >= DATE_ADD(NOW(), INTERVAL 1 WEEK) and status=1" ;
let query = db . query ( sql , ( err , resultss ) => {
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : resultss } ) ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//==========================
//15.last month filter
2021-12-31 09:33:23 +00:00
app . get ( '/lastmonthNews' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err
2021-12-31 09:33:23 +00:00
if ( results . length > 0 ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM news WHERE YEAR(created_date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(created_date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) and status=1" ;
let query = db . query ( sql , ( err , resultss ) => {
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : resultss } ) ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//=================
//16.custom range filter
2021-12-31 09:33:23 +00:00
app . get ( '/customrangeNews/:startDate/:endDate' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err
2021-12-31 09:33:23 +00:00
if ( results . length > 0 ) {
2021-12-31 14:08:02 +00:00
let sql = "select * from news where created_date between '" + req . params . startDate + "' and '" + req . params . endDate + "' and status=1" ;
let query = db . query ( sql , ( err , resultss ) => {
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : resultss } ) ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
} ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
//==============
//17.particular news category
2021-12-31 09:33:23 +00:00
app . get ( '/listNewsCategory/:newsCatId' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err
2021-12-31 09:33:23 +00:00
if ( results . length > 0 ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM news where categories=" + req . params . newsCatId + " and status=1" ;
let query = db . query ( sql , ( err , resultss ) => {
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : resultss } ) ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//=======================
//18.particular news details
2021-12-31 09:33:23 +00:00
app . get ( '/listNews/:newsId' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err
2021-12-31 09:33:23 +00:00
if ( results . length > 0 ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM news where id=" + req . params . newsId + " and status=1" ;
let query = db . query ( sql , ( err , resultss ) => {
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : resultss } ) ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//======================
//19.price graph using productid & marketid
2021-12-31 09:33:23 +00:00
app . get ( '/weeklyPriceGraph/:marketId/:productId' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err
2021-12-31 09:33:23 +00:00
if ( results . length > 0 ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM product_price where created_date >= DATE_SUB(NOW(), INTERVAL 1 WEEK) and product_id=" + req . params . productId + " AND market_id=" + req . params . marketId + " and status=1" ;
let query = db . query ( sql , ( err , resultss ) => {
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : resultss } ) ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
} ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
//=================
//20.price graph using productid & marketid //lastmonth
2021-12-31 09:33:23 +00:00
app . get ( '/monthlyPriceGraph/:marketId/:productId' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
2021-12-31 09:33:23 +00:00
let query = db . query ( sql , ( err , results ) => {
2021-12-31 14:08:02 +00:00
if ( err ) throw err
2021-12-31 09:33:23 +00:00
if ( results . length > 0 ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM product_price where YEAR(created_date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(created_date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) and product_id=" + req . params . productId + " AND market_id=" + req . params . marketId + " and status=1" ;
let query = db . query ( sql , ( err , resultss ) => {
if ( err ) throw err
if ( resultss . length > 0 ) {
2021-12-31 09:33:23 +00:00
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
2021-12-31 14:08:02 +00:00
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : resultss } ) ) ;
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//=====================
//21.price graph using productid & marketid==>pending
2021-12-31 09:33:23 +00:00
app . get ( '/statewisePriceGraph/:stateId/:productId' , function ( req , res ) {
2021-12-31 14:08:02 +00:00
let sql = "SELECT * FROM register WHERE id=" + req . body . userId + " and device_unique_id='" + req . body . deviceId + "'" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
if ( results . length > 0 ) {
2021-12-31 09:33:23 +00:00
let sql = "SELECT pp.*,m.name as marketName FROM product_price as pp left join market as m on m.id=pp.market_id where pp.product_id=" + req . params . productId + " AND m.state=" + req . params . stateId + " and pp.status=1" ;
console . log ( sql ) ;
let query = db . query ( sql , ( err , results ) => {
if ( results . length > 0 ) {
var response _code = "1" ;
var response _msg = "Success" ;
}
else
{
2021-12-31 14:08:02 +00:00
var response _code = "0" ;
2021-12-31 09:33:23 +00:00
var response _msg = "No Data" ;
}
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : results } ) ) ;
} ) ;
2021-12-31 14:08:02 +00:00
}
else
{
var response _code = "2" ;
var response _msg = "This mobile number is register on another device please check" ;
res . send ( JSON . stringify ( { "code" : response _code , "message" : response _msg , "data" : "" } ) ) ;
}
2021-12-31 09:33:23 +00:00
} ) ;
2021-12-31 14:08:02 +00:00
} ) ;
//===========================================================
//************API END***************************************** */
//=============================================================
2021-12-31 09:33:23 +00:00
//==================================================================================================
//| |
//| API CRUD FOR CATEGORY |
//| |
//==================================================================================================
//======================================
//upload image
const filestorageengine = multer . diskStorage ( {
destination : ( req , file , cb ) => {
cb ( null , "./uploads" ) ;
} ,
filename : ( req , file , cb ) =>
{
console . log ( file ) ;
cb ( null , "ca-" + Date . now ( ) + path . extname ( file . originalname ) ) ;
}
} ) ;
const upload = multer ( {
storage : filestorageengine
} ) ;
//2.Create all categories
app . post ( '/insertCategories' , upload . single ( 'categoryImages' ) , function ( req , res ) {
let data =
{
name : req . body . categoryName ,
description : req . body . categoryDescription ,
status : req . body . categoryStatus ,
added _date : Date . now ( ) ,
created _by : req . body . sessionid ,
image : req . file . filename
}
let sql = "INSERT INTO category SET ?" ;
let query = db . query ( sql , data , ( err , results ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//===========================================
//3.Detailed category info
app . get ( '/showCategories/:categoryId' , ( req , res ) => {
let sql = "SELECT * FROM category WHERE id=" + req . params . categoryId ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err ;
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//===========================================
//4.Update category info
//upload image
const filestorageEngine = multer . diskStorage ( {
destination : ( req , file , cb ) => {
cb ( null , "./uploads" ) ;
} ,
filename : ( req , file , cb ) =>
{
cb ( null , "ca-" + Date . now ( ) + path . extname ( file . originalname ) ) ;
}
} ) ;
const uploads = multer ( {
storage : filestorageEngine
} ) ;
app . put ( '/updateCategories/(:categoryId)' , uploads . single ( 'categoryImages' ) , function ( req , res ) {
let new _image = "" ;
if ( req . file ) {
new _image = req . file . filename ;
}
let data = {
name : req . body . categoryName ,
description : req . body . categoryDescription ,
status : req . body . categoryStatus ,
added _date : Date . now ( ) ,
created _by : req . body . sessionid ,
image : new _image
} ;
let sql = "UPDATE category SET ? WHERE id=" + req . params . categoryId ;
let query = db . query ( sql , data , ( err , results ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//=============================================================
//5.Delete category
app . put ( '/deleteCategories/:categoryId' , ( req , res ) => {
let data = { status : 0 } ;
let sql = "UPDATE category SET ? WHERE id=" + req . params . categoryId + "" ;
let query = db . query ( sql , data , ( err , results ) => {
if ( err ) throw err ;
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//===============================================================
//==================================================================================================
//| |
//| API CRUD FOR Markets |
//| |
//==================================================================================================
//1.List Markets
//===============================================
//2.Create all markets
app . post ( '/insertMarkets' , function ( reqs , res ) {
let datas = {
name : reqs . body . marketName ,
location : reqs . body . marketLocation ,
city : reqs . body . marketCity ,
state : reqs . body . marketState ,
country : reqs . body . marketCountry ,
category _id : reqs . body . marketCategory _id ,
created : Date . now ( ) ,
status : reqs . body . marketStatus ,
created _by : reqs . body . marketCreated _by
} ;
let sql = "INSERT INTO market SET ?" ;
let query = db . query ( sql , datas , ( err , results ) => {
if ( err ) throw err ;
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//==================================================================
//3.Detailed market info
app . get ( '/showMarkets/:marketId' , function ( req , res ) {
let sql = "SELECT m.id,m.location,m.city,m.state,m.country,m.product_id,m.category_id,m.created,m.updated,m.status,m.created_by,m.name as marketName,ca.name as categoryName,c.name as cityName,cn.name as countryName,s.name as stateName FROM market as m LEFT JOIN cities as c ON c.id=m.city LEFT JOIN countries as cn ON cn.id=m.country LEFT JOIN states as s ON s.id=m.state LEFT JOIN category as ca on ca.id=m.category_id WHERE m.status=1 AND m.id=" + req . params . marketId ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err ;
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//======================================================================
//4.Update market info
app . put ( '/updatemarkets/(:marketId)' , function ( req , res ) {
let datas = {
name : req . body . marketName ,
location : req . body . marketLocation ,
city : req . body . marketCity ,
state : req . body . marketState ,
country : req . body . marketCountry ,
category _id : req . body . marketCategory _id ,
created : Date . now ( ) ,
status : req . body . marketStatus ,
created _by : req . body . marketCreated _by
} ;
let sql = "UPDATE market SET ? WHERE id=" + req . params . marketId ;
let query = db . query ( sql , datas , ( err , results ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//=========================================================
//5.Delete market
app . put ( '/deletemarkets/:markerId' , ( req , res ) => {
let data = { status : 0 } ;
let sql = "UPDATE market SET ? WHERE id=" + req . params . markerId + "" ;
let query = db . query ( sql , data , ( err , results ) => {
if ( err ) throw err ;
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//==========================================================
//==================================================================================================
//| |
//| API CRUD FOR Plans |
//| |
//==================================================================================================
//================================================
//2. Create all plans
app . post ( '/insertPlans' , function ( reqs , res ) {
let datas = {
name : reqs . body . planName ,
user _type : reqs . body . planUser _type ,
amount : reqs . body . planAmount ,
days : reqs . body . planDays ,
advertisement : reqs . body . planAdvertisement ,
description : reqs . body . planDescription ,
employee _id : reqs . body . planEmployee _id ,
status : reqs . body . planStatus ,
create _date : Date . now ( )
} ;
let sql = "INSERT INTO plan SET ?" ;
let query = db . query ( sql , datas , ( err , results ) => {
if ( err ) throw err ;
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//==================================================================
//3.Detailed plan info
app . get ( '/showPlans/:planId' , function ( req , res ) {
let sql = "SELECT * FROM plan WHERE status=1 AND id=" + req . params . planId ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err ;
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//======================================================================
//4.Update plan info
app . put ( '/updatePlans/(:planId)' , function ( reqs , res ) {
let datas = {
name : reqs . body . planName ,
user _type : reqs . body . planUser _type ,
amount : reqs . body . planAmount ,
days : reqs . body . planDays ,
advertisement : reqs . body . planAdvertisement ,
description : reqs . body . planDescription ,
employee _id : reqs . body . planEmployee _id ,
status : reqs . body . planStatus ,
create _date : Date . now ( )
} ;
let sql = "UPDATE plan SET ? WHERE id=" + reqs . params . planId ;
let query = db . query ( sql , datas , ( err , results ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//=========================================================
//5.Delete plan
app . put ( '/deletePlans/:planId' , ( req , res ) => {
let data = { status : 0 } ;
let sql = "UPDATE plan SET ? WHERE id=" + req . params . planId + "" ;
let query = db . query ( sql , data , ( err , results ) => {
if ( err ) throw err ;
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//==========================================================
//==================================================================================================
//| |
//| API CRUD FOR cities |
//| |
//==================================================================================================
//1. List cities
app . get ( '/getCities' , function ( req , res ) {
let sql = "SELECT * FROM cities" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//===============================
//==================================================================================================
//| |
//| API CRUD FOR country |
//| |
//==================================================================================================
//1. List country
app . get ( '/getCountries' , function ( req , res ) {
let sql = "SELECT * FROM countries" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//===============================
//==================================================================================================
//| |
//| API CRUD FOR state |
//| |
//==================================================================================================
//1. List state
app . get ( '/getStates' , function ( req , res ) {
let sql = "SELECT s.*,c.name as countryName FROM states as s LEFT JOIN countries as c on c.id=s.country_id" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//===============================
//==================================================================================================
//| |
//| API CRUD FOR measurements |
//| |
//==================================================================================================
//1. List measurements
app . get ( '/getMeasurements' , function ( req , res ) {
let sql = "SELECT * FROM measurement WHERE status=1" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
//==================================================================================================
//| |
//| API CRUD FOR role |
//| |
//==================================================================================================
//1. List role
app . get ( '/getRoles' , function ( req , res ) {
let sql = "SELECT * FROM role WHERE role_status=1" ;
let query = db . query ( sql , ( err , results ) => {
if ( err ) throw err
res . send ( JSON . stringify ( { "data" : results } ) ) ;
} ) ;
} ) ;
app . listen ( 8000 ) ;
module . exports = app ;