Skip to content

Commit

Permalink
Merge pull request #8527 from amplication/cherry-pick-pr-8522
Browse files Browse the repository at this point in the history
feat(server):alter reserved name on creation
  • Loading branch information
mulygottlieb committed May 22, 2024
2 parents a9e386e + d0919a6 commit 0a00d67
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
79 changes: 77 additions & 2 deletions packages/amplication-server/src/core/entity/entity.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,33 @@ describe("EntityService", () => {
},
});
});

it("should create entity field with a reserved name", async () => {
expect(
await service.createField(
{
data: {
...EXAMPLE_ENTITY_FIELD_DATA,
name: RESERVED_NAME,
entity: { connect: { id: EXAMPLE_ENTITY_ID } },
},
},
EXAMPLE_USER
)
).toEqual(EXAMPLE_ENTITY_FIELD);
expect(prismaEntityFieldCreateMock).toBeCalledTimes(1);
expect(prismaEntityFieldCreateMock).toBeCalledWith({
data: {
...EXAMPLE_ENTITY_FIELD_DATA,
name: `${RESERVED_NAME}Field`,
permanentId: expect.any(String),
},
include: {
entityVersion: true,
},
});
});

it("should fail to create entity field with bad name", async () => {
await expect(
service.createField(
Expand Down Expand Up @@ -1697,7 +1724,7 @@ describe("EntityService", () => {
expect(await service.hasPendingChanges(EXAMPLE_ENTITY.id)).toBe(false);
expect(areDifferentMock).not.toBeCalled();
});
it("should fail to create one entity with a reserved name", async () => {
it("should create one entity with a reserved name", async () => {
const createArgs = {
args: {
data: {
Expand All @@ -1719,8 +1746,56 @@ describe("EntityService", () => {
};
});

const fixedName = `${RESERVED_NAME}Model`;

const newEntityArgs = {
data: {
...createArgs.args.data,
name: fixedName,
lockedAt: expect.any(Date),
lockedByUser: {
connect: {
id: createArgs.user.id,
},
},
versions: {
create: {
commit: undefined,
versionNumber: CURRENT_VERSION_NUMBER,
name: fixedName,
displayName: createArgs.args.data.displayName,
pluralDisplayName: createArgs.args.data.pluralDisplayName,
customAttributes: createArgs.args.data.customAttributes,
description: createArgs.args.data.description,
permissions: {
create: DEFAULT_PERMISSIONS,
},
},
},
},
};

await service.createOneEntity(createArgs.args, createArgs.user);

expect(prismaEntityCreateMock).toBeCalledWith(newEntityArgs);
});
it("should fail to update one entity with a reserved name", async () => {
const updateArgs = {
args: {
where: { id: EXAMPLE_ENTITY_ID },
data: {
name: RESERVED_NAME,
displayName: EXAMPLE_ENTITY.displayName,
pluralDisplayName: EXAMPLE_ENTITY.pluralDisplayName,
customAttributes: EXAMPLE_ENTITY.customAttributes,
description: EXAMPLE_ENTITY.description,
},
},
user: EXAMPLE_USER,
};

await expect(
service.createOneEntity(createArgs.args, createArgs.user)
service.updateOneEntity(updateArgs.args, updateArgs.user)
).rejects.toThrow(new ReservedNameError(RESERVED_NAME));
});
it("should send unreserved name to a function that checks if its a reserved name", async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/amplication-server/src/core/entity/entity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export class EntityService {
enforceValidation &&
isReservedName(args.data?.name?.toLowerCase().trim())
) {
throw new ReservedNameError(args.data?.name?.toLowerCase().trim());
args.data.name = `${args.data?.name}Model`;
}

const newEntity = await this.prisma.entity.create({
Expand Down Expand Up @@ -2511,7 +2511,7 @@ export class EntityService {
enforceValidation &&
isReservedName(args.data?.name?.toLowerCase().trim())
) {
throw new ReservedNameError(args.data?.name?.toLowerCase().trim());
args.data.name = `${args.data?.name}Field`;
}

// Omit entity from received data
Expand Down

0 comments on commit 0a00d67

Please sign in to comment.