UNPKG

@finos/legend-application-marketplace

Version:
181 lines 8.58 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 { NAVIGATION_ZONE_SEPARATOR, } from '@finos/legend-application'; import { V1_AdhocTeam, V1_AppDirLevel, V1_AppDirNode, V1_AppDirNodeModelSchema, V1_createContractPayloadModelSchema, V1_DataContractsRecordModelSchemaToContracts, V1_ResourceType, V1_User, V1_UserType, } from '@finos/legend-graph'; import { action, computed, flow, makeObservable, observable } from 'mobx'; import { DATA_PRODUCT_WIKI_PAGE_SECTIONS, DataProductLayoutState, } from './DataProductLayoutState.js'; import { DATA_PRODUCT_VIEWER_ACTIVITY_MODE, generateAnchorForActivity, } from './DataProductViewerNavigation.js'; import { DataProductDataAccessState } from './DataProductDataAccessState.js'; import { ActionState, assertErrorThrown, guaranteeNonEmptyString, guaranteeNonNullable, } from '@finos/legend-shared'; import { serialize } from 'serializr'; import { dataContractContainsDataProduct } from './LakehouseUtils.js'; const buildAdhocUser = (user) => { const _user = new V1_User(); _user.name = user; _user.userType = V1_UserType.WORKFORCE_USER; const _adhocTeam = new V1_AdhocTeam(); _adhocTeam.users = [_user]; return _adhocTeam; }; export class DataProductViewerState { applicationStore; lakehouseStore; graphManagerState; layoutState; product; isSandboxProduct; project; retrieveGraphData; viewSDLCProject; viewIngestEnvironment; onZoneChange; // we may want to move this out eventually lakeServerClient; currentActivity = DATA_PRODUCT_VIEWER_ACTIVITY_MODE.DESCRIPTION; accessState; generation; associatedContracts; dataContractAccessPointGroup; dataContract; // actions creatingContractState = ActionState.create(); constructor(applicationStore, lakehouseStore, graphManagerState, lakeServerClient, project, product, isSandboxProduct, generation, actions) { makeObservable(this, { currentActivity: observable, setCurrentActivity: action, isVerified: computed, accessState: observable, fetchContracts: flow, associatedContracts: observable, dataContractAccessPointGroup: observable, setDataContractAccessPointGroup: action, dataContract: observable, setDataContract: action, setAssociatedContracts: action, createContract: flow, creatingContractState: observable, }); this.applicationStore = applicationStore; this.lakehouseStore = lakehouseStore; this.graphManagerState = graphManagerState; this.project = project; this.product = product; this.isSandboxProduct = isSandboxProduct; this.generation = generation; this.retrieveGraphData = actions.retrieveGraphData; this.viewSDLCProject = actions.viewSDLCProject; this.viewIngestEnvironment = actions.viewIngestEnvironment; this.onZoneChange = actions.onZoneChange; this.layoutState = new DataProductLayoutState(this); this.accessState = new DataProductDataAccessState(this); this.lakeServerClient = lakeServerClient; } setAssociatedContracts(val) { this.associatedContracts = val; } setDataContractAccessPointGroup(val) { this.dataContractAccessPointGroup = val; } setDataContract(val) { this.dataContract = val; } *fetchContracts(token) { try { this.accessState.accessGroupStates.forEach((e) => e.fetchingAccessState.inProgress()); const did = guaranteeNonEmptyString(this.generation?.dataProduct.deploymentId, 'did required to get contracts'); const didNode = new V1_AppDirNode(); didNode.appDirId = Number(did); didNode.level = V1_AppDirLevel.DEPLOYMENT; const _contracts = (yield this.lakeServerClient.getDataContractsFromDID([serialize(V1_AppDirNodeModelSchema, didNode)], token)); const dataProductContracts = V1_DataContractsRecordModelSchemaToContracts(_contracts).filter((_contract) => dataContractContainsDataProduct(this.product, this.deploymentId, _contract)); this.setAssociatedContracts(dataProductContracts); this.accessState.accessGroupStates.forEach((e) => e.handleDataProductContracts(dataProductContracts)); } catch (error) { assertErrorThrown(error); this.accessState.viewerState.applicationStore.notificationService.notifyError(`${error.message}`); } finally { this.accessState.accessGroupStates.forEach((e) => e.fetchingAccessState.complete()); } } *createContract(userId, description, group, token) { try { this.creatingContractState.inProgress(); const request = serialize(V1_createContractPayloadModelSchema, { description, resourceId: this.product.name, resourceType: V1_ResourceType.ACCESS_POINT_GROUP, deploymentId: guaranteeNonNullable(this.generation?.dataProduct.deploymentId, 'Cannot create contract. Data product generation is missing deployment ID'), accessPointGroup: group.id, consumer: buildAdhocUser(userId), }); const contracts = V1_DataContractsRecordModelSchemaToContracts((yield this.lakeServerClient.createContract(request, token))); const associatedContract = contracts[0]; // Only if the user has requested a contract for themself do we update the associated contract. if (associatedContract?.consumer instanceof V1_AdhocTeam && associatedContract.consumer.users.some((u) => u.name === this.applicationStore.identityService.currentUser)) { const groupAccessState = this.accessState.accessGroupStates.find((e) => e.group === group); groupAccessState?.setAssociatedContract(associatedContract); } this.setDataContractAccessPointGroup(undefined); this.setDataContract(associatedContract); this.applicationStore.notificationService.notifySuccess(`Contract created, please go to contract view for pending tasks`); } catch (error) { assertErrorThrown(error); this.accessState.viewerState.applicationStore.notificationService.notifyError(`${error.message}`); } finally { this.creatingContractState.complete(); } } setCurrentActivity(val) { this.currentActivity = val; } get isVerified() { // TODO what does it mean if data product is vertified ? return true; } get deploymentId() { return this.generation?.dataProduct.deploymentId; } changeZone(zone, force = false) { if (force) { this.layoutState.setCurrentNavigationZone(''); } if (zone !== this.layoutState.currentNavigationZone) { const zoneChunks = zone.split(NAVIGATION_ZONE_SEPARATOR); const activityChunk = zoneChunks[0]; const matchingActivity = Object.values(DATA_PRODUCT_VIEWER_ACTIVITY_MODE).find((activity) => generateAnchorForActivity(activity) === activityChunk); if (activityChunk && matchingActivity) { if (DATA_PRODUCT_WIKI_PAGE_SECTIONS.includes(matchingActivity)) { this.layoutState.setWikiPageAnchorToNavigate({ anchor: zone, }); } this.setCurrentActivity(matchingActivity); this.onZoneChange?.(zone); this.layoutState.setCurrentNavigationZone(zone); } else { this.setCurrentActivity(DATA_PRODUCT_VIEWER_ACTIVITY_MODE.DESCRIPTION); this.layoutState.setCurrentNavigationZone(''); } } } } //# sourceMappingURL=DataProductViewerState.js.map