Skip to content

Commit

Permalink
Change xxxTypeIdentifier to typeIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Oct 31, 2024
1 parent f9339d3 commit d03686b
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 149 deletions.
12 changes: 6 additions & 6 deletions Sources/Orders/Models/Concrete Models/Order.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ 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)
public var authenticationToken: String

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
}
}
Expand All @@ -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()
}
Expand All @@ -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")
}
}
12 changes: 6 additions & 6 deletions Sources/Orders/Models/OrderModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -36,14 +36,14 @@ extension OrderModel {
return id
}

var _$orderTypeIdentifier: Field<String> {
guard let mirror = Mirror(reflecting: self).descendant("_orderTypeIdentifier"),
let orderTypeIdentifier = mirror as? Field<String>
var _$typeIdentifier: Field<String> {
guard let mirror = Mirror(reflecting: self).descendant("_typeIdentifier"),
let typeIdentifier = mirror as? Field<String>
else {
fatalError("orderTypeIdentifier property must be declared using @Field")
fatalError("typeIdentifier property must be declared using @Field")
}

return orderTypeIdentifier
return typeIdentifier
}

var _$updatedAt: Timestamp<DefaultTimestampFormat> {
Expand Down
6 changes: 2 additions & 4 deletions Sources/Orders/Models/OrdersRegistrationModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ extension OrdersRegistrationModel {
return order
}

static func `for`(
deviceLibraryIdentifier: String, orderTypeIdentifier: String, on db: any Database
) -> QueryBuilder<Self> {
static func `for`(deviceLibraryIdentifier: String, typeIdentifier: String, on db: any Database) -> QueryBuilder<Self> {
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)
}
}
4 changes: 2 additions & 2 deletions Sources/Orders/Orders.docc/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ import Orders

final class OrderDelegate: OrdersDelegate {
func encode<O: OrderModel>(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())
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 3 additions & 5 deletions Sources/Orders/OrdersService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
59 changes: 27 additions & 32 deletions Sources/Orders/OrdersServiceCustom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<O>())
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) })
}
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -371,18 +366,18 @@ 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)
.join(parent: \._$order)
.join(parent: \._$device)
.with(\._$order)
.with(\._$device)
.filter(O.self, \._$orderTypeIdentifier == orderTypeIdentifier)
.filter(O.self, \._$typeIdentifier == typeIdentifier)
.filter(O.self, \._$id == id)
.all()
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Passes/Models/Concrete Models/Pass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
Expand All @@ -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,
Expand All @@ -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")
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/Passes/Models/PassModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 {
Expand All @@ -65,14 +65,14 @@ extension PassModel {
return id
}

var _$passTypeIdentifier: Field<String> {
guard let mirror = Mirror(reflecting: self).descendant("_passTypeIdentifier"),
let passTypeIdentifier = mirror as? Field<String>
var _$typeIdentifier: Field<String> {
guard let mirror = Mirror(reflecting: self).descendant("_typeIdentifier"),
let typeIdentifier = mirror as? Field<String>
else {
fatalError("passTypeIdentifier property must be declared using @Field")
fatalError("typeIdentifier property must be declared using @Field")
}

return passTypeIdentifier
return typeIdentifier
}

var _$updatedAt: Timestamp<DefaultTimestampFormat> {
Expand Down
6 changes: 2 additions & 4 deletions Sources/Passes/Models/PassesRegistrationModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ extension PassesRegistrationModel {
return pass
}

static func `for`(
deviceLibraryIdentifier: String, passTypeIdentifier: String, on db: any Database
) -> QueryBuilder<Self> {
static func `for`(deviceLibraryIdentifier: String, typeIdentifier: String, on db: any Database) -> QueryBuilder<Self> {
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)
}
}
4 changes: 2 additions & 2 deletions Sources/Passes/Passes.docc/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ import Passes

final class PassDelegate: PassesDelegate {
func encode<P: PassModel>(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())
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 3 additions & 5 deletions Sources/Passes/PassesService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit d03686b

Please sign in to comment.