|
| 1 | +/** |
| 2 | + * Implements the endpoints that handles notifications |
| 3 | + * |
| 4 | + * @category Controller |
| 5 | + * @module NotificationsController |
| 6 | + */ |
| 7 | +import { DateTime } from 'luxon' |
| 8 | +import { getPageNumber } from 'App/Utils/Paginate' |
| 9 | +import Notification from 'App/Models/Notification' |
| 10 | +import Subscription from 'App/Models/Subscription' |
| 11 | +import { ExtractScopes } from '@ioc:Adonis/Lucid/Model' |
| 12 | +import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' |
| 13 | +import PaginationValidator from 'App/Validators/PaginationValidator' |
| 14 | +import { getNotification, getValidatedData } from 'App/Utils/Request' |
| 15 | +import InvalidPushTokenException from 'App/Exceptions/InvalidPushTokenException' |
| 16 | +import NotificationFilterValidator from 'App/Validators/Notifications/FilterValidator' |
| 17 | + |
| 18 | +export default class NotificationsController { |
| 19 | + /** |
| 20 | + * Method that applies given filters to notifications |
| 21 | + * |
| 22 | + * @param scopes - The different scopes that exists in the system |
| 23 | + * @param filters - The filter to apply |
| 24 | + */ |
| 25 | + private applyFilters( |
| 26 | + scopes: ExtractScopes<typeof Notification>, |
| 27 | + filters: Record<string, DateTime | undefined> |
| 28 | + ) { |
| 29 | + if (filters.after) { |
| 30 | + // Filter based on when the event start, i.e. all events after a |
| 31 | + // certain date |
| 32 | + scopes.afterDate(filters.after) |
| 33 | + } |
| 34 | + |
| 35 | + // Order events based on the 'created_at' field |
| 36 | + scopes.inOrder() |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Method to retrieve all the notifications in the system |
| 41 | + * The actual function call is done by a request (CRUD) which are specified |
| 42 | + * in `Routes.ts` |
| 43 | + */ |
| 44 | + public async all({ request }: HttpContextContract) { |
| 45 | + const { after, token } = await getValidatedData(request, NotificationFilterValidator, true) |
| 46 | + const specified = await getValidatedData(request, PaginationValidator, true) |
| 47 | + |
| 48 | + const query = Notification.query().apply((scopes) => { |
| 49 | + this.applyFilters(scopes, { after }) |
| 50 | + }) |
| 51 | + |
| 52 | + if (token) { |
| 53 | + const subscriptions = await Subscription.forToken(token) |
| 54 | + |
| 55 | + if (!subscriptions) { |
| 56 | + throw new InvalidPushTokenException() |
| 57 | + } |
| 58 | + |
| 59 | + query.whereIn( |
| 60 | + ['nation_id', 'subscription_topic_id'], |
| 61 | + subscriptions.map((subscription) => [ |
| 62 | + subscription.nationId, |
| 63 | + subscription.subscriptionTopicId, |
| 64 | + ]) |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + const notifications = await query.paginate(getPageNumber(specified.page), specified.amount) |
| 69 | + return notifications.toJSON() |
| 70 | + } |
| 71 | + |
| 72 | + public async index({ request }: HttpContextContract) { |
| 73 | + return getNotification(request).toJSON() |
| 74 | + } |
| 75 | +} |
0 commit comments