diff --git a/app/controllers/construction_sites_controller.ts b/app/controllers/construction_sites_controller.ts index 397e899..c655d34 100644 --- a/app/controllers/construction_sites_controller.ts +++ b/app/controllers/construction_sites_controller.ts @@ -1,6 +1,7 @@ import type { HttpContext } from '@adonisjs/core/http' import ConstructionSite from '../models/construction_site_model.js' import Subscription from '#models/subscription_model' +import Notification from '#models/notification_model' import app from '@adonisjs/core/services/app' import { cuid } from '@adonisjs/core/helpers' @@ -81,6 +82,16 @@ export default class ConstructionSitesController { async update({ params, request, response }: HttpContext) { try { let constructionSite = await ConstructionSite.findByIdAndUpdate(params.id, request.all()) + let subscribers = await Subscription.find({ construction_site_id: params.id }) + // Send a notification to all subscribers + subscribers.forEach(async (sub) => { + let notification = new Notification({ + user_id: sub.user_id, + construction_site_id: params.id, + message: `Il cantiere ${constructionSite?.name} รจ stato aggiornato`, + }) + await notification.save() + }) return response.ok(constructionSite) } catch (error) { return response.notFound() diff --git a/app/controllers/notifications_controller.ts b/app/controllers/notifications_controller.ts index 0d84002..d40fb83 100644 --- a/app/controllers/notifications_controller.ts +++ b/app/controllers/notifications_controller.ts @@ -2,13 +2,23 @@ import type { HttpContext } from '@adonisjs/core/http' import Notification from '../models/notification_model.js' export default class NotificationsController { - public async getNotifications({ params }: HttpContext) { - let notifications = Notification.find({ user_id: params.user_id }) - return notifications + public async getNotifications({ request, response }: HttpContext) { + try { + let user = request.user + let notifications = await Notification.find({ user_id: user?._id, read: false }) + return response.ok(notifications) + } catch (error) { + return response.badRequest(error) + } } - public async markAsRead({ params }: HttpContext) { - let notifications = Notification.updateMany({ user_id: params.user_id }, { read: true }) - return notifications + public async markAsRead({ request, response }: HttpContext) { + try { + let user = request.user + await Notification.updateMany({ user_id: user?._id }, { read: true }) + return response.ok({ message: 'Notifications marked as read' }) + } catch (error) { + return response.badRequest(error) + } } } diff --git a/start/routes.ts b/start/routes.ts index fea29ef..ec1f39e 100644 --- a/start/routes.ts +++ b/start/routes.ts @@ -49,8 +49,8 @@ router // Auth routes router.post('/register', [AuthController, 'register']) router.post('/login', [AuthController, 'login']) - router.post('/refresh', [AuthController, 'refresh']) - router.post('/logout', [AuthController, 'logout']) + //router.post('/refresh', [AuthController, 'refresh']) + router.post('/logout', [AuthController, 'logout']).use(middleware.jwtAuth()) /** * Construction Sites Routes @@ -72,8 +72,8 @@ router router .group(() => { - router.get('/:user_id', [NotificationsController, 'getNotifications']) // List all notifications - router.post('/read', [NotificationsController, 'markAsRead']) // Mark notifications as read + router.get('/', [NotificationsController, 'getNotifications']).use(middleware.jwtAuth()) // List all notifications + router.post('/read', [NotificationsController, 'markAsRead']).use(middleware.jwtAuth()) // Mark notifications as read }) .prefix('/notifications') // Prefix for notifications routes