import FluentKit import Foundation /// A FluentKit model of a single `conversations` row: a buyer and a shop, talking about a listing or the shop at large. final class ConversationRecord: Model, @unchecked Sendable { // MARK: Properties static let schema = "conversations" @ID(key: .id) var id: UUID? @Parent(key: "buyer_id") var buyer: UserRecord @Parent(key: "shop_id") var shop: ShopRecord @OptionalParent(key: "listing_id") var listing: ListingRecord? @Field(key: "created_at") var createdAt: Date /// When the buyer last opened the conversation; what came later is unread to them. @Field(key: "buyer_read_at") var buyerReadAt: Date /// When the seller last opened the conversation; what came later is unread to them. @Field(key: "seller_read_at") var sellerReadAt: Date @Children(for: \.$conversation) var messages: [MessageRecord] @Children(for: \.$conversation) var offers: [OfferRecord] @Children(for: \.$conversation) var sales: [SaleRecord] // MARK: Initializers init() {} init( buyerID: UUID, shopID: UUID, listingID: UUID?, at date: Date ) { self.$buyer.id = buyerID self.$shop.id = shopID self.$listing.id = listingID self.createdAt = date self.buyerReadAt = date self.sellerReadAt = date } }