Skip to content

Commit

Permalink
feat(server):validate duplicate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
yuval-hazaz committed May 12, 2024
1 parent 9dc9ed2 commit 80b55e2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/amplication-server/src/core/module/module.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const EXAMPLE_MODULE: Module = {
inputParameters: null,
outputParameters: null,
versionNumber: 0,
resourceId: EXAMPLE_RESOURCE_ID,
};

export const subscriptionServiceFindOneMock = jest.fn(() => {
Expand Down Expand Up @@ -107,6 +108,7 @@ const blockServiceCreateMock = jest.fn((args: CreateModuleArgs): Module => {
description: data.description,
inputParameters: null,
outputParameters: null,
resourceId: EXAMPLE_RESOURCE_ID,
};
});

Expand Down Expand Up @@ -396,4 +398,31 @@ describe("ModuleService", () => {
)
);
});

it("should throw an error when updating a module with a name that is already used", async () => {
blockServiceFindManyByBlockTypeMock.mockReturnValue([
{
...EXAMPLE_MODULE,
id: "anotherModuleId",
name: EXAMPLE_MODULE_NAME,
},
]);

const args: UpdateModuleArgs = {
where: {
id: EXAMPLE_MODULE_ID,
},
data: {
displayName: EXAMPLE_MODULE_DISPLAY_NAME,
name: EXAMPLE_MODULE_NAME,
enabled: true,
},
};

await expect(service.update(args, EXAMPLE_USER)).rejects.toThrow(
new AmplicationError(
`Module with name ${args.data.name} already exists in resource ${EXAMPLE_RESOURCE_ID}`
)
);
});
});
14 changes: 14 additions & 0 deletions packages/amplication-server/src/core/module/module.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ export class ModuleService extends BlockTypeService<

this.validateModuleName(args.data.name);

const otherModule = await this.findModuleByName(
args.data.name,
existingModule.resourceId
);

if (
otherModule.length > 0 &&
otherModule.filter((module) => module.id !== args.where.id).length > 0
) {
throw new AmplicationError(
`Module with name ${args.data.name} already exists in resource ${existingModule.resourceId}`
);
}

const subscription = await this.billingService.getSubscription(
user.workspace?.id
);
Expand Down

0 comments on commit 80b55e2

Please sign in to comment.