use speech db over chainquery when more recently updated
This commit is contained in:
parent
dec8ade055
commit
e9c60f652f
2 changed files with 22 additions and 9 deletions
|
@ -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({
|
||||
|
|
20
server/utils/fetchClaimData.js
Normal file
20
server/utils/fetchClaimData.js
Normal 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;
|
Loading…
Add table
Reference in a new issue