Branchwell

Jobley / app / Sources / JobleyKit / Models.swift

1import Foundation
2
3// The shapes of the Jobley API (PRD section 7). Keys are mapped by hand: the API's value → label
4// maps (workplaces, categories) must keep their snake_case keys, so no automatic key strategy is used.
5
6public struct Pay: Codable, Hashable, Sendable {
7 public var min: Int
8 public var max: Int
9 public var currency: String
10 public var period: String
11
12 public init(min: Int, max: Int, currency: String, period: String) {
13 self.min = min; self.max = max; self.currency = currency; self.period = period
14 }
15
16 /// "$82,000 to $98,000 a year", "£41,000 to £48,000 a year", "$70 to $85 an hour".
17 public var text: String {
18 let formatter = NumberFormatter()
19 formatter.numberStyle = .currency
20 formatter.currencyCode = currency
21 formatter.maximumFractionDigits = 0
22 let low = formatter.string(from: NSNumber(value: min)) ?? "\(min)"
23 let high = formatter.string(from: NSNumber(value: max)) ?? "\(max)"
24 let range = min == max ? low : "\(low) to \(high)"
25 let article = period == "hour" ? "an" : "a"
26 return "\(range) \(article) \(period)"
27 }
28}
29
30public struct Contact: Codable, Hashable, Sendable {
31 public var name: String
32 public var email: String
33}
34
35public struct Review: Codable, Hashable, Sendable {
36 public var reason: String?
37 public var by: String?
38 public var at: String?
39}
40
41/// A job. The public shape, plus status, contact and review when it comes from the admin API.
42public struct Job: Codable, Hashable, Identifiable, Sendable {
43 public var id: String
44 public var url: String
45 public var title: String
46 public var company: String
47 public var companyURL: String?
48 public var location: String
49 public var workplace: String
50 public var employmentType: String
51 public var category: String
52 public var pay: Pay
53 public var description: String
54 public var applyURL: String?
55 public var publishedAt: String?
56 public var expiresAt: String?
57 public var status: String?
58 public var contact: Contact?
59 public var review: Review?
60 public var createdAt: String?
61 public var updatedAt: String?
62
63 enum CodingKeys: String, CodingKey {
64 case id, url, title, company, location, workplace, category, pay, description, status, contact, review
65 case companyURL = "company_url", employmentType = "employment_type", applyURL = "apply_url"
66 case publishedAt = "published_at", expiresAt = "expires_at", createdAt = "created_at", updatedAt = "updated_at"
67 }
68
69 /// The same job without the poster's private contact details (what an agent may see).
70 public var withoutContact: Job { var copy = self; copy.contact = nil; return copy }
71}
72
73public enum JobStatus: String, CaseIterable, Sendable {
74 case pending, approved, published, rejected, unpublished, closed
75
76 public var label: String {
77 switch self {
78 case .pending: "Waiting for review"
79 case .approved: "Approved"
80 case .published: "Published"
81 case .rejected: "Sent back"
82 case .unpublished: "Unpublished"
83 case .closed: "Closed by the poster"
84 }
85 }
86}
87
88public struct Meta: Codable, Sendable {
89 public var name: String
90 public var site: String
91 public var turnstileSiteKey: String?
92 public var workplaces: [String: String]
93 public var employmentTypes: [String: String]
94 public var categories: [String: String]
95 public var payPeriods: [String: String]
96 public var jobDaysLive: Int?
97
98 enum CodingKeys: String, CodingKey {
99 case name, site, workplaces, categories
100 case turnstileSiteKey = "turnstile_site_key", employmentTypes = "employment_types"
101 case payPeriods = "pay_periods", jobDaysLive = "job_days_live"
102 }
103
104 /// Labels used before /api/meta has loaded, and by the MCP server. They match src/config.js.
105 public static let fallback = Meta(
106 name: "Jobley", site: "https://jobley.ai", turnstileSiteKey: nil,
107 workplaces: ["remote": "Remote", "hybrid": "Hybrid", "onsite": "On site"],
108 employmentTypes: ["full_time": "Full time", "part_time": "Part time", "contract": "Contract", "temporary": "Temporary", "internship": "Internship"],
109 categories: ["engineering": "Engineering", "design": "Design", "product": "Product", "data": "Data", "marketing": "Marketing",
110 "sales": "Sales", "support": "Customer support", "operations": "Operations", "finance": "Finance",
111 "people": "People and HR", "writing": "Writing and editing", "education": "Education",
112 "healthcare": "Healthcare", "trades": "Skilled trades", "other": "Other"],
113 payPeriods: ["year": "a year", "month": "a month", "week": "a week", "day": "a day", "hour": "an hour"],
114 jobDaysLive: 45)
115
116 public init(from decoder: Decoder) throws {
117 let c = try decoder.container(keyedBy: CodingKeys.self)
118 name = try c.decode(String.self, forKey: .name)
119 site = try c.decode(String.self, forKey: .site)
120 turnstileSiteKey = try c.decodeIfPresent(String.self, forKey: .turnstileSiteKey)
121 workplaces = try c.decode([String: String].self, forKey: .workplaces)
122 employmentTypes = try c.decode([String: String].self, forKey: .employmentTypes)
123 categories = try c.decode([String: String].self, forKey: .categories)
124 // The API sends pay periods as a plain list; older drafts used a value → label map.
125 if let map = try? c.decode([String: String].self, forKey: .payPeriods) { payPeriods = map }
126 else {
127 let list = (try? c.decode([String].self, forKey: .payPeriods)) ?? Meta.payPeriodOrder
128 payPeriods = Dictionary(uniqueKeysWithValues: list.map { ($0, Meta.fallback.payPeriods[$0] ?? $0) })
129 }
130 jobDaysLive = try c.decodeIfPresent(Int.self, forKey: .jobDaysLive)
131 }
132
133 public init(name: String, site: String, turnstileSiteKey: String?, workplaces: [String: String], employmentTypes: [String: String],
134 categories: [String: String], payPeriods: [String: String], jobDaysLive: Int?) {
135 self.name = name; self.site = site; self.turnstileSiteKey = turnstileSiteKey; self.workplaces = workplaces
136 self.employmentTypes = employmentTypes; self.categories = categories; self.payPeriods = payPeriods; self.jobDaysLive = jobDaysLive
137 }
138
139 public static let workplaceOrder = ["remote", "hybrid", "onsite"]
140 public static let employmentTypeOrder = ["full_time", "part_time", "contract", "temporary", "internship"]
141 public static let categoryOrder = ["engineering", "design", "product", "data", "marketing", "sales", "support", "operations",
142 "finance", "people", "writing", "education", "healthcare", "trades", "other"]
143 public static let payPeriodOrder = ["year", "month", "week", "day", "hour"]
144}
145
146public struct JobPage: Codable, Sendable {
147 public var jobs: [Job]
148 public var nextCursor: String?
149 enum CodingKeys: String, CodingKey { case jobs, nextCursor = "next_cursor" }
150}
151
152public struct PostedJob: Codable, Sendable {
153 public var id: String
154 public var status: String
155 public var manageToken: String
156 enum CodingKeys: String, CodingKey { case id, status, manageToken = "manage_token" }
157}
158
159public struct Received: Codable, Sendable {
160 public var id: String
161 public var status: String
162 public var already: Bool?
163}
164
165public struct Me: Codable, Sendable, Equatable {
166 public var role: String
167 public var id: String
168 public var name: String
169 public var canSeeCandidates: Bool
170 enum CodingKeys: String, CodingKey { case role, id, name, canSeeCandidates = "can_see_candidates" }
171 public var isCurator: Bool { role == "curator" }
172}
173
174public struct Stats: Codable, Sendable, Equatable {
175 public struct Applications: Codable, Sendable, Equatable { public var new: Int }
176 public var jobs: [String: Int]
177 public var candidates: [String: Int]?
178 public var moderatorApplications: Applications?
179 enum CodingKeys: String, CodingKey { case jobs, candidates, moderatorApplications = "moderator_applications" }
180
181 public func jobs(_ status: JobStatus) -> Int { jobs[status.rawValue] ?? 0 }
182}
183
184public struct ActivityEntry: Codable, Hashable, Sendable, Identifiable {
185 public var actor: String
186 public var client: String?
187 public var action: String
188 public var detail: String?
189 public var createdAt: String
190 enum CodingKeys: String, CodingKey { case actor, client, action, detail, createdAt = "created_at" }
191 public var id: String { "\(createdAt)-\(action)-\(actor)" }
192}
193
194public struct JobReviewItem: Codable, Sendable {
195 public var job: Job
196 public var activity: [ActivityEntry]
197 public var candidateCount: Int
198 enum CodingKeys: String, CodingKey { case job, activity, candidateCount = "candidate_count" }
199}
200
201public struct Candidate: Codable, Hashable, Identifiable, Sendable {
202 public var id: String
203 public var jobID: String?
204 public var status: String
205 public var name: String
206 public var email: String
207 public var location: String?
208 public var headline: String
209 public var lookingFor: String?
210 public var about: String?
211 public var links: [String]
212 public var resumeURL: String?
213 public var note: String?
214 public var createdAt: String
215 public var updatedAt: String
216 enum CodingKeys: String, CodingKey {
217 case id, status, name, email, location, headline, about, links, note
218 case jobID = "job_id", lookingFor = "looking_for", resumeURL = "resume_url", createdAt = "created_at", updatedAt = "updated_at"
219 }
220}
221
222public struct Introduction: Codable, Hashable, Identifiable, Sendable {
223 public var id: String
224 public var jobID: String
225 public var introducedBy: String
226 public var note: String?
227 public var createdAt: String
228 enum CodingKeys: String, CodingKey { case id, note, jobID = "job_id", introducedBy = "introduced_by", createdAt = "created_at" }
229}
230
231public struct CandidateDetail: Codable, Sendable {
232 public var candidate: Candidate
233 public var introductions: [Introduction]
234}
235
236public struct IntroductionResult: Codable, Sendable {
237 public var id: String
238 public var candidateID: String
239 public var jobID: String
240 public var status: String
241 public var already: Bool?
242 enum CodingKeys: String, CodingKey { case id, status, already, candidateID = "candidate_id", jobID = "job_id" }
243}
244
245public struct ModeratorApplication: Codable, Hashable, Identifiable, Sendable {
246 public var id: String
247 public var status: String
248 public var name: String
249 public var email: String
250 public var timezone: String?
251 public var hoursPerWeek: Int?
252 public var why: String
253 public var experience: String?
254 public var links: [String]
255 public var note: String?
256 public var createdAt: String
257 enum CodingKeys: String, CodingKey {
258 case id, status, name, email, timezone, why, experience, links, note
259 case hoursPerWeek = "hours_per_week", createdAt = "created_at"
260 }
261}
262
263public struct Moderator: Codable, Hashable, Identifiable, Sendable {
264 public var id: String
265 public var name: String
266 public var email: String
267 public var canSeeCandidates: Bool
268 public var active: Bool
269 public var createdAt: String?
270 public var lastSeenAt: String?
271 enum CodingKeys: String, CodingKey {
272 case id, name, email, active
273 case canSeeCandidates = "can_see_candidates", createdAt = "created_at", lastSeenAt = "last_seen_at"
274 }
275
276 public init(from decoder: Decoder) throws {
277 let c = try decoder.container(keyedBy: CodingKeys.self)
278 id = try c.decode(String.self, forKey: .id)
279 name = try c.decode(String.self, forKey: .name)
280 email = try c.decode(String.self, forKey: .email)
281 canSeeCandidates = try c.decodeFlexibleBool(.canSeeCandidates)
282 active = (try? c.decodeFlexibleBool(.active)) ?? true
283 createdAt = try c.decodeIfPresent(String.self, forKey: .createdAt)
284 lastSeenAt = try c.decodeIfPresent(String.self, forKey: .lastSeenAt)
285 }
286}
287
288public struct NewModerator: Codable, Sendable {
289 public var moderator: Moderator
290 /// Shown once: the one-time link the new moderator opens in Jobley to add their passkey.
291 public var setup: SetupLink
292}
293
294public struct PurgeResult: Codable, Sendable {
295 public var olderThan: String
296 enum CodingKeys: String, CodingKey { case olderThan = "older_than" }
297}
298
299extension KeyedDecodingContainer {
300 /// SQLite booleans come back as 0/1.
301 func decodeFlexibleBool(_ key: Key) throws -> Bool {
302 if let value = try? decode(Bool.self, forKey: key) { return value }
303 return try decode(Int.self, forKey: key) != 0
304 }
305}
306
307/// When a time from the API is shown to a person: "Nov 27, 2026".
308public enum APIDate {
309 nonisolated(unsafe) private static let iso: ISO8601DateFormatter = {
310 let f = ISO8601DateFormatter(); f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]; return f
311 }()
312 nonisolated(unsafe) private static let isoPlain = ISO8601DateFormatter()
313
314 public static func date(_ text: String?) -> Date? {
315 guard let text else { return nil }
316 return iso.date(from: text) ?? isoPlain.date(from: text)
317 }
318
319 public static func short(_ text: String?) -> String {
320 guard let date = date(text) else { return text ?? "" }
321 return date.formatted(date: .abbreviated, time: .omitted)
322 }
323
324 public static func full(_ text: String?) -> String {
325 guard let date = date(text) else { return text ?? "" }
326 return date.formatted(date: .abbreviated, time: .shortened)
327 }
328}