31 lines
915 B
Swift
31 lines
915 B
Swift
import Foundation
|
|
|
|
protocol LoginUseCase: Sendable {
|
|
func execute(salesCode: String) async throws -> LoginSession
|
|
}
|
|
|
|
/// Shared by the use case and the form so that both enforce the same rule.
|
|
enum SalesCodeValidator {
|
|
static func normalized(_ input: String) -> String {
|
|
input.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
|
|
}
|
|
|
|
static func isValid(_ input: String) -> Bool {
|
|
normalized(input).count >= 3
|
|
}
|
|
}
|
|
|
|
struct DefaultLoginUseCase: LoginUseCase {
|
|
let authenticationService: any AuthenticationService
|
|
|
|
func execute(salesCode: String) async throws -> LoginSession {
|
|
guard SalesCodeValidator.isValid(salesCode) else {
|
|
throw LoginError.invalidSalesCode
|
|
}
|
|
try Task.checkCancellation()
|
|
return try await authenticationService.signIn(
|
|
salesCode: SalesCodeValidator.normalized(salesCode)
|
|
)
|
|
}
|
|
}
|