Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
fix: allow negative entity creation in single call (#603)
Browse files Browse the repository at this point in the history
* chore: update models to include ChangeEntityResponse
* fix: update client to return entity with negativeId set
  • Loading branch information
mattmazzola authored Jun 13, 2018
1 parent a31cc3c commit 03c5aef
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"private": true,
"dependencies": {
"@conversationlearner/models": "0.167.0",
"@conversationlearner/models": "0.167.1",
"@conversationlearner/webchat": "0.105.0",
"@types/file-saver": "^1.3.0",
"adaptivecards": "1.0.0",
Expand Down
28 changes: 7 additions & 21 deletions src/actions/createActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,19 @@ export const createEntityThunkAsync = (appId: string, entity: EntityBase) => {
const clClient = ClientFactory.getInstance(AT.CREATE_ENTITY_ASYNC)

try {
let posEntity = await clClient.entitiesCreate(appId, entity);

// If it's a negatable entity
if (posEntity.isNegatible) {
// Create negative entity with ref to positive entity
let negEntity = {
...entity,
entityName: `~${entity.entityName}`,
positiveId: posEntity.entityId
}
// Remove pos entityId from negative entity
delete negEntity.entityId;

negEntity = await clClient.entitiesCreate(appId, negEntity);
dispatch(createEntityFulfilled(negEntity));
const posEntity = await clClient.entitiesCreate(appId, entity);
dispatch(createEntityFulfilled(posEntity));

// Update positive entity with ref to negative entity
posEntity.negativeId = negEntity.entityId;
posEntity = await clClient.entitiesUpdate(appId, posEntity);
if (posEntity.negativeId) {
// Need to load negative entity in order to load it into memory
const negEntity = await clClient.entitiesGetById(appId, posEntity.negativeId)
dispatch(createEntityFulfilled(negEntity));
}
dispatch(createEntityFulfilled(posEntity));

dispatch(fetchApplicationTrainingStatusThunkAsync(appId));
return true;
} catch (e) {
const error = e as Error
dispatch(setErrorDisplay(ErrorType.Error, error.name, [error.message], AT.CREATE_ENTITY_ASYNC))
return false;
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/services/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,29 @@ export default class ClClient {
.then(response => response.data)
}

entitiesGetById(appId: string, entityId: string): Promise<models.EntityBase> {
return this.send<models.EntityBase>({
url: `${this.baseUrl}/app/${appId}/entity/${entityId}`
}).then(response => response.data)
}

entities(appId: string): Promise<models.EntityBase[]> {
return this.send<models.EntityList>({
url: `${this.baseUrl}/app/${appId}/entities`
}).then(response => response.data.entities)
}

entitiesCreate(appId: string, entity: models.EntityBase): Promise<models.EntityBase> {
return this.send<string>({
return this.send<models.ChangeEntityResponse>({
method: 'post',
url: `${this.baseUrl}/app/${appId}/entity`,
data: entity
}).then(response => {
entity.entityId = response.data
return entity
})
const changeEntityResponse = response.data
entity.entityId = changeEntityResponse.entityId
entity.negativeId = changeEntityResponse.negativeEntityId
return entity
})
}

entitiesDelete(appId: string, entityId: string): Promise<models.DeleteEditResponse> {
Expand Down

0 comments on commit 03c5aef

Please sign in to comment.