diff --git a/Sources/Orders/OrdersService.swift b/Sources/Orders/OrdersService.swift index 81a711c..b6f944c 100644 --- a/Sources/Orders/OrdersService.swift +++ b/Sources/Orders/OrdersService.swift @@ -69,16 +69,6 @@ public final class OrdersService: Sendable { migrations.add(OrdersErrorLog()) } - /// Sends push notifications for a given order. - /// - /// - Parameters: - /// - id: The `UUID` of the order to send the notifications for. - /// - typeIdentifier: The type identifier of the order. - /// - db: The `Database` to use. - public func sendPushNotificationsForOrder(id: UUID, of typeIdentifier: String, on db: any Database) async throws { - try await service.sendPushNotificationsForOrder(id: id, of: typeIdentifier, on: db) - } - /// Sends push notifications for a given order. /// /// - Parameters: diff --git a/Sources/Orders/OrdersServiceCustom.swift b/Sources/Orders/OrdersServiceCustom.swift index 664ea1a..89b16ad 100644 --- a/Sources/Orders/OrdersServiceCustom.swift +++ b/Sources/Orders/OrdersServiceCustom.swift @@ -306,7 +306,16 @@ extension OrdersServiceCustom { } let orderTypeIdentifier = req.parameters.get("orderTypeIdentifier")! - try await sendPushNotificationsForOrder(id: id, of: orderTypeIdentifier, on: req.db) + guard + let order = try await O.query(on: req.db) + .filter(\._$id == id) + .filter(\._$typeIdentifier == orderTypeIdentifier) + .first() + else { + throw Abort(.notFound) + } + + try await sendPushNotifications(for: order, on: req.db) return .noContent } @@ -318,7 +327,16 @@ extension OrdersServiceCustom { } let orderTypeIdentifier = req.parameters.get("orderTypeIdentifier")! - return try await Self.registrationsForOrder(id: id, of: orderTypeIdentifier, on: req.db).map { $0.device.pushToken } + guard + let order = try await O.query(on: req.db) + .filter(\._$id == id) + .filter(\._$typeIdentifier == orderTypeIdentifier) + .first() + else { + throw Abort(.notFound) + } + + return try await Self.registrations(for: order, on: req.db).map { $0.device.pushToken } } } @@ -327,11 +345,10 @@ extension OrdersServiceCustom { /// Sends push notifications for a given order. /// /// - Parameters: - /// - id: The `UUID` of the order to send the notifications for. - /// - typeIdentifier: The type identifier of the order. + /// - order: The order to send the notifications for. /// - db: The `Database` to use. - public func sendPushNotificationsForOrder(id: UUID, of typeIdentifier: String, on db: any Database) async throws { - let registrations = try await Self.registrationsForOrder(id: id, of: typeIdentifier, on: db) + public func sendPushNotifications(for order: O, on db: any Database) async throws { + let registrations = try await Self.registrations(for: order, on: db) for reg in registrations { let backgroundNotification = APNSBackgroundNotification( expiration: .immediately, @@ -350,16 +367,7 @@ extension OrdersServiceCustom { } } - /// Sends push notifications for a given order. - /// - /// - Parameters: - /// - order: The order to send the notifications for. - /// - db: The `Database` to use. - public func sendPushNotifications(for order: O, on db: any Database) async throws { - try await sendPushNotificationsForOrder(id: order.requireID(), of: order.typeIdentifier, on: db) - } - - private static func registrationsForOrder(id: UUID, of typeIdentifier: String, on db: any Database) async throws -> [R] { + private static func registrations(for order: O, on db: any Database) async throws -> [R] { // This could be done by enforcing the caller to have a Siblings property wrapper, // but there's not really any value to forcing that on them when we can just do the query ourselves like this. try await R.query(on: db) @@ -367,8 +375,8 @@ extension OrdersServiceCustom { .join(parent: \._$device) .with(\._$order) .with(\._$device) - .filter(O.self, \._$typeIdentifier == typeIdentifier) - .filter(O.self, \._$id == id) + .filter(O.self, \._$typeIdentifier == order._$typeIdentifier.value!) + .filter(O.self, \._$id == order.requireID()) .all() } } diff --git a/Sources/Passes/PassesService.swift b/Sources/Passes/PassesService.swift index 74c6040..4243277 100644 --- a/Sources/Passes/PassesService.swift +++ b/Sources/Passes/PassesService.swift @@ -106,16 +106,6 @@ public final class PassesService: Sendable { migrations.add(PassesErrorLog()) } - /// Sends push notifications for a given pass. - /// - /// - Parameters: - /// - id: The `UUID` of the pass to send the notifications for. - /// - typeIdentifier: The type identifier of the pass. - /// - db: The `Database` to use. - public func sendPushNotificationsForPass(id: UUID, of typeIdentifier: String, on db: any Database) async throws { - try await service.sendPushNotificationsForPass(id: id, of: typeIdentifier, on: db) - } - /// Sends push notifications for a given pass. /// /// - Parameters: diff --git a/Sources/Passes/PassesServiceCustom.swift b/Sources/Passes/PassesServiceCustom.swift index 5500c8b..8d14a18 100644 --- a/Sources/Passes/PassesServiceCustom.swift +++ b/Sources/Passes/PassesServiceCustom.swift @@ -351,7 +351,16 @@ extension PassesServiceCustom { } let passTypeIdentifier = req.parameters.get("passTypeIdentifier")! - try await sendPushNotificationsForPass(id: id, of: passTypeIdentifier, on: req.db) + guard + let pass = try await P.query(on: req.db) + .filter(\._$id == id) + .filter(\._$typeIdentifier == passTypeIdentifier) + .first() + else { + throw Abort(.notFound) + } + + try await sendPushNotifications(for: pass, on: req.db) return .noContent } @@ -363,7 +372,16 @@ extension PassesServiceCustom { } let passTypeIdentifier = req.parameters.get("passTypeIdentifier")! - return try await Self.registrationsForPass(id: id, of: passTypeIdentifier, on: req.db).map { $0.device.pushToken } + guard + let pass = try await P.query(on: req.db) + .filter(\._$id == id) + .filter(\._$typeIdentifier == passTypeIdentifier) + .first() + else { + throw Abort(.notFound) + } + + return try await Self.registrations(for: pass, on: req.db).map { $0.device.pushToken } } } @@ -372,11 +390,10 @@ extension PassesServiceCustom { /// Sends push notifications for a given pass. /// /// - Parameters: - /// - id: The `UUID` of the pass to send the notifications for. - /// - typeIdentifier: The type identifier of the pass. + /// - pass: The pass to send the notifications for. /// - db: The `Database` to use. - public func sendPushNotificationsForPass(id: UUID, of typeIdentifier: String, on db: any Database) async throws { - let registrations = try await Self.registrationsForPass(id: id, of: typeIdentifier, on: db) + public func sendPushNotifications(for pass: P, on db: any Database) async throws { + let registrations = try await Self.registrations(for: pass, on: db) for reg in registrations { let backgroundNotification = APNSBackgroundNotification( expiration: .immediately, @@ -395,16 +412,7 @@ extension PassesServiceCustom { } } - /// Sends push notifications for a given pass. - /// - /// - Parameters: - /// - pass: The pass to send the notifications for. - /// - db: The `Database` to use. - public func sendPushNotifications(for pass: P, on db: any Database) async throws { - try await sendPushNotificationsForPass(id: pass.requireID(), of: pass.typeIdentifier, on: db) - } - - private static func registrationsForPass(id: UUID, of typeIdentifier: String, on db: any Database) async throws -> [R] { + private static func registrations(for pass: P, on db: any Database) async throws -> [R] { // This could be done by enforcing the caller to have a Siblings property wrapper, // but there's not really any value to forcing that on them when we can just do the query ourselves like this. try await R.query(on: db) @@ -412,8 +420,8 @@ extension PassesServiceCustom { .join(parent: \._$device) .with(\._$pass) .with(\._$device) - .filter(P.self, \._$typeIdentifier == typeIdentifier) - .filter(P.self, \._$id == id) + .filter(P.self, \._$typeIdentifier == pass._$typeIdentifier.value!) + .filter(P.self, \._$id == pass.requireID()) .all() } } diff --git a/Tests/OrdersTests/OrdersTests.swift b/Tests/OrdersTests/OrdersTests.swift index 9b843e3..ab61f1b 100644 --- a/Tests/OrdersTests/OrdersTests.swift +++ b/Tests/OrdersTests/OrdersTests.swift @@ -327,7 +327,7 @@ struct OrdersTests { try await orderData.create(on: app.db) let order = try await orderData._$order.get(on: app.db) - try await ordersService.sendPushNotificationsForOrder(id: order.requireID(), of: order.typeIdentifier, on: app.db) + try await ordersService.sendPushNotifications(for: order, on: app.db) let deviceLibraryIdentifier = "abcdefg" let pushToken = "1234567890" diff --git a/Tests/PassesTests/PassesTests.swift b/Tests/PassesTests/PassesTests.swift index 34848cf..fc6b7fa 100644 --- a/Tests/PassesTests/PassesTests.swift +++ b/Tests/PassesTests/PassesTests.swift @@ -465,7 +465,7 @@ struct PassesTests { try await passData.create(on: app.db) let pass = try await passData._$pass.get(on: app.db) - try await passesService.sendPushNotificationsForPass(id: pass.requireID(), of: pass.typeIdentifier, on: app.db) + try await passesService.sendPushNotifications(for: pass, on: app.db) let deviceLibraryIdentifier = "abcdefg" let pushToken = "1234567890"