@finos/legend-application-marketplace
Version:
Legend Marketplace application core
100 lines • 4.43 kB
JavaScript
/**
* Copyright (c) 2025-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { makeObservable, observable, action, flow } from 'mobx';
import { LogEvent, assertErrorThrown, ActionState, LegendUser, } from '@finos/legend-shared';
import { APPLICATION_EVENT } from '@finos/legend-application';
import { SubscriptionResponse, } from '@finos/legend-server-marketplace';
export class SubscriptionStore {
baseStore;
subscriptionFeeds = [];
totalCost = 0;
selectedUser = new LegendUser();
selectedSubscriptions = [];
fetchSubscriptionState = ActionState.create();
fetchUsersState = ActionState.create();
cancelSubscriptionState = ActionState.create();
constructor(baseStore) {
makeObservable(this, {
subscriptionFeeds: observable,
totalCost: observable,
selectedSubscriptions: observable,
addSelectedSubscriptions: action,
removeSelectedSubscription: action,
clearSelectedSubscriptions: action,
setSelectedUser: action,
resetSelectedUser: action,
refresh: flow,
fetchSubscription: flow,
cancelSubscription: flow,
});
this.baseStore = baseStore;
}
addSelectedSubscriptions(subscription) {
const exists = this.selectedSubscriptions.some((sub) => sub.id === subscription?.id);
if (!exists && subscription) {
this.selectedSubscriptions.push(subscription);
}
}
removeSelectedSubscription(subscription) {
this.selectedSubscriptions = this.selectedSubscriptions.filter((sub) => sub.id !== subscription?.id);
}
clearSelectedSubscriptions() {
this.selectedSubscriptions = [];
}
setSelectedUser(user) {
this.selectedUser = user;
}
resetSelectedUser() {
this.selectedUser = new LegendUser();
}
*refresh() {
const user = this.baseStore.applicationStore.identityService.currentUser;
this.fetchSubscription(user);
}
*fetchSubscription(user) {
this.fetchSubscriptionState.inProgress();
try {
const rawResponse = (yield this.baseStore.marketplaceServerClient.getSubscriptions(user));
const response = SubscriptionResponse.serialization.fromJson(rawResponse);
this.subscriptionFeeds = response.subscriptionFeeds;
this.totalCost = response.TotalMonthlyCost;
this.fetchSubscriptionState.complete();
}
catch (error) {
assertErrorThrown(error);
this.baseStore.applicationStore.logService.error(LogEvent.create(APPLICATION_EVENT.GENERIC_FAILURE), `Failed to fetch Subscriptions: ${error.message}`);
this.baseStore.applicationStore.notificationService.notifyError(`Failed to fetch Subscriptions: ${error.message}`);
this.fetchSubscriptionState.fail();
}
}
*cancelSubscription(cancellationRequest) {
this.cancelSubscriptionState.inProgress();
try {
const response = (yield this.baseStore.marketplaceServerClient.cancelSubscriptions(cancellationRequest));
this.baseStore.applicationStore.notificationService.notifySuccess(`Subscriptions Cancelled Successfully: ${response.message}`);
this.cancelSubscriptionState.complete();
this.clearSelectedSubscriptions();
this.refresh();
}
catch (error) {
assertErrorThrown(error);
this.baseStore.applicationStore.logService.error(LogEvent.create(APPLICATION_EVENT.GENERIC_FAILURE), `Failed to cancel Subscriptions: ${error.message}`);
this.baseStore.applicationStore.notificationService.notifyError(`Failed to cancel Subscriptions: ${error.message}`);
this.cancelSubscriptionState.fail();
}
}
}
//# sourceMappingURL=SubscriptionStore.js.map