2
I have a subscriber for NestJS to listen to any create, update or delete events (TypeORM). When one of these events is fired, I'd like to use an injected service in order to create a new revision entry.
However, it seems I cannot get the dependency . I getting error Nest can't resolve dependencies of the JobInterviewerSubscriber (?). Please make sure that the argument Connection at index [0] is available in the JobPostingModule context
Keys Files: Jobs.entity.ts JobPostingModule.ts
Jobs.entity.ts
import { BaseEntity, PrimaryGeneratedColumn, Column, Entity, CreateDateColumn, UpdateDateColumn, OneToMany, ManyToOne, ManyToMany, JoinTable, EventSubscriber, EntitySubscriberInterface, InsertEvent, Repository, UpdateEvent, Connection } from 'typeorm';
import { Panelist } from './panelist.entity';
import { JobStatus, JobLocation, JobType, JobPriority } from '../job-posting/enum/job.enum';
import { Agency } from './agency.entity';
import { Candidates } from '../entities/candidates.entity';
import { JobInterviewers } from './job-interviewers.entity';
import { User } from '../entities/user.entity';
import { Injectable } from '@nestjs/common';
import { InjectConnection } from '@nestjs/typeorm/dist/common/typeorm.decorators';
@Entity()
export class Jobs extends BaseEntity{
@PrimaryGeneratedColumn()
id: number;
@Column({type: 'varchar', length: 100})
title: string;
@Column({type: 'enum', enum: JobPriority})
priority: JobPriority;
@Column({type: 'int2', default: 0})
roundCount: number;
@OneToMany(type => JobInterviewers, jobInterviewers => jobInterviewers.jobs, {eager:true})
jobInterviewers: JobInterviewers[];
@ManyToMany(type => Agency, agency => agency.jobs)
@JoinTable({name: 'job_agency_table'})
agencies: Agency[];
@UpdateDateColumn()
updatedAt: Date;
}
@EventSubscriber()
@Injectable()
export class JobInterviewerSubscriber
implements EntitySubscriberInterface<JobInterviewers> {
constructor(
@InjectConnection() readonly connection: Connection,
) {
connection.subscribers.push(this);
}
listenTo() {
return JobInterviewers;
}
async afterInsert(event: InsertEvent<JobInterviewers>) {
const jobsRepo: Repository<Jobs> = event.connection.manager.getRepository<Jobs>('Jobs');
const jobInterviewersRepo: Repository<JobInterviewers> = event.connection.manager.getRepository<JobInterviewers>('JobInterviewers');
jobInterviewersRepo
.count({
where: { jobs: { id: event.entity.jobs.id } },
})
.then((count: number) => {
jobsRepo.update({ id: event.entity.jobs.id }, { roundCount: count });
});
}
}
JobPostingModule.ts
import { Module } from '@nestjs/common';
import { JobPostingController } from './job-posting.controller';
import { JobPostingService } from './job-posting.service';
import { AuthModule } from '../auth/auth.module';
import { RecruitmentService } from '../recruitment/recruitment.service';
import { JobInterviewerSubscriber } from 'src/entities/job-posting.entity';
@Module({
imports: [
AuthModule
],
controllers: [JobPostingController],
providers: [JobPostingService, RecruitmentService,JobInterviewerSubscriber]
})
export class JobPostingModule {}