Skip to content

Commit a21f916

Browse files
authored
🐛 [Frontend] Fix in_debt tracking (#7927)
1 parent 431ac36 commit a21f916

File tree

5 files changed

+64
-16
lines changed

5 files changed

+64
-16
lines changed

services/static-webserver/client/source/class/osparc/dashboard/ResourceDetails.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ qx.Class.define("osparc.dashboard.ResourceDetails", {
5757
case "template":
5858
case "tutorial":
5959
case "hypertool":
60+
// when getting the latest study data, the debt information was lost
61+
if (osparc.study.Utils.isInDebt(this.__resourceData)) {
62+
const mainStore = osparc.store.Store.getInstance();
63+
this.__resourceData["debt"] = mainStore.getStudyDebt(this.__resourceData["uuid"]);
64+
}
6065
osparc.store.Services.getStudyServicesMetadata(latestResourceData)
6166
.finally(() => {
6267
this.__resourceModel = new osparc.data.model.Study(latestResourceData);
@@ -89,6 +94,7 @@ qx.Class.define("osparc.dashboard.ResourceDetails", {
8994
"updateService": "qx.event.type.Data",
9095
"updateHypertool": "qx.event.type.Data",
9196
"publishTemplate": "qx.event.type.Data",
97+
"closeWindow": "qx.event.type.Event",
9298
},
9399

94100

@@ -107,6 +113,9 @@ qx.Class.define("osparc.dashboard.ResourceDetails", {
107113
width: this.WIDTH,
108114
height: this.HEIGHT,
109115
});
116+
resourceDetails.addListener("closeWindow", () => {
117+
win.close();
118+
});
110119
return win;
111120
},
112121

@@ -479,6 +488,9 @@ qx.Class.define("osparc.dashboard.ResourceDetails", {
479488
const enabled = osparc.study.Utils.canBeOpened(resourceData);
480489
page.openButton.setEnabled(enabled);
481490
})
491+
billingSettings.addListener("closeWindow", () => {
492+
this.fireEvent("closeWindow");
493+
}, this);
482494
const billingScroll = new qx.ui.container.Scroll(billingSettings);
483495
page.addToContent(billingScroll);
484496
}

services/static-webserver/client/source/class/osparc/desktop/StudyEditor.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,7 @@ qx.Class.define("osparc.desktop.StudyEditor", {
179179
if ("status" in err && err["status"]) {
180180
if (err["status"] == 402) {
181181
msg = err["message"];
182-
// The backend might have thrown a 402 because the wallet was negative
183-
const match = msg.match(/last transaction of\s([-]?\d+(\.\d+)?)\sresulted/);
184-
let debt = null;
185-
if ("debtAmount" in err) {
186-
// the study has some debt that needs to be paid
187-
debt = err["debtAmount"];
188-
} else if (match) {
189-
// the study has some debt that needs to be paid
190-
debt = parseFloat(match[1]); // Convert the captured string to a number
191-
}
192-
if (debt) {
193-
// if get here, it means that the 402 was thrown due to the debt
194-
osparc.store.Store.getInstance().setStudyDebt(study.getUuid(), debt);
195-
}
182+
osparc.study.Utils.extractDebtFromError(study.getUuid(), err);
196183
} else if (err["status"] == 409) { // max_open_studies_per_user
197184
msg = err["message"];
198185
} else if (err["status"] == 423) { // Locked

services/static-webserver/client/source/class/osparc/store/Store.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ qx.Class.define("osparc.store.Store", {
253253
},
254254

255255
members: {
256+
__studiesInDebt: null,
257+
256258
// fetch resources that do not require log in
257259
preloadCalls: async function() {
258260
await osparc.data.Resources.get("config");
@@ -454,6 +456,16 @@ qx.Class.define("osparc.store.Store", {
454456
},
455457

456458
setStudyDebt: function(studyId, debt) {
459+
// init object if it does not exist
460+
if (this.__studiesInDebt === null) {
461+
this.__studiesInDebt = {};
462+
}
463+
if (debt) {
464+
this.__studiesInDebt[studyId] = debt;
465+
} else {
466+
delete this.__studiesInDebt[studyId];
467+
}
468+
457469
const studiesWStateCache = this.getStudies();
458470
const idx = studiesWStateCache.findIndex(studyWStateCache => studyWStateCache["uuid"] === studyId);
459471
if (idx !== -1) {
@@ -470,6 +482,17 @@ qx.Class.define("osparc.store.Store", {
470482
});
471483
},
472484

485+
getStudyDebt: function(studyId) {
486+
if (this.__studiesInDebt && studyId in this.__studiesInDebt) {
487+
return this.__studiesInDebt[studyId];
488+
}
489+
return null;
490+
},
491+
492+
isStudyInDebt: function(studyId) {
493+
return Boolean(this.getStudyDebt(studyId));
494+
},
495+
473496
trashStudy: function(studyId) {
474497
const params = {
475498
url: {

services/static-webserver/client/source/class/osparc/study/BillingSettings.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ qx.Class.define("osparc.study.BillingSettings", {
3333

3434
events: {
3535
"debtPayed": "qx.event.type.Event",
36+
"closeWindow": "qx.event.type.Event",
3637
},
3738

3839
members: {
@@ -286,7 +287,13 @@ qx.Class.define("osparc.study.BillingSettings", {
286287
const msg = this.tr("Credit Account saved");
287288
osparc.FlashMessenger.logAs(msg, "INFO");
288289
})
289-
.catch(err => osparc.FlashMessenger.logError(err))
290+
.catch(err => {
291+
if ("status" in err && err["status"] == 402) {
292+
osparc.study.Utils.extractDebtFromError(this.__studyData["uuid"], err);
293+
}
294+
osparc.FlashMessenger.logError(err);
295+
this.fireEvent("closeWindow");
296+
})
290297
.finally(() => {
291298
creditAccountBox.setEnabled(true);
292299
});

services/static-webserver/client/source/class/osparc/study/Utils.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,26 @@ qx.Class.define("osparc.study.Utils", {
344344
},
345345

346346
isInDebt: function(studyData) {
347-
return Boolean("debt" in studyData && studyData["debt"] < 0);
347+
return osparc.store.Store.getInstance().isStudyInDebt(studyData["uuid"]);
348+
},
349+
350+
extractDebtFromError: function(studyId, err) {
351+
const msg = err["message"];
352+
// The backend might have thrown a 402 because the wallet was negative
353+
const match = msg.match(/last transaction of\s([-]?\d+(\.\d+)?)\sresulted/);
354+
let debt = null;
355+
if ("debtAmount" in err) {
356+
// the study has some debt that needs to be paid
357+
debt = err["debtAmount"];
358+
} else if (match) {
359+
// the study has some debt that needs to be paid
360+
debt = parseFloat(match[1]); // Convert the captured string to a number
361+
}
362+
if (debt) {
363+
// if get here, it means that the 402 was thrown due to the debt
364+
osparc.store.Store.getInstance().setStudyDebt(studyId, debt);
365+
}
366+
return debt;
348367
},
349368

350369
getUiMode: function(studyData) {

0 commit comments

Comments
 (0)