Skip to content

Commit

Permalink
Merge pull request #39 from gertrude-app/device-filter-version
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredh159 authored Aug 25, 2023
2 parents 2645482 + 113ab9d commit ed5d2a0
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 6 deletions.
1 change: 1 addition & 0 deletions api/Sources/Api/Configure/migrations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ extension Configure {
app.migrations.add(DeviceRefactor())
app.migrations.add(AddReleaseNotes())
app.migrations.add(DeviceIdForeignKey())
app.migrations.add(DeviceFilterVersion())
}
}
2 changes: 2 additions & 0 deletions api/Sources/Api/Database/Column.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum ColumnType {
case bigint
case boolean
case jsonb
case varchar(Int)
case custom(String)
case timestampWithTimezone

Expand All @@ -25,6 +26,7 @@ public enum ColumnType {
case .bigint: return "bigint"
case .boolean: return "boolean"
case .jsonb: return "jsonb"
case .varchar(let length): return "varchar(\(length))"
case .timestampWithTimezone: return "timestamp with time zone"
case .custom(let type): return type
}
Expand Down
22 changes: 22 additions & 0 deletions api/Sources/Api/Database/Migrations/014_DeviceFilterVersion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import FluentSQL

struct DeviceFilterVersion: GertieMigration {
func up(sql: SQLDatabase) async throws {
try await sql.addColumn(
Device.M14.filterVersion,
on: Device.M3.self,
type: .varchar(12),
nullable: true
)
}

func down(sql: SQLDatabase) async throws {
try await sql.dropColumn(Device.M14.filterVersion, on: Device.M3.self)
}
}

extension Device {
enum M14 {
static let filterVersion = FieldKey("filter_version")
}
}
3 changes: 3 additions & 0 deletions api/Sources/Api/Models/Admin/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ final class Device: Codable {
var modelIdentifier: String
var serialNumber: String
var appReleaseChannel: ReleaseChannel
var filterVersion: Semver?
var createdAt = Date()
var updatedAt = Date()

Expand All @@ -19,6 +20,7 @@ final class Device: Codable {
adminId: Admin.Id,
customName: String? = nil,
appReleaseChannel: ReleaseChannel = .stable,
filterVersion: Semver? = nil,
modelIdentifier: String,
serialNumber: String
) {
Expand All @@ -28,6 +30,7 @@ final class Device: Codable {
self.appReleaseChannel = appReleaseChannel
self.modelIdentifier = modelIdentifier
self.serialNumber = serialNumber
self.filterVersion = filterVersion
}
}

Expand Down
3 changes: 2 additions & 1 deletion api/Sources/Api/Models/Models+Duet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension Model {
try await Current.db.update(self)
}

static func find<M: Model>(_ id: Tagged<M, UUID>) async throws -> Self {
static func find(_ id: Tagged<Self, UUID>) async throws -> Self {
try await Current.db.query(Self.self).byId(id).first()
}

Expand Down Expand Up @@ -175,6 +175,7 @@ extension Device {
case modelIdentifier
case serialNumber
case appReleaseChannel
case filterVersion
case createdAt
case updatedAt
}
Expand Down
3 changes: 3 additions & 0 deletions api/Sources/Api/Models/Models+DuetSQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ extension Device: Model {
return .string(serialNumber)
case .appReleaseChannel:
return .enum(appReleaseChannel)
case .filterVersion:
return .varchar(filterVersion?.string)
case .createdAt:
return .date(createdAt)
case .updatedAt:
Expand All @@ -266,6 +268,7 @@ extension Device: Model {
.modelIdentifier: .string(modelIdentifier),
.serialNumber: .string(serialNumber),
.appReleaseChannel: .enum(appReleaseChannel),
.filterVersion: .varchar(filterVersion?.string),
.createdAt: .currentTimestamp,
.updatedAt: .currentTimestamp,
]
Expand Down
12 changes: 9 additions & 3 deletions api/Sources/Api/PairQL/MacApp/Resolvers/CheckIn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ extension CheckIn: Resolver {
async let v1 = RefreshRules.resolve(with: .init(appVersion: input.appVersion), in: context)
async let admin = context.user.admin()
async let userDevice = context.userDevice()
let channel = try await userDevice.adminDevice().appReleaseChannel
let adminDevice = try await userDevice.adminDevice()
let channel = adminDevice.appReleaseChannel

async let latestRelease = LatestAppVersion.resolve(
with: .init(releaseChannel: channel, currentVersion: input.appVersion),
in: .init(requestId: context.requestId, dashboardUrl: context.dashboardUrl)
)

// TODO: use `input.filterVersion`
// https://github.com/gertrude-app/project/issues/185
if let filterVersionSemver = input.filterVersion,
let filterVersion = Semver(filterVersionSemver),
filterVersion != adminDevice.filterVersion {
adminDevice.filterVersion = filterVersion
try await adminDevice.save()
}

return Output(
adminAccountStatus: try await admin.accountStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ final class CheckInResolverTests: ApiTestCase {
}).withDevice()

let output = try await CheckIn.resolve(
with: .init(appVersion: "1.0.0", filterVersion: nil),
with: .init(appVersion: "1.0.0", filterVersion: "3.3.3"),
in: user.context
)
expect(output.userData.name).toBe(user.name)
expect(output.userData.keyloggingEnabled).toBeFalse()
expect(output.userData.screenshotsEnabled).toBeTrue()
expect(output.userData.screenshotFrequency).toEqual(376)
expect(output.userData.screenshotSize).toEqual(1081)

let device = try await Device.find(user.adminDevice.id)
expect(device.filterVersion).toEqual("3.3.3")
}

func testCheckIn_OtherProps() async throws {
Expand Down
7 changes: 7 additions & 0 deletions duet/Sources/DuetSQL/Postgres.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum Postgres {
public enum Data {
case id(UUIDIdentifiable)
case string(String?)
case varchar(String?)
case intArray([Int]?)
case int(Int?)
case int64(Int64?)
Expand All @@ -64,6 +65,8 @@ public enum Postgres {
return false
case .string(let wrapped):
return wrapped == nil
case .varchar(let wrapped):
return wrapped == nil
case .intArray(let wrapped):
return wrapped == nil
case .int(let wrapped):
Expand Down Expand Up @@ -91,6 +94,8 @@ public enum Postgres {
switch self {
case .string:
return "text"
case .varchar:
return "varchar"
case .int, .int64, .double, .float:
return "numeric"
case .intArray:
Expand Down Expand Up @@ -118,6 +123,8 @@ public enum Postgres {
return nullable(enumVal?.rawValue)
case .string(let string):
return nullable(string)
case .varchar(let string):
return nullable(string)
case .int64(let int64):
return nullable(int64)
case .int(let int):
Expand Down
2 changes: 1 addition & 1 deletion duet/Tests/DuetSQLTests/SQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ final class SqlTests: XCTestCase {

let query = """
UPDATE "things"
SET "int" = $1, "updated_at" = $2, "bool" = $3, "optional_string" = $4, "custom_enum" = $5, "optional_custom_enum" = $6, "string" = $7, "optional_int" = $8;
SET "int" = $1, "updated_at" = $2, "bool" = $3, "optional_string" = $4, "custom_enum" = $5, "optional_custom_enum" = $6, "version" = $7, "string" = $8, "optional_int" = $9;
"""

expect(statement.query).toEqual(query)
Expand Down
7 changes: 7 additions & 0 deletions duet/Tests/DuetSQLTests/Thing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class Thing: Codable {

var id: Id
var string: String
var version: String
var int: Int
var bool: Bool
var customEnum: CustomEnum
Expand All @@ -22,6 +23,7 @@ final class Thing: Codable {
init(
id: Id = .init(),
string: String = "foo",
version: String = "1.0.0",
int: Int = 123,
bool: Bool = true,
customEnum: CustomEnum = .foo,
Expand All @@ -34,6 +36,7 @@ final class Thing: Codable {
) {
self.id = id
self.string = string
self.version = version
self.int = int
self.bool = bool
self.customEnum = customEnum
Expand All @@ -57,6 +60,7 @@ extension Thing {
[
.id: .id(self),
.string: .string(string),
.version: .varchar(version),
.int: .int(int),
.bool: .bool(bool),
.optionalInt: .int(optionalInt),
Expand All @@ -73,6 +77,7 @@ extension Thing {
enum CodingKeys: String, CodingKey, CaseIterable {
case id
case string
case version
case int
case bool
case optionalInt
Expand All @@ -95,6 +100,8 @@ extension Thing: Model {
return .id(self)
case .string:
return .string(string)
case .version:
return .varchar(version)
case .int:
return .int(int)
case .bool:
Expand Down
2 changes: 2 additions & 0 deletions gertie/Sources/Gertie/Semver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ extension Semver: LosslessStringConvertible {
}
return result
}

public var string: String { description }
}

extension Semver: ExpressibleByStringLiteral {
Expand Down

0 comments on commit ed5d2a0

Please sign in to comment.