import Foundation // The shapes of the Jobley API (PRD section 7). Keys are mapped by hand: the API's value → label // maps (workplaces, categories) must keep their snake_case keys, so no automatic key strategy is used. public struct Pay: Codable, Hashable, Sendable { public var min: Int public var max: Int public var currency: String public var period: String public init(min: Int, max: Int, currency: String, period: String) { self.min = min; self.max = max; self.currency = currency; self.period = period } /// "$82,000 to $98,000 a year", "£41,000 to £48,000 a year", "$70 to $85 an hour". public var text: String { let formatter = NumberFormatter() formatter.numberStyle = .currency formatter.currencyCode = currency formatter.maximumFractionDigits = 0 let low = formatter.string(from: NSNumber(value: min)) ?? "\(min)" let high = formatter.string(from: NSNumber(value: max)) ?? "\(max)" let range = min == max ? low : "\(low) to \(high)" let article = period == "hour" ? "an" : "a" return "\(range) \(article) \(period)" } } public struct Contact: Codable, Hashable, Sendable { public var name: String public var email: String } public struct Review: Codable, Hashable, Sendable { public var reason: String? public var by: String? public var at: String? } /// A job. The public shape, plus status, contact and review when it comes from the admin API. public struct Job: Codable, Hashable, Identifiable, Sendable { public var id: String public var url: String public var title: String public var company: String public var companyURL: String? public var location: String public var workplace: String public var employmentType: String public var category: String public var pay: Pay public var description: String public var applyURL: String? public var publishedAt: String? public var expiresAt: String? public var status: String? public var contact: Contact? public var review: Review? public var createdAt: String? public var updatedAt: String? enum CodingKeys: String, CodingKey { case id, url, title, company, location, workplace, category, pay, description, status, contact, review case companyURL = "company_url", employmentType = "employment_type", applyURL = "apply_url" case publishedAt = "published_at", expiresAt = "expires_at", createdAt = "created_at", updatedAt = "updated_at" } /// The same job without the poster's private contact details (what an agent may see). public var withoutContact: Job { var copy = self; copy.contact = nil; return copy } } public enum JobStatus: String, CaseIterable, Sendable { case pending, approved, published, rejected, unpublished, closed public var label: String { switch self { case .pending: "Waiting for review" case .approved: "Approved" case .published: "Published" case .rejected: "Sent back" case .unpublished: "Unpublished" case .closed: "Closed by the poster" } } } public struct Meta: Codable, Sendable { public var name: String public var site: String public var turnstileSiteKey: String? public var workplaces: [String: String] public var employmentTypes: [String: String] public var categories: [String: String] public var payPeriods: [String: String] public var jobDaysLive: Int? enum CodingKeys: String, CodingKey { case name, site, workplaces, categories case turnstileSiteKey = "turnstile_site_key", employmentTypes = "employment_types" case payPeriods = "pay_periods", jobDaysLive = "job_days_live" } /// Labels used before /api/meta has loaded, and by the MCP server. They match src/config.js. public static let fallback = Meta( name: "Jobley", site: "https://jobley.ai", turnstileSiteKey: nil, workplaces: ["remote": "Remote", "hybrid": "Hybrid", "onsite": "On site"], employmentTypes: ["full_time": "Full time", "part_time": "Part time", "contract": "Contract", "temporary": "Temporary", "internship": "Internship"], categories: ["engineering": "Engineering", "design": "Design", "product": "Product", "data": "Data", "marketing": "Marketing", "sales": "Sales", "support": "Customer support", "operations": "Operations", "finance": "Finance", "people": "People and HR", "writing": "Writing and editing", "education": "Education", "healthcare": "Healthcare", "trades": "Skilled trades", "other": "Other"], payPeriods: ["year": "a year", "month": "a month", "week": "a week", "day": "a day", "hour": "an hour"], jobDaysLive: 45) public init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) name = try c.decode(String.self, forKey: .name) site = try c.decode(String.self, forKey: .site) turnstileSiteKey = try c.decodeIfPresent(String.self, forKey: .turnstileSiteKey) workplaces = try c.decode([String: String].self, forKey: .workplaces) employmentTypes = try c.decode([String: String].self, forKey: .employmentTypes) categories = try c.decode([String: String].self, forKey: .categories) // The API sends pay periods as a plain list; older drafts used a value → label map. if let map = try? c.decode([String: String].self, forKey: .payPeriods) { payPeriods = map } else { let list = (try? c.decode([String].self, forKey: .payPeriods)) ?? Meta.payPeriodOrder payPeriods = Dictionary(uniqueKeysWithValues: list.map { ($0, Meta.fallback.payPeriods[$0] ?? $0) }) } jobDaysLive = try c.decodeIfPresent(Int.self, forKey: .jobDaysLive) } public init(name: String, site: String, turnstileSiteKey: String?, workplaces: [String: String], employmentTypes: [String: String], categories: [String: String], payPeriods: [String: String], jobDaysLive: Int?) { self.name = name; self.site = site; self.turnstileSiteKey = turnstileSiteKey; self.workplaces = workplaces self.employmentTypes = employmentTypes; self.categories = categories; self.payPeriods = payPeriods; self.jobDaysLive = jobDaysLive } public static let workplaceOrder = ["remote", "hybrid", "onsite"] public static let employmentTypeOrder = ["full_time", "part_time", "contract", "temporary", "internship"] public static let categoryOrder = ["engineering", "design", "product", "data", "marketing", "sales", "support", "operations", "finance", "people", "writing", "education", "healthcare", "trades", "other"] public static let payPeriodOrder = ["year", "month", "week", "day", "hour"] } public struct JobPage: Codable, Sendable { public var jobs: [Job] public var nextCursor: String? enum CodingKeys: String, CodingKey { case jobs, nextCursor = "next_cursor" } } public struct PostedJob: Codable, Sendable { public var id: String public var status: String public var manageToken: String enum CodingKeys: String, CodingKey { case id, status, manageToken = "manage_token" } } public struct Received: Codable, Sendable { public var id: String public var status: String public var already: Bool? } public struct Me: Codable, Sendable, Equatable { public var role: String public var id: String public var name: String public var canSeeCandidates: Bool enum CodingKeys: String, CodingKey { case role, id, name, canSeeCandidates = "can_see_candidates" } public var isCurator: Bool { role == "curator" } } public struct Stats: Codable, Sendable, Equatable { public struct Applications: Codable, Sendable, Equatable { public var new: Int } public var jobs: [String: Int] public var candidates: [String: Int]? public var moderatorApplications: Applications? enum CodingKeys: String, CodingKey { case jobs, candidates, moderatorApplications = "moderator_applications" } public func jobs(_ status: JobStatus) -> Int { jobs[status.rawValue] ?? 0 } } public struct ActivityEntry: Codable, Hashable, Sendable, Identifiable { public var actor: String public var client: String? public var action: String public var detail: String? public var createdAt: String enum CodingKeys: String, CodingKey { case actor, client, action, detail, createdAt = "created_at" } public var id: String { "\(createdAt)-\(action)-\(actor)" } } public struct JobReviewItem: Codable, Sendable { public var job: Job public var activity: [ActivityEntry] public var candidateCount: Int enum CodingKeys: String, CodingKey { case job, activity, candidateCount = "candidate_count" } } public struct Candidate: Codable, Hashable, Identifiable, Sendable { public var id: String public var jobID: String? public var status: String public var name: String public var email: String public var location: String? public var headline: String public var lookingFor: String? public var about: String? public var links: [String] public var resumeURL: String? public var note: String? public var createdAt: String public var updatedAt: String enum CodingKeys: String, CodingKey { case id, status, name, email, location, headline, about, links, note case jobID = "job_id", lookingFor = "looking_for", resumeURL = "resume_url", createdAt = "created_at", updatedAt = "updated_at" } } public struct Introduction: Codable, Hashable, Identifiable, Sendable { public var id: String public var jobID: String public var introducedBy: String public var note: String? public var createdAt: String enum CodingKeys: String, CodingKey { case id, note, jobID = "job_id", introducedBy = "introduced_by", createdAt = "created_at" } } public struct CandidateDetail: Codable, Sendable { public var candidate: Candidate public var introductions: [Introduction] } public struct IntroductionResult: Codable, Sendable { public var id: String public var candidateID: String public var jobID: String public var status: String public var already: Bool? enum CodingKeys: String, CodingKey { case id, status, already, candidateID = "candidate_id", jobID = "job_id" } } public struct ModeratorApplication: Codable, Hashable, Identifiable, Sendable { public var id: String public var status: String public var name: String public var email: String public var timezone: String? public var hoursPerWeek: Int? public var why: String public var experience: String? public var links: [String] public var note: String? public var createdAt: String enum CodingKeys: String, CodingKey { case id, status, name, email, timezone, why, experience, links, note case hoursPerWeek = "hours_per_week", createdAt = "created_at" } } public struct Moderator: Codable, Hashable, Identifiable, Sendable { public var id: String public var name: String public var email: String public var canSeeCandidates: Bool public var active: Bool public var createdAt: String? public var lastSeenAt: String? enum CodingKeys: String, CodingKey { case id, name, email, active case canSeeCandidates = "can_see_candidates", createdAt = "created_at", lastSeenAt = "last_seen_at" } public init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) id = try c.decode(String.self, forKey: .id) name = try c.decode(String.self, forKey: .name) email = try c.decode(String.self, forKey: .email) canSeeCandidates = try c.decodeFlexibleBool(.canSeeCandidates) active = (try? c.decodeFlexibleBool(.active)) ?? true createdAt = try c.decodeIfPresent(String.self, forKey: .createdAt) lastSeenAt = try c.decodeIfPresent(String.self, forKey: .lastSeenAt) } } public struct NewModerator: Codable, Sendable { public var moderator: Moderator /// Shown once: the one-time link the new moderator opens in Jobley to add their passkey. public var setup: SetupLink } public struct PurgeResult: Codable, Sendable { public var olderThan: String enum CodingKeys: String, CodingKey { case olderThan = "older_than" } } extension KeyedDecodingContainer { /// SQLite booleans come back as 0/1. func decodeFlexibleBool(_ key: Key) throws -> Bool { if let value = try? decode(Bool.self, forKey: key) { return value } return try decode(Int.self, forKey: key) != 0 } } /// When a time from the API is shown to a person: "Nov 27, 2026". public enum APIDate { nonisolated(unsafe) private static let iso: ISO8601DateFormatter = { let f = ISO8601DateFormatter(); f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]; return f }() nonisolated(unsafe) private static let isoPlain = ISO8601DateFormatter() public static func date(_ text: String?) -> Date? { guard let text else { return nil } return iso.date(from: text) ?? isoPlain.date(from: text) } public static func short(_ text: String?) -> String { guard let date = date(text) else { return text ?? "" } return date.formatted(date: .abbreviated, time: .omitted) } public static func full(_ text: String?) -> String { guard let date = date(text) else { return text ?? "" } return date.formatted(date: .abbreviated, time: .shortened) } }