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
48 changes: 48 additions & 0 deletions backend/src/harouns-ux/admin-stats.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// [BE-52] Add admin dashboard statistics endpoint
import { Controller, Get, UseGuards } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { Document, DocumentStatus } from '../documents/entities/document.entity';
import { User, UserRole } from '../users/entities/user.entity';

@Controller('admin/stats')
@UseGuards(JwtAuthGuard)
export class AdminStatsController {
constructor(
@InjectRepository(Document)
private readonly documentRepo: Repository<Document>,
@InjectRepository(User)
private readonly userRepo: Repository<User>,
) {}

@Get()
async getDashboardStats() {
const [
totalUsers,
totalDocuments,
pendingDocuments,
verifiedDocuments,
flaggedDocuments,
rejectedDocuments,
] = await Promise.all([
this.userRepo.count({ where: { role: UserRole.USER } }),
this.documentRepo.count(),
this.documentRepo.count({ where: { status: DocumentStatus.PENDING } }),
this.documentRepo.count({ where: { status: DocumentStatus.VERIFIED } }),
this.documentRepo.count({ where: { status: DocumentStatus.FLAGGED } }),
this.documentRepo.count({ where: { status: DocumentStatus.REJECTED } }),
]);

return {
users: { total: totalUsers },
documents: {
total: totalDocuments,
pending: pendingDocuments,
verified: verifiedDocuments,
flagged: flaggedDocuments,
rejected: rejectedDocuments,
},
};
}
}
44 changes: 44 additions & 0 deletions backend/src/harouns-ux/document-stats.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// [BE-49] Add document statistics endpoint
import { Controller, Get, Param, UseGuards } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { Document, DocumentStatus } from '../documents/entities/document.entity';

@Controller('documents/stats')
@UseGuards(JwtAuthGuard)
export class DocumentStatsController {
constructor(
@InjectRepository(Document)
private readonly documentRepo: Repository<Document>,
) {}

@Get()
async getOverallStats() {
const total = await this.documentRepo.count();
const byStatus = await this.documentRepo
.createQueryBuilder('doc')
.select('doc.status', 'status')
.addSelect('COUNT(*)', 'count')
.groupBy('doc.status')
.getRawMany();

return { total, byStatus };
}

@Get('owner/:ownerId')
async getStatsByOwner(@Param('ownerId') ownerId: string) {
const total = await this.documentRepo.count({ where: { ownerId } });
const verified = await this.documentRepo.count({
where: { ownerId, status: DocumentStatus.VERIFIED },
});
const flagged = await this.documentRepo.count({
where: { ownerId, status: DocumentStatus.FLAGGED },
});
const pending = await this.documentRepo.count({
where: { ownerId, status: DocumentStatus.PENDING },
});

return { ownerId, total, verified, flagged, pending };
}
}
52 changes: 52 additions & 0 deletions backend/src/harouns-ux/notification.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// [BE-51] Add in-app notification entity and endpoints
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
ManyToOne,
JoinColumn,
Index,
} from 'typeorm';
import { User } from '../users/entities/user.entity';

export enum NotificationType {
DOCUMENT_VERIFIED = 'document_verified',
DOCUMENT_FLAGGED = 'document_flagged',
DOCUMENT_REJECTED = 'document_rejected',
PROCESSING_COMPLETE = 'processing_complete',
SYSTEM = 'system',
}

@Entity('notifications')
@Index('IDX_NOTIFICATION_USER_ID', ['userId'])
@Index('IDX_NOTIFICATION_IS_READ', ['isRead'])
export class Notification {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ name: 'user_id' })
userId: string;

@ManyToOne(() => User, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'user_id' })
user: User;

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

@Column()
title: string;

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

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

@Column({ name: 'document_id', nullable: true })
documentId?: string;

@CreateDateColumn({ name: 'created_at' })
createdAt: Date;
}
63 changes: 63 additions & 0 deletions backend/src/harouns-ux/processing.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// [BE-50] Add WebSocket gateway for real-time document processing updates
import {
WebSocketGateway,
WebSocketServer,
SubscribeMessage,
MessageBody,
ConnectedSocket,
OnGatewayConnection,
OnGatewayDisconnect,
} from '@nestjs/websockets';
import { Server, Socket } from 'socket.io';
import { UseGuards } from '@nestjs/common';

export interface ProcessingUpdate {
documentId: string;
status: string;
progress?: number;
message?: string;
}

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

handleConnection(client: Socket) {
console.log(`Client connected: ${client.id}`);
}

handleDisconnect(client: Socket) {
console.log(`Client disconnected: ${client.id}`);
}

@SubscribeMessage('subscribe')
handleSubscribe(
@MessageBody() documentId: string,
@ConnectedSocket() client: Socket,
) {
client.join(`document:${documentId}`);
return { event: 'subscribed', documentId };
}

@SubscribeMessage('unsubscribe')
handleUnsubscribe(
@MessageBody() documentId: string,
@ConnectedSocket() client: Socket,
) {
client.leave(`document:${documentId}`);
return { event: 'unsubscribed', documentId };
}

emitProcessingUpdate(update: ProcessingUpdate) {
this.server
.to(`document:${update.documentId}`)
.emit('processing:update', update);
}

emitProcessingComplete(documentId: string, status: string) {
this.server
.to(`document:${documentId}`)
.emit('processing:complete', { documentId, status });
}
}