90 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import Subscription from '#models/subscription'
 | |
| import { EVENTS, NotificationService } from '#services/notification_service'
 | |
| import { subscribeValidator, updateNotificationValidator } from '#validators/notification'
 | |
| import { inject } from '@adonisjs/core'
 | |
| import type { HttpContext } from '@adonisjs/core/http'
 | |
| 
 | |
| @inject()
 | |
| export default class NotificationsController {
 | |
|   constructor(private service: NotificationService) {}
 | |
| 
 | |
|   // GET /notifications
 | |
|   async index({ auth }: HttpContext) {
 | |
|     return Subscription.query().where('userId', auth.user!.id)
 | |
|   }
 | |
| 
 | |
|   // POST /notifications/subscribe
 | |
|   async subscribe({ request, response, auth }: HttpContext) {
 | |
|     const ua = request.headers()['user-agent']
 | |
|     if (!ua) {
 | |
|       return response.badRequest({
 | |
|         message: 'User-Agent header is required',
 | |
|       })
 | |
|     }
 | |
|     const device = this.service.getUserSignature(ua)
 | |
| 
 | |
|     const data = await request.validateUsing(subscribeValidator)
 | |
|     return Subscription.create({
 | |
|       userId: auth.user!.id,
 | |
|       device,
 | |
|       events: 0, // Default to no events
 | |
|       data,
 | |
|       failedAttempts: 0,
 | |
|       enabled: true,
 | |
|     })
 | |
|   }
 | |
| 
 | |
|   // POST /notifications/:id/unsubscribe
 | |
|   async unsubscribe({ request, auth }: HttpContext) {
 | |
|     const subscriptionId = request.param('id')
 | |
|     const subscription = await Subscription.query()
 | |
|       .where('userId', auth.user!.id)
 | |
|       .where('id', subscriptionId)
 | |
|       .firstOrFail()
 | |
| 
 | |
|     await subscription.delete()
 | |
|     return { success: true }
 | |
|   }
 | |
| 
 | |
|   // POST /notifications/:id/
 | |
|   async update({ request, response, auth }: HttpContext) {
 | |
|     const subscriptionId = request.param('id')
 | |
|     const subscription = await Subscription.query()
 | |
|       .where('userId', auth.user!.id)
 | |
|       .where('id', subscriptionId)
 | |
|       .firstOrFail()
 | |
| 
 | |
|     const { events } = await request.validateUsing(updateNotificationValidator)
 | |
|     // Validate events
 | |
|     if (!events.every((key) => key in EVENTS)) {
 | |
|       return response.badRequest({
 | |
|         message: 'Invalid events provided',
 | |
|       })
 | |
|     }
 | |
| 
 | |
|     // Update subscription events
 | |
|     const validEvents = events.map((key) => EVENTS[key as keyof typeof EVENTS])
 | |
|     subscription.events = this.service.setEvents(validEvents)
 | |
|     await subscription.save()
 | |
| 
 | |
|     return subscription
 | |
|   }
 | |
| 
 | |
|   // POST /notifications/:id/test
 | |
|   async test({ request, response, auth }: HttpContext) {
 | |
|     const subscriptionId = request.param('id')
 | |
|     const subscription = await Subscription.query()
 | |
|       .where('userId', auth.user!.id)
 | |
|       .where('id', subscriptionId)
 | |
|       .firstOrFail()
 | |
| 
 | |
|     const result = await this.service.sendTestNotification(subscription.data)
 | |
|     if (result) {
 | |
|       return { success: true }
 | |
|     }
 | |
| 
 | |
|     return response.internalServerError({
 | |
|       message: 'Failed to send test notification',
 | |
|     })
 | |
|   }
 | |
| }
 | 
