import { aggregateAcrossTeams, formatAggregationResult, loopGet, } from "../lib/loop-api.js"; // Loop API V2: GET /property/residential/sold/{channel} // Distinct from /listed/{channel} — returns exchanged or completed properties // (Loop swagger summary: "USE THIS TO POWER WEBSITE SOLD GALLERIES"). // Use case: canvassing letters built from recent sales in a target postcode. interface LoopPropertyListing { listingId?: number; propertyId?: number; propertyRefId?: string; channel?: string; propertyAddress?: string; displayAddress?: string; status?: string; propertyType?: string; dateLaunched?: string; price?: number; bedrooms?: number; bathrooms?: number; receptionRooms?: number; shortDescription?: string; features?: string[]; images?: { url: string }[]; [key: string]: unknown; } type Channel = "rightmove" | "onTheMarket" | "zoopla" | "website"; export async function propertySold(params: { accountId: string; channel: Channel; teamName?: string; limit?: number; }): Promise { const { accountId, channel, teamName, limit } = params; const result = await aggregateAcrossTeams( accountId, "properties", "loop-property-sold", async (apiKey, team) => { const path = `/property/residential/sold/${channel}`; const data = await loopGet(apiKey, path, "loop-property-sold", team); return Array.isArray(data) ? data : []; }, { teamName, limitPerTeam: limit ?? 50, limitTotal: limit ?? 200 } ); return formatAggregationResult( result, (p) => { const addr = p.displayAddress ?? p.propertyAddress ?? "Unknown address"; const id = p.propertyId != null ? ` [ID: ${p.propertyId}]` : ""; const price = p.price ? ` — £${p.price.toLocaleString("en-GB")}` : ""; const beds = p.bedrooms ? ` ${p.bedrooms}bed` : ""; const propType = p.propertyType ? ` (${p.propertyType})` : ""; const status = p.status ? ` [${p.status}]` : ""; return `- ${addr}${id}${price}${beds}${propType}${status}`; }, `${channel} sold` ); }