use speech db over chainquery when more recently updated

This commit is contained in:
Travis Eden 2018-10-28 14:00:31 -04:00
parent dec8ade055
commit e9c60f652f
2 changed files with 22 additions and 9 deletions

View file

@ -1,8 +1,8 @@
const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const getClaimData = require('server/utils/getClaimData'); const getClaimData = require('server/utils/getClaimData');
const fetchClaimData = require('server/utils/fetchClaimData');
const chainquery = require('chainquery'); const chainquery = require('chainquery');
const db = require('server/models'); const db = require('server/models');
/* /*
route to return data for a claim 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 claimData = async ({ ip, originalUrl, body, params }, res) => {
const claimName = params.claimName;
let claimId = params.claimId;
if (claimId === 'none') claimId = null;
try { try {
let resolvedClaim = await chainquery.claim.queries.resolveClaim(claimName, claimId).catch(() => {}); const resolvedClaim = await fetchClaimData(params);
if(!resolvedClaim) {
resolvedClaim = await db.Claim.resolveClaim(claimName, claimId);
}
if (!resolvedClaim) { if (!resolvedClaim) {
return res.status(404).json({ return res.status(404).json({

View file

@ -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;