Added the catalogue models and seed to support phase 3 of the POC prototype in the Persistence package.

This commit is contained in:
javier committed 2026-09-21 12:11:01 +02:00
1 parent 43dcc03da6
commit b1ceed7790
20 files changed
+2035 -8

No files matched your search

@@ -0,0 +1,29 @@
import FluentKit
/// Adds an owner to shops and a sold date to listings.
///
/// Registered ahead of ``SeedCatalogue``, whose models write both columns. The owner carries no foreign key: on a fresh database the
/// `users` table does not exist yet at this point.
struct AddTradeColumns: AsyncMigration {
// MARK: Methods
func prepare(on database: Database) async throws {
try await database.schema(ShopRecord.schema)
.field("owner_id", .uuid)
.update()
try await database.schema(ListingRecord.schema)
.field("sold_at", .datetime)
.update()
}
func revert(on database: Database) async throws {
try await database.schema(ListingRecord.schema)
.deleteField("sold_at")
.update()
try await database.schema(ShopRecord.schema)
.deleteField("owner_id")
.update()
}
}
@@ -0,0 +1,82 @@
import FluentKit
/// Creates the conversations, offers, sales, and messages tables, and hands the demo shops to seller accounts, created here when missing.
struct CreateTrade: AsyncMigration {
// MARK: Methods
func prepare(on database: Database) async throws {
try await database.schema(ConversationRecord.schema)
.id()
.field("buyer_id", .uuid, .required, .references(UserRecord.schema, "id"))
.field("shop_id", .uuid, .required, .references(ShopRecord.schema, "id"))
.field("listing_id", .uuid, .references(ListingRecord.schema, "id"))
.field("created_at", .datetime, .required)
.field("buyer_read_at", .datetime, .required)
.field("seller_read_at", .datetime, .required)
.create()
try await database.schema(OfferRecord.schema)
.id()
.field("conversation_id", .uuid, .required, .references(ConversationRecord.schema, "id"))
.field("listing_id", .uuid, .required, .references(ListingRecord.schema, "id"))
.field("buyer_id", .uuid, .required, .references(UserRecord.schema, "id"))
.field("amount_cents", .int, .required)
.field("status", .string, .required)
.field("created_at", .datetime, .required)
.field("expires_at", .datetime, .required)
.create()
try await database.schema(SaleRecord.schema)
.id()
.field("number", .int, .required)
.field("conversation_id", .uuid, .required, .references(ConversationRecord.schema, "id"))
.field("listing_id", .uuid, .required, .references(ListingRecord.schema, "id"))
.field("buyer_id", .uuid, .required, .references(UserRecord.schema, "id"))
.field("seller_id", .uuid, .required, .references(UserRecord.schema, "id"))
.field("price_cents", .int, .required)
.field("asked_cents", .int, .required)
.field("shipping_cents", .int, .required)
.field("declared_grade", .string, .required)
.field("declared_page_quality", .string)
.field("declared_printing", .string, .required)
.field("declared_notes", .string, .required)
.field("agreed_at", .datetime, .required)
.field("paid_at", .datetime)
.field("payment_note", .string)
.field("sent_at", .datetime)
.field("shipping_note", .string)
.field("received_at", .datetime)
.field("buyer_rating", .int)
.field("seller_rating", .int)
.unique(on: "number")
.create()
try await database.schema(MessageRecord.schema)
.id()
.field("conversation_id", .uuid, .required, .references(ConversationRecord.schema, "id"))
.field("sender_id", .uuid, .required, .references(UserRecord.schema, "id"))
.field("kind", .string, .required)
.field("body", .string, .required)
.field("offer_id", .uuid, .references(OfferRecord.schema, "id"))
.field("sale_number", .int)
.field("created_at", .datetime, .required)
.create()
for (slug, seller) in Seed.owners {
let owner = try await UserRecord.query(on: database)
.filter(\.$email == seller.email)
.first() ?? UserRecord(email: seller.email, displayName: seller.displayName)
try await owner.save(on: database)
try await ShopRecord.query(on: database)
.filter(\.$slug == slug)
.set(\.$owner.$id, to: try owner.requireID())
.update()
}
}
func revert(on database: Database) async throws {
for schema in [MessageRecord.schema, SaleRecord.schema, OfferRecord.schema, ConversationRecord.schema] {
try await database.schema(schema).delete()
}
}
}
@@ -0,0 +1,61 @@
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
}
}
@@ -91,6 +91,9 @@ final class ListingRecord: Model, @unchecked Sendable {
@Field(key: "listed_at")
var listedAt: Date
@OptionalField(key: "sold_at")
var soldAt: Date?
// MARK: Initializers
init() {}
@@ -169,7 +172,8 @@ extension ListingRecord {
imprint: coverImprint,
number: coverNumber
),
listedAt: listedAt
listedAt: listedAt,
soldAt: soldAt
)
}
@@ -0,0 +1,58 @@
import FluentKit
import Foundation
/// A FluentKit model of a single `messages` row: something said, an offer made, or an event stamped into a conversation.
final class MessageRecord: Model, @unchecked Sendable {
// MARK: Properties
static let schema = "messages"
@ID(key: .id)
var id: UUID?
@Parent(key: "conversation_id")
var conversation: ConversationRecord
/// Who wrote the message, made the offer, or caused the event.
@Parent(key: "sender_id")
var sender: UserRecord
@Field(key: "kind")
var kind: String
@Field(key: "body")
var body: String
@OptionalParent(key: "offer_id")
var offer: OfferRecord?
@OptionalField(key: "sale_number")
var saleNumber: Int?
@Field(key: "created_at")
var createdAt: Date
// MARK: Initializers
init() {}
init(
conversationID: UUID,
senderID: UUID,
kind: Conversation.Entry.Kind,
body: String,
offerID: UUID? = nil,
saleNumber: Int? = nil,
at date: Date
) {
self.$conversation.id = conversationID
self.$sender.id = senderID
self.kind = kind.rawValue
self.body = body
self.$offer.id = offerID
self.saleNumber = saleNumber
self.createdAt = date
}
}
@@ -0,0 +1,82 @@
import FluentKit
import Foundation
/// A FluentKit model of a single `offers` row.
final class OfferRecord: Model, @unchecked Sendable {
// MARK: Properties
static let schema = "offers"
@ID(key: .id)
var id: UUID?
@Parent(key: "conversation_id")
var conversation: ConversationRecord
@Parent(key: "listing_id")
var listing: ListingRecord
@Parent(key: "buyer_id")
var buyer: UserRecord
@Field(key: "amount_cents")
var amountCents: Int
@Field(key: "status")
var status: String
@Field(key: "created_at")
var createdAt: Date
@Field(key: "expires_at")
var expiresAt: Date
// MARK: Initializers
init() {}
init(
conversationID: UUID,
listingID: UUID,
buyerID: UUID,
amountCents: Int,
at date: Date
) {
self.$conversation.id = conversationID
self.$listing.id = listingID
self.$buyer.id = buyerID
self.amountCents = amountCents
self.status = Offer.Status.open.rawValue
self.createdAt = date
self.expiresAt = date.addingTimeInterval(Offer.lifetime)
}
}
// MARK: - Extensions
extension OfferRecord {
// MARK: Methods
/// The `Sendable` snapshot of the saved record; its buyer, and its listing with its shop, must be eager-loaded.
/// - Parameter now: the moment an open offer's expiry is judged at.
func snapshot(
at now: Date
) throws -> Offer {
let stored = Offer.Status(rawValue: status) ?? .declined
return .init(
id: try requireID(),
conversationID: $conversation.id,
listing: listing.snapshot,
buyer: try buyer.snapshot,
amountCents: amountCents,
status: stored == .open && expiresAt <= now ? .expired : stored,
createdAt: createdAt,
expiresAt: expiresAt
)
}
}
@@ -0,0 +1,116 @@
import FluentKit
import Foundation
/// A FluentKit model of a single `sales` row: the record of an agreement and of its handshake.
final class SaleRecord: Model, @unchecked Sendable {
// MARK: Properties
static let schema = "sales"
@ID(key: .id)
var id: UUID?
@Field(key: "number")
var number: Int
@Parent(key: "conversation_id")
var conversation: ConversationRecord
@Parent(key: "listing_id")
var listing: ListingRecord
@Parent(key: "buyer_id")
var buyer: UserRecord
@Parent(key: "seller_id")
var seller: UserRecord
@Field(key: "price_cents")
var priceCents: Int
@Field(key: "asked_cents")
var askedCents: Int
@Field(key: "shipping_cents")
var shippingCents: Int
@Field(key: "declared_grade")
var declaredGrade: String
@OptionalField(key: "declared_page_quality")
var declaredPageQuality: String?
@Field(key: "declared_printing")
var declaredPrinting: String
@Field(key: "declared_notes")
var declaredNotes: String
@Field(key: "agreed_at")
var agreedAt: Date
@OptionalField(key: "paid_at")
var paidAt: Date?
@OptionalField(key: "payment_note")
var paymentNote: String?
@OptionalField(key: "sent_at")
var sentAt: Date?
@OptionalField(key: "shipping_note")
var shippingNote: String?
@OptionalField(key: "received_at")
var receivedAt: Date?
@OptionalField(key: "buyer_rating")
var buyerRating: Int?
@OptionalField(key: "seller_rating")
var sellerRating: Int?
// MARK: Initializers
init() {}
}
// MARK: - Extensions
extension SaleRecord {
// MARK: Computed
/// The `Sendable` snapshot of the saved record; its buyer, its seller, and its listing with its shop must be eager-loaded.
var snapshot: Sale {
get throws {
.init(
number: number,
conversationID: $conversation.id,
listing: listing.snapshot,
buyer: try buyer.snapshot,
seller: try seller.snapshot,
priceCents: priceCents,
askedCents: askedCents,
shippingCents: shippingCents,
declared: .init(
grade: declaredGrade,
pageQuality: declaredPageQuality,
printing: declaredPrinting,
notes: declaredNotes
),
agreedAt: agreedAt,
paidAt: paidAt,
paymentNote: paymentNote,
sentAt: sentAt,
shippingNote: shippingNote,
receivedAt: receivedAt,
buyerRating: buyerRating,
sellerRating: sellerRating
)
}
}
}
@@ -50,6 +50,9 @@ final class ShopRecord: Model, @unchecked Sendable {
@Field(key: "about")
var about: String
@OptionalParent(key: "owner_id")
var owner: UserRecord?
@Children(for: \.$shop)
var listings: [ListingRecord]
@@ -11,6 +11,15 @@ enum Seed {
("joost@kestrelrow.example", "Joost de Wit"),
]
/// The seller account owning each demo shop, by the shop's slug.
static let owners: [(shop: String, seller: (email: String, displayName: String))] = [
("kestrel-row-comics", accounts[1]),
("third-panel-supply", ("hello@thirdpanel.example", "Noor Janssens")),
("longbox-atlantic", ("sales@longboxatlantic.example", "Owen Pryce")),
("silverfish-comics", ("shop@silverfish.example", "Fenna Bos")),
("inkwell-and-staple", ("post@inkwellstaple.example", "Lukas Brandt")),
]
/// The demo listings, newest first.
static let listings: [Listing] = [
single(