Skip to content
Merged
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
4 changes: 4 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { ContractsModule } from './contracts/contracts.module';
import { LicensesModule } from './licenses/licenses.module';
import { PurchaseOrdersModule } from './purchase-orders/purchase-orders.module';
import { TasksModule } from './tasks/tasks.module';
import { ReportsModule } from './reports/reports.module';
import { NotificationsModule } from './notifications/notifications.module';
import { ApiKeysModule } from './api-keys/api-keys.module';
import { StellarModule } from './stellar/stellar.module';
import { NotificationModule } from './notifications/notification.module';
Expand Down Expand Up @@ -110,6 +112,8 @@ import { NotificationModule } from './notifications/notification.module';
InventoryModule,
VendorsModule,
DashboardModule,
ReportsModule,
NotificationsModule,
ApiKeysModule,
StellarModule,
NotificationModule,
Expand Down
27 changes: 27 additions & 0 deletions backend/src/notifications/notification.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
} from 'typeorm';

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

@Column()
userId: string;

@Column()
type: string;

@Column('text')
message: string;

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

@CreateDateColumn()
createdAt: Date;
}
24 changes: 24 additions & 0 deletions backend/src/notifications/notifications.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controller, Get, Patch, Param, Req, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { NotificationsService } from './notifications.service';

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

@Get()
findAll(@Req() req: any) {
return this.notificationsService.findByUser(req.user?.id);
}

@Patch(':id/read')
markRead(@Param('id') id: string, @Req() req: any) {
return this.notificationsService.markRead(id, req.user?.id);
}

@Patch('read-all')
markAllRead(@Req() req: any) {
return this.notificationsService.markAllRead(req.user?.id);
}
}
44 changes: 44 additions & 0 deletions backend/src/notifications/notifications.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
WebSocketGateway,
WebSocketServer,
SubscribeMessage,
OnGatewayConnection,
OnGatewayDisconnect,
} from '@nestjs/websockets';
import { Server, Socket } from 'socket.io';
import { JwtService } from '@nestjs/jwt';
import { Injectable } from '@nestjs/common';

@Injectable()
@WebSocketGateway({ namespace: '/notifications', cors: { origin: '*' } })
export class NotificationsGateway
implements OnGatewayConnection, OnGatewayDisconnect
{
@WebSocketServer()
server: Server;

constructor(private readonly jwtService: JwtService) {}

handleConnection(client: Socket) {
try {
const token = (client.handshake.auth?.token ||
client.handshake.query?.token) as string;
const payload = this.jwtService.verify(token);
client.data.userId = payload.sub;
void client.join(`user:${String(payload.sub)}`);
} catch {
client.disconnect();
}
}

handleDisconnect(_client: Socket) {}

sendToUser(userId: string, event: string, data: unknown) {
this.server.to(`user:${userId}`).emit(event, data);
}

@SubscribeMessage('ping')
handlePing() {
return { event: 'pong', data: {} };
}
}
27 changes: 27 additions & 0 deletions backend/src/notifications/notifications.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { JwtModule } from '@nestjs/jwt';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Notification } from './entities/notification.entity';
import { NotificationsGateway } from './notifications.gateway';
import { NotificationsService } from './notifications.service';
import { NotificationsController } from './notifications.controller';

@Module({
imports: [
TypeOrmModule.forFeature([Notification]),
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
secret: config.get('JWT_SECRET', 'change-me-in-env'),
signOptions: { expiresIn: '15m' },
}),
}),
ConfigModule,
],
controllers: [NotificationsController],
providers: [NotificationsGateway, NotificationsService],
exports: [NotificationsService, NotificationsGateway],
})
export class NotificationsModule {}
32 changes: 32 additions & 0 deletions backend/src/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,36 @@ export class NotificationsService {
where: { userId, isRead: false },
});
}

async sendToUser(
userId: string,
type: string,
message: string,
): Promise<Notification> {
return this.create({
userId,
event: type as any,
title: type,
message,
emailTemplate: '',
emailSubject: '',
emailContext: {},
} as any);
}

async findByUser(userId: string): Promise<Notification[]> {
return this.findByUserId(userId);
}

async markRead(id: string, userId: string): Promise<Notification | null> {
const notification = await this.notificationRepository.findOne({
where: { id, userId },
});
if (!notification) return null;
return this.markAsRead(id);
}

async markAllRead(userId: string): Promise<void> {
return this.markAllAsRead(userId);
}
}
40 changes: 40 additions & 0 deletions backend/src/reports/report-schedule.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm';

@Entity('report_schedules')
export class ReportSchedule {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column()
userId: string;

@Column()
reportType: string;

@Column()
frequency: string;

@Column()
email: string;

@Column({ default: true })
isActive: boolean;

@Column({ nullable: true, type: 'timestamp' })
lastRunAt: Date;

@Column({ nullable: true, type: 'timestamp' })
nextRunAt: Date;

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;
}
69 changes: 69 additions & 0 deletions backend/src/reports/reports.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
Controller,
Get,
Post,
Delete,
Query,
Body,
Param,
Req,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ReportsService } from './reports.service';

@Controller('reports')
@UseGuards(AuthGuard('jwt'))
export class ReportsController {
constructor(private readonly reportsService: ReportsService) {}

@Get('summary')
getSummary() {
return this.reportsService.getSummary();
}

@Get('warranty-expiring')
getWarrantyExpiring(@Query('days') days?: string) {
return this.reportsService.getWarrantyExpiring(
days ? parseInt(days, 10) : 30,
);
}

@Get('maintenance-costs')
getMaintenanceCosts() {
return this.reportsService.getMaintenanceCosts();
}

@Get('depreciation')
getDepreciation() {
return this.reportsService.getDepreciation();
}

@Get('asset-utilisation')
getAssetUtilisation() {
return this.reportsService.getAssetUtilisation();
}

@Get('schedules')
getSchedules(@Req() req: any) {
return this.reportsService.getSchedules(req.user?.id);
}

@Post('schedules')
createSchedule(
@Req() req: any,
@Body() body: { reportType: string; frequency: string; email: string },
) {
return this.reportsService.createSchedule(
req.user?.id,
body.reportType,
body.frequency,
body.email,
);
}

@Delete('schedules/:id')
deleteSchedule(@Param('id') id: string, @Req() req: any) {
return this.reportsService.deleteSchedule(id, req.user?.id);
}
}
14 changes: 14 additions & 0 deletions backend/src/reports/reports.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Asset } from '../assets/asset.entity';
import { ReportSchedule } from './report-schedule.entity';
import { ReportsService } from './reports.service';
import { ReportsController } from './reports.controller';

@Module({
imports: [TypeOrmModule.forFeature([Asset, ReportSchedule])],
controllers: [ReportsController],
providers: [ReportsService],
exports: [ReportsService],
})
export class ReportsModule {}
Loading
Loading