diff --git a/Sources/Orders/Models/Concrete Models/Order.swift b/Sources/Orders/Models/Concrete Models/Order.swift index 2430a2d..8cf29ef 100644 --- a/Sources/Orders/Models/Concrete Models/Order.swift +++ b/Sources/Orders/Models/Concrete Models/Order.swift @@ -28,8 +28,8 @@ final public class Order: OrderModel, @unchecked Sendable { public var updatedAt: Date? /// An identifier for the order type associated with the order. - @Field(key: Order.FieldKeys.orderTypeIdentifier) - public var orderTypeIdentifier: String + @Field(key: Order.FieldKeys.typeIdentifier) + public var typeIdentifier: String /// The authentication token supplied to your web service. @Field(key: Order.FieldKeys.authenticationToken) @@ -37,8 +37,8 @@ final public class Order: OrderModel, @unchecked Sendable { public required init() {} - public required init(orderTypeIdentifier: String, authenticationToken: String) { - self.orderTypeIdentifier = orderTypeIdentifier + public required init(typeIdentifier: String, authenticationToken: String) { + self.typeIdentifier = typeIdentifier self.authenticationToken = authenticationToken } } @@ -49,7 +49,7 @@ extension Order: AsyncMigration { .id() .field(Order.FieldKeys.createdAt, .datetime, .required) .field(Order.FieldKeys.updatedAt, .datetime, .required) - .field(Order.FieldKeys.orderTypeIdentifier, .string, .required) + .field(Order.FieldKeys.typeIdentifier, .string, .required) .field(Order.FieldKeys.authenticationToken, .string, .required) .create() } @@ -64,7 +64,7 @@ extension Order { static let schemaName = "orders" static let createdAt = FieldKey(stringLiteral: "created_at") static let updatedAt = FieldKey(stringLiteral: "updated_at") - static let orderTypeIdentifier = FieldKey(stringLiteral: "order_type_identifier") + static let typeIdentifier = FieldKey(stringLiteral: "type_identifier") static let authenticationToken = FieldKey(stringLiteral: "authentication_token") } } diff --git a/Sources/Orders/Models/OrderModel.swift b/Sources/Orders/Models/OrderModel.swift index a9045fd..92cbd4f 100644 --- a/Sources/Orders/Models/OrderModel.swift +++ b/Sources/Orders/Models/OrderModel.swift @@ -13,7 +13,7 @@ import Foundation /// Uses a UUID so people can't easily guess order IDs. public protocol OrderModel: Model where IDValue == UUID { /// An identifier for the order type associated with the order. - var orderTypeIdentifier: String { get set } + var typeIdentifier: String { get set } /// The date and time when the customer created the order. var createdAt: Date? { get set } @@ -36,14 +36,14 @@ extension OrderModel { return id } - var _$orderTypeIdentifier: Field { - guard let mirror = Mirror(reflecting: self).descendant("_orderTypeIdentifier"), - let orderTypeIdentifier = mirror as? Field + var _$typeIdentifier: Field { + guard let mirror = Mirror(reflecting: self).descendant("_typeIdentifier"), + let typeIdentifier = mirror as? Field else { - fatalError("orderTypeIdentifier property must be declared using @Field") + fatalError("typeIdentifier property must be declared using @Field") } - return orderTypeIdentifier + return typeIdentifier } var _$updatedAt: Timestamp { diff --git a/Sources/Orders/Models/OrdersRegistrationModel.swift b/Sources/Orders/Models/OrdersRegistrationModel.swift index 6550627..64e41e3 100644 --- a/Sources/Orders/Models/OrdersRegistrationModel.swift +++ b/Sources/Orders/Models/OrdersRegistrationModel.swift @@ -41,15 +41,13 @@ extension OrdersRegistrationModel { return order } - static func `for`( - deviceLibraryIdentifier: String, orderTypeIdentifier: String, on db: any Database - ) -> QueryBuilder { + static func `for`(deviceLibraryIdentifier: String, typeIdentifier: String, on db: any Database) -> QueryBuilder { Self.query(on: db) .join(parent: \._$order) .join(parent: \._$device) .with(\._$order) .with(\._$device) - .filter(OrderType.self, \._$orderTypeIdentifier == orderTypeIdentifier) + .filter(OrderType.self, \._$typeIdentifier == typeIdentifier) .filter(DeviceType.self, \._$deviceLibraryIdentifier == deviceLibraryIdentifier) } } diff --git a/Sources/Orders/Orders.docc/GettingStarted.md b/Sources/Orders/Orders.docc/GettingStarted.md index 2628281..9870283 100644 --- a/Sources/Orders/Orders.docc/GettingStarted.md +++ b/Sources/Orders/Orders.docc/GettingStarted.md @@ -122,7 +122,7 @@ import Orders final class OrderDelegate: OrdersDelegate { func encode(order: O, db: Database, encoder: JSONEncoder) async throws -> Data { - // The specific OrderData class you use here may vary based on the `order.orderTypeIdentifier` + // The specific OrderData class you use here may vary based on the `order.typeIdentifier` // if you have multiple different types of orders, and thus multiple types of order data. guard let orderData = try await OrderData.query(on: db) .filter(\.$order.$id == order.requireID()) @@ -239,7 +239,7 @@ struct OrderDataMiddleware: AsyncModelMiddleware { // Create the `Order` and add it to the `OrderData` automatically at creation func create(model: OrderData, on db: Database, next: AnyAsyncModelResponder) async throws { let order = Order( - orderTypeIdentifier: Environment.get("ORDER_TYPE_IDENTIFIER")!, + typeIdentifier: Environment.get("ORDER_TYPE_IDENTIFIER")!, authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString()) try await order.save(on: db) model.$order.id = try order.requireID() diff --git a/Sources/Orders/OrdersService.swift b/Sources/Orders/OrdersService.swift index 016c134..0a9d1e0 100644 --- a/Sources/Orders/OrdersService.swift +++ b/Sources/Orders/OrdersService.swift @@ -75,12 +75,10 @@ public final class OrdersService: Sendable { /// /// - Parameters: /// - id: The `UUID` of the order to send the notifications for. - /// - orderTypeIdentifier: The type identifier of the order. + /// - typeIdentifier: The type identifier of the order. /// - db: The `Database` to use. - public func sendPushNotificationsForOrder( - id: UUID, of orderTypeIdentifier: String, on db: any Database - ) async throws { - try await service.sendPushNotificationsForOrder(id: id, of: orderTypeIdentifier, on: db) + 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. diff --git a/Sources/Orders/OrdersServiceCustom.swift b/Sources/Orders/OrdersServiceCustom.swift index 664ae80..f174720 100644 --- a/Sources/Orders/OrdersServiceCustom.swift +++ b/Sources/Orders/OrdersServiceCustom.swift @@ -113,30 +113,24 @@ where O == R.OrderType, D == R.DeviceType { ) let v1 = app.grouped("api", "orders", "v1") - v1.get( - "devices", ":deviceIdentifier", "registrations", ":orderTypeIdentifier", - use: { try await self.ordersForDevice(req: $0) }) + v1.get("devices", ":deviceIdentifier", "registrations", ":orderTypeIdentifier", use: { try await self.ordersForDevice(req: $0) }) v1.post("log", use: { try await self.logError(req: $0) }) let v1auth = v1.grouped(AppleOrderMiddleware()) v1auth.post( - "devices", ":deviceIdentifier", "registrations", ":orderTypeIdentifier", - ":orderIdentifier", use: { try await self.registerDevice(req: $0) }) - v1auth.get( - "orders", ":orderTypeIdentifier", ":orderIdentifier", - use: { try await self.latestVersionOfOrder(req: $0) }) + "devices", ":deviceIdentifier", "registrations", ":orderTypeIdentifier", ":orderIdentifier", + use: { try await self.registerDevice(req: $0) } + ) + v1auth.get("orders", ":orderTypeIdentifier", ":orderIdentifier", use: { try await self.latestVersionOfOrder(req: $0) }) v1auth.delete( - "devices", ":deviceIdentifier", "registrations", ":orderTypeIdentifier", - ":orderIdentifier", use: { try await self.unregisterDevice(req: $0) }) + "devices", ":deviceIdentifier", "registrations", ":orderTypeIdentifier", ":orderIdentifier", + use: { try await self.unregisterDevice(req: $0) } + ) if let pushRoutesMiddleware { let pushAuth = v1.grouped(pushRoutesMiddleware) - pushAuth.post( - "push", ":orderTypeIdentifier", ":orderIdentifier", - use: { try await self.pushUpdatesForOrder(req: $0) }) - pushAuth.get( - "push", ":orderTypeIdentifier", ":orderIdentifier", - use: { try await self.tokensForOrderUpdate(req: $0) }) + pushAuth.post("push", ":orderTypeIdentifier", ":orderIdentifier", use: { try await self.pushUpdatesForOrder(req: $0) }) + pushAuth.get("push", ":orderTypeIdentifier", ":orderIdentifier", use: { try await self.tokensForOrderUpdate(req: $0) }) } } } @@ -159,7 +153,7 @@ extension OrdersServiceCustom { guard let order = try await O.query(on: req.db) .filter(\._$id == id) - .filter(\._$orderTypeIdentifier == orderTypeIdentifier) + .filter(\._$typeIdentifier == orderTypeIdentifier) .first() else { throw Abort(.notFound) @@ -198,7 +192,7 @@ extension OrdersServiceCustom { guard let order = try await O.query(on: req.db) .filter(\._$id == orderIdentifier) - .filter(\._$orderTypeIdentifier == orderTypeIdentifier) + .filter(\._$typeIdentifier == orderTypeIdentifier) .first() else { throw Abort(.notFound) @@ -222,7 +216,8 @@ extension OrdersServiceCustom { ) async throws -> HTTPStatus { let r = try await R.for( deviceLibraryIdentifier: device.deviceLibraryIdentifier, - orderTypeIdentifier: order.orderTypeIdentifier, on: db + typeIdentifier: order.typeIdentifier, + on: db ) .filter(O.self, \._$id == order.requireID()) .first() @@ -243,8 +238,10 @@ extension OrdersServiceCustom { let deviceIdentifier = req.parameters.get("deviceIdentifier")! var query = R.for( - deviceLibraryIdentifier: deviceIdentifier, orderTypeIdentifier: orderTypeIdentifier, - on: req.db) + deviceLibraryIdentifier: deviceIdentifier, + typeIdentifier: orderTypeIdentifier, + on: req.db + ) if let since: TimeInterval = req.query["ordersModifiedSince"] { let when = Date(timeIntervalSince1970: since) query = query.filter(O.self, \._$updatedAt > when) @@ -297,7 +294,8 @@ extension OrdersServiceCustom { guard let r = try await R.for( - deviceLibraryIdentifier: deviceIdentifier, orderTypeIdentifier: orderTypeIdentifier, + deviceLibraryIdentifier: deviceIdentifier, + typeIdentifier: orderTypeIdentifier, on: req.db ) .filter(O.self, \._$id == orderIdentifier) @@ -340,17 +338,14 @@ extension OrdersServiceCustom { /// /// - Parameters: /// - id: The `UUID` of the order to send the notifications for. - /// - orderTypeIdentifier: The type identifier of the order. + /// - typeIdentifier: The type identifier of the order. /// - db: The `Database` to use. - public func sendPushNotificationsForOrder( - id: UUID, of orderTypeIdentifier: String, on db: any Database - ) async throws { - let registrations = try await Self.registrationsForOrder( - id: id, of: orderTypeIdentifier, on: db) + 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) for reg in registrations { let backgroundNotification = APNSBackgroundNotification( expiration: .immediately, - topic: reg.order.orderTypeIdentifier, + topic: reg.order.typeIdentifier, payload: EmptyPayload() ) do { @@ -371,10 +366,10 @@ extension OrdersServiceCustom { /// - 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.orderTypeIdentifier, on: db) + try await sendPushNotificationsForOrder(id: order.requireID(), of: order.typeIdentifier, on: db) } - private static func registrationsForOrder(id: UUID, of orderTypeIdentifier: String, on db: any Database) async throws -> [R] { + private static func registrationsForOrder(id: UUID, of typeIdentifier: String, 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) @@ -382,7 +377,7 @@ extension OrdersServiceCustom { .join(parent: \._$device) .with(\._$order) .with(\._$device) - .filter(O.self, \._$orderTypeIdentifier == orderTypeIdentifier) + .filter(O.self, \._$typeIdentifier == typeIdentifier) .filter(O.self, \._$id == id) .all() } diff --git a/Sources/Passes/Models/Concrete Models/Pass.swift b/Sources/Passes/Models/Concrete Models/Pass.swift index 87a3212..e723208 100644 --- a/Sources/Passes/Models/Concrete Models/Pass.swift +++ b/Sources/Passes/Models/Concrete Models/Pass.swift @@ -29,8 +29,8 @@ final public class Pass: PassModel, @unchecked Sendable { public var updatedAt: Date? /// The pass type identifier that’s registered with Apple. - @Field(key: Pass.FieldKeys.passTypeIdentifier) - public var passTypeIdentifier: String + @Field(key: Pass.FieldKeys.typeIdentifier) + public var typeIdentifier: String /// The authentication token to use with the web service in the `webServiceURL` key. @Field(key: Pass.FieldKeys.authenticationToken) @@ -42,8 +42,8 @@ final public class Pass: PassModel, @unchecked Sendable { public required init() {} - public required init(passTypeIdentifier: String, authenticationToken: String) { - self.passTypeIdentifier = passTypeIdentifier + public required init(typeIdentifier: String, authenticationToken: String) { + self.typeIdentifier = typeIdentifier self.authenticationToken = authenticationToken } } @@ -53,7 +53,7 @@ extension Pass: AsyncMigration { try await database.schema(Self.schema) .id() .field(Pass.FieldKeys.updatedAt, .datetime, .required) - .field(Pass.FieldKeys.passTypeIdentifier, .string, .required) + .field(Pass.FieldKeys.typeIdentifier, .string, .required) .field(Pass.FieldKeys.authenticationToken, .string, .required) .field( Pass.FieldKeys.userPersonalizationID, .int, @@ -72,7 +72,7 @@ extension Pass { enum FieldKeys { static let schemaName = "passes" static let updatedAt = FieldKey(stringLiteral: "updated_at") - static let passTypeIdentifier = FieldKey(stringLiteral: "pass_type_identifier") + static let typeIdentifier = FieldKey(stringLiteral: "type_identifier") static let authenticationToken = FieldKey(stringLiteral: "authentication_token") static let userPersonalizationID = FieldKey(stringLiteral: "user_personalization_id") } diff --git a/Sources/Passes/Models/PassModel.swift b/Sources/Passes/Models/PassModel.swift index ca6dd12..e4fe6a8 100644 --- a/Sources/Passes/Models/PassModel.swift +++ b/Sources/Passes/Models/PassModel.swift @@ -36,7 +36,7 @@ public protocol PassModel: Model where IDValue == UUID { associatedtype UserPersonalizationType: UserPersonalizationModel /// The pass type identifier that’s registered with Apple. - var passTypeIdentifier: String { get set } + var typeIdentifier: String { get set } /// The last time the pass was modified. var updatedAt: Date? { get set } @@ -49,9 +49,9 @@ public protocol PassModel: Model where IDValue == UUID { /// The designated initializer. /// - Parameters: - /// - passTypeIdentifier: The pass type identifier that’s registered with Apple. + /// - typeIdentifier: The pass type identifier that’s registered with Apple. /// - authenticationToken: The authentication token to use with the web service in the `webServiceURL` key. - init(passTypeIdentifier: String, authenticationToken: String) + init(typeIdentifier: String, authenticationToken: String) } extension PassModel { @@ -65,14 +65,14 @@ extension PassModel { return id } - var _$passTypeIdentifier: Field { - guard let mirror = Mirror(reflecting: self).descendant("_passTypeIdentifier"), - let passTypeIdentifier = mirror as? Field + var _$typeIdentifier: Field { + guard let mirror = Mirror(reflecting: self).descendant("_typeIdentifier"), + let typeIdentifier = mirror as? Field else { - fatalError("passTypeIdentifier property must be declared using @Field") + fatalError("typeIdentifier property must be declared using @Field") } - return passTypeIdentifier + return typeIdentifier } var _$updatedAt: Timestamp { diff --git a/Sources/Passes/Models/PassesRegistrationModel.swift b/Sources/Passes/Models/PassesRegistrationModel.swift index 4eb7e39..cb35fcb 100644 --- a/Sources/Passes/Models/PassesRegistrationModel.swift +++ b/Sources/Passes/Models/PassesRegistrationModel.swift @@ -62,15 +62,13 @@ extension PassesRegistrationModel { return pass } - static func `for`( - deviceLibraryIdentifier: String, passTypeIdentifier: String, on db: any Database - ) -> QueryBuilder { + static func `for`(deviceLibraryIdentifier: String, typeIdentifier: String, on db: any Database) -> QueryBuilder { Self.query(on: db) .join(parent: \._$pass) .join(parent: \._$device) .with(\._$pass) .with(\._$device) - .filter(PassType.self, \._$passTypeIdentifier == passTypeIdentifier) + .filter(PassType.self, \._$typeIdentifier == typeIdentifier) .filter(DeviceType.self, \._$deviceLibraryIdentifier == deviceLibraryIdentifier) } } diff --git a/Sources/Passes/Passes.docc/GettingStarted.md b/Sources/Passes/Passes.docc/GettingStarted.md index 5f2d701..f7c679e 100644 --- a/Sources/Passes/Passes.docc/GettingStarted.md +++ b/Sources/Passes/Passes.docc/GettingStarted.md @@ -146,7 +146,7 @@ import Passes final class PassDelegate: PassesDelegate { func encode(pass: P, db: Database, encoder: JSONEncoder) async throws -> Data { - // The specific PassData class you use here may vary based on the `pass.passTypeIdentifier` + // The specific PassData class you use here may vary based on the `pass.typeIdentifier` // if you have multiple different types of passes, and thus multiple types of pass data. guard let passData = try await PassData.query(on: db) .filter(\.$pass.$id == pass.requireID()) @@ -264,7 +264,7 @@ struct PassDataMiddleware: AsyncModelMiddleware { // Create the `Pass` and add it to the `PassData` automatically at creation func create(model: PassData, on db: Database, next: AnyAsyncModelResponder) async throws { let pass = Pass( - passTypeIdentifier: Environment.get("PASS_TYPE_IDENTIFIER")!, + typeIdentifier: Environment.get("PASS_TYPE_IDENTIFIER")!, authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString()) try await pass.save(on: db) model.$pass.id = try pass.requireID() diff --git a/Sources/Passes/PassesService.swift b/Sources/Passes/PassesService.swift index 4be56a9..572f07f 100644 --- a/Sources/Passes/PassesService.swift +++ b/Sources/Passes/PassesService.swift @@ -111,12 +111,10 @@ public final class PassesService: Sendable { /// /// - Parameters: /// - id: The `UUID` of the pass to send the notifications for. - /// - passTypeIdentifier: The type identifier of the pass. + /// - typeIdentifier: The type identifier of the pass. /// - db: The `Database` to use. - public func sendPushNotificationsForPass( - id: UUID, of passTypeIdentifier: String, on db: any Database - ) async throws { - try await service.sendPushNotificationsForPass(id: id, of: passTypeIdentifier, on: db) + 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. diff --git a/Sources/Passes/PassesServiceCustom.swift b/Sources/Passes/PassesServiceCustom.swift index a447be6..eb79390 100644 --- a/Sources/Passes/PassesServiceCustom.swift +++ b/Sources/Passes/PassesServiceCustom.swift @@ -161,7 +161,7 @@ extension PassesServiceCustom { let deviceLibraryIdentifier = req.parameters.get("deviceLibraryIdentifier")! guard let pass = try await P.query(on: req.db) - .filter(\._$passTypeIdentifier == passTypeIdentifier) + .filter(\._$typeIdentifier == passTypeIdentifier) .filter(\._$id == serial) .first() else { @@ -182,14 +182,11 @@ extension PassesServiceCustom { } } - private static func createRegistration( - device: D, - pass: P, - db: any Database - ) async throws -> HTTPStatus { + private static func createRegistration(device: D, pass: P, db: any Database) async throws -> HTTPStatus { let r = try await R.for( deviceLibraryIdentifier: device.deviceLibraryIdentifier, - passTypeIdentifier: pass.passTypeIdentifier, on: db + typeIdentifier: pass.typeIdentifier, + on: db ) .filter(P.self, \._$id == pass.requireID()) .first() @@ -211,7 +208,9 @@ extension PassesServiceCustom { var query = R.for( deviceLibraryIdentifier: deviceLibraryIdentifier, - passTypeIdentifier: passTypeIdentifier, on: req.db) + typeIdentifier: passTypeIdentifier, + on: req.db + ) if let since: TimeInterval = req.query["passesUpdatedSince"] { let when = Date(timeIntervalSince1970: since) query = query.filter(P.self, \._$updatedAt > when) @@ -251,7 +250,7 @@ extension PassesServiceCustom { guard let pass = try await P.query(on: req.db) .filter(\._$id == id) - .filter(\._$passTypeIdentifier == passTypeIdentifier) + .filter(\._$typeIdentifier == passTypeIdentifier) .first() else { throw Abort(.notFound) @@ -284,7 +283,8 @@ extension PassesServiceCustom { guard let r = try await R.for( deviceLibraryIdentifier: deviceLibraryIdentifier, - passTypeIdentifier: passTypeIdentifier, on: req.db + typeIdentifier: passTypeIdentifier, + on: req.db ) .filter(P.self, \._$id == passId) .first() @@ -324,7 +324,7 @@ extension PassesServiceCustom { guard let pass = try await P.query(on: req.db) .filter(\._$id == id) - .filter(\._$passTypeIdentifier == passTypeIdentifier) + .filter(\._$typeIdentifier == passTypeIdentifier) .first() else { throw Abort(.notFound) @@ -444,17 +444,14 @@ extension PassesServiceCustom { /// /// - Parameters: /// - id: The `UUID` of the pass to send the notifications for. - /// - passTypeIdentifier: The type identifier of the pass. + /// - typeIdentifier: The type identifier of the pass. /// - db: The `Database` to use. - public func sendPushNotificationsForPass( - id: UUID, of passTypeIdentifier: String, on db: any Database - ) async throws { - let registrations = try await Self.registrationsForPass( - id: id, of: passTypeIdentifier, on: db) + 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) for reg in registrations { let backgroundNotification = APNSBackgroundNotification( expiration: .immediately, - topic: reg.pass.passTypeIdentifier, + topic: reg.pass.typeIdentifier, payload: EmptyPayload() ) do { @@ -475,10 +472,10 @@ extension PassesServiceCustom { /// - 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.passTypeIdentifier, on: db) + try await sendPushNotificationsForPass(id: pass.requireID(), of: pass.typeIdentifier, on: db) } - private static func registrationsForPass(id: UUID, of passTypeIdentifier: String, on db: any Database) async throws -> [R] { + private static func registrationsForPass(id: UUID, of typeIdentifier: String, 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) @@ -486,7 +483,7 @@ extension PassesServiceCustom { .join(parent: \._$device) .with(\._$pass) .with(\._$device) - .filter(P.self, \._$passTypeIdentifier == passTypeIdentifier) + .filter(P.self, \._$typeIdentifier == typeIdentifier) .filter(P.self, \._$id == id) .all() } diff --git a/Tests/OrdersTests/OrderData.swift b/Tests/OrdersTests/OrderData.swift index 32e4dc9..627ecd3 100644 --- a/Tests/OrdersTests/OrderData.swift +++ b/Tests/OrdersTests/OrderData.swift @@ -104,7 +104,7 @@ struct OrderDataMiddleware: AsyncModelMiddleware { func create(model: OrderData, on db: any Database, next: any AnyAsyncModelResponder) async throws { let order = Order( - orderTypeIdentifier: "order.com.example.pet-store", + typeIdentifier: "order.com.example.pet-store", authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString() ) try await order.save(on: db) diff --git a/Tests/OrdersTests/OrdersTests.swift b/Tests/OrdersTests/OrdersTests.swift index 24ab490..b5afb84 100644 --- a/Tests/OrdersTests/OrdersTests.swift +++ b/Tests/OrdersTests/OrdersTests.swift @@ -49,7 +49,7 @@ struct OrdersTests { try await app.test( .GET, - "\(ordersURI)orders/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)orders/\(order.typeIdentifier)/\(order.requireID())", headers: [ "Authorization": "AppleOrder \(order.authenticationToken)", "If-Modified-Since": "0", @@ -65,7 +65,7 @@ struct OrdersTests { // Test call with invalid authentication token try await app.test( .GET, - "\(ordersURI)orders/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)orders/\(order.typeIdentifier)/\(order.requireID())", headers: [ "Authorization": "AppleOrder invalidToken", "If-Modified-Since": "0", @@ -78,7 +78,7 @@ struct OrdersTests { // Test distant future `If-Modified-Since` date try await app.test( .GET, - "\(ordersURI)orders/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)orders/\(order.typeIdentifier)/\(order.requireID())", headers: [ "Authorization": "AppleOrder \(order.authenticationToken)", "If-Modified-Since": "2147483647", @@ -91,7 +91,7 @@ struct OrdersTests { // Test call with invalid order ID try await app.test( .GET, - "\(ordersURI)orders/\(order.orderTypeIdentifier)/invalidID", + "\(ordersURI)orders/\(order.typeIdentifier)/invalidID", headers: [ "Authorization": "AppleOrder \(order.authenticationToken)", "If-Modified-Since": "0", @@ -127,7 +127,7 @@ struct OrdersTests { try await app.test( .GET, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)?ordersModifiedSince=0", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)?ordersModifiedSince=0", afterResponse: { res async throws in #expect(res.status == .noContent) } @@ -135,7 +135,7 @@ struct OrdersTests { try await app.test( .DELETE, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)/\(order.requireID())", headers: ["Authorization": "AppleOrder \(order.authenticationToken)"], afterResponse: { res async throws in #expect(res.status == .notFound) @@ -145,7 +145,7 @@ struct OrdersTests { // Test registration without authentication token try await app.test( .POST, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)/\(order.requireID())", beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) }, @@ -170,7 +170,7 @@ struct OrdersTests { // Test call without DTO try await app.test( .POST, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)/\(order.requireID())", headers: ["Authorization": "AppleOrder \(order.authenticationToken)"], afterResponse: { res async throws in #expect(res.status == .badRequest) @@ -180,7 +180,7 @@ struct OrdersTests { // Test call with invalid UUID try await app.test( .POST, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\("not-a-uuid")", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)/\("not-a-uuid")", headers: ["Authorization": "AppleOrder \(order.authenticationToken)"], beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) @@ -192,7 +192,7 @@ struct OrdersTests { try await app.test( .POST, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)/\(order.requireID())", headers: ["Authorization": "AppleOrder \(order.authenticationToken)"], beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) @@ -205,7 +205,7 @@ struct OrdersTests { // Test registration of an already registered device try await app.test( .POST, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)/\(order.requireID())", headers: ["Authorization": "AppleOrder \(order.authenticationToken)"], beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) @@ -217,7 +217,7 @@ struct OrdersTests { try await app.test( .GET, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)?ordersModifiedSince=0", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)?ordersModifiedSince=0", afterResponse: { res async throws in let orders = try res.content.decode(OrdersForDeviceDTO.self) #expect(orders.orderIdentifiers.count == 1) @@ -229,7 +229,7 @@ struct OrdersTests { try await app.test( .GET, - "\(ordersURI)push/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)push/\(order.typeIdentifier)/\(order.requireID())", headers: ["X-Secret": "foo"], afterResponse: { res async throws in let pushTokens = try res.content.decode([String].self) @@ -241,7 +241,7 @@ struct OrdersTests { // Test call with invalid UUID try await app.test( .GET, - "\(ordersURI)push/\(order.orderTypeIdentifier)/\("not-a-uuid")", + "\(ordersURI)push/\(order.typeIdentifier)/\("not-a-uuid")", headers: ["X-Secret": "foo"], afterResponse: { res async throws in #expect(res.status == .badRequest) @@ -251,7 +251,7 @@ struct OrdersTests { // Test call with invalid UUID try await app.test( .DELETE, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\("not-a-uuid")", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)/\("not-a-uuid")", headers: ["Authorization": "AppleOrder \(order.authenticationToken)"], afterResponse: { res async throws in #expect(res.status == .badRequest) @@ -260,7 +260,7 @@ struct OrdersTests { try await app.test( .DELETE, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)/\(order.requireID())", headers: ["Authorization": "AppleOrder \(order.authenticationToken)"], afterResponse: { res async throws in #expect(res.status == .ok) @@ -323,7 +323,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.orderTypeIdentifier, on: app.db) + try await ordersService.sendPushNotificationsForOrder(id: order.requireID(), of: order.typeIdentifier, on: app.db) let deviceLibraryIdentifier = "abcdefg" let pushToken = "1234567890" @@ -331,7 +331,7 @@ struct OrdersTests { // Test call with incorrect secret try await app.test( .POST, - "\(ordersURI)push/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)push/\(order.typeIdentifier)/\(order.requireID())", headers: ["X-Secret": "bar"], afterResponse: { res async throws in #expect(res.status == .unauthorized) @@ -340,7 +340,7 @@ struct OrdersTests { try await app.test( .POST, - "\(ordersURI)push/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)push/\(order.typeIdentifier)/\(order.requireID())", headers: ["X-Secret": "foo"], afterResponse: { res async throws in #expect(res.status == .noContent) @@ -349,7 +349,7 @@ struct OrdersTests { try await app.test( .POST, - "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.typeIdentifier)/\(order.requireID())", headers: ["Authorization": "AppleOrder \(order.authenticationToken)"], beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) @@ -361,7 +361,7 @@ struct OrdersTests { try await app.test( .POST, - "\(ordersURI)push/\(order.orderTypeIdentifier)/\(order.requireID())", + "\(ordersURI)push/\(order.typeIdentifier)/\(order.requireID())", headers: ["X-Secret": "foo"], afterResponse: { res async throws in #expect(res.status == .internalServerError) @@ -371,7 +371,7 @@ struct OrdersTests { // Test call with invalid UUID try await app.test( .POST, - "\(ordersURI)push/\(order.orderTypeIdentifier)/\("not-a-uuid")", + "\(ordersURI)push/\(order.typeIdentifier)/\("not-a-uuid")", headers: ["X-Secret": "foo"], afterResponse: { res async throws in #expect(res.status == .badRequest) diff --git a/Tests/PassesTests/PassData.swift b/Tests/PassesTests/PassData.swift index 38614f5..c5388c4 100644 --- a/Tests/PassesTests/PassData.swift +++ b/Tests/PassesTests/PassData.swift @@ -126,7 +126,7 @@ struct PassDataMiddleware: AsyncModelMiddleware { func create(model: PassData, on db: any Database, next: any AnyAsyncModelResponder) async throws { let pass = Pass( - passTypeIdentifier: "pass.com.vapor-community.PassKit", + typeIdentifier: "pass.com.vapor-community.PassKit", authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString() ) try await pass.save(on: db) diff --git a/Tests/PassesTests/PassesTests.swift b/Tests/PassesTests/PassesTests.swift index 27c525b..5bccc26 100644 --- a/Tests/PassesTests/PassesTests.swift +++ b/Tests/PassesTests/PassesTests.swift @@ -107,7 +107,7 @@ struct PassesTests { try await app.test( .GET, - "\(passesURI)passes/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)passes/\(pass.typeIdentifier)/\(pass.requireID())", headers: [ "Authorization": "ApplePass \(pass.authenticationToken)", "If-Modified-Since": "0", @@ -123,7 +123,7 @@ struct PassesTests { // Test call with invalid authentication token try await app.test( .GET, - "\(passesURI)passes/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)passes/\(pass.typeIdentifier)/\(pass.requireID())", headers: [ "Authorization": "ApplePass invalid-token", "If-Modified-Since": "0", @@ -136,7 +136,7 @@ struct PassesTests { // Test distant future `If-Modified-Since` date try await app.test( .GET, - "\(passesURI)passes/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)passes/\(pass.typeIdentifier)/\(pass.requireID())", headers: [ "Authorization": "ApplePass \(pass.authenticationToken)", "If-Modified-Since": "2147483647", @@ -149,7 +149,7 @@ struct PassesTests { // Test call with invalid pass ID try await app.test( .GET, - "\(passesURI)passes/\(pass.passTypeIdentifier)/invalid-uuid", + "\(passesURI)passes/\(pass.typeIdentifier)/invalid-uuid", headers: [ "Authorization": "ApplePass \(pass.authenticationToken)", "If-Modified-Since": "0", @@ -195,7 +195,7 @@ struct PassesTests { try await app.test( .POST, - "\(passesURI)passes/\(pass.passTypeIdentifier)/\(pass.requireID())/personalize", + "\(passesURI)passes/\(pass.typeIdentifier)/\(pass.requireID())/personalize", beforeRequest: { req async throws in try req.content.encode(personalizationDict) }, @@ -221,7 +221,7 @@ struct PassesTests { // Test call with invalid pass ID try await app.test( .POST, - "\(passesURI)passes/\(pass.passTypeIdentifier)/invalid-uuid/personalize", + "\(passesURI)passes/\(pass.typeIdentifier)/invalid-uuid/personalize", beforeRequest: { req async throws in try req.content.encode(personalizationDict) }, @@ -255,7 +255,7 @@ struct PassesTests { try await app.test( .GET, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)?passesUpdatedSince=0", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)?passesUpdatedSince=0", afterResponse: { res async throws in #expect(res.status == .noContent) } @@ -263,7 +263,7 @@ struct PassesTests { try await app.test( .DELETE, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["Authorization": "ApplePass \(pass.authenticationToken)"], afterResponse: { res async throws in #expect(res.status == .notFound) @@ -273,7 +273,7 @@ struct PassesTests { // Test registration without authentication token try await app.test( .POST, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)/\(pass.requireID())", beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) }, @@ -298,7 +298,7 @@ struct PassesTests { // Test call without DTO try await app.test( .POST, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["Authorization": "ApplePass \(pass.authenticationToken)"], afterResponse: { res async throws in #expect(res.status == .badRequest) @@ -308,7 +308,7 @@ struct PassesTests { // Test call with invalid UUID try await app.test( .POST, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\("not-a-uuid")", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)/\("not-a-uuid")", headers: ["Authorization": "ApplePass \(pass.authenticationToken)"], beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) @@ -320,7 +320,7 @@ struct PassesTests { try await app.test( .POST, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["Authorization": "ApplePass \(pass.authenticationToken)"], beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) @@ -333,7 +333,7 @@ struct PassesTests { // Test registration of an already registered device try await app.test( .POST, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["Authorization": "ApplePass \(pass.authenticationToken)"], beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) @@ -345,7 +345,7 @@ struct PassesTests { try await app.test( .GET, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)?passesUpdatedSince=0", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)?passesUpdatedSince=0", afterResponse: { res async throws in let passes = try res.content.decode(PassesForDeviceDTO.self) #expect(passes.serialNumbers.count == 1) @@ -357,7 +357,7 @@ struct PassesTests { try await app.test( .GET, - "\(passesURI)push/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)push/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["X-Secret": "foo"], afterResponse: { res async throws in let pushTokens = try res.content.decode([String].self) @@ -369,7 +369,7 @@ struct PassesTests { // Test call with invalid UUID try await app.test( .GET, - "\(passesURI)push/\(pass.passTypeIdentifier)/\("not-a-uuid")", + "\(passesURI)push/\(pass.typeIdentifier)/\("not-a-uuid")", headers: ["X-Secret": "foo"], afterResponse: { res async throws in #expect(res.status == .badRequest) @@ -379,7 +379,7 @@ struct PassesTests { // Test call with invalid UUID try await app.test( .DELETE, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\("not-a-uuid")", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)/\("not-a-uuid")", headers: ["Authorization": "ApplePass \(pass.authenticationToken)"], afterResponse: { res async throws in #expect(res.status == .badRequest) @@ -388,7 +388,7 @@ struct PassesTests { try await app.test( .DELETE, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["Authorization": "ApplePass \(pass.authenticationToken)"], afterResponse: { res async throws in #expect(res.status == .ok) @@ -451,7 +451,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.passTypeIdentifier, on: app.db) + try await passesService.sendPushNotificationsForPass(id: pass.requireID(), of: pass.typeIdentifier, on: app.db) let deviceLibraryIdentifier = "abcdefg" let pushToken = "1234567890" @@ -459,7 +459,7 @@ struct PassesTests { // Test call with incorrect secret try await app.test( .POST, - "\(passesURI)push/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)push/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["X-Secret": "bar"], afterResponse: { res async throws in #expect(res.status == .unauthorized) @@ -468,7 +468,7 @@ struct PassesTests { try await app.test( .POST, - "\(passesURI)push/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)push/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["X-Secret": "foo"], afterResponse: { res async throws in #expect(res.status == .noContent) @@ -477,7 +477,7 @@ struct PassesTests { try await app.test( .POST, - "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["Authorization": "ApplePass \(pass.authenticationToken)"], beforeRequest: { req async throws in try req.content.encode(RegistrationDTO(pushToken: pushToken)) @@ -489,7 +489,7 @@ struct PassesTests { try await app.test( .POST, - "\(passesURI)push/\(pass.passTypeIdentifier)/\(pass.requireID())", + "\(passesURI)push/\(pass.typeIdentifier)/\(pass.requireID())", headers: ["X-Secret": "foo"], afterResponse: { res async throws in #expect(res.status == .internalServerError) @@ -499,7 +499,7 @@ struct PassesTests { // Test call with invalid UUID try await app.test( .POST, - "\(passesURI)push/\(pass.passTypeIdentifier)/\("not-a-uuid")", + "\(passesURI)push/\(pass.typeIdentifier)/\("not-a-uuid")", headers: ["X-Secret": "foo"], afterResponse: { res async throws in #expect(res.status == .badRequest)