From 38cd47e2c4b7d6e2b98594216d430fcd6ced4c85 Mon Sep 17 00:00:00 2001 From: odeimaiz Date: Mon, 9 Dec 2024 11:21:47 +0100 Subject: [PATCH] Pricing own store and models --- .../source/class/osparc/data/Resources.js | 4 +- .../class/osparc/data/model/PricingUnit.js | 2 +- .../source/class/osparc/pricing/PlanData.js | 89 -------------- .../source/class/osparc/pricing/PlanEditor.js | 14 +-- .../source/class/osparc/pricing/Plans.js | 6 +- .../source/class/osparc/pricing/UnitData.js | 90 -------------- .../source/class/osparc/store/Pricing.js | 116 ++++++++++++++++++ .../source/class/osparc/study/PricingUnit.js | 4 +- 8 files changed, 130 insertions(+), 195 deletions(-) delete mode 100644 services/static-webserver/client/source/class/osparc/pricing/PlanData.js delete mode 100644 services/static-webserver/client/source/class/osparc/pricing/UnitData.js create mode 100644 services/static-webserver/client/source/class/osparc/store/Pricing.js diff --git a/services/static-webserver/client/source/class/osparc/data/Resources.js b/services/static-webserver/client/source/class/osparc/data/Resources.js index 1843481bfcc..01c0fee7b27 100644 --- a/services/static-webserver/client/source/class/osparc/data/Resources.js +++ b/services/static-webserver/client/source/class/osparc/data/Resources.js @@ -631,7 +631,7 @@ qx.Class.define("osparc.data.Resources", { * PRICING PLANS */ "pricingPlans": { - useCache: true, + useCache: false, // handled in osparc.store.Pricing endpoints: { get: { method: "GET", @@ -656,7 +656,7 @@ qx.Class.define("osparc.data.Resources", { * PRICING UNITS */ "pricingUnits": { - useCache: true, + useCache: false, // handled in osparc.store.Pricing endpoints: { getOne: { method: "GET", diff --git a/services/static-webserver/client/source/class/osparc/data/model/PricingUnit.js b/services/static-webserver/client/source/class/osparc/data/model/PricingUnit.js index 426397639e2..ebc71bbdad9 100644 --- a/services/static-webserver/client/source/class/osparc/data/model/PricingUnit.js +++ b/services/static-webserver/client/source/class/osparc/data/model/PricingUnit.js @@ -31,7 +31,7 @@ qx.Class.define("osparc.data.model.PricingUnit", { this.set({ pricingUnitId: pricingUnitData.pricingUnitId, name: pricingUnitData.unitName, - cost: pricingUnitData.currentCostPerUnit, + cost: parseFloat(pricingUnitData.currentCostPerUnit), isDefault: pricingUnitData.default, extraInfo: pricingUnitData.unitExtraInfo, }); diff --git a/services/static-webserver/client/source/class/osparc/pricing/PlanData.js b/services/static-webserver/client/source/class/osparc/pricing/PlanData.js deleted file mode 100644 index d2c3e00275c..00000000000 --- a/services/static-webserver/client/source/class/osparc/pricing/PlanData.js +++ /dev/null @@ -1,89 +0,0 @@ -/* ************************************************************************ - - osparc - the simcore frontend - - https://osparc.io - - Copyright: - 2024 IT'IS Foundation, https://itis.swiss - - License: - MIT: https://opensource.org/licenses/MIT - - Authors: - * Odei Maiz (odeimaiz) - -************************************************************************ */ - -/** - * Class that stores Pricing Plan data. - * - */ - -qx.Class.define("osparc.pricing.PlanData", { - extend: qx.core.Object, - - construct: function(planData) { - this.base(arguments); - - this.set({ - pricingPlanId: planData.pricingPlanId, - pricingPlanKey: planData.pricingPlanKey, - displayName: planData.displayName, - description: planData.description, - classification: planData.classification, - isActive: planData.isActive - }); - }, - - properties: { - pricingPlanId: { - check: "Number", - nullable: false, - init: 0, - event: "changePricingPlanId" - }, - - pricingPlanKey: { - check: "String", - nullable: false, - init: "", - event: "changePricingPlanKey" - }, - - displayName: { - check: "String", - init: "", - nullable: false, - event: "changeDisplayName" - }, - - description: { - check: "String", - init: "", - nullable: false, - event: "changeDescription" - }, - - classification: { - check: "String", - init: "TIER", - nullable: false, - event: "changeClassification" - }, - - isActive: { - check: "Boolean", - init: true, - nullable: false, - event: "changeIsActive" - }, - - pricingUnits: { - check: "Array", - init: [], - nullable: false, - event: "changePricingUnits" - } - } -}); diff --git a/services/static-webserver/client/source/class/osparc/pricing/PlanEditor.js b/services/static-webserver/client/source/class/osparc/pricing/PlanEditor.js index c638343ed88..905e423fa8d 100644 --- a/services/static-webserver/client/source/class/osparc/pricing/PlanEditor.js +++ b/services/static-webserver/client/source/class/osparc/pricing/PlanEditor.js @@ -200,15 +200,13 @@ qx.Class.define("osparc.pricing.PlanEditor", { const name = this.getName(); const description = this.getDescription(); const classification = this.getClassification(); - const params = { - data: { - "pricingPlanKey": ppKey, - "displayName": name, - "description": description, - "classification": classification - } + const newPricingPlanData = { + "pricingPlanKey": ppKey, + "displayName": name, + "description": description, + "classification": classification }; - osparc.data.Resources.fetch("pricingPlans", "post", params) + osparc.store.Pricing.getInstance().postPricingPlan(newPricingPlanData) .then(() => { osparc.FlashMessenger.getInstance().logAs(name + this.tr(" successfully created")); this.fireEvent("done"); diff --git a/services/static-webserver/client/source/class/osparc/pricing/Plans.js b/services/static-webserver/client/source/class/osparc/pricing/Plans.js index d539698f8d8..7d50a7e0c66 100644 --- a/services/static-webserver/client/source/class/osparc/pricing/Plans.js +++ b/services/static-webserver/client/source/class/osparc/pricing/Plans.js @@ -91,7 +91,7 @@ qx.Class.define("osparc.pricing.Plans", { ctrl.bindProperty("pricingPlanId", "model", null, item, id); ctrl.bindProperty("pricingPlanId", "ppId", null, item, id); ctrl.bindProperty("pricingPlanKey", "ppKey", null, item, id); - ctrl.bindProperty("displayName", "title", null, item, id); + ctrl.bindProperty("name", "title", null, item, id); ctrl.bindProperty("description", "description", null, item, id); ctrl.bindProperty("isActive", "isActive", null, item, id); }, @@ -103,13 +103,13 @@ qx.Class.define("osparc.pricing.Plans", { }, fetchPlans: function() { - osparc.data.Resources.fetch("pricingPlans", "get") + osparc.store.Pricing.getInstance().fetchPricingPlans() .then(data => this.__populateList(data)); }, __populateList: function(pricingPlans) { this.__model.removeAll(); - pricingPlans.forEach(pricingPlan => this.__model.append(new osparc.pricing.PlanData(pricingPlan))); + pricingPlans.forEach(pricingPlan => this.__model.append(pricingPlan)); }, __openCreatePricingPlan: function() { diff --git a/services/static-webserver/client/source/class/osparc/pricing/UnitData.js b/services/static-webserver/client/source/class/osparc/pricing/UnitData.js deleted file mode 100644 index 7f636ee9ab8..00000000000 --- a/services/static-webserver/client/source/class/osparc/pricing/UnitData.js +++ /dev/null @@ -1,90 +0,0 @@ -/* ************************************************************************ - - osparc - the simcore frontend - - https://osparc.io - - Copyright: - 2024 IT'IS Foundation, https://itis.swiss - - License: - MIT: https://opensource.org/licenses/MIT - - Authors: - * Odei Maiz (odeimaiz) - -************************************************************************ */ - -/** - * Class that stores Pricing Unit data. - * - */ - -qx.Class.define("osparc.pricing.UnitData", { - extend: qx.core.Object, - - construct: function(unitData) { - this.base(arguments); - - this.set({ - pricingUnitId: unitData.pricingUnitId ? unitData.pricingUnitId : null, - unitName: unitData.unitName, - currentCostPerUnit: parseFloat(unitData.currentCostPerUnit), - comment: unitData.comment ? unitData.comment : "", - awsSpecificInfo: unitData.specificInfo && unitData.specificInfo["aws_ec2_instances"] ? unitData.specificInfo["aws_ec2_instances"].toString() : "", - unitExtraInfo: unitData.unitExtraInfo, - default: unitData.default - }); - }, - - properties: { - pricingUnitId: { - check: "Number", - nullable: true, - init: null, - event: "changePricingUnitId" - }, - - unitName: { - check: "String", - init: "", - nullable: false, - event: "changeUnitName" - }, - - currentCostPerUnit: { - check: "Number", - nullable: false, - init: 0, - event: "changeCurrentCostPerUnit" - }, - - comment: { - check: "String", - init: "", - nullable: false, - event: "changeComment" - }, - - awsSpecificInfo: { - check: "String", - init: "", - nullable: false, - event: "changeAwsSpecificInfo" - }, - - unitExtraInfo: { - check: "Object", - init: {}, - nullable: false, - event: "changeUnitExtraInfo" - }, - - default: { - check: "Boolean", - init: true, - nullable: false, - event: "changeDefault" - } - } -}); diff --git a/services/static-webserver/client/source/class/osparc/store/Pricing.js b/services/static-webserver/client/source/class/osparc/store/Pricing.js new file mode 100644 index 00000000000..037badab44b --- /dev/null +++ b/services/static-webserver/client/source/class/osparc/store/Pricing.js @@ -0,0 +1,116 @@ +/* ************************************************************************ + + osparc - the simcore frontend + + https://osparc.io + + Copyright: + 2024 IT'IS Foundation, https://itis.swiss + + License: + MIT: https://opensource.org/licenses/MIT + + Authors: + * Odei Maiz (odeimaiz) + +************************************************************************ */ + +qx.Class.define("osparc.store.Pricing", { + extend: qx.core.Object, + type: "singleton", + + construct: function() { + this.base(arguments); + + this.pricingPlansCached = []; + }, + + events: { + "pricingPlansChanged": "qx.event.type.Data", + }, + + members: { + pricingPlansCached: null, + + fetchPricingPlans: function() { + return osparc.data.Resources.fetch("pricingPlans", "get") + .then(pricingPlansData => { + const pricingPlans = []; + pricingPlansData.forEach(pricingPlanData => { + const pricingPlan = this.__addToCache(pricingPlanData); + pricingPlans.push(pricingPlan); + }); + return pricingPlans; + }); + }, + + postPricingPlan: function(newPricingPlanData) { + const params = { + data: newPricingPlanData + }; + return osparc.data.Resources.fetch("pricingPlans", "post", params) + .then(pricingPlanData => { + const pricingPlan = this.__addToCache(pricingPlanData); + this.fireDataEvent("pricingPlansChanged", pricingPlan); + return pricingPlan; + }); + }, + + putPricingPlan: function(pricingPlanId, updateData) { + const params = { + url: { + pricingPlanId + }, + data: updateData + }; + return osparc.data.Resources.getInstance().fetch("pricingPlans", "put", params) + .then(pricingPlanData => { + return this.__addToCache(pricingPlanData); + }) + .catch(console.error); + }, + + fetchPricingUnits: function(pricingPlanId) { + const params = { + url: { + pricingPlanId, + } + }; + return osparc.data.Resources.fetch("pricingUnits", "get", params) + .then(pricingPlansData => { + const pricingPlans = []; + pricingPlansData.forEach(pricingPlanData => { + const pricingPlan = this.__addToCache(pricingPlanData); + pricingPlans.push(pricingPlan); + }); + return pricingPlans; + }); + }, + + getPricingPlans: function() { + return this.pricingPlansCached; + }, + + getPricingPlan: function(pricingPlanId = null) { + return this.pricingPlansCached.find(f => f.getPricingPlanId() === pricingPlanId); + }, + + __addToCache: function(pricingPlanData) { + let pricingPlan = this.pricingPlansCached.find(f => f.getPricingPlanId() === pricingPlanData["pricingPlanId"]); + if (pricingPlan) { + const props = Object.keys(qx.util.PropertyUtil.getProperties(osparc.data.model.PricingPlan)); + // put + Object.keys(pricingPlanData).forEach(key => { + if (props.includes(key)) { + pricingPlan.set(key, pricingPlanData[key]); + } + }); + } else { + // get and post + pricingPlan = new osparc.data.model.PricingPlan(pricingPlanData); + this.pricingPlansCached.unshift(pricingPlan); + } + return pricingPlan; + }, + } +}); diff --git a/services/static-webserver/client/source/class/osparc/study/PricingUnit.js b/services/static-webserver/client/source/class/osparc/study/PricingUnit.js index c2de6606e30..4f93907cea3 100644 --- a/services/static-webserver/client/source/class/osparc/study/PricingUnit.js +++ b/services/static-webserver/client/source/class/osparc/study/PricingUnit.js @@ -27,7 +27,7 @@ qx.Class.define("osparc.study.PricingUnit", { decorator: "rounded", }); - this.setUnitData(new osparc.pricing.UnitData(pricingUnit)); + this.setUnitData(new osparc.data.model.PricingUnit(pricingUnit)); }, events: { @@ -37,7 +37,7 @@ qx.Class.define("osparc.study.PricingUnit", { properties: { unitData: { - check: "osparc.pricing.UnitData", + check: "osparc.data.model.PricingUnit", nullable: false, init: null, apply: "__buildLayout"