I want to execute this cron function in my NestJs project :
#Cron('59 23 * * *')
async CashPendingCRON(){
let stores = await this.storeRepository.find();
for (let store of stores){
await this.connection
.createQueryBuilder()
.insert()
.into(CashPending)
.values([
{ cashPending: store.cashPending, store: store }
])
.execute()
}
As you can see the corn job is supposed to execute at 11:59 pm everyday. But it gets executed twice and the entries are logged in the DB two times. When I use intervals like 10 seconds (*/10 * * * * *) it gets called only once.
Please let me know if there is a fix or if I am doing something wrong.
Here is how I added the ScheduleModule in the app.module.ts
#Module({
imports: [
ScheduleModule.forRoot(),
ConfigModule.forRoot({
load: [appConfig, devConfig, stagConfig],
ignoreEnvFile: true,
isGlobal: true,
}),
TypeOrmModule.forRoot(
configService.getTypeOrmConfig(),
),
TypeOrmModule.forFeature([
User,
Vendor,
Store,
Product,
Category,
Brand,
AppVersion
]),
JwtModule.registerAsync({
imports: [ConfigModule],
useFactory: async () => ({
secret: process.env.TOKEN_KEY,
}),
inject: [ConfigService],
}),
UserModule,
UserClusterModule,
StoreModule,
OperationManagerModule,
UserBrandModule,
UserCatalogueModule,
UserPropertyModule,
FileModule,
BrandModule,
CategoryModule,
ProductsModule,
WarehouseModule,
SubCategoryModule,
StoreStocksModule,
WarehouseStockModule,
RtvStocksModule,
VendorModule,
CustomerModule,
W2sModule,
S2sModule,
W2wModule,
BillerModule,
WarehouseManagerModule,
AuthModule,
OrderModule,
GRNModule,
SKUTimelinesModule,
BannerModule,
OrderReturnModule,
UtilModule,
POModule,
AppVersion,
S2wModule,
CashOutModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Please help. Thank you.
I was going through the same issue and the problem was that I was using the imports: [ScheduleModule.forRoot()] in a module that was imported twice (by other two modules). I solved it by creating a new module that is not imported by any other module and adding the ScheduleModule.forRoot() in it.
scheduler.module.ts
#Module({
providers: [SchedulerService],
imports: [ScheduleModule.forRoot()],
})
export class SchedulerModule {}
scheduler.service.ts
import { Injectable } from '#nestjs/common';
import { Cron, CronExpression } from '#nestjs/schedule';
#Injectable()
export class SchedulerService {
#Cron(CronExpression.EVERY_10_SECONDS)
handleExpiration() {
console.log(new Date());
}
}
Console output:
2022-12-21T14:04:00.005Z
2022-12-21T14:04:10.004Z
2022-12-21T14:04:20.009Z
2022-12-21T14:04:30.004Z
2022-12-21T14:04:40.011Z
...
Related
I have a Nest.js app with a REST module and a GraphQL module. Both are imported into an App.module.ts. I'm using Nest's Throttler Guard to protect the whole application. As it's already known, GraphQL does not work with the normal ThrottlerGuard, so I created a GqlThrottlerGuard and imported it on the GraphQL module, while importing the original ThrottlerGuard on the REST module.
So, my graphQL module looks like this:
#Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: true
}),
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
ttl: config.get('security.throttle.ttl'),
limit: config.get('security.throttle.limit'),
}),
}),
],
providers: [
{
provide: APP_GUARD,
useClass: GqlThrottlerGuard,
},
],
})
export class GraphModule { }
And the REST module, like this:
#Module({
imports: [
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
ttl: config.get('security.throttle.ttl'),
limit: config.get('security.throttle.limit'),
}),
}),
],
controllers: [RestController],
providers: [
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
})
export class RestModule implements NestModule {}
Finally, both modules are imported to the App Module, which is the module I actually run:
#Module({
imports: [
RestModule,
GraphModule,
],
})
export class AppModule { }
For some reason, the error seen here is still happening to me on the GraphModule, even though the normal ThrottlerGuard is imported only on the RestModule. Should it work like this? How can I solve it?
APP_GUARD is a global binding, it applies to all routes. You should make one coherent guard that returns the proper request response based on the ExecutionContext#getType method which will either return http or graphql
#Injectable()
export class CustomThrottlerGuard extends ThrottlerGuard {
getRequestResponse(context: ExecutionContext) {
const reqType = context.getType<ContextType | 'graphql'>()
if (reqType === 'graphql') {
const gqlCtx = GqlExecutionContext.create(context);
const ctx = gqlCtx.getContext();
return { req: ctx.req, res: ctx.res };
} else if (reqType === 'http') {
return {
req: context.switchToHttp().getRequest(),
res: context.switchToHttp().getResponse()
}
} else {
// handle rpc and ws if you have them, otherwise ignore and make previous `else if` just an `else`
}
}
im trying to test jwt auth in nestjs.
when i called jwtService.sign();
it shows error secretOrPrivateKey must have a value - {}
secret is undefined.
but in AuthController, porcess.env.JWT_SECRET_KEY is work.
i dont know why it is not work.
how can i fix it ?
auth.module.ts
#Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [AppConfig, MySqlConfig, OracleConfig],
envFilePath: `${process.env.NODE_ENV}` == '' ? '.env.dev' : `.env.${process.env.NODE_ENV}`,
validationSchema: Joi.object({
NODE_ENV: Joi.string()
.valid('dev', 'stg', 'prd'),
}),
}),
AuthModule,
],
controllers: [AppController],
providers: [AppService, Logger],
})
export class AppModule {}
app.module.ts
#Module({
imports: [
PassportModule,
JwtModule.register({
secret: process.env.JWT_SECRET_KEY,
signOptions: { expiresIn: '1d' },
}),
],
controllers: [AuthController],
providers: [AuthService, JwtStrategy, LocalStrategy],
exports: [AuthService],
})
export class AuthModule {}
main.ts
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
bufferLogs: true,
});
app.enableShutdownHooks();
app.useLogger(app.get(MyLogger));
const configService = app.get(ConfigService);
const logger = app.get(MyLogger);
const config = new DocumentBuilder()
.setTitle('nestjs-tst-boilerplate')
.setDescription('The nestjs-tst-boilerplate API description')
.setVersion('0.0.1')
.addTag('tag')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
await app.listen(configService.get<number>('app.port'));
logger.log(`Application running on [${configService.get<string>('app.env')}] enviroment. ${await app.getUrl()}`);
}
bootstrap();
at the time that process.env.JWT_SECRET_KEY is read, the .env might not be parsed yet. Thus, don't rely on process.env. if you're using some module like #nestjs/config. Use the async version of JwtModule.register instead and inject the ConfigService. See: https://github.com/nestjs/jwt/blob/master/README.md#async-options
I do not need complete passport, I just need to sign a JWT token.
I tried working with this repository
I tried all possible combinations, but I just cant integrate it in the project. I had followed the course of several different errors. I fix one, and the another pops up. So, I am including minimum part, and the current error that is thrown.
AuthModule:
import { Module } from '#nestjs/common';
import { JwtModule } from '#nestjs/jwt';
import { AuthService } from './auth.service';
import { JwtService } from '#nestjs/jwt';
#Module({
imports: [ JwtModule.register({ secret: 'hard!to-guess_secret' })],
providers: [AuthService],
exports: [AuthService, JwtService]
})
export class AuthModule {}
AuthService:
import { Injectable } from '#nestjs/common';
import { UsersService } from '../users/users.service';
import { JwtService } from '#nestjs/jwt';
#Injectable()
export class AuthService {
constructor(
private readonly jwtService: JwtService,
) {}
async signPayload (user: any) {
const payload = { username: 'HARDCORE', color:'RED' };
return {
whatever: this.jwtService.sign(payload),
};
}
}
AppModule:
#Module({
imports: [
ConfigModule.forRoot(),
JwtModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
secret: 'wefwefwef',
}),
inject: [ConfigService],
}),
TypeOrmModule.forRoot({
type: 'mysql',
...
}),
UsersModule,
SubscriptionsModule,
ProductsModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {
}
SomeController:
export class UsersController {
constructor(private readonly usersService: UsersService,
private readonly authService: AuthService
) {}
...
#ApiResponse({ status: 401, description: 'Unauthorized.' })
#ApiResponse({ status: 404, description: 'User Not Found.' })
#Get('users')
async findOne(#Query() userGetDto: UserGetDto): Promise<User> {
const user = await this.usersService.findByUsername(userGetDto.userName);
if (!user) throw new NotFoundException('User Not Found')
let signedUser = this.authService.signPayload(user);
return user;
And this is the error with this setup that I get:
Nest can't resolve dependencies of the JwtService (?). Please make
sure that the argument JWT_MODULE_OPTIONS at index [0] is available in
the JwtService context.
I spend lot of time on this one, but I just cant make it work.
Based on your error, JwtService is in an imports array somewhere. Providers (classes marked with #Injectable()) never go in the imports array, but rather should be added to the exports array and that module where the exports was added should be put in the consuming module's imports array.
Also, if you are working with a Dynamic Module (any module that uses register or forRoot or an async variant of the two) you should always export the module instead of its services, as the module most likely has important configurations necessary for the service to work.
I import the redis module in app.module.ts file:
#Module({
imports: [
TypegooseModule.forRoot('mongodb://mongo:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}),
RedisModule.register({ url: 'redis://sessions' }),
UsersModule,
AuthModule,
],
controllers: [],
providers: [],
})
export class AppModule {}
I would like to create a helper class SessionManager that would manage user's sessions (store, update, delete them in Redis), e.g something like this:
import { RedisService } from 'nestjs-redis';
import { User } from 'src/users/schemas/user.schema';
import { IUserSessions } from '../interfaces/Session.interface';
class SessionManager {
constructor(private redisService: RedisService) {}
saveUserSession(user: User, userAgent: string) {}
static getSessionsByUserId(userId: string): IUserSessions {
const client = this.redisService.getClient()
const sessions = client.hmgetall(userId)
return sessions
}
}
export default SessionManager
and use it in my auth.service.ts. How do I do that? Does it have to be represented as some Nest.js entity (module or provider)?
Ended up creating a separate sessions module, adding my service as a Provider and exporting it, then using it in my desired module:
import { Module } from '#nestjs/common';
import { SessionsService } from './sessions.service';
#Module({
providers: [SessionsService],
exports: [SessionsService]
})
export class SessionsModule {}
I'm having trouble to mock multiple repositories from different modules in NestJS.
I'm using a UsersRepository from a UsersModule inside another module service (NotesService). The code is working fine, but I can't make the unit tests work.
I have the following error: Error: Nest can't resolve dependencies of the UserRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.
Minimal reproduction
// [users.module.ts]
#Module({
imports: [TypeOrmModule.forFeature([User])],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
// [users.service.ts]
#Injectable()
export class UsersService {
constructor(#InjectRepository(User) private usersRepository: UsersRepository) {}
...
}
// [notes.module.ts]
#Module({
imports: [
TypeOrmModule.forFeature([Note, User]),
UsersModule,
],
controllers: [NotesController],
providers: [NotesService],
})
export class NotesModule {
}
// [notes.service.ts]
#Injectable()
export class NotesService {
constructor(
#InjectRepository(Note) private notesRepository: NotesRepository,
#InjectRepository(User) private usersRepository: UsersRepository,
) {}
...
}
Here is my unit test configuration:
// [notes.service.spec.ts]
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [UsersModule],
providers: [
NotesService,
{ provide: getRepositoryToken(Note), useFactory: repositoryMockFactory },
{ provide: getRepositoryToken(User), useFactory: repositoryMockFactory },
],
}).compile();
notesService = module.get<NotesService>(NotesService);
notesRepositoryMock = module.get(getRepositoryToken(Note));
});
The problem is I can't make a proper mock of the UsersRepository that comes from another module.
I tried importing TypeOrmModule directly inside the test, and everything I could but I can't make it work.
You don't need to import the UsersModule if you are directly providing the NotesService and the mocks that it will depend on. The reason for the error is that Nest is trying to resolve TypeormModule.forFeature([User]) from the UsersModule. Simply remove the import in the test and you should be golden.