From 4f16cc675fa0099f12bcc461290fca89dd1bee0f Mon Sep 17 00:00:00 2001 From: Jaume Date: Wed, 4 Dec 2024 18:09:43 +0100 Subject: [PATCH] Fixing some more comments from coderabbit --- .../bank-transaction.service.swift | 3 +- src/swift-server/core/error.swift | 3 +- .../importer/importer.service.swift | 10 +- src/swift-server/rules/rules.service.swift | 91 ++++++++++--------- 4 files changed, 63 insertions(+), 44 deletions(-) diff --git a/src/swift-server/bank-transaction/bank-transaction.service.swift b/src/swift-server/bank-transaction/bank-transaction.service.swift index 4d366ed..f5c91f8 100644 --- a/src/swift-server/bank-transaction/bank-transaction.service.swift +++ b/src/swift-server/bank-transaction/bank-transaction.service.swift @@ -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 } diff --git a/src/swift-server/core/error.swift b/src/swift-server/core/error.swift index dc00ac7..06f5350 100644 --- a/src/swift-server/core/error.swift +++ b/src/swift-server/core/error.swift @@ -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 { @@ -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 { diff --git a/src/swift-server/importer/importer.service.swift b/src/swift-server/importer/importer.service.swift index 6ad78a5..4738135 100644 --- a/src/swift-server/importer/importer.service.swift +++ b/src/swift-server/importer/importer.service.swift @@ -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) diff --git a/src/swift-server/rules/rules.service.swift b/src/swift-server/rules/rules.service.swift index fb838eb..24a3556 100644 --- a/src/swift-server/rules/rules.service.swift +++ b/src/swift-server/rules/rules.service.swift @@ -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 { @@ -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 @@ -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)