diff --git a/app/controllers/colles_controller.ts b/app/controllers/colles_controller.ts index 5095459..d5bb6fb 100644 --- a/app/controllers/colles_controller.ts +++ b/app/controllers/colles_controller.ts @@ -1,5 +1,6 @@ import Colle from '#models/colle' import { ColleService } from '#services/colle_service' +import { NotificationService } from '#services/notification_service' import { createColleValidator, createUpcomingCollesValidator } from '#validators/colle' import { inject } from '@adonisjs/core' import type { HttpContext } from '@adonisjs/core/http' @@ -8,7 +9,10 @@ import { DateTime } from 'luxon' @inject() export default class CollesController { - constructor(private service: ColleService) {} + constructor( + private service: ColleService, + private notificationService: NotificationService + ) {} async index({ request, response, auth }: HttpContext) { const { startDate: rawStartDate } = request.qs() @@ -137,6 +141,7 @@ export default class CollesController { // If it exists, update the existing colle if (existing) { // Merge the new data with the existing colle + const beforeColle = existing.serialize() Object.assign(existing, colleData) // Handle attachments if any @@ -161,6 +166,19 @@ export default class CollesController { } } + const afterColle = existing.serialize() + if (!beforeColle.grade && afterColle.grade) { + await this.notificationService.sendNotification('GRADE_ADDED', existing) + } else if (parseFloat(beforeColle.grade) !== afterColle.grade) { + await this.notificationService.sendNotification( + 'GRADE_UPDATED', + existing, + parseFloat(beforeColle.grade) + ) + } + if (!deepEqual(beforeColle, afterColle)) { + await this.notificationService.sendNotification('COLLE_UPDATED', existing) + } return existing.save() } const colle = await Colle.create(colleData) @@ -226,25 +244,49 @@ export default class CollesController { // Create a new colle if it doesn't exist if (!oldColle) { - await Colle.create(updatedColle) + const colle = await Colle.create(updatedColle) + await this.notificationService.sendNotification('COLLE_ADDED', colle) continue } // Update the colle with the new data // and remove it from the list + const beforeColle = oldColle.serialize() Object.assign(oldColle, updatedColle) await oldColle.save() + + const afterColle = oldColle.serialize() upcomingCollesIds.delete(oldColle.id) + + if (beforeColle.room !== afterColle.room) { + await this.notificationService.sendNotification('ROOM_UPDATED', oldColle) + } else if (!deepEqual(beforeColle, afterColle)) { + await this.notificationService.sendNotification('COLLE_UPDATED', oldColle) + } } // Delete the colles that were not updated - const deleted = await Colle.query() - .whereHas('student', (query) => { - query.where('className', payload.className) + const collesToDelete = await Promise.all( + Array.from(upcomingCollesIds).map((id) => { + return Colle.query() + .where('id', id) + .whereHas('student', (query) => { + query.where('className', payload.className) + }) + .first() }) - .whereIn('id', Array.from(upcomingCollesIds)) - .delete() + ) + for (const colle of collesToDelete) { + if (colle) { + await this.notificationService.sendNotification('COLLE_REMOVED', colle) + await colle.delete() + } + } - console.log(`Deleted ${deleted} upcoming colles that were not updated`) + console.log(`Deleted ${collesToDelete.length} upcoming colles that were not updated`) } } + +function deepEqual(a: any, b: any) { + return JSON.stringify(a) === JSON.stringify(b) +} diff --git a/app/services/notification_service.ts b/app/services/notification_service.ts index 25bfb9a..6b7343a 100644 --- a/app/services/notification_service.ts +++ b/app/services/notification_service.ts @@ -45,8 +45,12 @@ export class NotificationService { }) } - public async sendNotification(notificationId: NotificationId, colle: Colle) { - const payload = Object.assign(DEFAULT_NOTIFICATION, NOTIFICATIONS[notificationId](colle)) + public async sendNotification(notificationId: NotificationId, colle: Colle, args?: any) { + await colle.load('subject') + if (notificationId === 'ROOM_UPDATED') { + await colle.load('room') + } + const payload = Object.assign(DEFAULT_NOTIFICATION, NOTIFICATIONS[notificationId](colle, args)) const subscriptions = await Subscription.query() .where('enabled', true) @@ -92,7 +96,7 @@ export class NotificationService { const NOTIFICATIONS = { COLLE_ADDED: (colle: Colle) => ({ title: 'Nouvelle colle', - body: `Colle de ${colle.subject} ajoutée le ${formatDate(colle.date)}.`, + body: `Colle de ${colle.subject.name} ajoutée le ${formatDate(colle.date)}.`, data: { id: colle.id, }, @@ -100,12 +104,12 @@ const NOTIFICATIONS = { }), COLLE_REMOVED: (colle: Colle) => ({ title: 'Colle supprimée', - body: `Votre colle de ${colle.subject}, le ${formatDate(colle.date)} a été supprimée.`, + body: `Votre colle de ${colle.subject.name}, le ${formatDate(colle.date)} a été supprimée.`, actions: [HOME_ACTION], }), COLLE_UPDATED: (colle: Colle) => ({ title: 'Colle modifiée', - body: `Votre colle de ${colle.subject} du ${formatDate(colle.date)} a été modifiée.`, + body: `Votre colle de ${colle.subject.name} du ${formatDate(colle.date)} a été modifiée.`, data: { id: colle.id, }, @@ -114,15 +118,15 @@ const NOTIFICATIONS = { GRADE_ADDED: (colle: Colle) => ({ title: 'Nouvelle note', - body: `Colle de ${colle.subject} : ${colle.grade}/20`, + body: `Colle de ${colle.subject.name} : ${colle.grade}/20`, data: { id: colle.id, }, actions: [OPEN_ACION, HOME_ACTION], }), - GRADE_UPDATED: (colle: Colle) => ({ + GRADE_UPDATED: (colle: Colle, oldGrade: number) => ({ title: 'Note modifiée', - body: `Colle de ${colle.subject} : ${colle.grade}/20`, + body: `Colle de ${colle.subject.name} : ${oldGrade}/20 --> ${colle.grade}/20`, data: { id: colle.id, }, @@ -131,7 +135,7 @@ const NOTIFICATIONS = { ROOM_UPDATED: (colle: Colle) => ({ title: 'Salle modifiée', - body: `Colle de ${colle.subject} en ${colle.room}.`, + body: `Colle de ${colle.subject.name} en ${colle.room.name}.`, data: { id: colle.id, },