spee.ch/server/controllers/api/claim/get/index.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-04-27 18:54:36 +02:00
const { getClaim } = require('../../../../lbrynet');
const { createFileRecordDataAfterGet } = require('../../../../models/utils/createFileRecordData.js');
2018-04-27 18:54:36 +02:00
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const db = require('../../../../models');
2018-03-29 02:35:41 +02:00
/*
route to get a claim
*/
2018-03-29 20:24:52 +02:00
const claimGet = ({ ip, originalUrl, params }, res) => {
const name = params.name;
const claimId = params.claimId;
2018-07-26 02:21:02 +02:00
let resolveResult;
let getResult;
2018-03-29 20:24:52 +02:00
db.Claim.resolveClaim(name, claimId)
2018-07-26 02:21:02 +02:00
.then(result => {
if (!result) {
2018-03-29 20:24:52 +02:00
throw new Error('No matching uri found in Claim table');
}
2018-07-26 02:21:02 +02:00
resolveResult = result;
return getClaim(`${name}#${claimId}`);
2018-03-29 20:24:52 +02:00
})
2018-07-26 02:21:02 +02:00
.then(result => {
2018-08-07 21:54:13 +02:00
if (!result) {
throw new Error(`Unable to Get ${name}#${claimId}`);
}
2018-07-26 02:21:02 +02:00
getResult = result;
if (result.completed) {
return createFileRecordDataAfterGet(resolveResult, getResult)
.then(fileData => {
const upsertCriteria = {name, claimId};
return db.upsert(db.File, fileData, upsertCriteria, 'File');
});
}
2018-03-29 20:24:52 +02:00
})
2018-07-26 02:21:02 +02:00
.then(() => {
2018-07-26 02:29:59 +02:00
const { message, completed } = getResult;
2018-07-26 02:21:02 +02:00
res.status(200).json({
success: true,
message,
completed,
});
2018-03-29 20:24:52 +02:00
})
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
2018-03-29 02:35:41 +02:00
};
module.exports = claimGet;