feat(medusa): Add events for creating, updating, and deleting users to event bus (#1836)
This commit is contained in:
1 parent
dafbfa7799
commit
f5091c6370
2 files changed
+49
-4
No files matched your search
@@ -35,11 +35,15 @@ describe("UserService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("create", () => {
|
describe("create", () => {
|
||||||
const userRepository = MockRepository({})
|
const userRepository = MockRepository({
|
||||||
|
create: (any) => Promise.resolve({ id: IdMap.getId("ironman") }),
|
||||||
|
save: (any) => Promise.resolve({ id: IdMap.getId("ironman") }),
|
||||||
|
})
|
||||||
|
|
||||||
const userService = new UserService({
|
const userService = new UserService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
userRepository,
|
userRepository,
|
||||||
|
eventBusService,
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
@@ -62,6 +66,13 @@ describe("UserService", () => {
|
|||||||
name: "Oliver",
|
name: "Oliver",
|
||||||
password_hash: expect.stringMatching(/.{128}$/),
|
password_hash: expect.stringMatching(/.{128}$/),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
expect(eventBusService.emit).toHaveBeenCalledWith(
|
||||||
|
UserService.Events.CREATED,
|
||||||
|
{
|
||||||
|
id: expect.any(String),
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -72,6 +83,7 @@ describe("UserService", () => {
|
|||||||
const userService = new UserService({
|
const userService = new UserService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
userRepository,
|
userRepository,
|
||||||
|
eventBusService,
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
@@ -90,6 +102,13 @@ describe("UserService", () => {
|
|||||||
first_name: "Tony",
|
first_name: "Tony",
|
||||||
last_name: "Stark",
|
last_name: "Stark",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
expect(eventBusService.emit).toHaveBeenCalledWith(
|
||||||
|
UserService.Events.UPDATED,
|
||||||
|
{
|
||||||
|
id: IdMap.getId("ironman"),
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("successfully updates user metadata", async () => {
|
it("successfully updates user metadata", async () => {
|
||||||
@@ -106,6 +125,13 @@ describe("UserService", () => {
|
|||||||
company: "Stark Industries",
|
company: "Stark Industries",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
expect(eventBusService.emit).toHaveBeenCalledWith(
|
||||||
|
UserService.Events.UPDATED,
|
||||||
|
{
|
||||||
|
id: IdMap.getId("ironman"),
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("fails on email update", async () => {
|
it("fails on email update", async () => {
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ type UserServiceProps = {
|
|||||||
class UserService extends TransactionBaseService<UserService> {
|
class UserService extends TransactionBaseService<UserService> {
|
||||||
static Events = {
|
static Events = {
|
||||||
PASSWORD_RESET: "user.password_reset",
|
PASSWORD_RESET: "user.password_reset",
|
||||||
|
CREATED: "user.created",
|
||||||
|
UPDATED: "user.updated",
|
||||||
|
DELETED: "user.deleted",
|
||||||
}
|
}
|
||||||
|
|
||||||
protected manager_: EntityManager
|
protected manager_: EntityManager
|
||||||
@@ -48,7 +51,9 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
* @return {string} the validated email
|
* @return {string} the validated email
|
||||||
*/
|
*/
|
||||||
validateEmail_(email: string): string {
|
validateEmail_(email: string): string {
|
||||||
const schema = Validator.string().email().required()
|
const schema = Validator.string()
|
||||||
|
.email()
|
||||||
|
.required()
|
||||||
const { value, error } = schema.validate(email)
|
const { value, error } = schema.validate(email)
|
||||||
if (error) {
|
if (error) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
@@ -198,7 +203,13 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
|
|
||||||
const created = userRepo.create(createData)
|
const created = userRepo.create(createData)
|
||||||
|
|
||||||
return await userRepo.save(created)
|
const newUser = await userRepo.save(created)
|
||||||
|
|
||||||
|
await this.eventBus_
|
||||||
|
.withTransaction(manager)
|
||||||
|
.emit(UserService.Events.CREATED, { id: newUser.id })
|
||||||
|
|
||||||
|
return newUser
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,7 +249,13 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
user[key as keyof User] = value
|
user[key as keyof User] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
return await userRepo.save(user)
|
const updatedUser = await userRepo.save(user)
|
||||||
|
|
||||||
|
await this.eventBus_
|
||||||
|
.withTransaction(manager)
|
||||||
|
.emit(UserService.Events.UPDATED, { id: updatedUser.id })
|
||||||
|
|
||||||
|
return updatedUser
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,6 +278,8 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
|
|
||||||
await userRepo.softRemove(user)
|
await userRepo.softRemove(user)
|
||||||
|
|
||||||
|
await this.eventBus_.emit(UserService.Events.DELETED, { id: user.id })
|
||||||
|
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user