Skip to content

Commit

Permalink
fix(financekit): tweaking schema based on testing
Browse files Browse the repository at this point in the history
  • Loading branch information
nikwotton committed Jul 5, 2024
1 parent 33de0b1 commit d37da86
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
41 changes: 21 additions & 20 deletions packages/npm/financekit/ios/Financekit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ class UnknownCurrentBalanceTypeError : Error {
let message : String = MESSAGE_UNKNOWN_CURRENT_BALANCE_TYPE
}

struct FinanceKitQuery : Codable {
struct CandleQuery : Codable {
let limit: Int?
let offset: Int?
}

@available(iOS 17.4, *)
struct AccountDetailsHistoryParams : Codable {
let accountId: UUID
let accountID: UUID
let token: FinanceStore.HistoryToken?
let isMonitoring: Bool?
}
Expand All @@ -70,15 +70,15 @@ struct AccountHistoryParams : Codable {
}

@available(iOS 17.4, *)
struct FinanceKitChanges<Model> : Encodable where Model : Identifiable, Model : Encodable, Model.ID : Codable {
struct CandleChanges<Model> : Encodable where Model : Identifiable, Model : Encodable, Model.ID : Codable {
public let inserted: [Model]
public let updated: [Model]
public let deleted: [Model.ID]
public let newToken: FinanceStore.HistoryToken
}

@available(iOS 17.4, *)
struct FinanceKitAccount : Encodable, Identifiable {
struct CandleAccount : Encodable, Identifiable {
public let _tag: String
public let id: UUID
public let displayName: String
Expand Down Expand Up @@ -108,15 +108,16 @@ struct FinanceKitAccount : Encodable, Identifiable {
}

@available(iOS 17.4, *)
struct FinanceKitAccountBalance : Encodable, Identifiable {
struct CandleAccountBalance : Encodable, Identifiable {
public let id: UUID
public let accountID: UUID
public let currentBalance: FinanceKitCurrentBalance
public let currentBalance: CandleCurrentBalance
}

@available(iOS 17.4, *)
struct FinanceKitCurrentBalance : Encodable {
struct CandleCurrentBalance : Encodable {
public let _tag: String
// TODO: We should define our own models for everything as Apple seems to have custom logic in some of their Codable implementations
public let booked: Balance?
public let available: Balance?
init(currentBalance: CurrentBalance) throws {
Expand Down Expand Up @@ -191,7 +192,7 @@ class Financekit: NSObject {
guard let queryData = stringifiedQuery.data(using: .utf8) else {
return reject(CODE_QUERY_INVALID, MESSAGE_UNKNOWN_PARAMS_ISSUE, nil)
}
let queryObject = try JSONDecoder().decode(FinanceKitQuery.self, from: queryData)
let queryObject = try JSONDecoder().decode(CandleQuery.self, from: queryData)
let query = TransactionQuery(sortDescriptors: [], predicate: nil, limit: queryObject.limit, offset: queryObject.offset)

let transactions = try await FinanceStore.shared.transactions(query: query)
Expand Down Expand Up @@ -219,7 +220,7 @@ class Financekit: NSObject {
let params = try JSONDecoder().decode(AccountDetailsHistoryParams.self, from: paramsData)
let transactionHistory = FinanceStore.shared.transactionHistory(forAccountID: params.accountId, since: params.token, isMonitoring: params.isMonitoring ?? true)
let transactionList = try await transactionHistory.reduce([]) { partialResult, element in
partialResult + [FinanceKitChanges(inserted: element.inserted, updated: element.updated, deleted: element.deleted, newToken: element.newToken)]
partialResult + [CandleChanges(inserted: element.inserted, updated: element.updated, deleted: element.deleted, newToken: element.newToken)]
}

let data = try jsonEncoder.encode(transactionList)
Expand All @@ -243,12 +244,12 @@ class Financekit: NSObject {
guard let queryData: Data = stringifiedQuery.data(using: .utf8) else {
return reject(CODE_QUERY_INVALID, MESSAGE_UNKNOWN_PARAMS_ISSUE, nil)
}
let queryObject = try JSONDecoder().decode(FinanceKitQuery.self, from: queryData)
let queryObject = try JSONDecoder().decode(CandleQuery.self, from: queryData)
let query = AccountQuery(sortDescriptors: [], predicate: nil, limit: queryObject.limit, offset: queryObject.offset)

let accounts = try await FinanceStore.shared.accounts(query: query)
let financekitAccounts = try accounts.map { account in
return try FinanceKitAccount(account: account)
return try CandleAccount(account: account)
}

let data = try jsonEncoder.encode(financekitAccounts)
Expand Down Expand Up @@ -278,9 +279,9 @@ class Financekit: NSObject {
let accountHistory = FinanceStore.shared.accountHistory(since: params.token, isMonitoring: params.isMonitoring ?? true)
let accountList = try await accountHistory.reduce([]) { partialResult, element in
partialResult + [
FinanceKitChanges(
inserted: try element.inserted.map({ account in try FinanceKitAccount(account: account)}),
updated: try element.updated.map({ account in try FinanceKitAccount(account: account)}),
CandleChanges(
inserted: try element.inserted.map({ account in try CandleAccount(account: account)}),
updated: try element.updated.map({ account in try CandleAccount(account: account)}),
deleted: element.deleted, newToken: element.newToken)
]
}
Expand Down Expand Up @@ -308,12 +309,12 @@ class Financekit: NSObject {
guard let queryData: Data = stringifiedQuery.data(using: .utf8) else {
return reject(CODE_QUERY_INVALID, MESSAGE_UNKNOWN_PARAMS_ISSUE, nil)
}
let queryObject = try JSONDecoder().decode(FinanceKitQuery.self, from: queryData)
let queryObject = try JSONDecoder().decode(CandleQuery.self, from: queryData)
let query = AccountBalanceQuery(sortDescriptors: [], predicate: nil, limit: queryObject.limit, offset: queryObject.offset)
let balances = try await FinanceStore.shared.accountBalances(query: query)
let financekitBalances = try balances.map { accountBalance in
let currentBalance = try FinanceKitCurrentBalance(currentBalance: accountBalance.currentBalance)
return FinanceKitAccountBalance(id: accountBalance.id, accountID: accountBalance.accountID, currentBalance: currentBalance)
let currentBalance = try CandleCurrentBalance(currentBalance: accountBalance.currentBalance)
return CandleAccountBalance(id: accountBalance.id, accountID: accountBalance.accountID, currentBalance: currentBalance)
}

let data = try jsonEncoder.encode(financekitBalances)
Expand Down Expand Up @@ -343,9 +344,9 @@ class Financekit: NSObject {
let accountBalanceHistory = FinanceStore.shared.accountBalanceHistory(forAccountID: params.accountId, since: params.token, isMonitoring: params.isMonitoring ?? true)
let accountBalanceList = try await accountBalanceHistory.reduce([]) { partialResult, element in
partialResult + [
FinanceKitChanges(
inserted: try element.inserted.map({ accountBalance in try FinanceKitAccountBalance(id: accountBalance.id, accountID: accountBalance.accountID, currentBalance: FinanceKitCurrentBalance(currentBalance: accountBalance.currentBalance))}),
updated: try element.updated.map({ accountBalance in try FinanceKitAccountBalance(id: accountBalance.id, accountID: accountBalance.accountID, currentBalance: FinanceKitCurrentBalance(currentBalance: accountBalance.currentBalance))}),
CandleChanges(
inserted: try element.inserted.map({ accountBalance in try CandleAccountBalance(id: accountBalance.id, accountID: accountBalance.accountID, currentBalance: CandleCurrentBalance(currentBalance: accountBalance.currentBalance))}),
updated: try element.updated.map({ accountBalance in try CandleAccountBalance(id: accountBalance.id, accountID: accountBalance.accountID, currentBalance: CandleCurrentBalance(currentBalance: accountBalance.currentBalance))}),
deleted: element.deleted,
newToken: element.newToken
)
Expand Down
6 changes: 3 additions & 3 deletions packages/npm/financekit/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ enum TransactionStatus {

const CurrencyAmount = S.Struct({
amount: S.NumberFromString,
currencyCode: S.String,
currencyCode: S.OptionFromUndefinedOr(S.String),
})

const Transaction = S.Struct({
id: S_CNDL_UUID,
accountId: S_CNDL_UUID,
accountID: S_CNDL_UUID,
transactionAmount: CurrencyAmount,
foreignCurrencyAmount: S.OptionFromUndefinedOr(CurrencyAmount),
creditDebitIndicator: S.Enums(CreditDebitIndicator),
Expand All @@ -106,7 +106,7 @@ const AccountHistoryParams = S.Struct({
const AccountDetailsHistoryParams = AccountHistoryParams.pipe(
S.extend(
S.Struct({
accountId: S_CNDL_UUID,
accountID: S_CNDL_UUID,
})
)
)
Expand Down

0 comments on commit d37da86

Please sign in to comment.