Skip to content

Commit

Permalink
preparing to get the list of imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Dracks committed Sep 12, 2024
1 parent 55a6eff commit 92d4cc9
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 29 deletions.
15 changes: 14 additions & 1 deletion src/view/api/client/imports/get-import-status.graphql
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
query getImportStatus($cursor: String, $limit: Int) {
getImports(cursor: $cursor, limit: $limit) {
imports(cursor: $cursor, limit: $limit) {
... on GetImports {
results {
id
description
status
context
rows {
movementName
date
dateValue
details
value
description
message
transactionId
}
}
next
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class CaixaEnginyersImporterTests: BaseImporterTests {
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?.status, .ok)

let transactions = try await bankTransactionService.getAll(
on: db, groupIds: [groupOwnerId])
Expand Down Expand Up @@ -71,7 +71,7 @@ final class CaixaEnginyersImporterTests: BaseImporterTests {
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?.status, .ok)
XCTAssertEqual(reports.list.first?.description, "")
XCTAssertNil(reports.list.first?.context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class CommerzBankEnImporterTests: BaseImporterTests {
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?.status, .ok)
XCTAssertEqual(reports.list.first?.description, "")
XCTAssertNil(reports.list.first?.context)

Expand Down
4 changes: 2 additions & 2 deletions swift-server/app-tests/importers/importer-n26.tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class N26ImporterTests: BaseImporterTests {
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?.status, .ok)

let transactions = try await bankTransactionService.getAll(
on: db, groupIds: [groupOwnerId])
Expand All @@ -50,7 +50,7 @@ final class N26ImporterTests: BaseImporterTests {
let reports = try await statusReportsService.getAll(
on: db, groupIds: [groupOwnerId])
XCTAssertEqual(reports.list.count, 1)
XCTAssertEqual(reports.list.first?.status, "ERR")
XCTAssertEqual(reports.list.first?.status, .error)
XCTAssertEqual(reports.list.first?.fileName, "n26-file.csv")
XCTAssertContains(
reports.list.first?.description, "E10010: Csv cannot be parsed at")
Expand Down
12 changes: 6 additions & 6 deletions swift-server/app-tests/importers/importer-service.tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class ImporterServiceTests: BaseImporterTests {
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?.status, .ok)
XCTAssertEqual(reports.list.first?.description, "")
XCTAssertEqual(reports.list.first?.context, nil)

Expand All @@ -45,7 +45,7 @@ final class ImporterServiceTests: BaseImporterTests {
print(reports)
XCTAssertEqual(reports.list.count, 1)
let importReport = reports.list.first
XCTAssertEqual(importReport?.status, "ERR")
XCTAssertEqual(importReport?.status, .error)
XCTAssertContains(importReport?.description, "E10000: Parser not found")
XCTAssertEqual(importReport?.context, "{\"parserKey\":\"invalid-key\"}")
}
Expand All @@ -70,7 +70,7 @@ final class ImporterServiceTests: BaseImporterTests {
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?.status, .ok)

// Check transactions
let transactions = try await bankTransactionService.getAll(
Expand Down Expand Up @@ -104,9 +104,9 @@ final class ImporterServiceTests: BaseImporterTests {
let reports = try await statusReportsService.getAll(
on: db, groupIds: [groupOwnerId])
XCTAssertEqual(reports.list.count, 2)
XCTAssertEqual(reports.list[1].status, "OK")
XCTAssertEqual(reports.list[1].status, .ok)
XCTAssertEqual(reports.list[1].description, "")
XCTAssertEqual(reports.list[0].status, "WARN")
XCTAssertEqual(reports.list[0].status, .warn)
XCTAssertEqual(reports.list[0].description, "")

// Check transactions
Expand Down Expand Up @@ -135,7 +135,7 @@ final class ImporterServiceTests: BaseImporterTests {

let report = reports.list.first

XCTAssertEqual(report?.status, "ERR")
XCTAssertEqual(report?.status, .error)
XCTAssertNotNil(report?.description)
XCTAssertTrue(report?.description.contains("E10004") ?? false)
XCTAssertEqual(
Expand Down
66 changes: 55 additions & 11 deletions swift-server/app-tests/imports.tests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import GraphQL
import Graphiti
import XCTVapor

@testable import App
Expand All @@ -10,14 +11,9 @@ final class ImportTests: AbstractBaseTestsClass {
return "\(pwd)/\(file)"
}

func testUploadFile() async throws {
let app = try getApp()

var headers = try await app.getHeaders(
forUser: SessionTypes.Credentials(
username: testUser.username, password: "test-password"))

let testFilePath = getTestFile(file: "test_files/n26_es.csv")
func sendImportRequest(app: Application, headers immutableHeaders: HTTPHeaders, file: String, andKind kind: String) async throws -> XCTHTTPResponse {
var headers = immutableHeaders
let testFilePath = getTestFile(file: file)
let fileURL = URL(fileURLWithPath: testFilePath)
let fileData = try Data(contentsOf: fileURL)

Expand All @@ -28,7 +24,7 @@ final class ImportTests: AbstractBaseTestsClass {
body.append("--\(boundary)\r\n".data(using: .utf8)!)
body.append(
"Content-Disposition: form-data; name=\"kind\"\r\n\r\n".data(using: .utf8)!)
body.append("n26/es\r\n".data(using: .utf8)!)
body.append("\(kind)\r\n".data(using: .utf8)!)

// Add file data
body.append("--\(boundary)\r\n".data(using: .utf8)!)
Expand All @@ -43,9 +39,57 @@ final class ImportTests: AbstractBaseTestsClass {

headers.add(
name: "Content-Type", value: "multipart/form-data; boundary=\(boundary)")
let response = try await app.sendRequest(
return try await app.sendRequest(
.POST, "/import/upload", headers: headers, body: ByteBuffer(data: body))
}

func testUploadFile() async throws {
let app = try getApp()

// Get headers for authenticated request
let headers = try await app.getHeaders(
forUser: SessionTypes.Credentials(
username: testUser.username, password: "test-password"))

let response = try await sendImportRequest(app: app, headers: headers, file: "test_files/n26_es.csv", andKind: "n26/es")

XCTAssertEqual(response.status, .ok)
}

func testGetImports() async throws {
let query = try GraphQLFileLoader.sharedInstance.getContent(of: [
"/imports/get-import-status.graphql"
])
let app = try getApp()

// Get headers for authenticated request
let headers = try await app.getHeaders(
forUser: SessionTypes.Credentials(
username: testUser.username, password: "test-password"))

let _ = try await sendImportRequest(app: app, headers: headers, file: "test_files/commerz_bank.CSV", andKind: "commerz-bank/en")

let response = try await app.queryGql(
GraphQLRequest(query: query), headers: headers)

// Assert the response
XCTAssertEqual(response.status, .ok)

let body = try XCTUnwrap(response.body)
let data = try JSONDecoder().decode(GraphQLResult.self, from: Data(buffer: body))

XCTAssertEqual(data.errors, [])

let imports = try XCTUnwrap(data.data?["getImports"]["results"].array)
XCTAssertGreaterThan(imports.count, 0) // Assuming there are some imports to test

for importResult in imports {
XCTAssertNotNil(importResult["id"].string)
XCTAssertNotNil(importResult["description"].string)
XCTAssertNotNil(importResult["status"].string)
XCTAssertNotNil(importResult["context"].string)
XCTAssertNotNil(importResult["rows"].array)
}

}
}
1 change: 1 addition & 0 deletions swift-server/app/graphql/mrscrooge.schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ class BaseSchema: PartialSchema<MrScroogeResolver, Request> {
Type(DeleteConfirmation.self) {
Field("confirm", at: \.confirm)
}
Scalar(Date.self)
}
}
11 changes: 8 additions & 3 deletions swift-server/app/importers/importer.models.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Fluent
import Vapor

enum StatusReportStatus : String, Codable {
case ok, warn, error

}

final class StatusReport: Model, Content {
static let schema = "status_report"

Expand All @@ -22,8 +27,8 @@ final class StatusReport: Model, Content {
@Field(key: "kind")
var kind: String

@Field(key: "status")
var status: String
@Enum(key: "status")
var status: StatusReportStatus

@OptionalField(key: "stack")
var stack: String?
Expand All @@ -35,7 +40,7 @@ final class StatusReport: Model, Content {

init(
id: UUID? = nil, description: String, fileName: String, groupOwnerId: UUID,
kind: String, status: String
kind: String, status: StatusReportStatus
) {
self.id = id
self.description = description
Expand Down
83 changes: 83 additions & 0 deletions swift-server/app/importers/importer.resolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,45 @@ class ImporterTypes {
let name: String
let regex: String
}

struct GqlImport: Codable {
let id: UUID
let createdAt: Date
let description: String
let fileName: String
let groupOwnerId: UUID
let kind: String
let status: StatusReportStatus
let context: String?
let rows: [GqlImportRow]
}

struct GqlImportRow: Codable {
let movementName: String
let date: DateOnly
let dateValue: DateOnly?
let details: String?
let value: Double
let description: String?
let message: String?
let transactionId: UUID?
}

struct GetImportsArgs: Codable {
let cursor: String?
let limit: Int?
}

struct GetImports: Codable, GetImportsResponse {
let results: [GqlImport]
let next: String?
}

protocol GetImportsResponse {

}



class Schema: PartialSchema<MrScroogeResolver, Request> {
@TypeDefinitions
Expand All @@ -20,11 +59,51 @@ class ImporterTypes {
Field("name", at: \.name)
Field("regex", at: \.regex)
}

Type(GqlImportRow.self) {
Field("movementName", at: \.movementName)
Field("date", at: \.date)
Field("dateValue", at: \.dateValue)
Field("details", at: \.details)
Field("value", at: \.value)
Field("description", at: \.description)
Field("message", at: \.message)
Field("transactionId", at: \.transactionId)
}

Type(GqlImport.self) {
Field("id", at: \.id)
Field("createdAt", at: \.createdAt)
Field("description", at: \.description)
Field("fileName", at: \.fileName)
Field("groupOwnerId", at: \.groupOwnerId)
Field("kind", at: \.kind)
Field("status", at: \.status)
Field("context", at: \.context)
Field("rows", at: \.rows)
}

Enum(StatusReportStatus.self, as: "ImportStatus") {
Value(.ok)
Value(.warn)
Value(.error)
}

Type(GetImports.self){
Field("results", at: \.results)
Field("next", at: \.next)
}

Union(GetImportsResponse.self, members: GetImports.self)
}

@FieldDefinitions
override var query: Fields {
Field("importKinds", at: MrScroogeResolver.importKinds)
Field("imports", at: MrScroogeResolver.imports) {
Argument("cursor", at: \.cursor)
Argument("limit", at: \.limit)
}
}
}
}
Expand All @@ -37,4 +116,8 @@ extension MrScroogeResolver {
name: parser.key, regex: parser.fileRegex.description)
}
}

func imports(req: Request, arguments: ImporterTypes.GetImportsArgs) throws -> ImporterTypes.GetImportsResponse {
throw Abort(.internalServerError)
}
}
6 changes: 3 additions & 3 deletions swift-server/app/importers/importers/new-import.service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class NewImportService {
fileName: fileName,
groupOwnerId: groupOwnerId,
kind: key,
status: "OK"
status: .ok
)
try await status.create(on: db)

Expand All @@ -90,7 +90,7 @@ class NewImportService {
{
if discarting {
let msg = "repeated row, not inserted"
status.status = "WARN"
status.status = .warn
statusTransaction.message = msg
if let previousStateValidated = previousState {
previousStateValidated.message = msg
Expand Down Expand Up @@ -140,7 +140,7 @@ class NewImportService {
withJSONObject: error.allContext, options: [])
status.context = String(data: jsonData, encoding: .utf8)
}
status.status = "ERR"
status.status = .error
try await status.save(on: db)
}

Expand Down

0 comments on commit 92d4cc9

Please sign in to comment.