Skip to content

Commit

Permalink
Merge pull request #101 from gertrude-app/iosapp-events
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredh159 authored Oct 16, 2024
2 parents 3b33790 + a81adab commit 8acb28a
Show file tree
Hide file tree
Showing 67 changed files with 1,025 additions and 403 deletions.
2 changes: 2 additions & 0 deletions api/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let package = Package(
.package(path: "../gertie"),
.package(path: "../pairql"),
.package(path: "../pairql-macapp"),
.package(path: "../pairql-iosapp"),
.package(path: "../ts-interop"),
.package(path: "../x-aws"),
.package(path: "../x-sendgrid"),
Expand All @@ -40,6 +41,7 @@ let package = Package(
.product(name: "Gertie", package: "gertie"),
.product(name: "PairQL", package: "pairql"),
.product(name: "MacAppRoute", package: "pairql-macapp"),
.product(name: "IOSRoute", package: "pairql-iosapp"),
.product(name: "TaggedTime", package: "swift-tagged"),
.product(name: "VaporRouting", package: "vapor-routing"),
.product(name: "Dependencies", package: "swift-dependencies"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private func errorLoc(_ detail: String) -> String {
}
}

private func githubSearch(_ eventId: String) -> String {
func githubSearch(_ eventId: String) -> String {
Slack.link(
to: "https://github.com/search?q=repo%3Agertrude-app%2Fswift%20\(eventId)&type=code",
withText: eventId
Expand Down
25 changes: 25 additions & 0 deletions api/Sources/Api/PairQL/iOS/LogIOSEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import IOSRoute

extension LogIOSEvent: Resolver {
static func resolve(with input: Input, in context: Context) async throws -> Output {
let detail = "\(input.detail ?? ""), " + [
"device: `\(input.deviceType)`",
"iOS: `\(input.iOSVersion)`",
"vendorId: `\(input.vendorId?.lowercased ?? "(nil)")`",
].joined(separator: ", ")

try await context.db.create(InterestingEvent(
eventId: input.eventId,
kind: input.kind,
context: "ios",
detail: detail
))

if context.env.mode == .prod {
await with(dependency: \.slack)
.sysLog("iOS app event: \(githubSearch(input.eventId)) \(detail)")
}

return .success
}
}
12 changes: 12 additions & 0 deletions api/Sources/Api/PairQL/iOS/iOSRoute.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import IOSRoute
import Vapor

extension IOSRoute: RouteResponder {
static func respond(to route: Self, in context: Context) async throws -> Response {
switch route {
case .logIOSEvent(let input):
let output = try await LogIOSEvent.resolve(with: input, in: context)
return try await self.respond(with: output)
}
}
}
12 changes: 12 additions & 0 deletions api/Sources/Api/Routes/PairQL.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Dependencies
import IOSRoute
import MacAppRoute
import URLRouting
import Vapor
Expand All @@ -17,13 +18,19 @@ enum PairQLRoute: Equatable, RouteResponder {
case dashboard(DashboardRoute)
case macApp(MacAppRoute)
case superAdmin(SuperAdminRoute)
case iOS(IOSRoute)

nonisolated(unsafe) static let router = OneOf {
Route(.case(PairQLRoute.macApp)) {
Method("POST")
Path { "macos-app" }
MacAppRoute.router
}
Route(.case(PairQLRoute.iOS)) {
Method("POST")
Path { "ios-app" }
IOSRoute.router
}
Route(.case(PairQLRoute.dashboard)) {
Method("POST")
Path { "dashboard" }
Expand All @@ -44,6 +51,8 @@ enum PairQLRoute: Equatable, RouteResponder {
return try await DashboardRoute.respond(to: dashboardRoute, in: context)
case .superAdmin(let superAdminRoute):
return try await SuperAdminRoute.respond(to: superAdminRoute, in: context)
case .iOS(let iosAppRoute):
return try await IOSRoute.respond(to: iosAppRoute, in: context)
}
}

Expand Down Expand Up @@ -107,6 +116,9 @@ private func logOperation(_ route: PairQLRoute, _ request: Request) {
case .superAdmin:
request.logger
.notice("PairQL request: \("SuperAdmin".cyan) \(operation.yellow)")
case .iOS:
request.logger
.notice("PairQL request: \("iOS".blue) \(operation.yellow)")
}
}

Expand Down
35 changes: 35 additions & 0 deletions api/Tests/ApiTests/iOSPairResolvers/iOSResolverTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import DuetSQL
import IOSRoute
import XCTest
import XExpect

@testable import Api

final class iOSResolverTests: ApiTestCase {
func testLogIOSEvent() async throws {
let eventId = UUID().uuidString
let vendorId = UUID()
_ = try await LogIOSEvent.resolve(
with: .init(
eventId: eventId,
kind: "event",
deviceType: "iPhone",
iOSVersion: "18.0.1",
vendorId: vendorId,
detail: "first launch"
),
in: .mock
)

let retrieved = try await InterestingEvent.query()
.where(.eventId == eventId)
.first(in: self.db)

expect(retrieved.kind).toEqual("event")
expect(retrieved.context).toEqual("ios")
expect(retrieved.detail!).toContain("iPhone")
expect(retrieved.detail!).toContain("18.0.1")
expect(retrieved.detail!).toContain(vendorId.lowercased)
expect(retrieved.detail!).toContain("first launch")
}
}
Loading

0 comments on commit 8acb28a

Please sign in to comment.