/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */ import * as ClientFactory from '../services/clientFactory' ; import * as CLM from '@conversationlearner/models' ; import { ActionObject , ErrorType } from '../types' ; import { AT } from '../types/ActionTypes' ; import { Dispatch } from 'redux' ; import { setErrorDisplay } from './displayActions' ; import { fetchApplicationTrainingStatusThunkAsync } from './appActions' ; import { AxiosError } from 'axios' ; import { fetchAllActionsThunkAsync } from './actionActions' ; import { fetchAllTrainDialogsThunkAsync } from './trainActions' ; const createEntityAsync = ( appId , entity ) : ActionObject => { return { type : AT . CREATE_ENTITY_ASYNC , entity : entity , appId : appId } ; } ; const createEntityFulfilled = ( entity ) : ActionObject => { return { type : AT . CREATE_ENTITY_FULFILLED , entity : entity } ; } ; export const createEntityThunkAsync = ( appId , entity ) => { return async ( dispatch ) => { dispatch ( createEntityAsync ( appId , entity ) ) ; const clClient = ClientFactory . getInstance ( AT . CREATE_ENTITY_ASYNC ) ; try { const posEntity = await clClient . entitiesCreate ( appId , entity ) ; dispatch ( createEntityFulfilled ( posEntity ) ) ; if ( posEntity . negativeId ) { const negEntity = await clClient . entitiesGetById ( appId , posEntity . negativeId ) ; dispatch ( createEntityFulfilled ( negEntity ) ) ; } if ( entity . resolverType !== undefined && entity . resolverType !== null ) { void dispatch ( fetchAllEntitiesThunkAsync ( appId ) ) ; } return posEntity . entityId ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . CREATE_ENTITY_ASYNC ) ) ; return null ; } } ; } ; const editEntityAsync = ( appId , entity ) : ActionObject => { return { type : AT . EDIT_ENTITY_ASYNC , appId , entity } ; } ; const editEntityFulfilled = ( entity ) : ActionObject => { return { type : AT . EDIT_ENTITY_FULFILLED , entity : entity } ; } ; export const editEntityThunkAsync = ( appId , entity , prevEntity ) => { return async ( dispatch ) => { const clClient = ClientFactory . getInstance ( AT . EDIT_ENTITY_ASYNC ) ; dispatch ( editEntityAsync ( appId , entity ) ) ; try { const changedEntityResponse = await clClient . entitiesUpdate ( appId , entity ) ; dispatch ( editEntityFulfilled ( entity ) ) ; if ( entity . isNegatible && ! prevEntity . isNegatible ) { const negEntity = await clClient . entitiesGetById ( appId , changedEntityResponse . negativeEntityId ) ; dispatch ( createEntityFulfilled ( negEntity ) ) ; } else if ( ! entity . isNegatible && prevEntity . isNegatible ) { dispatch ( deleteEntityFulfilled ( prevEntity . negativeId ! ) ) ; } else if ( entity . isNegatible && changedEntityResponse . negativeEntityId ) { const negEntity = await clClient . entitiesGetById ( appId , changedEntityResponse . negativeEntityId ) ; dispatch ( editEntityFulfilled ( negEntity ) ) ; } if ( entity . resolverType !== prevEntity . resolverType ) { void dispatch ( fetchAllEntitiesThunkAsync ( appId ) ) ; } else if ( entity . entityType === CLM . EntityType . ENUM ) { const updatedEntity = await clClient . entitiesGetById ( appId , entity . entityId ) ; dispatch ( editEntityFulfilled ( updatedEntity ) ) ; void dispatch ( fetchAllActionsThunkAsync ( appId ) ) ; } if ( changedEntityResponse . trainDialogIds ?. length > 0 ) { void dispatch ( fetchAllTrainDialogsThunkAsync ( appId ) ) ; } void dispatch ( fetchApplicationTrainingStatusThunkAsync ( appId ) ) ; return entity ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . EDIT_ENTITY_ASYNC ) ) ; throw error ; } } ; } ; const deleteEntityAsync = ( appId , entityId ) : ActionObject => { return { type : AT . DELETE_ENTITY_ASYNC , entityId : entityId , appId : appId } ; } ; const deleteEntityFulfilled = ( entityId ) : ActionObject => { return { type : AT . DELETE_ENTITY_FULFILLED , entityId : entityId } ; } ; export const deleteEntityThunkAsync = ( appId , entity ) => { return async ( dispatch ) => { const entityId = entity . entityId ; dispatch ( deleteEntityAsync ( appId , entityId ) ) ; const clClient = ClientFactory . getInstance ( AT . DELETE_ENTITY_ASYNC ) ; try { const deleteEditResponse = await clClient . entitiesDelete ( appId , entityId ) ; dispatch ( deleteEntityFulfilled ( entityId ) ) ; if ( entity . isNegatible ) { const negativeEntityId = entity . negativeId ! ; dispatch ( deleteEntityFulfilled ( negativeEntityId ) ) ; } if ( CLM . isPrebuilt ( entity ) ) { void dispatch ( fetchAllEntitiesThunkAsync ( appId ) ) ; } if ( deleteEditResponse . actionIds ?. length > 0 ) { void dispatch ( fetchAllActionsThunkAsync ( appId ) ) ; } if ( deleteEditResponse . trainDialogIds ?. length > 0 ) { void dispatch ( fetchAllTrainDialogsThunkAsync ( appId ) ) ; } void dispatch ( fetchApplicationTrainingStatusThunkAsync ( appId ) ) ; return true ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . DELETE_ENTITY_ASYNC ) ) ; return false ; } } ; } ; const fetchAllEntitiesAsync = ( appId ) : ActionObject => { return { type : AT . FETCH_ENTITIES_ASYNC , appId : appId } ; } ; const fetchAllEntitiesFulfilled = ( entities ) : ActionObject => { return { type : AT . FETCH_ENTITIES_FULFILLED , allEntities : entities } ; } ; export const fetchAllEntitiesThunkAsync = ( appId ) => { return async ( dispatch ) => { const clClient = ClientFactory . getInstance ( AT . FETCH_ENTITIES_ASYNC ) ; dispatch ( fetchAllEntitiesAsync ( appId ) ) ; try { const entities = await clClient . entities ( appId ) ; dispatch ( fetchAllEntitiesFulfilled ( entities ) ) ; return entities ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . FETCH_ENTITIES_ASYNC ) ) ; return null ; } } ; } ; const fetchEntityDeleteValidationAsync = ( appId , packageId , entityId ) : ActionObject => { return { type : AT . FETCH_ENTITY_DELETE_VALIDATION_ASYNC , appId : appId , packageId : packageId , entityId : entityId } ; } ; const fetchEntityDeleteValidationFulfilled = ( ) : ActionObject => { return { type : AT . FETCH_ENTITY_DELETE_VALIDATION_FULFILLED } ; } ; export const fetchEntityDeleteValidationThunkAsync = ( appId , packageId , entityId ) => { return async ( dispatch ) => { const clClient = ClientFactory . getInstance ( AT . FETCH_ENTITY_DELETE_VALIDATION_ASYNC ) ; dispatch ( fetchEntityDeleteValidationAsync ( appId , packageId , entityId ) ) ; try { const invalidTrainDialogIds = await clClient . entitiesDeleteValidation ( appId , packageId , entityId ) ; dispatch ( fetchEntityDeleteValidationFulfilled ( ) ) ; return invalidTrainDialogIds ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . FETCH_ENTITY_DELETE_VALIDATION_ASYNC ) ) ; return null ; } } ; } ; const fetchEntityEditValidationAsync = ( appId , packageId , entity ) : ActionObject => { return { type : AT . FETCH_ENTITY_EDIT_VALIDATION_ASYNC , appId : appId , packageId : packageId , entity : entity } ; } ; const fetchEntityEditValidationFulfilled = ( ) : ActionObject => { return { type : AT . FETCH_ENTITY_EDIT_VALIDATION_FULFILLED } ; } ; export const fetchEntityEditValidationThunkAsync = ( appId , packageId , entity ) => { return async ( dispatch ) => { const clClient = ClientFactory . getInstance ( AT . FETCH_ENTITY_EDIT_VALIDATION_ASYNC ) ; dispatch ( fetchEntityEditValidationAsync ( appId , packageId , entity ) ) ; try { const invalidTrainDialogIds = await clClient . entitiesUpdateValidation ( appId , packageId , entity ) ; dispatch ( fetchEntityEditValidationFulfilled ( ) ) ; return invalidTrainDialogIds ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . FETCH_ENTITY_EDIT_VALIDATION_ASYNC ) ) ; return null ; } } ; } ;