UNPKG

@finos/legend-application-marketplace

Version:
142 lines 6.57 kB
/** * Copyright (c) 2020-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 { ActionState, assertErrorThrown, } from '@finos/legend-shared'; import { deserialize } from 'serializr'; import { V1_TaskStatusChangeResponseModelSchema, V1_pendingTasksRespondModelSchema, V1_DataContractsRecordModelSchemaToContracts, } from '@finos/legend-graph'; import { makeObservable, flow, observable, action, flowResult } from 'mobx'; import { TEST_USER, TEST_USER2, } from './LakehouseEntitlementsStore.js'; import { LakehouseViewerState } from './LakehouseViewerState.js'; export class EntitlementsDashboardState extends LakehouseViewerState { pendingTasks; pendingContracts; allContracts; changingState = ActionState.create(); constructor(state) { super(state); makeObservable(this, { pendingTasks: observable, pendingContracts: observable, allContracts: observable, changingState: observable, initializationState: observable, setPendingTasks: action, setPendingContracts: action, setAllContracts: action, approve: flow, fetchPendingContracts: flow, fetchPendingTasks: flow, fetchAllContracts: flow, init: flow, deny: flow, }); } *init(token) { this.initializationState.inProgress(); Promise.all([ flowResult(this.fetchPendingTasks(token)).catch(this.lakehouseEntitlementsStore.applicationStore.alertUnhandledError), flowResult(this.fetchPendingContracts(token)).catch(this.lakehouseEntitlementsStore.applicationStore.alertUnhandledError), flowResult(this.fetchAllContracts(token)).catch(this.lakehouseEntitlementsStore.applicationStore.alertUnhandledError), ]) .catch(this.lakehouseEntitlementsStore.applicationStore.alertUnhandledError) .finally(() => this.initializationState.complete()); } *fetchPendingContracts(token) { try { this.setPendingContracts(undefined); const pendingContracts = (yield this.lakehouseEntitlementsStore.lakehouseServerClient.getPendingContracts(TEST_USER2, token)); this.setPendingContracts(pendingContracts.records ?? []); } catch (error) { assertErrorThrown(error); this.lakehouseEntitlementsStore.applicationStore.notificationService.notifyError(`Error fetching pending contacts: ${error.message}`); } } *fetchPendingTasks(token) { try { this.setPendingTasks(undefined); const rawTasks = (yield this.lakehouseEntitlementsStore.lakehouseServerClient.getPendingTasks(TEST_USER, token)); const tasks = deserialize(V1_pendingTasksRespondModelSchema, rawTasks); this.setPendingTasks([...tasks.dataOwner, ...tasks.privilegeManager]); } catch (error) { assertErrorThrown(error); this.lakehouseEntitlementsStore.applicationStore.notificationService.notifyError(`Error fetching pending tasks: ${error.message}`); } } *fetchAllContracts(token) { try { this.setAllContracts(undefined); const rawContracts = (yield this.lakehouseEntitlementsStore.lakehouseServerClient.getDataContracts(token)); const contracts = V1_DataContractsRecordModelSchemaToContracts(rawContracts); this.setAllContracts([...contracts]); } catch (error) { assertErrorThrown(error); this.lakehouseEntitlementsStore.applicationStore.notificationService.notifyError(`Error fetching all data contracts: ${error.message}`); } } setPendingTasks(val) { this.pendingTasks = val; } setPendingContracts(val) { this.pendingContracts = val; } setAllContracts(val) { this.allContracts = val; } *approve(task, token) { try { this.changingState.inProgress(); this.changingState.setMessage('Approving Task'); const response = (yield this.lakehouseEntitlementsStore.lakehouseServerClient.approveTask(task.taskId, token)); const change = deserialize(V1_TaskStatusChangeResponseModelSchema, response); if (change.errorMessage) { throw new Error(`Unable to approve task: ${task.taskId}: ${change.errorMessage}`); } task.status = change.status; this.setPendingTasks([...(this.pendingTasks ?? [])]); this.lakehouseEntitlementsStore.applicationStore.notificationService.notifySuccess(`Task has been Approved`); } finally { this.changingState.complete(); this.changingState.setMessage(undefined); } } *deny(task, token) { try { this.changingState.inProgress(); this.lakehouseEntitlementsStore.applicationStore.alertService.setBlockingAlert({ message: 'Denying Task', prompt: 'Denying task...', showLoading: true, }); const response = (yield this.lakehouseEntitlementsStore.lakehouseServerClient.denyTask(task.taskId, token)); const change = deserialize(V1_TaskStatusChangeResponseModelSchema, response); if (change.errorMessage) { throw new Error(`Unable to deny task: ${task.taskId}: ${change.errorMessage}`); } task.status = change.status; this.setPendingTasks([...(this.pendingTasks ?? [])]); this.lakehouseEntitlementsStore.applicationStore.notificationService.notifySuccess(`Task has been denied`); } finally { this.changingState.complete(); this.changingState.setMessage(undefined); this.lakehouseEntitlementsStore.applicationStore.alertService.setBlockingAlert(undefined); } } } //# sourceMappingURL=EntitlementsDashboardState.js.map