From 192284771736075720085d364963cb38ffc384b3 Mon Sep 17 00:00:00 2001 From: schneefux Date: Mon, 30 Oct 2017 20:46:07 +0100 Subject: generate UUIDs if API IDs aren't valid --- worker.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'worker.js') diff --git a/worker.js b/worker.js index f83ffb1..c8582a8 100644 --- a/worker.js +++ b/worker.js @@ -10,6 +10,7 @@ const amqp = require("amqplib"), Promise = require("bluebird"), + uuidV4 = require("uuid/v4"), winston = require("winston"), loggly = require("winston-loggly-bulk"), Seq = require("sequelize"), @@ -88,6 +89,19 @@ function flatten(obj) { return snakeCaseKeys(o); } +// check for a valid UUID v4 or generate one +const uuidV4Regex = /^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/i; +function uuidfy(id) { + if (uuidV4Regex.test(id)) { + return id; + } + + const uuid = uuidV4(); + logger.error("found invalid UUID, generated a new one", { id, uuid }); + console.trace(); + return uuid; +} + amqp.connect(RABBITMQ_URI).then(async (rabbit) => { global.process.on("SIGINT", () => { rabbit.close(); @@ -333,6 +347,7 @@ amqp.connect(RABBITMQ_URI).then(async (rabbit) => { // flatten a deep clone, the original object is needed // so it can be moved into the failed queue let player = flatten(JSON.parse(JSON.stringify(msg.content))); + player.api_id = uuidfy(player.api_id); player.created_at = new Date(Date.parse(player.created_at)); player.last_match_created_date = player.created_at; player.last_update = seq.fn("NOW"); // TODO set msg.timestamp in bridge and parse here @@ -352,11 +367,13 @@ amqp.connect(RABBITMQ_URI).then(async (rabbit) => { // data from `/matches` match_objects.forEach((msg) => { let match = JSON.parse(JSON.stringify(msg.content)); // deep clone + match.id = uuidfy(match.id); match.createdAt = new Date(Date.parse(match.createdAt)); // flatten jsonapi nested response into our db structure-like shape // also, push missing fields match.rosters = match.rosters.map((roster) => { + roster.id = uuidfy(roster.id); roster.matchApiId = match.id; // TODO backwards compatibility, all objects have shardId since May 10th roster.attributes.shardId = roster.attributes.shardId || match.attributes.shardId; @@ -368,6 +385,7 @@ amqp.connect(RABBITMQ_URI).then(async (rabbit) => { roster.attributes.stats.winner = false; roster.participants = roster.participants.map((participant) => { + participant.id = uuidfy(participant.id); // ! attributes added here need to be added via `calculate_participant_stats` too participant.attributes.shardId = participant.attributes.shardId || roster.attributes.shardId; participant.rosterApiId = roster.id; @@ -451,6 +469,7 @@ amqp.connect(RABBITMQ_URI).then(async (rabbit) => { return flatten(roster); }); match.assets = match.assets.map((asset) => { + asset.id = uuidfy(asset.id); asset.matchApiId = match.id; asset.attributes.shardId = asset.attributes.shardId || match.attributes.shardId; return flatten(asset); -- cgit v1.3.1