Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 8 additions & 2 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { DepartmentsModule } from './departments/departments.module';
import { Department } from './departments/department.entity';
import { UsersModule } from './users/users.module';
import { User } from './users/entities/user.entity';
import { NotificationsService } from './notifications/notifications.service';
import { NotificationsController } from './notifications/notifications.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AssetTransfersModule } from './asset-transfers/asset-transfers.module';
import { NotificationsModule } from './notifications/notifications.module';

@Module({
imports: [
Expand All @@ -34,8 +39,9 @@ import { User } from './users/entities/user.entity';
DepartmentsModule,
AssetTransfersModule,
UsersModule,
NotificationsModule,
],
controllers: [AppController],
providers: [AppService],
controllers: [AppController, NotificationsController],
providers: [AppService, NotificationsService],
})
export class AppModule {}
19 changes: 19 additions & 0 deletions backend/src/notifications/dto/create-notification.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IsString, IsNotEmpty, IsOptional, IsObject } from 'class-validator';

export class CreateNotificationDto {
@IsString()
@IsNotEmpty()
userId: string;

@IsString()
@IsNotEmpty()
message: string;

@IsString()
@IsOptional()
type?: string;

@IsObject()
@IsOptional()
metadata?: Record<string, any>;
}
17 changes: 17 additions & 0 deletions backend/src/notifications/dto/query-notification.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IsOptional, IsString, IsBoolean } from 'class-validator';
import { Transform } from 'class-transformer';

export class QueryNotificationDto {
@IsOptional()
@IsString()
userId?: string;

@IsOptional()
@IsBoolean()
@Transform(({ value }) => value === 'true' || value === true)
isRead?: boolean;

@IsOptional()
@IsString()
type?: string;
}
7 changes: 7 additions & 0 deletions backend/src/notifications/dto/update-notification.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsBoolean, IsOptional } from 'class-validator';

export class UpdateNotificationDto {
@IsBoolean()
@IsOptional()
isRead?: boolean;
}
34 changes: 34 additions & 0 deletions backend/src/notifications/entities/notification.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm';

@Entity('notifications')
export class Notification {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column()
userId: string;

@Column('text')
message: string;

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

@Column({ nullable: true })
type: string; // 'asset_transfer', 'low_stock', 'maintenance_due'

@Column('json', { nullable: true })
metadata: Record<string, any>; // Additional data for the notification

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;
}
115 changes: 115 additions & 0 deletions backend/src/notifications/notifications.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import {
Controller,
Get,
Post,
Patch,
Delete,
HttpCode,
HttpStatus,
} from '@nestjs/common';
import type { NotificationsService } from './notifications.service';
import type { CreateNotificationDto } from './dto/create-notification.dto';
import type { UpdateNotificationDto } from './dto/update-notification.dto';
import type { QueryNotificationDto } from './dto/query-notification.dto';

@Controller('notifications')
export class NotificationsController {
constructor(private readonly notificationsService: NotificationsService) {}

@Post()
@HttpCode(HttpStatus.CREATED)
create(createNotificationDto: CreateNotificationDto) {
return this.notificationsService.create(createNotificationDto);
}

@Get()
findAll(query: QueryNotificationDto) {
return this.notificationsService.findAll(query);
}

@Get('user/:userId')
findByUserId(userId: string) {
return this.notificationsService.findByUserId(userId);
}

@Get('user/:userId/unread-count')
getUnreadCount(userId: string) {
return this.notificationsService.getUnreadCount(userId);
}

@Get(':id')
findOne(id: string) {
return this.notificationsService.findOne(id);
}

@Patch(':id')
update(id: string, updateNotificationDto: UpdateNotificationDto) {
return this.notificationsService.update(id, updateNotificationDto);
}

@Patch(':id/mark-read')
@HttpCode(HttpStatus.OK)
markAsRead(id: string) {
return this.notificationsService.markAsRead(id);
}

@Patch('user/:userId/mark-all-read')
@HttpCode(HttpStatus.OK)
async markAllAsRead(userId: string) {
await this.notificationsService.markAllAsRead(userId);
return { message: 'All notifications marked as read' };
}

@Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT)
remove(id: string) {
return this.notificationsService.remove(id);
}

// Specific notification creation endpoints
@Post('asset-transfer')
@HttpCode(HttpStatus.CREATED)
createAssetTransfer(body: {
userId: string;
assetName: string;
from: string;
to: string;
}) {
return this.notificationsService.createAssetTransferNotification(
body.userId,
body.assetName,
body.from,
body.to,
);
}

@Post('low-stock')
@HttpCode(HttpStatus.CREATED)
createLowStock(body: {
userId: string;
itemName: string;
currentStock: number;
threshold: number;
}) {
return this.notificationsService.createLowStockNotification(
body.userId,
body.itemName,
body.currentStock,
body.threshold,
);
}

@Post('maintenance-due')
@HttpCode(HttpStatus.CREATED)
createMaintenanceDue(body: {
userId: string;
assetName: string;
dueDate: string;
}) {
return this.notificationsService.createMaintenanceDueNotification(
body.userId,
body.assetName,
new Date(body.dueDate),
);
}
}
13 changes: 13 additions & 0 deletions backend/src/notifications/notifications.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { NotificationsService } from './notifications.service';
import { NotificationsController } from './notifications.controller';
import { Notification } from './entities/notification.entity';

@Module({
imports: [TypeOrmModule.forFeature([Notification])],
controllers: [NotificationsController],
providers: [NotificationsService],
exports: [NotificationsService], // Export for use in other modules
})
export class NotificationsModule {}
139 changes: 139 additions & 0 deletions backend/src/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import type { Repository } from 'typeorm';
import type { Notification } from './entities/notification.entity';
import type { CreateNotificationDto } from './dto/create-notification.dto';
import type { UpdateNotificationDto } from './dto/update-notification.dto';
import type { QueryNotificationDto } from './dto/query-notification.dto';

@Injectable()
export class NotificationsService {
private notificationsRepository: Repository<Notification>;

constructor(notificationsRepository: Repository<Notification>) {
this.notificationsRepository = notificationsRepository;
}

async create(
createNotificationDto: CreateNotificationDto,
): Promise<Notification> {
const notification = this.notificationsRepository.create(
createNotificationDto,
);
return await this.notificationsRepository.save(notification);
}

async findAll(query: QueryNotificationDto): Promise<Notification[]> {
const where: any = {};

if (query.userId) {
where.userId = query.userId;
}

if (query.isRead !== undefined) {
where.isRead = query.isRead;
}

if (query.type) {
where.type = query.type;
}

return await this.notificationsRepository.find({
where,
order: { createdAt: 'DESC' },
});
}

async findOne(id: string): Promise<Notification> {
const notification = await this.notificationsRepository.findOne({
where: { id },
});

if (!notification) {
throw new NotFoundException(`Notification with ID ${id} not found`);
}

return notification;
}

async findByUserId(userId: string): Promise<Notification[]> {
return await this.notificationsRepository.find({
where: { userId },
order: { createdAt: 'DESC' },
});
}

async getUnreadCount(userId: string): Promise<number> {
return await this.notificationsRepository.count({
where: { userId, isRead: false },
});
}

async markAsRead(id: string): Promise<Notification> {
const notification = await this.findOne(id);
notification.isRead = true;
return await this.notificationsRepository.save(notification);
}

async markAllAsRead(userId: string): Promise<void> {
await this.notificationsRepository.update(
{ userId, isRead: false },
{ isRead: true },
);
}

async update(
id: string,
updateNotificationDto: UpdateNotificationDto,
): Promise<Notification> {
const notification = await this.findOne(id);
Object.assign(notification, updateNotificationDto);
return await this.notificationsRepository.save(notification);
}

async remove(id: string): Promise<void> {
const notification = await this.findOne(id);
await this.notificationsRepository.remove(notification);
}

// Helper methods for creating specific notification types
async createAssetTransferNotification(
userId: string,
assetName: string,
from: string,
to: string,
): Promise<Notification> {
return this.create({
userId,
message: `Asset "${assetName}" has been transferred from ${from} to ${to}`,
type: 'asset_transfer',
metadata: { assetName, from, to },
});
}

async createLowStockNotification(
userId: string,
itemName: string,
currentStock: number,
threshold: number,
): Promise<Notification> {
return this.create({
userId,
message: `Low stock alert: "${itemName}" has only ${currentStock} units remaining (threshold: ${threshold})`,
type: 'low_stock',
metadata: { itemName, currentStock, threshold },
});
}

async createMaintenanceDueNotification(
userId: string,
assetName: string,
dueDate: Date,
): Promise<Notification> {
return this.create({
userId,
message: `Maintenance due for "${assetName}" on ${dueDate.toLocaleDateString()}`,
type: 'maintenance_due',
metadata: { assetName, dueDate },
});
}
}