Skip to content

Commit 3f83740

Browse files
committed
get purchases
1 parent d25b6f1 commit 3f83740

File tree

5 files changed

+71
-22
lines changed

5 files changed

+71
-22
lines changed

services/static-webserver/client/source/class/osparc/data/Resources.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,11 @@ qx.Class.define("osparc.data.Resources", {
918918
putAutoRecharge: {
919919
method: "PUT",
920920
url: statics.API + "/wallets/{walletId}/auto-recharge"
921-
}
921+
},
922+
purchases: {
923+
method: "GET",
924+
url: statics.API + "/wallets/{walletId}/licensed-items-purchases"
925+
},
922926
}
923927
},
924928
/*

services/static-webserver/client/source/class/osparc/data/model/PricingUnit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ qx.Class.define("osparc.data.model.PricingUnit", {
8989
},
9090

9191
purchased: {
92-
check: "Boolean",
92+
check: "Object",
9393
nullable: true,
94-
init: false,
94+
init: null,
9595
event: "changePurchased",
9696
},
9797
},

services/static-webserver/client/source/class/osparc/vipMarket/AnatomicalModelDetails.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ qx.Class.define("osparc.vipMarket.AnatomicalModelDetails", {
191191
this.fireDataEvent("modelPurchaseRequested", {
192192
modelId: anatomicalModelsData["modelId"],
193193
licensedItemId: anatomicalModelsData["licensedItemId"],
194+
pricingPlanId: anatomicalModelsData["pricingPlanId"],
194195
pricingUnitId: pricingUnit.getPricingUnitId(),
195196
});
196197
}, this);

services/static-webserver/client/source/class/osparc/vipMarket/AnatomicalModelListItem.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ qx.Class.define("osparc.vipMarket.AnatomicalModelListItem", {
9898
},
9999

100100
purchased: {
101-
check: "Boolean",
102-
init: false,
101+
check: "Object",
103102
nullable: true,
103+
init: null,
104104
event: "changePurchased",
105105
apply: "__applyPurchased",
106106
},

services/static-webserver/client/source/class/osparc/vipMarket/VipMarket.js

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ qx.Class.define("osparc.vipMarket.VipMarket", {
5555
} else {
5656
curatedModel[key] = model[key];
5757
}
58-
if (key === "ID") {
59-
curatedModel["purchased"] = model["ID"] < 4;
60-
}
6158
});
6259
anatomicalModels.push(curatedModel);
6360
});
@@ -67,7 +64,7 @@ qx.Class.define("osparc.vipMarket.VipMarket", {
6764

6865
members: {
6966
__anatomicalModels: null,
70-
__licensedItems: null,
67+
__purchasedItems: null,
7168
__anatomicalModelsModel: null,
7269

7370
_createChildControlImpl: function(id) {
@@ -191,14 +188,30 @@ qx.Class.define("osparc.vipMarket.VipMarket", {
191188
.then(anatomicalModelsRaw => {
192189
const allAnatomicalModels = this.self().curateAnatomicalModels(anatomicalModelsRaw);
193190

194-
osparc.data.Resources.get("market")
195-
.then(licensedItems => {
196-
this.__licensedItems = licensedItems;
191+
const store = osparc.store.Store.getInstance();
192+
const contextWallet = store.getContextWallet();
193+
if (!contextWallet) {
194+
return;
195+
}
196+
const walletId = contextWallet.getWalletId();
197+
const purchasesParams = {
198+
url: {
199+
walletId
200+
}
201+
};
202+
Promise.all([
203+
osparc.data.Resources.get("market"),
204+
osparc.data.Resources.fetch("wallets", "purchases", purchasesParams),
205+
])
206+
.then(values => {
207+
const licensedItems = values[0];
208+
const purchasedItems = values[1];
209+
this.__purchasedItems = purchasedItems;
197210

198211
this.__anatomicalModels = [];
199212
allAnatomicalModels.forEach(model => {
200213
const modelId = model["ID"];
201-
const licensedItem = this.__licensedItems.find(licItem => licItem["name"] == modelId);
214+
const licensedItem = licensedItems.find(licItem => licItem["name"] == modelId);
202215
if (licensedItem) {
203216
const anatomicalModel = {};
204217
anatomicalModel["modelId"] = model["ID"];
@@ -212,7 +225,14 @@ qx.Class.define("osparc.vipMarket.VipMarket", {
212225
anatomicalModel["licensedItemId"] = licensedItem["licensedItemId"];
213226
anatomicalModel["pricingPlanId"] = licensedItem["pricingPlanId"];
214227
// attach leased data
215-
anatomicalModel["purchased"] = model["purchased"];
228+
anatomicalModel["purchased"] = null; // default
229+
const purchasedItemFound = purchasedItems.find(purchasedItem => purchasedItem["licensedItemId"] === licensedItem["licensedItemId"])
230+
if (purchasedItemFound) {
231+
anatomicalModel["purchased"] = {
232+
expiresAt: new Date(purchasedItemFound["expireAt"]),
233+
numberOfSeats: purchasedItemFound["numOfSeats"],
234+
}
235+
}
216236
this.__anatomicalModels.push(anatomicalModel);
217237
}
218238
});
@@ -221,18 +241,42 @@ qx.Class.define("osparc.vipMarket.VipMarket", {
221241

222242
const anatomicModelDetails = this.getChildControl("models-details");
223243
anatomicModelDetails.addListener("modelPurchaseRequested", e => {
244+
if (!contextWallet) {
245+
return;
246+
}
224247
const {
225248
modelId,
226249
licensedItemId,
250+
pricingPlanId,
227251
pricingUnitId,
228252
} = e.getData();
229-
console.log("purchase", licensedItemId, pricingUnitId);
230-
const found = this.__anatomicalModels.find(model => model["ID"] === modelId);
231-
if (found) {
232-
found["purchased"] = true;
233-
this.__populateModels();
234-
anatomicModelDetails.setAnatomicalModelsData(found);
253+
const params = {
254+
url: {
255+
licensedItemId
256+
},
257+
data: {
258+
"wallet_id": walletId,
259+
"pricing_plan_id": pricingPlanId,
260+
"pricing_unit_id": pricingUnitId,
261+
"num_of_seats": 1, // it might not be used
262+
},
235263
}
264+
osparc.data.Resources.fetch("market", "purchase", params)
265+
.then(() => {
266+
const found = this.__anatomicalModels.find(model => model["ID"] === modelId);
267+
if (found) {
268+
found["purchased"] = {
269+
expiresAt: new Date(),
270+
numberOfSeats: 1,
271+
};
272+
this.__populateModels();
273+
anatomicModelDetails.setAnatomicalModelsData(found);
274+
}
275+
})
276+
.catch(err => {
277+
const msg = err.message || this.tr("Cannot purchase model");
278+
osparc.FlashMessenger.getInstance().logAs(msg, "ERROR");
279+
});
236280
}, this);
237281

238282
anatomicModelDetails.addListener("modelImportRequested", e => {
@@ -253,9 +297,9 @@ qx.Class.define("osparc.vipMarket.VipMarket", {
253297
const sortModel = sortBy => {
254298
models.sort((a, b) => {
255299
// first criteria
256-
if (b["purchased"] !== a["purchased"]) {
300+
if (Boolean(b["purchased"]) !== Boolean(a["purchased"])) {
257301
// leased first
258-
return b["purchased"] - a["purchased"];
302+
return Boolean(b["purchased"]) - Boolean(a["purchased"]);
259303
}
260304
// second criteria
261305
if (sortBy) {

0 commit comments

Comments
 (0)