From 83e037c298cfd790cc99f4e73fd493eb4965c21c Mon Sep 17 00:00:00 2001 From: "Josepablo C." Date: Thu, 5 Oct 2023 15:59:22 -0600 Subject: [PATCH] feat(Countries): Adding countries endpoint --- .env | 7 -- .gitignore | 1 + config/apiConfig_local.json | 35 ++++++++++ dotenv | 6 +- index.js | 49 ++++++++------ lib/Handlers/Companies.handler.js | 9 +++ lib/Handlers/Users.handler.js | 9 +++ lib/Misc.js | 30 +++++++++ lib/Models/branches.model.js | 16 +++++ lib/Models/budgets.model.js | 33 ++++++++++ lib/Models/cities.model.js | 11 ++++ lib/Models/companies.model.js | 65 +++++++++++++++++++ lib/Models/countries.model.js | 10 +++ lib/Models/loads.model.js | 90 ++++++++++++++++++++++++++ lib/Models/mailer.model.js | 8 +++ lib/Models/memberships.model.js | 10 +++ lib/Models/meta-data.model.js | 10 +++ lib/Models/meta-groups.model.js | 11 ++++ lib/Models/news.model.js | 26 ++++++++ lib/Models/orders.model.js | 11 ++++ lib/Models/product-categories.model.js | 8 +++ lib/Models/products.model.js | 9 +++ lib/Models/proposals.model.js | 21 ++++++ lib/Models/states.model.js | 12 ++++ lib/Models/trackings.model.js | 12 ++++ lib/Models/users.model.js | 64 ++++++++++++++++++ lib/Models/vehicles.model.js | 55 ++++++++++++++++ lib/jwtValidator.js | 17 ++--- sections/countries/routes.js | 8 +++ sections/countries/services.js | 17 +++++ sections/sections.js | 26 ++++++++ sections/test/services.js | 4 +- sections/users/routes.js | 4 +- sections/users/services.js | 12 ++-- 34 files changed, 669 insertions(+), 47 deletions(-) delete mode 100644 .env create mode 100644 config/apiConfig_local.json create mode 100644 lib/Handlers/Companies.handler.js create mode 100644 lib/Handlers/Users.handler.js create mode 100644 lib/Misc.js create mode 100644 lib/Models/branches.model.js create mode 100644 lib/Models/budgets.model.js create mode 100644 lib/Models/cities.model.js create mode 100644 lib/Models/companies.model.js create mode 100644 lib/Models/countries.model.js create mode 100644 lib/Models/loads.model.js create mode 100644 lib/Models/mailer.model.js create mode 100644 lib/Models/memberships.model.js create mode 100644 lib/Models/meta-data.model.js create mode 100644 lib/Models/meta-groups.model.js create mode 100644 lib/Models/news.model.js create mode 100644 lib/Models/orders.model.js create mode 100644 lib/Models/product-categories.model.js create mode 100644 lib/Models/products.model.js create mode 100644 lib/Models/proposals.model.js create mode 100644 lib/Models/states.model.js create mode 100644 lib/Models/trackings.model.js create mode 100644 lib/Models/users.model.js create mode 100644 lib/Models/vehicles.model.js create mode 100644 sections/countries/routes.js create mode 100644 sections/countries/services.js diff --git a/.env b/.env deleted file mode 100644 index ad40806..0000000 --- a/.env +++ /dev/null @@ -1,7 +0,0 @@ -SERVER_PORT=8080 -API_CONFIG=/config/apiConfig.json -ROOT_PATH=/home/josepablocb/Documents/Work/EnRuta/SysS/ETAAPI -############################ -# PATHS relative to ROOT -############################ -LIB_PATH=lib diff --git a/.gitignore b/.gitignore index 65d7854..06d14f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ .env package-lock.json +.env diff --git a/config/apiConfig_local.json b/config/apiConfig_local.json new file mode 100644 index 0000000..7c5c25f --- /dev/null +++ b/config/apiConfig_local.json @@ -0,0 +1,35 @@ +{ + "authentication": { + "jwtSecret":"9o3BBz0EsrwXliwEJ/SFuywZoN8=", + "jwtTimeout":720, + "tokenSecret":"9Z'jMt|(h_f(&/S+zv.K", + "jwtOptions": { + "header": { + "typ": "access" + }, + "audience": "https://www.etaviaporte.com", + "issuer": "etaviaporte", + "algorithm": "HS256", + "expiresIn": "1d" + } + }, + "version" : { + "version" : "0.0.0", + "name": "ETA Beta", + "date":"10/2023" + }, + "S3" : { + "accessKeyId": "AKIAXTQEUF6MLCHTUIKW", + "secretAccessKey": "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", + "bucket": "enruta", + "region": "us-west-1" + }, + "sendgrid" : { + "HOST": "smtp.sendgrid.net", + "PORT": "465", + "username": "apikey", + "API_KEY": "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", + "FROM": "noreply@etaviaporte.com" + }, + "mongodb": "mongodb://localhost/etaviaporte?retryWrites=true&w=majority" +} diff --git a/dotenv b/dotenv index f645ca9..c3fa0d8 100644 --- a/dotenv +++ b/dotenv @@ -1,7 +1,9 @@ SERVER_PORT=8080 -API_CONFIG=/config/apiConfig.json ROOT_PATH=/home/josepablocb/Documents/Work/EnRuta/SysS/ETAAPI +API_CONFIG=config/apiConfig.json ############################ # PATHS relative to ROOT ############################ -LIB_PATH=/lib +LIB_PATH=lib +MODELS_PATH=lib/Models +HANDLERS_PATH=lib/Handlers diff --git a/index.js b/index.js index 1171e8b..7b3885b 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,9 @@ 'use strict'; -const express = require('express'); require('dotenv').config(); -const { ROOT_PATH , LIB_PATH } = process.env; +const { ROOT_PATH, LIB_PATH, API_CONFIG } = process.env; +const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` ); +const express = require('express'); const cors = require('cors'); const compression = require('compression'); const morgan = require('morgan'); @@ -10,12 +11,20 @@ const helmet = require('helmet'); const bodyParser = require('body-parser'); const fileUpload = require('express-fileupload'); const sections = require('./sections/sections.js'); -const middleWares = require( `${ROOT_PATH}/${LIB_PATH}/Middlewares.js` ); +const middlewares = require( `${ROOT_PATH}/${LIB_PATH}/Middlewares.js` ); + +const mongoose = require('mongoose'); +mongoose.connect( + apiConfig.mongodb, + { useNewUrlParser: true } +).then( ( val ) => { + console.log( "MongoDB Connected" ); +});//catch throw error so service stops! const app = express(); const serverPort = process.env.SERVER_PORT || 3000; -app.use( middleWares.Auth ); +app.use( middlewares.Auth ); app.use( fileUpload({ limits: { fileSize: 4 * 1024 * 1024 }, @@ -39,24 +48,24 @@ app.use(morgan('dev')); app.use(helmet()); app.use(compression()); app.use(cors({ - origin: '*', - methods: [ - 'GET', - 'POST', - 'PATCH', - 'PUT', - 'DELETE' - ], - allowedHeaders: ['Content-Type', 'Authorization'] + origin: '*', + methods: [ + 'GET', + 'POST', + 'PATCH', + 'PUT', + 'DELETE' + ], + allowedHeaders: ['Content-Type', 'Authorization'] })); -app.use( middleWares.errorJSON ); +app.use( middlewares.errorJSON ); app.use( sections ); -app.use( middleWares.error404 ); +app.use( middlewares.error404 ); app.listen( serverPort , function(err){ - if( !err ){ - console.log('API listen on port', serverPort ); - }else{ - console.log( err ); - } + if( !err ){ + console.log('API listen on port', serverPort ); + }else{ + console.log( err ); + } }); diff --git a/lib/Handlers/Companies.handler.js b/lib/Handlers/Companies.handler.js new file mode 100644 index 0000000..f99c0c1 --- /dev/null +++ b/lib/Handlers/Companies.handler.js @@ -0,0 +1,9 @@ +'user strict'; +const { ROOT_PATH, HANDLERS_PATH, MODELS_PATH, API_CONFIG } = process.env; +const companiesModel = require( `${ROOT_PATH}/${MODELS_PATH}/companies.model.js` ); + +async function getCompanyData( id ){ + return await companiesModel.findById( id ); +} + +module.exports = { getCompanyData }; diff --git a/lib/Handlers/Users.handler.js b/lib/Handlers/Users.handler.js new file mode 100644 index 0000000..d06c0ab --- /dev/null +++ b/lib/Handlers/Users.handler.js @@ -0,0 +1,9 @@ +'user strict'; +const { ROOT_PATH, HANDLERS_PATH, MODELS_PATH, API_CONFIG } = process.env; +const usersModel = require( `${ROOT_PATH}/${MODELS_PATH}/users.model.js` ); + +async function getUserData( id ){ + return await usersModel.findById( id , { password : 0 } ); +} + +module.exports = { getUserData }; diff --git a/lib/Misc.js b/lib/Misc.js new file mode 100644 index 0000000..3a488cb --- /dev/null +++ b/lib/Misc.js @@ -0,0 +1,30 @@ +"use strict"; + +function getPagination( query ){ + let limit = { + page : 0, + elements : 10 + }; + + if( query.page ){ + limit.page = parseInt( query.page ) || 0; + } + if( query.elements ){ + limit.elements = parseInt( query.elements ) || 10; + } + return limit; +} + +async function queryPage( page , elements , model ){ + const skip = elements * page; + const total = await model.count(); + const list = await model.find( {} , null, { skip : skip } ); + return { + total : total, + limit : elements, + skip : skip, + data : list + } +} + +module.exports = { getPagination , queryPage }; \ No newline at end of file diff --git a/lib/Models/branches.model.js b/lib/Models/branches.model.js new file mode 100644 index 0000000..88a5803 --- /dev/null +++ b/lib/Models/branches.model.js @@ -0,0 +1,16 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + categories: [{ type: Schema.Types.ObjectId, ref: 'product-categories' }], + company: { type: Schema.Types.ObjectId, ref: 'companies' }, + branch_name: { type: String }, + phone: { type: String }, + city: { type: String }, + state: { type: String }, + truck_type: [{ type: String }], + description:{type: String}, + address : { type: String } +}); + +module.exports = mongoose.model( "branches", schema ); diff --git a/lib/Models/budgets.model.js b/lib/Models/budgets.model.js new file mode 100644 index 0000000..65bb5dd --- /dev/null +++ b/lib/Models/budgets.model.js @@ -0,0 +1,33 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + company: { type: Schema.Types.ObjectId, ref: 'companies', required: true}, + client: { type: String, required: true }, + material: { type: String }, + origin: { type: String }, + destination: { type: String }, + truck_type: { type: String }, + num_tons: { type: Number }, + price_per_ton: { type: Number }, + tonnage: { type: String }, + pickup_distance: { type: Number }, + delivery_distance: { type: Number }, + warehouse_distance: { type: Number }, + total_km_travel: { type: Number }, + cost_per_liter: { type: Number }, + fuel_price_per_liter: { type: Number }, + other_fuel_expenses: { type: Number }, + total_fuel_consumed: { type: Number }, + total_cost_fuel: { type: Number }, + driver_salary: { type: Number }, + accomadation_allowance: { type: Number }, + other_administrative_expenses: { type: Number }, + total_before_tax: { type: Number }, + total_utility_per_km: { type: Number }, + total_profit: { type: Number }, + profit_percentage: { type: Number }, + total_administrative_expenses: { type: Number } +}); + +module.exports = mongoose.model( "budgets", schema ); diff --git a/lib/Models/cities.model.js b/lib/Models/cities.model.js new file mode 100644 index 0000000..5d03ebb --- /dev/null +++ b/lib/Models/cities.model.js @@ -0,0 +1,11 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + country_name: { type: String }, + country_code: { type: String }, + state_name: { type: String }, + city_name: { type: String, required: true } +}); + +module.exports = mongoose.model( "cities", schema ); diff --git a/lib/Models/companies.model.js b/lib/Models/companies.model.js new file mode 100644 index 0000000..796b5b6 --- /dev/null +++ b/lib/Models/companies.model.js @@ -0,0 +1,65 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const meta_data = new Schema({ + meta_group: { type: String }, + meta_key: { type: String }, + meta_value: { type: String }, +}); + + +const company_contacts = new Schema({ + meta_key: { type: String }, + meta_value: { type: String }, +}); + +const address = new Schema({ + street_address1: { type: String }, + street_address2: { type: String }, + city: { type: String }, + state: { type: String }, + country: { type: String }, + zipcode: { type: String }, + landmark: { type: String }, + lat: { type: String }, + lng: { type: String }, +}); + +const schema = new Schema({ + company_serial_number: { type: String }, + company_code: { type: String }, + is_company: { type: String }, //1000 + company_name: { type: String, required: true }, + company_legal_name: { type: String }, + company_description: { type: String }, + rfc: { type: String }, + + + company_type: [{ type: String }], // SHIPPER , CARRIER + is_broker: { type: Boolean, default: false }, + membership: { type: String }, + membership_start_at: { type: Date }, + + meta_data: [meta_data], + categories: [{ type: Schema.Types.ObjectId, ref: 'productcategories' }], + products: { type: Schema.Types.ObjectId, ref: 'products' }, + users: [{ type: Schema.Types.ObjectId, ref: 'users' }], + branches: [{ type: Schema.Types.ObjectId, ref: 'branches' }], + company_city: [{ type: String }], + company_state: [{ type: String }], + truck_type: [{ type: String }], + + street_address1: { type: String }, + street_address2: { type: String }, + city: { type: String }, + state: { type: String }, + country: { type: String }, + zipcode: { type: String }, + landmark: { type: String }, + lat: { type: String }, + lng: { type: String }, + + is_hidden: { type: Boolean, default: false }, +}); + +module.exports = mongoose.model( "branches", schema ); diff --git a/lib/Models/countries.model.js b/lib/Models/countries.model.js new file mode 100644 index 0000000..8ee1402 --- /dev/null +++ b/lib/Models/countries.model.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + country_name: { type: String, required: true }, + country_code: { type: String, required: true }, + phone_code: { type: String, required: true } +}); + +module.exports = mongoose.model( "countries", schema ); diff --git a/lib/Models/loads.model.js b/lib/Models/loads.model.js new file mode 100644 index 0000000..3724727 --- /dev/null +++ b/lib/Models/loads.model.js @@ -0,0 +1,90 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const address = new Schema({ + company_name: { type: String }, + + street_address1: { type: String }, + street_address2: { type: String }, + city: { type: String }, + state: { type: String }, + country: { type: String }, + zipcode: { type: String }, + + landmark: { type: String }, + + lat: { type: String }, + lng: { type: String }, +}); + + +const pointSchema = new Schema({ + type: { + type: String, + enum: ['Point'], + required: true + }, + coordinates: { + type: [Number], + required: true + } +}); + + +const schema = new Schema({ + shipment_code: { type: String }, + company: { type: Schema.Types.ObjectId, ref: 'companies', required: true }, //shipper + carrier: { type: Schema.Types.ObjectId, ref: 'companies' }, // carrier + vehicle: { type: Schema.Types.ObjectId, ref: 'vehicles' }, + driver: { type: Schema.Types.ObjectId, ref: 'users' }, + posted_by: { type: Schema.Types.ObjectId, ref: 'users' }, // shipper + posted_by_name: { type: String }, // search purpose + bidder: { type: Schema.Types.ObjectId, ref: 'users' }, + + origin: address, + origin_geo: { + type: pointSchema, + }, + destination: address, + destination_geo: { + type: pointSchema, + }, + + categories: [{ type: Schema.Types.ObjectId, ref: 'product-categories' }], + product: { type: Schema.Types.ObjectId, ref: 'products' }, + + truck_type: { type: String }, + tyre_type: { type: String }, + weight: { type: Number }, + estimated_cost: { type: Number }, + + distance: { type: Number }, + actual_cost: { type: Number }, + + // 1. Posted Shipments (Posted) + // 2. Shipments Loading (Loading) + // 3. Enroute Shipments (In Transit) + // 4. Shipments in Unloading (Unloading) + // 5. Shipments pending finding truck + status: { type: String, default: 'Draft', enum: ['Draft', 'Published', 'Completed', 'Closed'] }, + load_status: { type: String, enum: ['Published', 'Loading', 'Transit', 'Downloading', 'Delivered'] }, + + + contract_start_date: { type: Date }, + contract_end_date: { type: Date }, + + est_loading_date: { type: Date }, + est_unloading_date: { type: Date }, + + published_date: { type: Date }, + loaded_date: { type: Date }, + transit_date: { type: Date }, + delivered_date: { type: Date }, + load_status_updated: { type: Date, default: Date.now() }, + notes: { type: String }, + + payment_term: { type: String }, + terms_and_conditions: { type: String }, +}); + +module.exports = mongoose.model( "loads", schema ); diff --git a/lib/Models/mailer.model.js b/lib/Models/mailer.model.js new file mode 100644 index 0000000..f695d81 --- /dev/null +++ b/lib/Models/mailer.model.js @@ -0,0 +1,8 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + text: { type: String, required: true } +}); + +module.exports = mongoose.model( "mailer", schema ); diff --git a/lib/Models/memberships.model.js b/lib/Models/memberships.model.js new file mode 100644 index 0000000..ea8deed --- /dev/null +++ b/lib/Models/memberships.model.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + name: { type: String, required: true }, // free, pro + price: { type: Number }, + validity : { type: Number }, // number 0f days +}); + +module.exports = mongoose.model( "memberships", schema ); diff --git a/lib/Models/meta-data.model.js b/lib/Models/meta-data.model.js new file mode 100644 index 0000000..ab10c8c --- /dev/null +++ b/lib/Models/meta-data.model.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + meta_group: { type: Schema.Types.ObjectId, ref: 'metagroups'},// settings, terms, collaborator + meta_key: { type: String, required: true }, // collaborator + meta_value: { type: String } +}); + +module.exports = mongoose.model( "metadatas", schema ); diff --git a/lib/Models/meta-groups.model.js b/lib/Models/meta-groups.model.js new file mode 100644 index 0000000..9a08421 --- /dev/null +++ b/lib/Models/meta-groups.model.js @@ -0,0 +1,11 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + group_label: { type: String, required: true }, + group_key: { type: String, required: true }, + group_field_type: { type: String,default:'text' }, // text, textarea, html, select + group_options: [{ type: String }] +}); + +module.exports = mongoose.model( "metagroups", schema ); diff --git a/lib/Models/news.model.js b/lib/Models/news.model.js new file mode 100644 index 0000000..a79b463 --- /dev/null +++ b/lib/Models/news.model.js @@ -0,0 +1,26 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const attachFile = new Schema({ + file:{ type: String }, + originalname:{ type: String }, + mimetype:{ type: String }, + focus_point:[{ + thumbnail:{ type: String, default:"main" }, + x:{ type: Number }, + y:{ type: Number }, + w:{ type: Number }, + h:{ type: Number }, + s:{ type: Number } + }] +}); + +const schema = new Schema({ + title : { type: String, required: true }, + description : { type: String, required: true }, + link_text : { type: String, required: true }, + link_url : { type: String, required: true }, + news_image: attachFile, +}); + +module.exports = mongoose.model( "news", schema ); diff --git a/lib/Models/orders.model.js b/lib/Models/orders.model.js new file mode 100644 index 0000000..12e6e88 --- /dev/null +++ b/lib/Models/orders.model.js @@ -0,0 +1,11 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + load: { type: Schema.Types.ObjectId, ref: 'loads' }, + shipper: { type: Schema.Types.ObjectId, ref: 'companies' }, // how offers load + carrier: { type: Schema.Types.ObjectId, ref: 'companies' }, // how transport the load + vehicle: { type: Schema.Types.ObjectId, ref: 'vehicles' }, +}); + +module.exports = mongoose.model( "orders", schema ); diff --git a/lib/Models/product-categories.model.js b/lib/Models/product-categories.model.js new file mode 100644 index 0000000..95067f7 --- /dev/null +++ b/lib/Models/product-categories.model.js @@ -0,0 +1,8 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + name: { type: String, required: true } // fruit, boxes, wood +}); + +module.exports = mongoose.model( "productcategories", schema ); diff --git a/lib/Models/products.model.js b/lib/Models/products.model.js new file mode 100644 index 0000000..cf8dd1d --- /dev/null +++ b/lib/Models/products.model.js @@ -0,0 +1,9 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + name: { type: String, required: true }, + segment: { type: String}, +}); + +module.exports = mongoose.model( "products", schema ); diff --git a/lib/Models/proposals.model.js b/lib/Models/proposals.model.js new file mode 100644 index 0000000..7c1a10e --- /dev/null +++ b/lib/Models/proposals.model.js @@ -0,0 +1,21 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + load: { type: Schema.Types.ObjectId, ref: 'loads' }, + shipper: { type: Schema.Types.ObjectId, ref: 'companies' }, // how offers load + carrier: { type: Schema.Types.ObjectId, ref: 'companies' }, // how transport the load + vehicle: { type: Schema.Types.ObjectId, ref: 'vehicles' }, + + bidder: { type: Schema.Types.ObjectId, ref: 'users' }, + + comment: { type: String }, + + is_withdrawn: { type: Boolean, default: false }, + + accepted_by: { type: Schema.Types.ObjectId, ref: 'users' }, + accepted_date: { type: Date }, + is_accepted: { type: Boolean, default: false }, +}); + +module.exports = mongoose.model( "proposals", schema ); diff --git a/lib/Models/states.model.js b/lib/Models/states.model.js new file mode 100644 index 0000000..65fdf34 --- /dev/null +++ b/lib/Models/states.model.js @@ -0,0 +1,12 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + country_name: { type: String}, + country_code: { type: String }, + state_name: { type: String, required: true }, + state_code: { type: String } +}); + +module.exports = mongoose.model( "states", schema ); + diff --git a/lib/Models/trackings.model.js b/lib/Models/trackings.model.js new file mode 100644 index 0000000..078b0bb --- /dev/null +++ b/lib/Models/trackings.model.js @@ -0,0 +1,12 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + lat: { type: String }, + lng: { type: String }, + user: { type: Schema.Types.ObjectId, ref: 'users' }, + load: { type: Schema.Types.ObjectId, ref: 'loads' }, + vehicle: { type: Schema.Types.ObjectId, ref: 'vehicles' }, +}); + +module.exports = mongoose.model( "trackings", schema ); diff --git a/lib/Models/users.model.js b/lib/Models/users.model.js new file mode 100644 index 0000000..15b1795 --- /dev/null +++ b/lib/Models/users.model.js @@ -0,0 +1,64 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const pointSchema = new Schema({ + type: { + type: String, + enum: ['Point'], + required: true + }, + coordinates: { + type: [Number], + required: true + } +}); + +const schema = new Schema({ + first_name: { type: String }, + last_name: { type: String }, + middle_name: { type: String }, + email: { type: String, unique: true, lowercase: true }, + password: { type: String }, + phone: { type: String }, + phone2: { type: String }, + permissions: [{ type: String, default: 'role_admin' }], + gender: { type: String }, + address: { type: String }, + dob: { type: String }, + + // vehicle_status: { type: String, enum: ['Free', 'Loading', 'Moving', 'Downloading'] }, + job_role: { type: String }, // admin, owner, driver, staff + + employee_id: { type: String }, //EM-1000-1 EM-1000-2 + company: { type: Schema.Types.ObjectId, ref: 'companies' }, + branch: { type: Schema.Types.ObjectId, ref: 'branches' }, + + vehicle: { type: Schema.Types.ObjectId, ref: 'vehicles' }, + + active_load: { type: Schema.Types.ObjectId, ref: 'loads' }, + + categories: [{ type: Schema.Types.ObjectId, ref: 'product-categories' }], + user_city: [{ type: String }], + user_state: [{ type: String }], + user_description: { type: String }, + truck_type: [{ type: String }], + + last_location_lat: { type: String }, + last_location_lng: { type: String }, + last_location_geo: { type: pointSchema }, + last_location_time: { type: Date }, + + isVerified: { type: Boolean }, + verifyToken: { type: String }, + verifyShortToken: { type: String }, + verifyExpires: { type: Date }, // or a long integer + verifyChanges: { type: Object }, // an object (key-value map), e.g. { field: "value" } + resetToken: { type: String }, + resetShortToken: { type: String }, + resetExpires: { type: Date }, // or a long integer + resetAttempts: { type: Number }, + + is_hidden: { type: Boolean, default: false }, +}); + +module.exports = mongoose.model( "users", schema ); diff --git a/lib/Models/vehicles.model.js b/lib/Models/vehicles.model.js new file mode 100644 index 0000000..6ca80f5 --- /dev/null +++ b/lib/Models/vehicles.model.js @@ -0,0 +1,55 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const pointSchema = new Schema({ + type: { + type: String, + enum: ['Point'], + required: true + }, + coordinates: { + type: [Number], + required: true + } +}); + +const schema = new Schema({ + company: { type: Schema.Types.ObjectId, ref: 'companies' }, // carrier + + vehicle_code: { type: String }, + vehicle_name: { type: String }, + vehicle_number: { type: String }, //camion001 002 etc + circulation_serial_number: { type: String }, + trailer_plate_1: { type: String }, + trailer_plate_2: { type: String }, + truck_type: { type: String }, + tyre_type: { type: String }, + city: { type: String }, + state: { type: String }, + + background_tracking: { type: Boolean, default: false }, + + status: { type: String, enum: ['Free', 'Loading', 'Transit', 'Downloading'] }, + + categories: [{ type: Schema.Types.ObjectId, ref: 'product-categories' }], + + published_date: { type: Date }, + available_date: { type: String }, + is_available: { type: Boolean, default: false }, + + active_load: { type: Schema.Types.ObjectId, ref: 'loads' }, + load_shipper: { type: Schema.Types.ObjectId, ref: 'companies' }, + + available_in: [{ type: String }], + + destino: { type: String }, + driver: { type: Schema.Types.ObjectId, ref: 'users' }, + notes: { type: String }, + + last_location_lat: { type: String }, + last_location_lng: { type: String }, + last_location_geo: { type: pointSchema }, + last_location_time: { type: Date }, +}); + +module.exports = mongoose.model( "vehicles", schema ); diff --git a/lib/jwtValidator.js b/lib/jwtValidator.js index b8ef7ba..0939635 100644 --- a/lib/jwtValidator.js +++ b/lib/jwtValidator.js @@ -1,17 +1,18 @@ 'user strict'; const { ROOT_PATH, API_CONFIG } = process.env; +const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` ); const jwt = require('jsonwebtoken'); - -const apiConfig = require(ROOT_PATH + API_CONFIG); -const secret = apiConfig.authentication.jwtSecret; +const jwtSecret = apiConfig.authentication.jwtSecret; function middleware( req, res, next ){ if( req.JWT ){ - req.JWT.payload = jwt.verify( req.JWT.raw, apiConfig.authentication.jwtSecret , (err, user) => { - if( err ){ - return res.status(401).send({error:"Unauthorized",code:401}); - } - }); + req.JWT.isValid = false; + req.JWT.payload = jwt.verify( req.JWT.raw, jwtSecret ); + if( !req.JWT.payload ){ + return res.status(401).send({error:"Unauthorized",code:401}); + }else{ + req.JWT.isValid = true; + } next(); }else{ return res.status(401).send({error:"Unauthorized",code:401}); diff --git a/sections/countries/routes.js b/sections/countries/routes.js new file mode 100644 index 0000000..1a2f08d --- /dev/null +++ b/sections/countries/routes.js @@ -0,0 +1,8 @@ +'use strict'; +const router = require('express').Router(); +const services= require('./services.js'); + +router.get('/', services.getCountriesList); +router.get('/:id', services.getCountry); + +module.exports = router; diff --git a/sections/countries/services.js b/sections/countries/services.js new file mode 100644 index 0000000..3973074 --- /dev/null +++ b/sections/countries/services.js @@ -0,0 +1,17 @@ +"use strict"; +const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env; +const { getPagination , queryPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` ); +const countriesModel = require( `${ROOT_PATH}/${MODELS_PATH}/countries.model.js` ); + +const getCountriesList = async(req, res) => { + const { page , elements } = getPagination( req.query ); + const retVal = await queryPage( page , elements, countriesModel ); + res.send( retVal ); +}; + +const getCountry = async(req, res) => { + const retVal = await countriesModel.findById( req.params.id ); + res.send( retVal ); +}; + +module.exports = { getCountriesList , getCountry }; \ No newline at end of file diff --git a/sections/sections.js b/sections/sections.js index e61a9b1..a45e991 100644 --- a/sections/sections.js +++ b/sections/sections.js @@ -7,11 +7,37 @@ const jwtValidator = require( `${ROOT_PATH}/${LIB_PATH}/jwtValidator.js` ); const test = require('./test/routes.js'); const users = require('./users/routes.js'); +const countries = require('./countries/routes.js'); router.use("/test", test); +router.use('/countries', countries); +router.use('/cities', test); +router.use('/states', test); router.use( jwtValidator.middleware ); router.use("/users", users); +router.use('/orders', test); +router.use('/companies', test); +router.use('/metaGroups', test); +router.use('/metaData', test); +router.use('/loads', test); +router.use('/vehicles', test); +router.use('/mailer', test); +router.use('/authmanagement', test); +router.use('/productCategories', test); +router.use('/memberships', test); +router.use('/checkAccount', test); +router.use('/proposals', test); +router.use('/bootresolvers', test); +router.use('/budgets', test); +router.use('/products', test); +router.use('/news', test); +router.use('/branches', test); +router.use('/trackings', test); +router.use('/upload', test); +router.use('/calendars', test); +router.use('/dashboard', test); + module.exports = router; diff --git a/sections/test/services.js b/sections/test/services.js index 316d4c2..3845aa6 100644 --- a/sections/test/services.js +++ b/sections/test/services.js @@ -1,6 +1,6 @@ "use strict"; - -const apiConfig = require( process.env.ROOT_PATH + process.env.API_CONFIG ); +const { ROOT_PATH, LIB_PATH, API_CONFIG } = process.env; +const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` ); const postTest = async(req, res) => { res.send({ diff --git a/sections/users/routes.js b/sections/users/routes.js index 86b17e3..b7f65c7 100644 --- a/sections/users/routes.js +++ b/sections/users/routes.js @@ -2,8 +2,8 @@ const router = require('express').Router(); const services= require('./services.js'); -router.get('/', services.getUsers); +router.get('/', services.getProfileData); router.get('/profile', services.getProfileData); -router.get('/:userId', services.getUserData); +router.get('/:userId', services.getProfileData); module.exports = router; diff --git a/sections/users/services.js b/sections/users/services.js index f5776a6..1aeb5b7 100644 --- a/sections/users/services.js +++ b/sections/users/services.js @@ -1,8 +1,8 @@ "use strict"; +const { ROOT_PATH, HANDLERS_PATH, API_CONFIG } = process.env; +const UsersHandler = require( `${ROOT_PATH}/${HANDLERS_PATH}/Users.handler.js` ); -const apiConfig = require( process.env.ROOT_PATH + process.env.API_CONFIG ); - -const getUsers = async(req, res) => { +const getUsersList = async(req, res) => { console.log( req.params ); res.send({ user : "hello world!" }); }; @@ -13,8 +13,8 @@ const getUserData = async(req, res) => { }; const getProfileData = async(req, res) => { - console.log( req.params ); - res.send({ user : "hello world!" }); + const user = await UsersHandler.getUserData( req.JWT.payload.sub ); + res.send( user ); }; -module.exports = { getUsers , getUserData , getProfileData}; \ No newline at end of file +module.exports = { getUsersList , getUserData , getProfileData}; \ No newline at end of file