feat(medusa): Add events for creating, updating, and deleting users to event bus (#1836)

This commit is contained in:
Derek Wene
2022-07-17 09:02:03 -05:00
committed by GitHub
parent dafbfa7799
commit f5091c6370
2 changed files with 49 additions and 4 deletions

View File

@@ -35,11 +35,15 @@ describe("UserService", () => {
})
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({
manager: MockManager,
userRepository,
eventBusService,
})
beforeEach(async () => {
@@ -62,6 +66,13 @@ describe("UserService", () => {
name: "Oliver",
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({
manager: MockManager,
userRepository,
eventBusService,
})
beforeEach(async () => {
@@ -90,6 +102,13 @@ describe("UserService", () => {
first_name: "Tony",
last_name: "Stark",
})
expect(eventBusService.emit).toHaveBeenCalledWith(
UserService.Events.UPDATED,
{
id: IdMap.getId("ironman"),
}
)
})
it("successfully updates user metadata", async () => {
@@ -106,6 +125,13 @@ describe("UserService", () => {
company: "Stark Industries",
},
})
expect(eventBusService.emit).toHaveBeenCalledWith(
UserService.Events.UPDATED,
{
id: IdMap.getId("ironman"),
}
)
})
it("fails on email update", async () => {

View File

@@ -27,6 +27,9 @@ type UserServiceProps = {
class UserService extends TransactionBaseService<UserService> {
static Events = {
PASSWORD_RESET: "user.password_reset",
CREATED: "user.created",
UPDATED: "user.updated",
DELETED: "user.deleted",
}
protected manager_: EntityManager
@@ -48,7 +51,9 @@ class UserService extends TransactionBaseService<UserService> {
* @return {string} the validated email
*/
validateEmail_(email: string): string {
const schema = Validator.string().email().required()
const schema = Validator.string()
.email()
.required()
const { value, error } = schema.validate(email)
if (error) {
throw new MedusaError(
@@ -198,7 +203,13 @@ class UserService extends TransactionBaseService<UserService> {
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
}
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 this.eventBus_.emit(UserService.Events.DELETED, { id: user.id })
return Promise.resolve()
})
}