Skip to content

Commit

Permalink
events + api events scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 15, 2024
1 parent 9258783 commit f810ca0
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.0.0",
"@nestjs/cqrs": "^10.2.7",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
Expand Down
2 changes: 0 additions & 2 deletions api/src/modules/auth/services/password-recovery.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export class PasswordRecoveryService {
async recoverPassword(email: string, origin: string): Promise<void> {
const user = await this.users.findByEmail(email);
if (!user) {
// TODO: We don't want to expose this info back, but we probably want to log and save this event internally, plus
// maybe sent an email to admin
this.logger.warn(
`Email ${email} not found when trying to recover password`,
);
Expand Down
28 changes: 28 additions & 0 deletions api/src/modules/events/api-events/api-events.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
} from 'typeorm';
import { API_EVENT_TYPES } from '@api/modules/events/events.enum';

@Entity('api_events')
export class ApiEventsEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({
type: 'enum',
enum: API_EVENT_TYPES,
})
eventType: API_EVENT_TYPES;

@Column({ type: 'uuid', nullable: true, name: 'resource_id' })
resourceId: string | null;

@Column('jsonb')
payload: any;

@CreateDateColumn({ name: 'created_at' })
createdAt: Date;
}
11 changes: 11 additions & 0 deletions api/src/modules/events/api-events/api-events.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ApiEventsEntity } from '@api/modules/events/api-events/api-events.entity';
import { ApiEventsService } from '@api/modules/events/api-events/api-events.service';

@Module({
imports: [TypeOrmModule.forFeature([ApiEventsEntity])],
providers: [ApiEventsService],
exports: [ApiEventsService],
})
export class ApiEventsModule {}
24 changes: 24 additions & 0 deletions api/src/modules/events/api-events/api-events.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// src/api-events/api-events.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ApiEventsEntity } from './api-events.entity';
import { API_EVENT_TYPES } from '@api/modules/events/events.enum';

type CreateApiEvent = {
eventType: API_EVENT_TYPES;
resourceId?: string;
payload?: any;
};

@Injectable()
export class ApiEventsService {
constructor(
@InjectRepository(ApiEventsEntity)
private readonly apiEventsRepository: Repository<ApiEventsEntity>,
) {}

async create(createEventDto: CreateApiEvent): Promise<void> {
await this.apiEventsRepository.insert(createEventDto);
}
}
8 changes: 8 additions & 0 deletions api/src/modules/events/events.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum API_EVENT_TYPES {
USER_SIGNED_UP = 'user.signed_up',
USER_PASSWORD_RECOVERY_REQUESTED = 'user.password_recovery_requested',
USER_PASSWORD_RECOVERY_REQUESTED_NON_EXISTENT = 'user.password_recovery_requested_non_existent',
// More events to come....
}

// More enums non related to events that we want to register in DB
7 changes: 7 additions & 0 deletions api/src/modules/events/events.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { ApiEventsModule } from './api-events/api-events.module';

@Module({
imports: [ApiEventsModule],
})
export class EventsModule {}
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f810ca0

Please sign in to comment.