Skip to content

Commit

Permalink
debugging not-found transfer ID, tracing promise returns back to router
Browse files Browse the repository at this point in the history
  • Loading branch information
suculent committed Nov 16, 2023
1 parent 14499b6 commit ae37170
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
28 changes: 20 additions & 8 deletions lib/router.transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);

}
Expand All @@ -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);
}

Expand All @@ -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);
});

Expand Down
36 changes: 18 additions & 18 deletions lib/thinx/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = [];
Expand All @@ -428,20 +428,22 @@ 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));

console.log(`🔨 [debug] [transfer] stringified DTID: ${dtid} with keys ${keys}`);

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);
Expand Down Expand Up @@ -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)}`);
Expand All @@ -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}`);
Expand All @@ -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;
Expand All @@ -592,8 +594,6 @@ module.exports = class Transfer {

// Store remaining (not declined) keys
await this.storeRemainingKeys(dtid, json_keys);

Promise.resolve("decline_completed");
}

};
5 changes: 4 additions & 1 deletion spec/jasmine/TransferSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ae37170

Please sign in to comment.