Skip to content

Commit

Permalink
from 6 failures, complete transfer refactoring to async/promises
Browse files Browse the repository at this point in the history
  • Loading branch information
suculent committed Nov 15, 2023
1 parent 499a329 commit c0bdb45
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 189 deletions.
87 changes: 47 additions & 40 deletions lib/router.transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ module.exports = function (app) {
async function requestTransfer(req, res) {
if (!Util.validateSession(req)) return res.status(401).end();
let owner = sanitka.owner(req.session.owner);
await transfer.request(owner, req.body, (success, response) => {
transferResultRedirect(success, res, response);

let response = await transfer.request(owner, req.body).catch( (error) => {
return transferResultRedirect(false, res, error);
});

transferResultRedirect(true, res, response);
}

function getDeclineTransfer(req, res) {
async function getDeclineTransfer(req, res) {

if (!Util.isDefined(sanitka.udid(req.body.transfer_id))) return Util.responder(res, false, "transfer_id_missing");

Expand All @@ -41,12 +44,14 @@ module.exports = function (app) {
udids: []
};

transfer.decline(body, (success, response) => {
transferResultRedirect(success, res, response);
await transfer.decline(body).catch((response)=> {

Check warning on line 47 in lib/router.transfer.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/router.transfer.js#L47

Missing space before =>.
transferResultRedirect(false, res, response);
});

transferResultRedirect(true, res, response);

Check notice on line 51 in lib/router.transfer.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/router.transfer.js#L51

'response' is not defined.
}

function postDeclineTransfer(req, res) {
async function postDeclineTransfer(req, res) {

if (!Util.validateSession(req)) return res.status(401).end();

Expand All @@ -59,24 +64,28 @@ module.exports = function (app) {
udids: sanitka.udid(req.body.udid)
};

transfer.decline(body, (success, response) => {
transferResultRedirect(success, res, response);
await transfer.decline(body).catch((response) => {
transferResultRedirect(false, res, response);
});

transferResultRedirect(true, res, response);
}

function getAcceptTransfer(req, res) {
async function getAcceptTransfer(req, res) {

if (!Util.isDefined(req.query.transfer_id)) return Util.responder(res, false, "transfer_id_missing");

transfer.accept({
let response = transfer.accept({
transfer_id: req.query.transfer_id,
udids: []
}, (success, response) => {
transferResultRedirect(success, res, response);
}).catch((error) => {
return transferResultRedirect(false, res, error);
});

transferResultRedirect(true, res, response);
}

function postAcceptTransfer(req, res) {
async function postAcceptTransfer(req, res) {

if (!Util.validateSession(req)) return res.status(401).end();
if (!Util.isDefined(req.body)) return Util.responder(res, false, "transfer_body_missing");
Expand All @@ -89,67 +98,65 @@ module.exports = function (app) {

console.log("🔨 [debug] <postAcceptTransfer> with body: ", {body});

transfer.accept(body, (success, response) => {
if (success === false) {
console.log("postAcceptTransfer response", response);
res.redirect(app_config.public_url + "/error.html?success=failed");
} else {
res.redirect(app_config.public_url + "/error.html?success=true");
}
await transfer.accept(body).catch((response) => {
console.log("postAcceptTransfer response", response);
res.redirect(app_config.public_url + "/error.html?success=failed");
});

res.redirect(app_config.public_url + "/error.html?success=true");
}

///////////////////////////////////////////////////////////////////////
// API ROUTES v2
//

app.post("/api/v2/transfer/request", function (req, res) {
requestTransfer(req, res);
app.post("/api/v2/transfer/request", async function (req, res) {
await requestTransfer(req, res);
});

app.get("/api/v2/transfer/decline", function (req, res) {
getDeclineTransfer(req, res);
app.get("/api/v2/transfer/decline", async function (req, res) {
await getDeclineTransfer(req, res);
});

app.post("/api/v2/transfer/decline", function (req, res) {
postDeclineTransfer(req, res);
app.post("/api/v2/transfer/decline", async function (req, res) {
await postDeclineTransfer(req, res);
});

app.get("/api/v2/transfer/accept", function (req, res) {
getAcceptTransfer(req, res);
app.get("/api/v2/transfer/accept", async function (req, res) {
await getAcceptTransfer(req, res);
});

app.post("/api/v2/transfer/accept", function (req, res) {
postAcceptTransfer(req, res);
app.post("/api/v2/transfer/accept", async function (req, res) {
await postAcceptTransfer(req, res);
});

///////////////////////////////////////////////////////////////////////
// API ROUTES v1
//

/* Request device transfer */
app.post("/api/transfer/request", function (req, res) {
requestTransfer(req, res);
app.post("/api/transfer/request", async function (req, res) {
await requestTransfer(req, res);
});

/* Decline device transfer (all by e-mail, selective will be POST) */
app.get("/api/transfer/decline", function (req, res) {
getDeclineTransfer(req, res);
app.get("/api/transfer/decline", async function (req, res) {
await getDeclineTransfer(req, res);
});

/* Decline selective device transfer */
app.post("/api/transfer/decline", function (req, res) {
postDeclineTransfer(req, res);
app.post("/api/transfer/decline", async function (req, res) {
await postDeclineTransfer(req, res);
});

/* Accept device transfer (all by e-mail, selective will be POST) */
app.get("/api/transfer/accept", function (req, res) {
getAcceptTransfer(req, res);
app.get("/api/transfer/accept", async function (req, res) {
await getAcceptTransfer(req, res);
});

/* Accept selective device transfer */
app.post("/api/transfer/accept", function (req, res) {
postAcceptTransfer(req, res);
app.post("/api/transfer/accept", async function (req, res) {
await postAcceptTransfer(req, res);
});

};
Loading

0 comments on commit c0bdb45

Please sign in to comment.