import Dependencies import Foundation /// Represents a vendor branch database model. /// /// A vendor branch is who purchase orders can be issued to on behalf an `Employee`. /// They are associated with a particular `Vendor`. public struct VendorBranch: Codable, Equatable, Identifiable, Sendable { public var id: UUID public var name: String public var vendorID: Vendor.ID public var createdAt: Date? public var updatedAt: Date? public init( id: UUID, name: String, vendorID: Vendor.ID, createdAt: Date? = nil, updatedAt: Date? = nil ) { self.id = id self.name = name self.vendorID = vendorID self.createdAt = createdAt self.updatedAt = updatedAt } } public extension VendorBranch { /// Represents the fields required to generate a new vendor branch in the database. struct Create: Codable, Sendable, Equatable { public let name: String public let vendorID: Vendor.ID public init(name: String, vendorID: Vendor.ID) { self.name = name self.vendorID = vendorID } } /// Represents the details of a vendor branch, which includes the parent vendor item. /// /// This is used in several of the views / api routes that require information about both the /// vendor branch and it's associated parent vendor item. struct Detail: Codable, Equatable, Identifiable, Sendable { public var id: UUID public var name: String public var vendor: Vendor public var createdAt: Date? public var updatedAt: Date? public init( id: UUID, name: String, vendor: Vendor, createdAt: Date? = nil, updatedAt: Date? = nil ) { self.id = id self.name = name self.vendor = vendor self.createdAt = createdAt self.updatedAt = updatedAt } } /// Represents the fields that are used to update attributes of a vendor branch in the database. struct Update: Codable, Sendable, Equatable { public let name: String? public init(name: String?) { self.name = name } } } #if DEBUG public extension VendorBranch.Create { static func generateMocks(countPerVendor: Int = 3, vendors: [Vendor]) -> [Self] { return vendors.reduce(into: [Self]()) { output, vendor in output = (0 ... countPerVendor).reduce(into: output) { array, _ in array.append(.init(name: RandomNames.cityNames.randomElement()!, vendorID: vendor.id)) } } } } #endif