Files

131 lines
3.2 KiB
Swift

import Dependencies
import Foundation
/// Represents a user database model.
///
/// User's are who can login to the system and generate purchase orders, manage
/// employees, vendors, etc.
///
public struct User: Codable, Equatable, Identifiable, Sendable {
public var id: UUID
public var email: String
public var username: String
public var createdAt: Date?
public var updatedAt: Date?
public init(
id: UUID,
email: String,
username: String,
createdAt: Date? = nil,
updatedAt: Date? = nil
) {
self.id = id
self.createdAt = createdAt
self.email = email
self.updatedAt = updatedAt
self.username = username
}
}
public extension User {
/// Represents the fields needed to generate a new user in the database.
struct Create: Codable, Sendable, Equatable {
public let username: String
public let email: String
public let password: String
public let confirmPassword: String
public init(
username: String,
email: String,
password: String,
confirmPassword: String
) {
self.username = username
self.email = email
self.password = password
self.confirmPassword = confirmPassword
}
}
/// Represents the fields needed for new user to login.
struct Login: Codable, Sendable, Equatable {
public let username: String?
public let email: String?
public let password: String
public init(
username: String?,
email: String? = nil,
password: String
) {
self.username = username
self.email = email
self.password = password
}
}
/// Represents the fields needed to reset the password of a user.
struct ResetPassword: Codable, Equatable, Sendable {
public let password: String
public let confirmPassword: String
public init(
password: String,
confirmPassword: String
) {
self.password = password
self.confirmPassword = confirmPassword
}
}
/// Represents a user token that can be used to authenticate a user, typically
/// used for interacting with api routes remotely.
struct Token: Codable, Equatable, Identifiable, Sendable {
public let id: UUID
public let userID: User.ID
public let value: String
public init(
id: UUID,
userID: User.ID,
value: String
) {
self.id = id
self.userID = userID
self.value = value
}
}
/// Represents the fields needed to update a user's attributes in the database.
struct Update: Codable, Equatable, Sendable {
public let username: String?
public let email: String?
public init(username: String?, email: String?) {
self.username = username
self.email = email
}
}
}
#if DEBUG
public extension User.Create {
static func generateMocks(count: Int = 5) -> [Self] {
(0 ... count).reduce(into: [Self]()) { array, _ in
array.append(.init(
username: RandomNames.userNames.randomElement()! + String(RandomNames.characterString.randomElement()!),
email: String(RandomNames.characterString.randomElement()!) + RandomNames.emails.randomElement()!,
password: "super-secret",
confirmPassword: "super-secret"
))
}
}
}
#endif