UNPKG

@finos/legend-application-marketplace

Version:
151 lines 6.89 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_DataContractsRecordModelSchema, V1_pendingTasksRespondModelSchema, } from '@finos/legend-graph'; import { makeObservable, flow, observable, flowResult, action } from 'mobx'; import { TEST_USER, } from './LakehouseEntitlementsStore.js'; import { LakehouseViewerState } from './LakehouseViewerState.js'; import { buildTaskGridItemDetail, } from '../LakehouseUtils.js'; import { generateLakehouseContractPath } from '../../../__lib__/LegendMarketplaceNavigation.js'; export class EntitlementsTaskViewerState extends LakehouseViewerState { value; approvalStatus = ActionState.create(); canApprove; dataContract; pendingAssociatedContractEvent; changingTaskState = ActionState.create(); taskAssignees; constructor(value, state) { super(state); this.value = value.rec; this.taskAssignees = value.assignees; makeObservable(this, { canApprove: observable, pendingAssociatedContractEvent: observable, setPendingAssociatedContractEvent: action, setCanApprove: action, approve: flow, deny: flow, init: flow, calculateApprovalRights: flow, fetchContract: flow, dataContract: observable, taskAssignees: observable, }); this.observeContract(); } get id() { return this.value.taskId; } setPendingAssociatedContractEvent(pendingAssociatedContractEvent) { this.pendingAssociatedContractEvent = pendingAssociatedContractEvent; } *init(token) { this.initializationState.inProgress(); Promise.all([ flowResult(this.calculateApprovalRights(token)).catch(this.lakehouseEntitlementsStore.applicationStore.alertUnhandledError), flowResult(this.fetchContract(token)).catch(this.lakehouseEntitlementsStore.applicationStore.alertUnhandledError), ]) .catch(this.lakehouseEntitlementsStore.applicationStore.alertUnhandledError) .finally(() => this.initializationState.complete()); } setCanApprove(val) { this.canApprove = val; } observeContract() { makeObservable(this.value, { status: observable, }); } *approve(token) { try { this.approvalStatus.inProgress(); const response = (yield this.lakehouseEntitlementsStore.lakehouseServerClient.approveTask(this.value.taskId, token)); const change = deserialize(V1_TaskStatusChangeResponseModelSchema, response); if (change.errorMessage) { this.approvalStatus.fail(); throw new Error(`Unable to approve task: ${this.value.taskId}: ${change.errorMessage}`); } this.value.status = change.status; this.setCanApprove(false); this.approvalStatus.pass(); this.lakehouseEntitlementsStore.applicationStore.notificationService.notifySuccess('Approval succeeded'); } catch (error) { this.approvalStatus.fail(); assertErrorThrown(error); this.lakehouseEntitlementsStore.applicationStore.notificationService.notifyError(`${error.message}`); } } *deny(token) { try { this.changingTaskState.inProgress(); this.lakehouseEntitlementsStore.applicationStore.alertService.setBlockingAlert({ message: 'Denying Task', prompt: 'Denying task...', showLoading: true, }); const response = (yield this.lakehouseEntitlementsStore.lakehouseServerClient.denyTask(this.value.taskId, token)); const change = deserialize(V1_TaskStatusChangeResponseModelSchema, response); if (change.errorMessage) { throw new Error(`Unable to deny task: ${this.value.taskId}: ${change.errorMessage}`); } this.value.status = change.status; this.setCanApprove(false); this.lakehouseEntitlementsStore.applicationStore.notificationService.notifySuccess(`Task has been denied`); } catch (error) { assertErrorThrown(error); this.lakehouseEntitlementsStore.applicationStore.notificationService.notifyError(`${error.message}`); } finally { this.changingTaskState.complete(); this.changingTaskState.setMessage(undefined); this.lakehouseEntitlementsStore.applicationStore.alertService.setBlockingAlert(undefined); } } *calculateApprovalRights(token) { this.canApprove = undefined; try { const rawTasks = (yield this.lakehouseEntitlementsStore.lakehouseServerClient.getPendingTasks(TEST_USER, token)); const tasks = deserialize(V1_pendingTasksRespondModelSchema, rawTasks); const allTasks = [...tasks.dataOwner, ...tasks.privilegeManager]; const canApprove = Boolean(allTasks.find((e) => e.taskId === this.value.taskId)); this.setCanApprove(canApprove); } catch (error) { assertErrorThrown(error); } } *fetchContract(token) { try { const contractId = this.value.dataContractId; const dataContracts = (yield this.lakehouseEntitlementsStore.lakehouseServerClient.getDataContract(contractId, token)); const dataContract = deserialize(V1_DataContractsRecordModelSchema, dataContracts).dataContracts?.[0]?.dataContract; this.dataContract = dataContract; } catch (error) { assertErrorThrown(error); } } get taskDetails() { return buildTaskGridItemDetail(this.value, this.taskAssignees, this.dataContract, (id) => { this.lakehouseEntitlementsStore.applicationStore.navigationService.navigator.updateCurrentLocation(generateLakehouseContractPath(id)); }, this.lakehouseEntitlementsStore.directoryCallBack, this.lakehouseEntitlementsStore.applicationCallBack); } } //# sourceMappingURL=EntitlementsTaskViewerState.js.map