-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
27 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 5 additions & 26 deletions
31
starters/plugin-module/src/modules/user/attachUserSubscribers.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,17 @@ | ||
import { Express, NextFunction, Request, Response } from 'express'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { | ||
MEDUSA_RESOLVER_KEYS, | ||
MedusaAuthenticatedRequest, | ||
MedusaRouteOptions, | ||
Middleware, | ||
Utils as MedusaUtils, | ||
} from 'medusa-extender'; | ||
import { Connection } from 'typeorm'; | ||
import UserSubscriber from './user.subscriber'; | ||
|
||
@Middleware({ requireAuth: true, routes: [{ method: 'post', path: '/admin/users/' }] }) | ||
export default class AttachUserSubscribersMiddleware { | ||
public static get routesOptions(): MedusaRouteOptions { | ||
return { | ||
path: '/admin/users/', | ||
method: 'post', | ||
}; | ||
} | ||
|
||
public consume(options: { | ||
app: Express; | ||
}): (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => void | Promise<void> { | ||
const routeOptions = AttachUserSubscribersMiddleware.routesOptions; | ||
options.app.use((req: MedusaAuthenticatedRequest, res: Response, next: NextFunction): void => { | ||
if ( | ||
req.method.toLowerCase() === routeOptions.method && | ||
req.originalUrl.toLowerCase() === routeOptions.path.toLowerCase() | ||
) { | ||
const { connection } = req.scope.resolve(MEDUSA_RESOLVER_KEYS.manager) as { connection: Connection }; | ||
MedusaUtils.attachOrReplaceEntitySubscriber(connection, UserSubscriber); | ||
} | ||
return next(); | ||
}); | ||
|
||
return (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => next(); | ||
public consume(req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction): void { | ||
const { connection } = req.scope.resolve(MEDUSA_RESOLVER_KEYS.manager) as { connection: Connection }; | ||
UserSubscriber.attachTo(connection); | ||
return next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,22 @@ | ||
import { Service } from 'medusa-extender'; | ||
import { EntityManager } from 'typeorm'; | ||
import { UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; | ||
import EventBusService from '@medusajs/medusa/dist/services/event-bus'; | ||
import { EventBusService, UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; | ||
import UserRepository from './user.repository'; | ||
import { User } from './user.entity'; | ||
|
||
type ConstructorParams = { | ||
loggedInUser: User; | ||
type InjectedDependencies = { | ||
manager: EntityManager; | ||
userRepository: typeof UserRepository; | ||
eventBusService: EventBusService; | ||
}; | ||
|
||
@Service({ override: MedusaUserService, scope: 'SCOPED' }) | ||
@Service({ override: MedusaUserService }) | ||
export default class UserService extends MedusaUserService { | ||
private readonly manager: EntityManager; | ||
private readonly userRepository: typeof UserRepository; | ||
private readonly eventBus: EventBusService; | ||
|
||
constructor(private readonly container: ConstructorParams) { | ||
super(container); | ||
this.manager = container.manager; | ||
this.userRepository = container.userRepository; | ||
this.eventBus = container.eventBusService; | ||
constructor({ manager, userRepository, eventBusService }: InjectedDependencies) { | ||
super({ manager, userRepository, eventBusService }); | ||
this.manager = manager; | ||
this.userRepository = userRepository; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 5 additions & 26 deletions
31
starters/server/src/modules/user/attachUserSubscribers.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,17 @@ | ||
import { Express, NextFunction, Request, Response } from 'express'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { | ||
MEDUSA_RESOLVER_KEYS, | ||
MedusaAuthenticatedRequest, | ||
MedusaRouteOptions, | ||
Middleware, | ||
Utils as MedusaUtils, | ||
} from 'medusa-extender'; | ||
import { Connection } from 'typeorm'; | ||
import UserSubscriber from './user.subscriber'; | ||
|
||
@Middleware({ requireAuth: true, routes: [{ method: 'post', path: '/admin/users/' }] }) | ||
export default class AttachUserSubscribersMiddleware { | ||
public static get routesOptions(): MedusaRouteOptions { | ||
return { | ||
path: '/admin/users/', | ||
method: 'post', | ||
}; | ||
} | ||
|
||
public consume(options: { | ||
app: Express; | ||
}): (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => void | Promise<void> { | ||
const routeOptions = AttachUserSubscribersMiddleware.routesOptions; | ||
options.app.use((req: MedusaAuthenticatedRequest, res: Response, next: NextFunction): void => { | ||
if ( | ||
req.method.toLowerCase() === routeOptions.method && | ||
req.originalUrl.toLowerCase() === routeOptions.path.toLowerCase() | ||
) { | ||
const { connection } = req.scope.resolve(MEDUSA_RESOLVER_KEYS.manager) as { connection: Connection }; | ||
MedusaUtils.attachOrReplaceEntitySubscriber(connection, UserSubscriber); | ||
} | ||
return next(); | ||
}); | ||
|
||
return (req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction) => next(); | ||
public consume(req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction): void { | ||
const { connection } = req.scope.resolve(MEDUSA_RESOLVER_KEYS.manager) as { connection: Connection }; | ||
UserSubscriber.attachTo(connection); | ||
return next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,22 @@ | ||
import { Service } from 'medusa-extender'; | ||
import { EntityManager } from 'typeorm'; | ||
import { UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; | ||
import EventBusService from '@medusajs/medusa/dist/services/event-bus'; | ||
import { EventBusService, UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; | ||
import UserRepository from './user.repository'; | ||
import { User } from './user.entity'; | ||
|
||
type ConstructorParams = { | ||
loggedInUser: User; | ||
type InjectedDependencies = { | ||
manager: EntityManager; | ||
userRepository: typeof UserRepository; | ||
eventBusService: EventBusService; | ||
}; | ||
|
||
@Service({ override: MedusaUserService, scope: 'SCOPED' }) | ||
@Service({ override: MedusaUserService }) | ||
export default class UserService extends MedusaUserService { | ||
private readonly manager: EntityManager; | ||
private readonly userRepository: typeof UserRepository; | ||
private readonly eventBus: EventBusService; | ||
|
||
constructor(private readonly container: ConstructorParams) { | ||
super(container); | ||
this.manager = container.manager; | ||
this.userRepository = container.userRepository; | ||
this.eventBus = container.eventBusService; | ||
constructor({ manager, userRepository, eventBusService }: InjectedDependencies) { | ||
super({ manager, userRepository, eventBusService }); | ||
this.manager = manager; | ||
this.userRepository = userRepository; | ||
} | ||
} |