Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 7 additions & 43 deletions src/notifications/entities/notification.entity.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
ManyToOne,
JoinColumn,
Index,
} from 'typeorm';
import { User } from 'src/auth/entities/user.entity';
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index } from 'typeorm';

export enum NotificationType {
JOB_APPLICATION = 'job_application',
MESSAGE = 'message',
JOB_MATCH = 'job_match',
PAYMENT = 'payment',
SYSTEM = 'system',
GENERAL = 'general',
JOB_STATUS_UPDATE = 'JOB_STATUS_UPDATE',
ALERT = 'alert',
INFO = 'info',
}

@Entity('notifications')
Expand All @@ -26,41 +13,18 @@ export class Notification {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ type: 'varchar', length: 255 })
title: string;
@Column({ type: 'uuid' })
userId: string;

@Column({ type: 'text' })
message: string;

@Column({
type: 'enum',
enum: NotificationType,
default: NotificationType.GENERAL,
})
@Column({ type: 'enum', enum: NotificationType, default: NotificationType.GENERAL })
type: NotificationType;

@Column({ type: 'boolean', default: false })
isRead: boolean;

@Column({ type: 'uuid' })
userId: string;

@ManyToOne(() => User, (user) => user.notifications, {
onDelete: 'CASCADE',
eager: false,
})
@JoinColumn({ name: 'userId' })
user: User;

@Column({ type: 'uuid', nullable: true })
relatedEntityId: string;

@Column({ type: 'varchar', length: 100, nullable: true })
relatedEntityType: string;

@Column({ type: 'json', nullable: true })
metadata: Record<string, any>;

@CreateDateColumn()
createdAt: Date;

Expand Down
13 changes: 2 additions & 11 deletions src/notifications/notifications.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import { Module, forwardRef } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { NotificationsController } from './notifications.controller';
import { NotificationsService } from './notifications.service';
import { Notification } from './entities/notification.entity';
import { AuthModule } from '../auth/auth.module';
import { FeedModule } from 'src/feed/feed.module';
import { ApplicationsModule } from 'src/applications/applications.module';

@Module({
imports: [
ApplicationsModule,
TypeOrmModule.forFeature([Notification]),
forwardRef(() => AuthModule),
],
controllers: [NotificationsController],
imports: [TypeOrmModule.forFeature([Notification])],
providers: [NotificationsService],
exports: [NotificationsService],
})
Expand Down
13 changes: 13 additions & 0 deletions src/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,17 @@ export class NotificationsService {
where: { userId, isRead: false },
});
}

async createNotification(userId: string, message: string, type: NotificationType = NotificationType.GENERAL): Promise<Notification> {
const notification = this.notificationRepository.create({ userId, message, type });
return this.notificationRepository.save(notification);
}

async getUserNotifications(userId: string, limit = 20): Promise<Notification[]> {
return this.notificationRepository.find({
where: { userId },
order: { createdAt: 'DESC' },
take: limit,
});
}
}