From e9c60f652fb2fd559a9d411fd84abeb6a139b14e Mon Sep 17 00:00:00 2001 From: Travis Eden Date: Sun, 28 Oct 2018 14:00:31 -0400 Subject: [PATCH] use speech db over chainquery when more recently updated --- server/controllers/api/claim/data/index.js | 11 ++--------- server/utils/fetchClaimData.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 server/utils/fetchClaimData.js diff --git a/server/controllers/api/claim/data/index.js b/server/controllers/api/claim/data/index.js index 6efe4e83..95146c37 100644 --- a/server/controllers/api/claim/data/index.js +++ b/server/controllers/api/claim/data/index.js @@ -1,8 +1,8 @@ const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const getClaimData = require('server/utils/getClaimData'); +const fetchClaimData = require('server/utils/fetchClaimData'); const chainquery = require('chainquery'); const db = require('server/models'); - /* route to return data for a claim @@ -10,16 +10,9 @@ const db = require('server/models'); */ const claimData = async ({ ip, originalUrl, body, params }, res) => { - const claimName = params.claimName; - let claimId = params.claimId; - if (claimId === 'none') claimId = null; try { - let resolvedClaim = await chainquery.claim.queries.resolveClaim(claimName, claimId).catch(() => {}); - - if(!resolvedClaim) { - resolvedClaim = await db.Claim.resolveClaim(claimName, claimId); - } + const resolvedClaim = await fetchClaimData(params); if (!resolvedClaim) { return res.status(404).json({ diff --git a/server/utils/fetchClaimData.js b/server/utils/fetchClaimData.js new file mode 100644 index 00000000..d143062d --- /dev/null +++ b/server/utils/fetchClaimData.js @@ -0,0 +1,20 @@ +const chainquery = require('chainquery'); +const db = require('server/models'); + +const fetchClaimData = async (params) => { + const name = params.claimName; + let claimId = params.claimId; + if (claimId === 'none') claimId = null; + + const [cq, local] = await Promise.all([ + chainquery.claim.queries.resolveClaim(name, claimId).then(res => res.dataValues).catch(() => {}), + db.Claim.resolveClaim(name, claimId).catch(() => {}), + ]); + + if (!cq && !local) return null; + if (cq.name === name && !local) return cq; + if (local.name === name && !cq) return local; + return local.updatedAt > cq.modified_at ? local : cq; +}; + +module.exports = fetchClaimData;