|
| 1 | +/** |
| 2 | + * OwnerService handles analytics and retrieval for property owners. |
| 3 | + * In a real-world scenario, this service would fetch historical data from a |
| 4 | + * blockchain indexer (like Soroban event logs) or a local database. |
| 5 | + */ |
| 6 | + |
| 7 | +class OwnerService { |
| 8 | + constructor() { |
| 9 | + // Mock data representing a subset of Indexed Leases |
| 10 | + // Status: 'Draft', 'Active', 'Completed', 'Cancelled' |
| 11 | + this.leases = [ |
| 12 | + { id: 'lease-101', owner_id: 'owner-A', status: 'Completed', tenant_id: 'tenant-1' }, |
| 13 | + { id: 'lease-102', owner_id: 'owner-B', status: 'Completed', tenant_id: 'tenant-2' }, |
| 14 | + { id: 'lease-103', owner_id: 'owner-A', status: 'Completed', tenant_id: 'tenant-3' }, |
| 15 | + { id: 'lease-104', owner_id: 'owner-C', status: 'Completed', tenant_id: 'tenant-4' }, |
| 16 | + { id: 'lease-105', owner_id: 'owner-B', status: 'Active', tenant_id: 'tenant-5' }, |
| 17 | + { id: 'lease-106', owner_id: 'owner-A', status: 'Completed', tenant_id: 'tenant-6' }, |
| 18 | + { id: 'lease-107', owner_id: 'owner-D', status: 'Completed', tenant_id: 'tenant-7' }, |
| 19 | + { id: 'lease-108', owner_id: 'owner-B', status: 'Completed', tenant_id: 'tenant-8' }, |
| 20 | + ]; |
| 21 | + |
| 22 | + // Mock data for Owner profiles |
| 23 | + this.owners = { |
| 24 | + 'owner-A': { id: 'owner-A', name: 'Alice Estate', public_key: 'G...A1' }, |
| 25 | + 'owner-B': { id: 'owner-B', name: 'Bob Properties', public_key: 'G...B2' }, |
| 26 | + 'owner-C': { id: 'owner-C', name: 'Charlie Homes', public_key: 'G...C3' }, |
| 27 | + 'owner-D': { id: 'owner-D', name: 'David Rentals', public_key: 'G...D4' }, |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Retrieves a leaderboard of Owners with the most 'Completed' leases. |
| 33 | + * @param {number} limit - Maximum number of top owners to return. |
| 34 | + * @returns {Array} - List of top owners with their success count. |
| 35 | + */ |
| 36 | + async getTopRatedOwners(limit = 5) { |
| 37 | + // 1. Filter for completed leases only |
| 38 | + const completedLeases = this.leases.filter(lease => lease.status === 'Completed'); |
| 39 | + |
| 40 | + // 2. Count completions per owner |
| 41 | + const countsByOwner = {}; |
| 42 | + completedLeases.forEach(lease => { |
| 43 | + countsByOwner[lease.owner_id] = (countsByOwner[lease.owner_id] || 0) + 1; |
| 44 | + }); |
| 45 | + |
| 46 | + // 3. Map to owner objects and sort |
| 47 | + const topRated = Object.keys(countsByOwner) |
| 48 | + .map(ownerId => ({ |
| 49 | + ...this.owners[ownerId], |
| 50 | + successful_rentals: countsByOwner[ownerId] |
| 51 | + })) |
| 52 | + .sort((a, b) => b.successful_rentals - a.successful_rentals) // Descending |
| 53 | + .slice(0, limit); |
| 54 | + |
| 55 | + return topRated; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +module.exports = new OwnerService(); |
0 commit comments