Skip to content

Commit

Permalink
Merge pull request #8424 from amplication/fix/duplicate-entities
Browse files Browse the repository at this point in the history
fix(server):send error per entity
  • Loading branch information
mulygottlieb committed May 8, 2024
2 parents 3c288d3 + 263f354 commit d6c428b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,63 @@ describe("AssistantFunctionsService", () => {
results: "User does not have access to this resource",
});
});

it("should return error for one entity, while creating others successfully ", async () => {
const EXAMPLE_SERVICE_ID = "exampleServiceId";

const functionName = EnumAssistantFunctions.CreateEntities;
const reservedEntityName = "function";
const params = {
names: ["entity 1", reservedEntityName, "entity 3"],
serviceId: EXAMPLE_SERVICE_ID,
};

const entityResults = {
...EXAMPLE_ENTITY,
updatedAt: expect.any(String),
createdAt: expect.any(String),
};

const errorMessage = `The name '${reservedEntityName}' is reserved`;

entityServiceCreateOneEntityMock.mockImplementationOnce(() => {
return EXAMPLE_ENTITY;
});
entityServiceCreateOneEntityMock.mockImplementationOnce(() => {
throw new Error(errorMessage);
});
entityServiceCreateOneEntityMock.mockImplementationOnce(() => {
return EXAMPLE_ENTITY;
});

const functionResults = await service.executeFunction(
EXAMPLE_CALL_ID,
functionName,
JSON.stringify(params),
EXAMPLE_ASSISTANT_CONTEXT,
EXAMPLE_LOGGER_CONTEXT
);

expect(entityServiceCreateOneEntityMock).toHaveBeenCalledTimes(3);

const executionResults = JSON.parse(functionResults.results);

expect(executionResults).toEqual(
expect.objectContaining({
allEntitiesLink: expect.any(String),
allApisLink: expect.any(String),
result: [
expect.objectContaining({
result: expect.objectContaining(entityResults),
}),
expect.objectContaining({
error: errorMessage,
}),
expect.objectContaining({
result: expect.objectContaining(entityResults),
}),
],
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -258,33 +258,47 @@ export class AssistantFunctionsService {
if (pluralDisplayName === entityName) {
pluralDisplayName = `${entityName}Items`;
}
const entity = await this.entityService.createOneEntity(
{
data: {
displayName: entityName,
pluralDisplayName: pluralDisplayName,
name: pascalCase(entityName),
resource: {
connect: {
id: args.serviceId,
try {
const entity = await this.entityService.createOneEntity(
{
data: {
displayName: entityName,
pluralDisplayName: pluralDisplayName,
name: pascalCase(entityName),
resource: {
connect: {
id: args.serviceId,
},
},
},
},
},
context.user
);

const defaultModuleId =
await this.moduleService.getDefaultModuleIdForEntity(
args.serviceId,
entity.id
context.user
);

return {
entityLink: `${this.clientHost}/${context.workspaceId}/${context.projectId}/${args.serviceId}/entities/${entity.id}`,
apisLink: `${this.clientHost}/${context.workspaceId}/${context.projectId}/${args.serviceId}/modules/${defaultModuleId}`,
result: entity,
};
const defaultModuleId =
await this.moduleService.getDefaultModuleIdForEntity(
args.serviceId,
entity.id
);

return {
entityLink: `${this.clientHost}/${context.workspaceId}/${context.projectId}/${args.serviceId}/entities/${entity.id}`,
apisLink: `${this.clientHost}/${context.workspaceId}/${context.projectId}/${args.serviceId}/modules/${defaultModuleId}`,
result: entity,
};
} catch (error) {
this.logger.error(
`Chat: Error creating entity ${entityName}`,
error,
loggerContext
);
return {
entityLink: null,
apisLink: null,
result: null,
error: error.message,
};
}
})
);

Expand Down

0 comments on commit d6c428b

Please sign in to comment.