Skip to content

Commit

Permalink
new test server
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoer committed Aug 31, 2022
1 parent 1301a0f commit 418e0fa
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 19 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"express-async-errors": "^3.1.1",
"mongoose": "^6.2.2",
"path": "^0.12.7",
"shortid": "^2.2.16"
Expand Down
4 changes: 2 additions & 2 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.initModules = ['environment', 'database', 'listen'];
module.exports.middlewareModules = ['express', 'routes', 'cors'];
module.exports.initModules = ['environment', 'asyncWrapper', 'database', 'listen'];
module.exports.middlewareModules = ['express', 'routes', 'cors', 'error'];

module.exports.apiRoutes = ['vendor', 'country'];

Expand Down
2 changes: 1 addition & 1 deletion src/controller/country.js
Original file line number Diff line number Diff line change
Expand Up @@ -2373,7 +2373,7 @@ export const getCountry = async (req, res, next) => {
};

export const searchCountry = async (req, res) => {
const filter = req.body.filter || {};
const filter = req.body.filter || {fields: [{condition: 'equal', value: true, dataField: 'euMember'}]};

res.send(await search(null, Country, searchTextFields, filter, defaultProjection, adminFields));
};
Expand Down
11 changes: 9 additions & 2 deletions src/controller/vendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const updateVendor = async (req, res) => {
res.send(await getFullVendor(vendor._id));
};
export const deleteVendor = async (req, res) => {
const {id} = req.body;
const {id} = req.params;
if (!id) throw new ErrorResponse(errors.INVALID_OR_MISSING_PROPERTY, 'id not found');

const vendor = await Vendor.findById(id);
Expand All @@ -63,7 +63,14 @@ export const deleteVendor = async (req, res) => {
res.send('OK');
};
export const searchVendor = async (req, res) => {
const filter = req.body.filter || {};
const filter = req.body.filter || {
filter: {
page: {
size: 500,
number: 0,
},
},
};

res.send(await search(null, Vendor, searchTextFields, filter, defaultProjection, adminFields, lookupInfo));
};
Expand Down
1 change: 1 addition & 0 deletions src/init/listen.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// const { PORT, NODE_ENV } = process.env
const errorHandler = require('utils/errorHandler');

module.exports = async (app) => {
// app.listen(PORT, () =>
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/cors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = async (app) => {
app.use(require('cors')())
}
app.use(require('cors')());
};
4 changes: 2 additions & 2 deletions src/routes/api/vendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {getVendor, searchVendor, createVendor, updateVendor, deleteVendor} from
const router = express.Router();

router.route('/search').post(searchVendor);
router.route('/:id').get(getVendor);
router.route('/').post(createVendor).put(updateVendor).delete(deleteVendor);
router.route('/:id').get(getVendor).delete(deleteVendor);
router.route('/').post(createVendor).put(updateVendor);

export default router;
20 changes: 10 additions & 10 deletions src/utils/errorHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import errors, {errorPriorities} from 'enums/errors';
import {createError} from 'controllers/error';
import errors, {errorPriorities} from 'helpers/errors';

const ErrorResponse = require('helpers/errorResponse');
let lastErrorString = '';
let lastErrorMillis = 0;
Expand All @@ -19,11 +19,11 @@ const errorHandler = (err, req, res, next) => {
// console.log('err stack', err.stack);
// console.log('req.user', req?.user);

if (lastErrorString !== errString || Date.now() - lastErrorMillis > 5000) {
createError(errObj, req?.user);
lastErrorString = errString;
lastErrorMillis = Date.now();
}
// if (lastErrorString !== errString || Date.now() - lastErrorMillis > 5000) {
// createError(errObj, req?.user);
// lastErrorString = errString;
// lastErrorMillis = Date.now();
// }

// Mongoose bad ObjectId
if (err.name === 'CastError') {
Expand All @@ -39,9 +39,9 @@ const errorHandler = (err, req, res, next) => {
if (err.name === 'ValidationError') {
error = new ErrorResponse(errors.VALIDATION_ERROR, err.message);
}
console.log('ERROR HANDLER');

console.log(error);
// console.log('ERROR HANDLER');
// console.log(error);
// console.log('res', res);

if (res) {
res.status(error.statusCode || 500).json({
Expand Down

0 comments on commit 418e0fa

Please sign in to comment.