Skip to content

Commit

Permalink
Fixing some more comments from coderabbit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dracks committed Dec 4, 2024
1 parent 35ab8da commit 4f16cc6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ class BankTransactionService: ServiceWithQueueAndDb {
try await queue.dispatch(
NewTransactionJob.self,
.init(
id: transaction.id!, movementName: transaction.movementName,
id: try transaction.requireID(),
movementName: transaction.movementName,
value: transaction.value, groupOwnerId: transaction.groupOwnerId))
return transaction
}
Expand Down
3 changes: 2 additions & 1 deletion src/swift-server/core/error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import swift_macros

enum ErrorCode: String, CaseIterable {
case E10000, E10001, E10002, E10003, E10004, E10005, E10006, E10007, E10008, E10009
case E10010, E10011, E10012, E10013, E10014, E10015, E10016
case E10010, E10011, E10012, E10013, E10014, E10015, E10016, E10017
}

enum ApiError: String, StringEnumType {
Expand Down Expand Up @@ -55,6 +55,7 @@ let errorDictionary: [ErrorCode: ErrorInfo] = [
message: "Rule parent cannot be found by the ID",
additionalInfo: "this can be because the parent rule is with another groupOwnerId"
),
.E10017: ErrorInfo(message: "Import from file returned an ID that was not found in the DB"),
]

extension ErrorCode {
Expand Down
10 changes: 9 additions & 1 deletion src/swift-server/importer/importer.service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ class FileImportService: ServiceWithQueueAndDb {
let importReport = try await FileImportReport.query(on: db).filter(
\.$id == importId
).with(\.$rows).first()
return importReport!
guard let importReport else {
throw Exception(
.E10017,
context: [
"importId": importId, "groupOwnerId": groupOwnerId,
"key": key, "fileName": fileName,
])
}
return importReport
}

func getAll(groupIds: [UUID], cursor: String? = nil, limit: Int = 100)
Expand Down
91 changes: 50 additions & 41 deletions src/swift-server/rules/rules.service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,32 @@ class RuleService: ServiceWithDb {
func deleteRule(withId ruleId: UUID, for validGroupsIds: [UUID]) async throws
-> DeleteRuleState
{
let rule = try await Rule.query(on: db).filter(
\.$groupOwner.$id ~~ validGroupsIds
).filter(\.$id == ruleId).with(\.$children).first()
guard let rule else {
return .notFound
}
if !rule.children.isEmpty {
return .hasChildren(
childrenIds: try rule.children.map { try $0.requireID() })
}
try await Condition.query(on: db).filter(\.$rule.$id == ruleId).delete()
let ruleLabelQuery = RuleLabelAction.query(on: db).filter(\.$rule.$id == ruleId)
//let ruleLabelPivotQuery = RuleLabelPivot.query(on: db).filter(\.$rule.$id == ruleId)
let ruleLabelActions = try await ruleLabelQuery.with(\.$transactionPivot)
.all()
for ruleLabelAction in ruleLabelActions {
try await removeLabelActionPivot(labelAction: ruleLabelAction)
try await db.transaction { database in
let rule = try await Rule.query(on: database).filter(
\.$groupOwner.$id ~~ validGroupsIds
).filter(\.$id == ruleId).with(\.$children).first()
guard let rule else {
return .notFound
}
if !rule.children.isEmpty {
return .hasChildren(
childrenIds: try rule.children.map { try $0.requireID() })
}
try await Condition.query(on: database).filter(\.$rule.$id == ruleId)
.delete()
let ruleLabelQuery = RuleLabelAction.query(on: database).filter(
\.$rule.$id == ruleId)
let ruleLabelActions = try await ruleLabelQuery.with(\.$transactionPivot)
.all()
for ruleLabelAction in ruleLabelActions {
try await RuleService.removeLabelActionPivot(
labelAction: ruleLabelAction, on: database)
}
try await ruleLabelQuery.delete()
try await rule.delete(on: database)
return .ok
}
try await ruleLabelQuery.delete()
//try await ruleLabelPivotQuery.delete()
try await rule.delete(on: db)
return .ok

}

enum AddCodingState {
Expand Down Expand Up @@ -218,7 +222,7 @@ class RuleService: ServiceWithDb {
try await rule.$labels.load(on: db)
return .ok(rule: rule)
}

enum RemoveLabelState {
case ok(rule: Rule)
case notFound
Expand All @@ -229,32 +233,37 @@ class RuleService: ServiceWithDb {
fromRule ruleId: UUID,
for validGroupsIds: [UUID]
) async throws -> RemoveLabelState {
let rule = try await Rule.query(on: db).filter(
\.$groupOwner.$id ~~ validGroupsIds
).filter(\.$id == ruleId).first()
guard let rule else {
return .notFound
}
try await db.transaction { database in
let rule = try await Rule.query(on: database).filter(
\.$groupOwner.$id ~~ validGroupsIds
).filter(\.$id == ruleId).first()
guard let rule else {
return .notFound
}

let labelAction = try await RuleLabelAction.query(on: db)
.filter(\.$rule.$id == ruleId)
.filter(\.$label.$id == labelId)
.with(\.$transactionPivot)
.first()
let labelAction = try await RuleLabelAction.query(on: database)
.filter(\.$rule.$id == ruleId)
.filter(\.$label.$id == labelId)
.with(\.$transactionPivot)
.first()

if let labelAction {
try await removeLabelActionPivot(labelAction: labelAction)
try await labelAction.delete(on: db)
}
if let labelAction {
try await RuleService.removeLabelActionPivot(
labelAction: labelAction, on: database)
try await labelAction.delete(on: database)
}

try await rule.$conditions.load(on: db)
try await rule.$labels.load(on: db)
return .ok(rule: rule)
try await rule.$conditions.load(on: database)
try await rule.$labels.load(on: database)
return .ok(rule: rule)
}
}
}

extension RuleService {
private func removeLabelActionPivot(labelAction: RuleLabelAction) async throws {
private static func removeLabelActionPivot(labelAction: RuleLabelAction, on db: Database)
async throws
{
for labelTransaction in labelAction.transactionPivot {
labelTransaction.linkReason = .manualEnabled
try await labelTransaction.save(on: db)
Expand Down

0 comments on commit 4f16cc6

Please sign in to comment.