diff --git a/node_common/managers/library.js b/node_common/managers/library.js index 250a34d4..d761ae13 100644 --- a/node_common/managers/library.js +++ b/node_common/managers/library.js @@ -17,9 +17,7 @@ export const createBucket = ({ id, name }) => { // NOTE(jim): // Every root level user gets a bucket. -export const init = ({ bucketName, readableName }) => [ - createBucket({ id: bucketName, name: readableName }), -]; +export const init = ({ bucketName, readableName }) => [createBucket({ id: bucketName, name: readableName })]; export const createLocalDataIncomplete = ({ type, size, name }) => { return { @@ -75,7 +73,7 @@ export const updateDataById = ({ user, id, data }) => { for (let j = 0; j < library[i].children.length; j++) { if (library[i].children[j].id === id) { library[i].children[j] = data; - break; + return { ...user.data, library }; } } } diff --git a/pages/api/data/cid-status.js b/pages/api/data/cid-status.js index 338f60cb..7d11c827 100644 --- a/pages/api/data/cid-status.js +++ b/pages/api/data/cid-status.js @@ -21,75 +21,76 @@ export default async (req, res) => { initAuth(req, res); if (!req.body.data) { - return res - .status(500) - .send({ decorator: "SERVER_NO_CIDS_TO_CHECK", error: true }); + return res.status(500).send({ decorator: "SERVER_NO_CIDS_TO_CHECK", error: true }); + } + + if (!req.body.data.length) { + return res.status(500).send({ decorator: "SERVER_NO_CIDS_TO_CHECK", error: true }); } const id = Utilities.getIdFromCookie(req); const user = await Data.getUserById({ id, }); + const PG = Powergate.get(user); const success = []; const failed = []; - try { - for (let i = 0; i < req.body.data.length; i++) { - const x = req.body.data[i]; - if (!x.job) { - console.log("-- NO JOB (3)"); - failed.push(x); - continue; - } + for (let i = 0; i < req.body.data.length; i++) { + const x = req.body.data[i]; - if (!x.ipfs) { - console.log("-- NO IPFS (3)"); - failed.push(x); - } - - // TODO(jim): A hack, I just want the first job object from a stream. - const job = await check(PG, x.job); - console.log({ job }); - - // TODO(jim): This isn't correct. - // There is a bug where everything is going to hot storage. - // Which is fine... But its not real Filecoin storage then. - if (job.status === 3) { - console.log("CID ERROR (3): ", job.cid); - x.error = job.errCause; - failed.push(x); - continue; - } - - if (job.status === 5) { - console.log("CID SUCCESS (5): ", job.cid); - success.push(x); - continue; - } - - console.log("CID: ", job.cid); + if (!x.job) { + console.log("-- NO JOB (3)"); + failed.push(x); + continue; } - } catch (e) { - console.log(e); + + if (!x.ipfs) { + console.log("-- NO IPFS (3)"); + failed.push(x); + } + + let job; + try { + job = await check(PG, x.job); + } catch (e) { + console.log(e); + } + + if (job.status === 3) { + console.log({ message: "ERROR", job }); + x.error = job.errCause; + failed.push(x); + continue; + } + + if (job.status === 5) { + console.log({ message: "SUCCESS", job }); + success.push(x); + continue; + } + + console.log({ message: "NOOP", job }); } - let userDataFields = { ...user.data }; + let targetUser = { ...user }; + if (failed.length) { for (let i = 0; i < failed.length; i++) { - let data = LibraryManager.getDataByIPFS( - { ...user, data: userDataFields }, - failed[i].ipfs - ); + let data = LibraryManager.getDataByIPFS(targetUser, failed[i].ipfs); + if (!data) { + continue; + } data.networks = data.networks.filter((each) => each !== "FILECOIN"); data.job = null; data.storage = 0; data.error = failed[i].error; - userDataFields = LibraryManager.updateDataById({ - user: { ...user, data: userDataFields }, + targetUser.data = LibraryManager.updateDataById({ + user: targetUser, id: data.id, data, }); @@ -98,16 +99,16 @@ export default async (req, res) => { if (success.length) { for (let i = 0; i < success.length; i++) { - let data = LibraryManager.getDataByIPFS( - { ...user, data: userDataFields }, - success[i].ipfs - ); + let data = LibraryManager.getDataByIPFS(targetUser, success[i].ipfs); + if (!data) { + continue; + } data.storage = 1; data.error = null; - userDataFields = LibraryManager.updateDataById({ - user: { ...user, data: userDataFields }, + targetUser.data = LibraryManager.updateDataById({ + user: targetUser, id: data.id, data, }); @@ -116,12 +117,10 @@ export default async (req, res) => { let response; if (success.length || failed.length) { - console.log("update"); response = await Data.updateUserById({ - id: user.id, - data: userDataFields, + id: targetUser.id, + data: targetUser.data, }); - console.log(response); } return res.status(200).send({