Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newdevbranch #2

Open
wants to merge 4 commits into
base: dev-upgrade
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 5 additions & 1 deletion controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const { signNack, errorNack, ack } = require("../utils/responses");
const { dynamicReponse, dynamicFlow } = require("../core/operations/main");
const { configLoader } = require("../core/loadConfig");
const { comapreObjects } = require("../core/attributeValidation");
const {compareMessageId} = require("../core/contextMesValidation")
const {compareTransaction} = require("../core/contextTransValidation")



const ASYNC_MODE = "ASYNC";
const SYNC_MODE = "SYNC";
Expand Down Expand Up @@ -249,7 +253,7 @@ const businessToBecknMethod = async (body) => {
(transactionId = data.context.transaction_id),
(type = data.context.action),
(config = type);
seller = true;
seller = tru;
}

let session = body.session;
Expand Down
47 changes: 47 additions & 0 deletions core/contextMesValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
async function compareMessageId(examples, attributes, example_sets) {
let errors = [];

for (const key in examples) {
if (key !== "tags") {
if (typeof examples[key] === "object" && typeof attributes[key] === "object") {
if (!attributes[key]) {
console.log(`null value found for ${key} in ${example_sets}`);
errors.push(`null value found for ${key} in ${example_sets}`);
} else if (Array.isArray(examples[key])) {
for (let i = 0; i < examples[key]?.length; i++) {
const exampleItem = examples[key][i];
const attributeItem = attributes[key];
if (typeof exampleItem === "object" && typeof attributeItem === "object") {
const err = await compareMessageId(exampleItem, attributeItem, example_sets);
errors = [...errors, ...err];
} else if (typeof exampleItem === "string" && attributeItem && key === "message_id") {
if (exampleItem !== attributeItem) {
console.log(`Message ID mismatch for ${key} in ${example_sets}: Expected ${attributeItem}, Received ${exampleItem}`);
errors.push(`Message ID mismatch for ${key} in ${example_sets}: Expected ${attributeItem}, Received ${exampleItem}`);
}
} else {
const err = await compareMessageId(exampleItem, attributeItem, example_sets);
errors = [...errors, ...err];
}
}
} else if (typeof examples[key] === "string" && typeof attributes[key] === "string" && key === "message_id") {
if (examples[key] !== attributes[key]) {
console.log(`Message ID mismatch for ${key} in ${example_sets}: Expected ${attributes[key]}, Received ${examples[key]}`);
errors.push(`Message ID mismatch for ${key} in ${example_sets}: Expected ${attributes[key]}, Received ${examples[key]}`);
}
} else {
const err = await compareMessageId(examples[key], attributes[key], example_sets);
errors = [...errors, ...err];
}
} else if (!attributes.hasOwnProperty(key)) {
console.log(`keys not found, ${key} in ${example_sets}`);
errors.push(`keys not found, ${key} in ${example_sets}`);
}
}
}

return errors;
}

module.exports = { compareMessageId };

185 changes: 185 additions & 0 deletions core/contextTransValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// 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;
async function compareTransaction(example, attribute, example_sets, type) {
let errors = [];

for (const key in example) {
if (key !== "tags") {
if (typeof example[key] === "object" && typeof attribute[key] === "object") {
if (!attribute[key]) {
console.log(`null value found for ${key} in ${example_sets}`);
errors.push(`null value found for ${key} in ${example_sets}`);
} else if (Array.isArray(example[key])) {
for (let i = 0; i < example[key]?.length; i++) {
const exampleItem = example[key][i];
const attributeItem = attribute[key];
if (typeof exampleItem === "object" && typeof attributeItem === "object") {
const err = await compareTransaction(exampleItem, attributeItem, example_sets, type);
errors = [...errors, ...err];
} else if (typeof exampleItem === "string" && attributeItem && key === type) {
if (exampleItem !== attributeItem) {
console.log(`${type} mismatch for ${key} in ${example_sets}: Expected ${attributeItem}, Received ${exampleItem}`);
errors.push(`${type} mismatch for ${key} in ${example_sets}: Expected ${attributeItem}, Received ${exampleItem}`);
}
} else {
const err = await compareTransaction(exampleItem, attributeItem, example_sets, type);
errors = [...errors, ...err];
}
}
} else if (typeof example[key] === "string" && typeof attribute[key] === "string" && key === type) {
if (example[key] !== attribute[key]) {
console.log(`${type} mismatch for ${key} in ${example_sets}: Expected ${attribute[key]}, Received ${example[key]}`);
errors.push(`${type} mismatch for ${key} in ${example_sets}: Expected ${attribute[key]}, Received ${example[key]}`);
}
} else {
const err = await compareTransaction(example[key], attribute[key], example_sets, type);
errors = [...errors, ...err];
}
} else if (!attribute.hasOwnProperty(key)) {
console.log(`keys not found, ${key} in ${example_sets}`);
errors.push(`keys not found, ${key} in ${example_sets}`);
}
}
}

return errors;
}

module.exports = { compareTransaction };
Empty file added core/enumValidation.js
Empty file.
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