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 dae3dbb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/npm/financekit/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default function App() {
funcInput: {
token: O.none(),
isMonitoring: O.none(),
accountId: S_CNDL_UUID.make('00000000-0000-0000-0000-000000000000'),
accountID: S_CNDL_UUID.make('00000000-0000-0000-0000-000000000000'),
},
onResult: setTransactionHistoryResult,
})
Expand Down Expand Up @@ -107,7 +107,7 @@ export default function App() {
funcInput: {
token: O.none(),
isMonitoring: O.none(),
accountId: S_CNDL_UUID.make('00000000-0000-0000-0000-000000000000'),
accountID: S_CNDL_UUID.make('00000000-0000-0000-0000-000000000000'),
},
onResult: setAccountBalanceHistoryResult,
})
Expand Down
44 changes: 22 additions & 22 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,14 +108,14 @@ 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
public let booked: Balance?
public let available: Balance?
Expand Down Expand Up @@ -191,7 +191,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 All @@ -217,9 +217,9 @@ class Financekit: NSObject {
return reject(CODE_PARAMS_INVALID, MESSAGE_UNKNOWN_PARAMS_ISSUE, nil)
}
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 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 +243,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 +278,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 +308,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 All @@ -340,12 +340,12 @@ class Financekit: NSObject {
return reject(CODE_PARAMS_INVALID, MESSAGE_UNKNOWN_PARAMS_ISSUE, nil)
}
let params = try JSONDecoder().decode(AccountDetailsHistoryParams.self, from: paramsData)
let accountBalanceHistory = FinanceStore.shared.accountBalanceHistory(forAccountID: params.accountId, since: params.token, isMonitoring: params.isMonitoring ?? true)
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
8 changes: 5 additions & 3 deletions packages/npm/financekit/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ enum TransactionStatus {

const CurrencyAmount = S.Struct({
amount: S.NumberFromString,
currencyCode: S.String,
// TODO: We should define our own models for everything in Financekit.swift as Apple seems to have custom logic in some of their Codable implementations
// This line for example following apple's own code should always be present but per testing can be undefined
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 +108,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 dae3dbb

Please sign in to comment.