fix(user): check if user account with email already exist on invite create (#9243)

**What**
- when creating an invite, validate that provided email is not already used for existing user account

---

FIXES CC-513
This commit is contained in:
Frane Polić
2024-09-23 10:53:53 +02:00
committed by GitHub
parent 4a68bc1334
commit 39034e2249
2 changed files with 36 additions and 0 deletions

View File

@@ -223,6 +223,29 @@ moduleIntegrationTestRunner<IUserModuleService>({
)
})
it("should throw if there is an existing user with the invite email", async () => {
let error
await service.createUsers([
{
email: "existing@email.com",
},
])
try {
await service.createInvites([
{
email: "existing@email.com",
},
])
} catch (e) {
error = e
}
expect(error.message).toBe(
`User account for following email(s) already exist: existing@email.com`
)
})
it("should emit invite created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.createInvites(defaultInviteData)

View File

@@ -298,6 +298,19 @@ export default class UserModuleService
data: UserTypes.CreateInviteDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<Invite[]> {
const alreadyExistingUsers = await this.listUsers({
email: data.map((d) => d.email),
})
if (alreadyExistingUsers.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`User account for following email(s) already exist: ${alreadyExistingUsers
.map((u) => u.email)
.join(", ")}`
)
}
const toCreate = data.map((invite) => {
return {
...invite,