diff --git a/lib/router.transfer.js b/lib/router.transfer.js index 2d25d1bc3..555771e82 100644 --- a/lib/router.transfer.js +++ b/lib/router.transfer.js @@ -16,6 +16,7 @@ module.exports = function (app) { // solves `Headers already set` issue? if ((process.env.ENVIRONMENT === "test") || (process.env.ENVIRONMENT === "development")) { //return Util.responder(res, success, response); + console.log("[test] returning promise resolve with response, instead of redirect:", response); return Promise.resolve(response); } @@ -31,10 +32,10 @@ module.exports = function (app) { let owner = sanitka.owner(req.session.owner); const body = req.body; let response = await transfer.request(owner, body).catch( (error) => { - console.log("[transferResultRedirect] await transfer.request with error", error, "in body", {body}); + console.log("[requestTransfer] await transfer.request with error", error, "in body", {body}); transferResultRedirect(false, res, error); }); - console.log("[transferResultRedirect] with response", response); + console.log("[requestTransfer] with response: ", response); transferResultRedirect(true, res, response); } @@ -51,6 +52,9 @@ module.exports = function (app) { let response = await transfer.decline(body).catch((response) => { transferResultRedirect(false, res, response); }); + + console.log("[getDeclineTransfer] transfer.decline response", response); + transferResultRedirect(true, res, response); } @@ -67,22 +71,30 @@ module.exports = function (app) { udids: sanitka.udid(req.body.udid) }; - await transfer.decline(body).catch((error) => { + let response = await transfer.decline(body).catch((error) => { transferResultRedirect(false, res, error); - }).then(() => { - transferResultRedirect(true, res); }); + + console.log("[getDeclineTransfer] transfer.decline response", response); + + transferResultRedirect(true, res); } + // called from e-mail to accept a transfer ID async function getAcceptTransfer(req, res) { + + let transfer_id = req.query.transfer_id; - if (!Util.isDefined(req.query.transfer_id)) return Util.responder(res, false, "transfer_id_missing"); + if (!Util.isDefined(transfer_id)) { + console.log("[warning] Transfer ID missing in getAcceptTransfer query", req.query); + return Util.responder(res, false, "transfer_id_missing"); + } let response = await transfer.accept({ - transfer_id: req.query.transfer_id, + transfer_id: transfer_id, udids: [] }).catch((error) => { - console.log("[getAcceptTransfer] failed with error", error); + console.log("[getAcceptTransfer] failed with error", error.message); transferResultRedirect(false, res, error); }); diff --git a/lib/thinx/transfer.js b/lib/thinx/transfer.js index 2d4af6f20..7fe6cdbec 100644 --- a/lib/thinx/transfer.js +++ b/lib/thinx/transfer.js @@ -366,7 +366,7 @@ module.exports = class Transfer { var transfer_id = "dt:" + transfer_uuid; await this.redis.set(transfer_id, JSON.stringify(body)); - console.log("[debug] [transfer] setting dtid with body", transfer_id, {body}); + console.log("[debug] [transfer] setting dtid with body", transfer_id, { body }); // 4. store pending transfer for each device for (var did in body.udids) { @@ -407,11 +407,11 @@ module.exports = class Transfer { // optional body should look like { "transfer_id":"uuid", "udids" : [ ... ] } if (typeof (body.transfer_id) === "undefined") { - console.log("[warning] [transfer] [accept] transfer_id undefined in body " + {body} + ", rejecting promise..."); - return Promise.reject( new Error("transfer_id_missing") ); + console.log("[warning] [transfer] [accept] transfer_id undefined in body " + { body } + ", rejecting promise..."); + return Promise.reject(new Error("transfer_id_missing")); } - console.log(`🔨 [debug] [transfer] body`, {body}); + console.log(`🔨 [debug] [transfer] body`, { body }); var transfer_id = body.transfer_id; var udids = []; @@ -428,12 +428,14 @@ module.exports = class Transfer { const dtid = "dt:" + transfer_id; let encoded_json_keys = await this.redis.get(dtid); - console.log(`🔨 [debug] [transfer] await Fetched DTID: ${dtid} encoded_json_keys ${encoded_json_keys}`); + console.log(`🔨 [debug] [transfer] await Fetched DTID: ${dtid}`); - if ((typeof(encoded_json_keys) === "undefined") || (encoded_json_keys === null)) { - return Promise.reject(new Error("transfer_id_not_found") ); + if ((typeof (encoded_json_keys) === "undefined") || (encoded_json_keys === null)) { + return Promise.reject(new Error("transfer_id_not_found")); } - + + console.log(`🔨 [debug] [transfer] await Fetched encoded_json_keys ${encoded_json_keys}`); + // stringify is only for printing cases let keys = String(JSON.stringify(encoded_json_keys)); @@ -441,7 +443,7 @@ module.exports = class Transfer { if (keys.length == 0) { console.log("⚠️ [warning] [transfer] transfer_id not found (empty response array)"); - return Promise.reject( new Error("transfer_id_not_found") ); + return Promise.reject(new Error("transfer_id_not_found")); } var json_keys = JSON.parse(encoded_json_keys); @@ -525,7 +527,7 @@ module.exports = class Transfer { // optional body should look like { "transfer_id":"uuid", "udids" : [ ... ] } if (typeof (body.transfer_id) === "undefined") { - return Promise.reject("transfer_id_missing"); + return Promise.reject(new Error("transfer_id_missing")); } console.log(`[transfer][decline] body: ${JSON.stringify(body)}`); @@ -541,23 +543,23 @@ module.exports = class Transfer { var dtid = "dt:" + transfer_id; console.log(`🔨 [debug] [transfer] getting DTID ${dtid} on decline`); - + let json = await this.redis.get(dtid); - if (typeof(json) === "undefined") { + if (typeof (json) === "undefined") { console.log(`[warning] [transfer] no such DTID ${dtid}`); - return Promise.reject("invalid_device_transfer_identifier"); + return Promise.reject(new Error("invalid_device_transfer_identifier")); } let json_keys = JSON.parse(json); if (json_keys.length == 0) { console.log("[transfer] json_keys", json_keys); - return Promise.reject("transfer_id_invalid"); + return Promise.reject(new Error("transfer_id_invalid")); } if (json_keys === null) { console.log("[transfer] no such transfer anymore"); - return Promise.reject("transfer_id_missing"); + return Promise.reject(new Error("transfer_id_missing")); } console.log(`🔨 [debug] [transfer] L5 udids ${udids}`); @@ -571,7 +573,7 @@ module.exports = class Transfer { console.log(`🔨 [debug] [transfer] L6 udids ${json_keys.udids}`); if (json_keys.udids.length == 0) { - this.redis.del(dtid); + await this.redis.del(dtid); } var recipient_email = json_keys.to; @@ -592,8 +594,6 @@ module.exports = class Transfer { // Store remaining (not declined) keys await this.storeRemainingKeys(dtid, json_keys); - - Promise.resolve("decline_completed"); } }; diff --git a/spec/jasmine/TransferSpec.js b/spec/jasmine/TransferSpec.js index 2b06229ad..dc17a5381 100644 --- a/spec/jasmine/TransferSpec.js +++ b/spec/jasmine/TransferSpec.js @@ -81,7 +81,10 @@ describe("Transfer", function () { udids: [envi.udid] }; - const response3 = await transfer.accept(transfer_body); + const response3 = await transfer.accept(transfer_body).catch((e) => { + console.log("[spec] transfer.accept throws:", e); + }); + console.log("[spec] transfer.accept response3:", response3); expect(response3).to.be.a('string'); }); // it-00