Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RupalSingla committed Jun 20, 2024
1 parent 3034fe7 commit 1357c38
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .env.buyer
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
config_url= https://raw.githubusercontent.com/ONDC-Official/protocol-server-config/metro/build/build.json
PORT = 80
BUSINESS_SERVER_IS_SYNC = false
IS_VERIFY_AUTH = false
SERVER_TYPE = BAP
SUBSCRIBER_URL = https://2f59-59-145-217-117.ngrok-free.app
BACKEND_SERVER_URL= http://localhost:8000
GATEWAY_URL = "http://localhost:5500/ondc/"
PRIVATE_KEY=Un205TSOdDXTq8E+N/sJOLJ8xalnzZ1EUP1Wcv23sKx70fOfFd4Q2bzfpzPQ+6XZhZv65SH7Pr6YMk8SuFHpxQ==
SUBSCRIBER_ID=mobility-staging.ondc.org
SUBSCRIBER_UNIQUE_KEY=UK-MOBILITY
PROTOCOL_SERVER = https://2f59-59-145-217-117.ngrok-free.app
is_loadConfigFromGit = true

DATABASE_CONNECTION_STRING = mongodb://localhost:27017/protocolServerBAP
USE_DB = false
VERSION = 1.0.0
16 changes: 16 additions & 0 deletions .env.seller
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
config_url= https://raw.githubusercontent.com/ONDC-Official/protocol-server-config/metro-dev/build/build.json
PORT = 5500
IS_VERIFY_AUTH = false
SERVER_TYPE = BPP
SUBSCRIBER_URL = http://localhost:3000
BACKEND_SERVER_URL= http://localhost:5502
PRIVATE_KEY=Un205TSOdDXTq8E+N/sJOLJ8xalnzZ1EUP1Wcv23sKx70fOfFd4Q2bzfpzPQ+6XZhZv65SH7Pr6YMk8SuFHpxQ==
SUBSCRIBER_ID=mobility-staging.ondc.org
SUBSCRIBER_UNIQUE_KEY=UK-MOBILITY
PROTOCOL_SERVER = http://localhost:3000
localConfig= false
flow = metro-flow-1

DATABASE_CONNECTION_STRING = mongodb://localhost:27017/protocolServerBPP
USE_DB = false
VERSION = 1.0.0
139 changes: 139 additions & 0 deletions core/contextValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// const Ajv = require("ajv");
// const addFormats = require("ajv-formats");
// const ajvErrors = require("ajv-errors");

// const logger = require("../utils/logger").init();
// const { formatted_error } = require("../utils/utils");

// const ajv = new Ajv({
// allErrors: true,
// strict: "log",
// });

// // Add formats and custom error messages to Ajv
// addFormats(ajv);
// ajvErrors(ajv);

// // Define the schema for transaction ID validation
// const transactionIdSchema = {
// type: "object",
// properties: {
// context: {
// type: "object",
// properties: {
// transaction_id: { type: "string", minLength: 1, errorMessage: "Transaction ID is required and must be a non-empty string." }
// },
// required: ["transaction_id"],
// additionalProperties: true,
// }
// },
// required: ["context"],
// additionalProperties: true,
// errorMessage: {
// required: {
// "context.transaction_id": "Transaction ID is required."
// }
// }
// };

// /**
// * Middleware to validate transaction ID.
// * @param {Object} req - Express request object.
// * @param {Object} res - Express response object.
// * @param {Function} next - Express next middleware function.
// */
// const validateTransactionId = (req, res, next) => {
// const payload = req.body;
// const validate = ajv.compile(transactionIdSchema);

// const valid = validate(payload);
// if (!valid) {
// const errorList = validate.errors;
// const formattedErrors = formatted_error(errorList);
// logger.error(`Transaction ID validation failed: ${JSON.stringify(formattedErrors)}`);

// return res.status(400).json({ status: false, errors: formattedErrors });
// }

// logger.info(`Transaction ID validation succeeded for Transaction ID: ${payload.context.transaction_id}`);
// next();
// };

// module.exports = validateTransactionId;


const Ajv = require("ajv");
const addFormats = require("ajv-formats");
const ajvErrors = require("ajv-errors");

const logger = require("../utils/logger").init();
const { formatted_error } = require("../utils/utils");

const ajv = new Ajv({
allErrors: true,
strict: "log",
});

// Add formats and custom error messages to Ajv
addFormats(ajv);
ajvErrors(ajv);

// Define the schema for multiple transaction IDs validation
const transactionIdsSchema = {
type: "object",
properties: {
context: {
type: "object",
properties: {
transaction_id: { type: "string", minLength: 1, errorMessage: "Transaction ID is required and must be a non-empty string." },
secondary_transaction_id: { type: "string", minLength: 1, errorMessage: "Secondary Transaction ID must be a non-empty string." }
},
required: ["transaction_id"],
additionalProperties: true,
},
anotherContext: {
type: "object",
properties: {
another_transaction_id: { type: "string", minLength: 1, errorMessage: "Another Transaction ID must be a non-empty string." }
},
additionalProperties: true,
}
},
additionalProperties: true,
errorMessage: {
required: {
"context.transaction_id": "Transaction ID is required."
}
}
};

/**
* Middleware to validate multiple transaction IDs.
* @param {Object} req - Express request object.
* @param {Object} res - Express response object.
* @param {Function} next - Express next middleware function.
*/
const validateTransactionIds = (req, res, next) => {
const payload = req.body;
const validate = ajv.compile(transactionIdsSchema);

const valid = validate(payload);
if (!valid) {
const errorList = validate.errors;
const formattedErrors = formatted_error(errorList);
logger.error(`Transaction IDs validation failed: ${JSON.stringify(formattedErrors)}`);

return res.status(400).json({ status: false, errors: formattedErrors });
}

logger.info(`Transaction IDs validation succeeded for Transaction ID: ${payload.context.transaction_id}`);
if (payload.context.secondary_transaction_id) {
logger.info(`Secondary Transaction ID: ${payload.context.secondary_transaction_id}`);
}
if (payload.anotherContext && payload.anotherContext.another_transaction_id) {
logger.info(`Another Transaction ID: ${payload.anotherContext.another_transaction_id}`);
}
next();
};

module.exports = validateTransactionIds;
1 change: 1 addition & 0 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const formatted_error = (errors) => {
}`,
details: error.instancePath,
};

error_list.push(error_dict);
}
});
Expand Down

0 comments on commit 1357c38

Please sign in to comment.