|
| 1 | +/* ************************************************************************ |
| 2 | +
|
| 3 | + osparc - the simcore frontend |
| 4 | +
|
| 5 | + https://osparc.io |
| 6 | +
|
| 7 | + Copyright: |
| 8 | + 2025 IT'IS Foundation, https://itis.swiss |
| 9 | +
|
| 10 | + License: |
| 11 | + MIT: https://opensource.org/licenses/MIT |
| 12 | +
|
| 13 | + Authors: |
| 14 | + * Odei Maiz (odeimaiz) |
| 15 | +
|
| 16 | +************************************************************************ */ |
| 17 | + |
| 18 | + |
| 19 | +qx.Class.define("osparc.desktop.credits.CheckoutsTableModel", { |
| 20 | + extend: qx.ui.table.model.Remote, |
| 21 | + |
| 22 | + construct(walletId, filters) { |
| 23 | + this.base(arguments); |
| 24 | + |
| 25 | + const checkoutsCols = osparc.desktop.credits.CheckoutsTable.COLS; |
| 26 | + const colLabels = Object.values(checkoutsCols).map(col => col.label); |
| 27 | + const colIDs = Object.values(checkoutsCols).map(col => col.id); |
| 28 | + |
| 29 | + this.setColumns(colLabels, colIDs); |
| 30 | + this.setWalletId(walletId) |
| 31 | + if (filters) { |
| 32 | + this.setFilters(filters) |
| 33 | + } |
| 34 | + this.setSortColumnIndexWithoutSortingData(checkoutsCols.START.column); |
| 35 | + this.setSortAscendingWithoutSortingData(false); |
| 36 | + }, |
| 37 | + |
| 38 | + properties: { |
| 39 | + walletId: { |
| 40 | + check: "Number", |
| 41 | + nullable: true |
| 42 | + }, |
| 43 | + filters: { |
| 44 | + check: "Object", |
| 45 | + init: null |
| 46 | + }, |
| 47 | + isFetching: { |
| 48 | + check: "Boolean", |
| 49 | + init: false, |
| 50 | + event: "changeFetching" |
| 51 | + }, |
| 52 | + orderBy: { |
| 53 | + check: "Object", |
| 54 | + init: { |
| 55 | + field: "purchased_at", |
| 56 | + direction: "desc" |
| 57 | + } |
| 58 | + } |
| 59 | + }, |
| 60 | + |
| 61 | + statics: { |
| 62 | + SERVER_MAX_LIMIT: 49, |
| 63 | + COLUMN_ID_TO_DB_COLUMN_MAP: { |
| 64 | + 0: "purchased_at", |
| 65 | + }, |
| 66 | + }, |
| 67 | + |
| 68 | + members: { |
| 69 | + // overridden |
| 70 | + sortByColumn(columnIndex, ascending) { |
| 71 | + this.setOrderBy({ |
| 72 | + field: this.self().COLUMN_ID_TO_DB_COLUMN_MAP[columnIndex], |
| 73 | + direction: ascending ? "asc" : "desc" |
| 74 | + }) |
| 75 | + this.base(arguments, columnIndex, ascending) |
| 76 | + }, |
| 77 | + |
| 78 | + // overridden |
| 79 | + _loadRowCount() { |
| 80 | + const walletId = this.getWalletId(); |
| 81 | + const urlParams = { |
| 82 | + offset: 0, |
| 83 | + limit: 1, |
| 84 | + filters: this.getFilters() ? |
| 85 | + JSON.stringify({ |
| 86 | + "started_at": this.getFilters() |
| 87 | + }) : |
| 88 | + null, |
| 89 | + orderBy: JSON.stringify(this.getOrderBy()), |
| 90 | + }; |
| 91 | + const options = { |
| 92 | + resolveWResponse: true |
| 93 | + }; |
| 94 | + osparc.store.LicensedItems.getInstance().getPurchasedLicensedItems(walletId, urlParams, options) |
| 95 | + .then(resp => { |
| 96 | + this._onRowCountLoaded(resp["_meta"].total) |
| 97 | + }) |
| 98 | + .catch(() => { |
| 99 | + this._onRowCountLoaded(null) |
| 100 | + }); |
| 101 | + }, |
| 102 | + |
| 103 | + // overridden |
| 104 | + _loadRowData(firstRow, qxLastRow) { |
| 105 | + this.setIsFetching(true); |
| 106 | + |
| 107 | + const lastRow = Math.min(qxLastRow, this._rowCount - 1); |
| 108 | + // Returns a request promise with given offset and limit |
| 109 | + const getFetchPromise = (offset, limit=this.self().SERVER_MAX_LIMIT) => { |
| 110 | + const walletId = this.getWalletId(); |
| 111 | + const urlParams = { |
| 112 | + limit, |
| 113 | + offset, |
| 114 | + filters: this.getFilters() ? |
| 115 | + JSON.stringify({ |
| 116 | + "started_at": this.getFilters() |
| 117 | + }) : |
| 118 | + null, |
| 119 | + orderBy: JSON.stringify(this.getOrderBy()) |
| 120 | + }; |
| 121 | + const licensedItemsStore = osparc.store.LicensedItems.getInstance(); |
| 122 | + return Promise.all([ |
| 123 | + licensedItemsStore.getLicensedItems(), |
| 124 | + licensedItemsStore.getPurchasedLicensedItems(walletId, urlParams), |
| 125 | + licensedItemsStore.getVipModels(), |
| 126 | + ]) |
| 127 | + .then(values => { |
| 128 | + const licensedItems = values[0]; |
| 129 | + const checkoutsItems = values[1]; |
| 130 | + const vipModels = values[2]; |
| 131 | + |
| 132 | + const data = []; |
| 133 | + const checkoutsCols = osparc.desktop.credits.CheckoutsTable.COLS; |
| 134 | + checkoutsItems.forEach(checkoutsItem => { |
| 135 | + const licensedItemId = checkoutsItem["licensedItemId"]; |
| 136 | + const licensedItem = licensedItems.find(licItem => licItem["licensedItemId"] === licensedItemId); |
| 137 | + const vipModel = vipModels.find(vipMdl => vipMdl["modelId"] == licensedItem["name"]); |
| 138 | + data.push({ |
| 139 | + [checkoutsCols.PURCHASE_ID.id]: checkoutsItem["licensedItemPurchaseId"], |
| 140 | + [checkoutsCols.ITEM_ID.id]: licensedItemId, |
| 141 | + [checkoutsCols.ITEM_LABEL.id]: vipModel ? vipModel["name"] : "unknown model", |
| 142 | + [checkoutsCols.START.id]: osparc.utils.Utils.formatDateAndTime(new Date(checkoutsItem["startAt"])), |
| 143 | + [checkoutsCols.END.id]: osparc.utils.Utils.formatDateAndTime(new Date(checkoutsItem["expireAt"])), |
| 144 | + [checkoutsCols.SEATS.id]: checkoutsItem["numOfSeats"], |
| 145 | + [checkoutsCols.COST.id]: checkoutsItem["pricingUnitCost"] ? ("-" + parseFloat(checkoutsItem["pricingUnitCost"]).toFixed(2)) : "", // show it negative |
| 146 | + [checkoutsCols.USER.id]: checkoutsItem["purchasedByUser"], |
| 147 | + }); |
| 148 | + }); |
| 149 | + return data; |
| 150 | + }); |
| 151 | + }; |
| 152 | + |
| 153 | + // Divides the model row request into several server requests to comply with the number of rows server limit |
| 154 | + const reqLimit = lastRow - firstRow + 1; // Number of requested rows |
| 155 | + const nRequests = Math.ceil(reqLimit / this.self().SERVER_MAX_LIMIT); |
| 156 | + if (nRequests > 1) { |
| 157 | + const requests = []; |
| 158 | + for (let i=firstRow; i <= lastRow; i += this.self().SERVER_MAX_LIMIT) { |
| 159 | + requests.push(getFetchPromise(i, i > lastRow - this.self().SERVER_MAX_LIMIT + 1 ? reqLimit % this.self().SERVER_MAX_LIMIT : this.self().SERVER_MAX_LIMIT)) |
| 160 | + } |
| 161 | + Promise.all(requests) |
| 162 | + .then(responses => { |
| 163 | + this._onRowDataLoaded(responses.flat()); |
| 164 | + }) |
| 165 | + .catch(err => { |
| 166 | + console.error(err); |
| 167 | + this._onRowDataLoaded(null); |
| 168 | + }) |
| 169 | + .finally(() => this.setIsFetching(false)); |
| 170 | + } else { |
| 171 | + getFetchPromise(firstRow, reqLimit) |
| 172 | + .then(data => { |
| 173 | + this._onRowDataLoaded(data); |
| 174 | + }) |
| 175 | + .catch(err => { |
| 176 | + console.error(err) |
| 177 | + this._onRowDataLoaded(null); |
| 178 | + }) |
| 179 | + .finally(() => this.setIsFetching(false)); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +}) |
0 commit comments