Skip to content

Commit

Permalink
Merge pull request #1 from ONDC-Official/attribute_validation
Browse files Browse the repository at this point in the history
added attribute validation
  • Loading branch information
Sarthak-kavidayal authored May 1, 2024
2 parents 314a50d + 5b34ea9 commit 00d4d00
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 13 deletions.
41 changes: 28 additions & 13 deletions controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const logger = require("../utils/logger").init();
const { signNack, errorNack, ack } = require("../utils/responses");
const {dynamicReponse,dynamicFlow} = require("../core/operations/main");
const { configLoader } = require("../core/loadConfig");
const {
checkAttributes,
comapreObjects,
} = require("../core/attributeValidation");

const ASYNC_MODE = "ASYNC";
const SYNC_MODE = "SYNC";
Expand Down Expand Up @@ -82,12 +86,16 @@ const validateIncommingRequest = async (body, transaction_id, config, res) => {
}
}

const schema = configLoader.getSchema(session.configName)[config];
// const schema = configLoader.getSchema(session.configName)[config];

const schemaValidation = await validateSchema(body, schema);
if (!schemaValidation?.status) {
return res.status(400).send(schemaValidation.message);
}
// const schemaValidation = await validateSchema(body, schema);
// if (!schemaValidation?.status) {
// return res.status(400).send(schemaValidation.message);
// }

const attributeConfig = configLoader.getAttributeConfig(session.configName);

comapreObjects(body, attributeConfig[config], config);

logger.info("Recieved request: " + JSON.stringify(body?.context));
res.send(ack);
Expand All @@ -110,14 +118,19 @@ const handleRequest = async (response, session, sessionId) => {
return console.log("Message ID not defined");
}

// extarct protocol mapping
// const protocol = mapping[session.configName][action];
// const protocol = session.protocol[action];
const protocol = configLoader.getMapping(session.configName)[action];
// let becknPayload,updatedSession;
// mapping/extraction

if (is_buyer) {
let config = null;

session.calls.map((call) => {
if (call?.message_id === response.context.message_id) {
config = call.config;
}
});

console.log("config >>>>>", config);

const protocol = configLoader.getMapping(session.configName)[config];

const { result: businessPayload, session: updatedSession } =
extractBusinessData(action, response, session, protocol);

Expand Down Expand Up @@ -146,7 +159,7 @@ const handleRequest = async (response, session, sessionId) => {
delete updatedSession.schema;
}

logger.info("mode::::::::: " + mode);
logger.info("mode>>>>>>>>> " + mode);
if (mode === ASYNC_MODE) {
await axios.post(`${process.env.BACKEND_SERVER_URL}/${urlEndpint}`, {
businessPayload,
Expand All @@ -157,6 +170,8 @@ const handleRequest = async (response, session, sessionId) => {
});
}
} else {
const protocol = configLoader.getMapping(session.configName)[action];

let { callback, serviceUrl, sync } = dynamicReponse(
response,
session.api[action]
Expand Down
55 changes: 55 additions & 0 deletions core/attributeValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
async function checkAttributes(example_set, attribute_set) {
// console.log("exampleSets", example_set, attribute_set);

try {
for (const example_sets of Object.keys(example_set)) {
const { examples } = example_set[example_sets] || [];
for (const example of examples) {
//sending only matched examples=attribute set like search=search
if (attribute_set[example_sets]) {
const currentAttribute = attribute_set[example_sets];
// if(example_sets == "on_init")
await comapreObjects(example?.value, currentAttribute, example_sets);
} else {
console.log(`attribute not found for ${example_sets}`);
}
}
}
} catch (error) {
console.log(`Error checking attributes, ${error}`);
}
}

async function comapreObjects(examples, attributes, example_sets) {
for (const key in examples) {
//un-commnet this if key is not found
//console.log('key', key, examples[key])
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}`);
} else if (Array.isArray(examples[key])) {
for (let i = 0; i < examples[key]?.length; i++) {
const exampleItem = examples[key][i];
const attributeItem = attributes[key];
//use if array has no keys like: category_ids
if (typeof exampleItem === "string" && attributeItem) {
//found
} else {
await comapreObjects(exampleItem, attributeItem, example_sets);
}
}
} else {
await comapreObjects(examples[key], attributes[key], example_sets);
}
} else if (!attributes.hasOwnProperty(key)) {
console.log(`keys not found, ${key} in ${example_sets}`);
}
}
console.log("Attribute validation succesful");
}

module.exports = { checkAttributes, comapreObjects };
4 changes: 4 additions & 0 deletions core/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class ConfigLoader {

return mapping;
}

getAttributeConfig(configName) {
return this.config.attributes[configName];
}
}

const configLoader = new ConfigLoader();
Expand Down
Binary file added log_report.2024-04-17.gz
Binary file not shown.
Loading

0 comments on commit 00d4d00

Please sign in to comment.