-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
227 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
swift-server/app-tests/importers/importer-commerz-bank.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import XCTest | ||
@testable import App | ||
|
||
final class CommerzBankEnImporterTests: BaseImporterTests { | ||
override func getParsers() throws -> [any ParserFactory] { | ||
return [CommerzBankEnImporter()] | ||
} | ||
func testInsertingDataInCommerzBank() async throws { | ||
let groupOwnerId = try self.group.requireID() | ||
let db = try getDb() | ||
|
||
let filePath = getTestFile(file: "test_files/commerz_bank.CSV") | ||
|
||
try await importerService.importFromFile(on: db, groupOwnerId: groupOwnerId, key: "commerz-bank/en", fileName: "CommerzBank", filePath: filePath) | ||
|
||
|
||
let reports = try await statusReportsService.getAll( | ||
on: db, groupIds: [groupOwnerId]) | ||
XCTAssertEqual(reports.list.count, 1) | ||
XCTAssertEqual(reports.list.first?.status, "OK") | ||
XCTAssertEqual(reports.list.first?.description, "") | ||
XCTAssertNil(reports.list.first?.context) | ||
|
||
let transactions = try await bankTransactionService.getAll( | ||
on: db, groupIds: [groupOwnerId]) | ||
XCTAssertEqual(transactions.list.count, 5) | ||
|
||
let queryTest = transactions.list.first(where: { $0.date == DateOnly("2019-05-02") }) | ||
XCTAssertEqual(queryTest?.value, 256.01) | ||
XCTAssertEqual(queryTest?.movementName, "Concept and more concepts") | ||
|
||
let queryTest2 = transactions.list.first(where: { $0.date == DateOnly("2020-01-09") }) | ||
XCTAssertEqual(queryTest2?.value, -25) | ||
XCTAssertEqual(queryTest2?.movementName, "Commerzbank 0321554") | ||
|
||
let queryTest3 = transactions.list.first(where: { $0.date == DateOnly("2020-01-07") }) | ||
XCTAssertEqual(queryTest3?.movementName, "Backerei Sipl GmbH Fil.35 GIR 69036") | ||
|
||
let queryTest4 = transactions.list.first(where: { $0.date == DateOnly("2020-02-09") }) | ||
XCTAssertEqual(queryTest4?.movementName, "ARAL Some address") | ||
|
||
let queryTest5 = transactions.list.first(where: { $0.date == DateOnly("2020-02-07") }) | ||
XCTAssertEqual(queryTest5?.movementName, "BACKSTUBE WUENSCHE GMBH") | ||
} | ||
} | ||
|
||
final class CommerzBankUnitTests: XCTestCase { | ||
func testSplitMessage() throws { | ||
let importerService = CommerzBankEnImporter() | ||
|
||
let (msg, details, date) = try importerService.splitMessage("Kartenzahlung ARAL Some address 2020-02-09T21:13:19 KFN 1 VJ 2442 Kartenzahlung") | ||
XCTAssertEqual(msg, "ARAL Some address") | ||
|
||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
swift-server/app/importers/importers/commerz-bank.importer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import Foundation | ||
import Vapor | ||
import CSV | ||
|
||
class CommerzBankEnImporter: ParserFactory { | ||
let DATE_REGEX = try! NSRegularExpression(pattern: "(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})T\\d{2}:\\d{2}:\\d{2}", options: []) | ||
let transformHelper: TransformHelper<[String?]> | ||
let key: String = "commerz-bank/en" | ||
let fileRegex: String = "Umsaetze_KtoNr.*\\.CSV" | ||
|
||
init() { | ||
let fieldsMap = FieldsMap<Int>( | ||
movementName: 9, | ||
date: 11, | ||
dateValue: 1, | ||
details: 10, value: 4 | ||
) | ||
self.transformHelper = TransformHelper(fieldsMap, dateFormat: "dd.MM.yyyy") | ||
} | ||
|
||
func splitMessage(_ msg: String) throws -> (String, String?, String?){ | ||
var message = msg | ||
|
||
if message.hasPrefix("Auszahlung") { | ||
message.removeFirst("Auszahlung".count) | ||
} else if message.hasPrefix("Kartenzahlung") { | ||
message.removeFirst("Kartenzahlung".count) | ||
} | ||
|
||
let dateMatch = DATE_REGEX.firstMatch(in: message, options: [], range: NSRange(location: 0, length: message.count)) | ||
if let dateMatch = dateMatch { | ||
let range = Range(dateMatch.range(at: 0), in: message) | ||
guard let range = range else { | ||
throw Exception(.E10000) | ||
} | ||
var movementName = String(message[..<range.lowerBound]) | ||
let dayRange = Range(dateMatch.range(withName: "day"), in: message) | ||
let monthRange = Range(dateMatch.range(withName: "month"), in: message) | ||
let yearRange = Range(dateMatch.range(withName: "year"), in: message) | ||
let dateInfo = "\(message[dayRange!]).\(message[monthRange!]).\(message[yearRange!])" | ||
var details : String? | ||
if let slashIndex = movementName.firstIndex(of: "/") { | ||
details = String(movementName[slashIndex...]).trimmingCharacters(in: .whitespaces) | ||
movementName.removeSubrange(slashIndex...) | ||
} | ||
|
||
return (movementName.trimmingCharacters(in: .whitespaces), details, dateInfo) | ||
} else if let strRange = message.range(of: "End-to-End-Ref") { | ||
let splitMsg = message[..<strRange.lowerBound] | ||
return (splitMsg.trimmingCharacters(in: .whitespaces), nil, nil) | ||
} | ||
|
||
return (message.trimmingCharacters(in: .whitespaces), nil, nil) | ||
} | ||
|
||
func create(filePath: String) -> AsyncThrowingStream<PartialBankTransaction, Error> { | ||
AsyncThrowingStream { continuation in | ||
Task { | ||
do { | ||
let csv = try parseCsv(filePath: filePath) | ||
|
||
var lineCounter = 0 | ||
while let row = csv.next() { | ||
lineCounter += 1 | ||
do { | ||
var mappedData : [String?] = row | ||
|
||
let (movementName, details, newDate) = try splitMessage(mappedData[3] ?? "") | ||
|
||
mappedData.append(movementName) | ||
mappedData.append(details) | ||
if let newDate = newDate { | ||
mappedData.append(newDate) | ||
} else { | ||
mappedData.append(row[0]) | ||
} | ||
let transaction = try self.transformHelper.map(mappedData) | ||
continuation.yield(transaction) | ||
} catch { | ||
throw Exception(.E10011, context: ["line": lineCounter], cause: error) | ||
} | ||
} | ||
|
||
continuation.finish() | ||
} catch { | ||
continuation.finish(throwing: error) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters