From 8634d540b88aa1367846bacf015db0975627f3d2 Mon Sep 17 00:00:00 2001 From: alexandercerutti Date: Mon, 20 Aug 2018 16:40:04 +0200 Subject: [PATCH] Added relevace() method to set beacons and locations; Added schemas for beaconsDict and locationsDict --- index.js | 33 +++++++++++++++++++++++++++++++++ schema.js | 18 +++++++++++++++++- server.js | 9 ++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9b0876f..fa38ac0 100644 --- a/index.js +++ b/index.js @@ -194,6 +194,39 @@ class Pass { return this; } + relevance(type, data) { + let types = ["beacons", "locations", "maxDistance", "relevantDate"]; + + if (!type || !data || !types.includes(type)) { + return 0; + } + + if (type === "beacons" || type === "locations") { + if (!(data instanceof Array)) { + data = [data]; + } + + let valid = data.filter(d => schema.isValid(d, schema.constants[type+"Dict"])); + this.props[type] = valid; + + return valid.length; + } + + if (type === "maxDistance" && (typeof data === "string" || typeof data === "number")) { + this.props[type] = String(data); + + return 1; + } else if (type === "relevantDate") { + let convDate = dateToW3CString(data); + + if (convDate) { + this.props[type] = convDate; + } + + return Number(!!convDate); + } + } + /** * Checks if pass model type is one of the supported ones * diff --git a/schema.js b/schema.js index 5063f5e..804e588 100644 --- a/schema.js +++ b/schema.js @@ -30,6 +30,20 @@ let field = Joi.object().keys({ value: Joi.string().required() }); +let beaconsDict = Joi.object().keys({ + major: Joi.number().integer().positive().max(65536), + minor: Joi.number().integer().positive().max(65536), + proximityUUID: Joi.string().required(), + relevantText: Joi.string() +}); + +let locationsDict = Joi.object().keys({ + altitude: Joi.number(), + latitude: Joi.number().required(), + longitude: Joi.number().required(), + relevantText: Joi.string() +}); + let struct = { auxiliaryFields: Joi.array().items(field), backFields: Joi.array().items(field), @@ -49,7 +63,9 @@ module.exports = { barcode, field, basicStructure, - boardingStructure + boardingStructure, + beaconsDict, + locationsDict }, isValid: (opts, schemaName, debug = false) => { let validation = Joi.validate(opts, schemaName); diff --git a/server.js b/server.js index d1e84af..a6527ba 100644 --- a/server.js +++ b/server.js @@ -64,12 +64,19 @@ function manageRequest(request, response) { "LOCATION": "Location" }); - // non-existing language code + // non-existing language code get also inserted but (probabily) not used pass.localize("ol", { "EVENT": "numashat", "LOCATION": "abelret" }); + pass.relevance("locations", [{ + latitude: 45.601565099999995, + longitude: 12.320653600000002 + }]); + + pass.relevance("maxDistance", 150); + //pass.localize("zu", {}); pass.primaryFields.push({