forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapters.ts
More file actions
196 lines (182 loc) · 6.58 KB
/
Copy pathadapters.ts
File metadata and controls
196 lines (182 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import type {
Bounty,
Milestone,
MaintenancePool,
TeamSplit,
Difficulty,
ReputationProfile,
} from "@/types";
import {
coerceDecimal,
coerceNonNegative,
coercePercentage,
validateTeamSplits,
} from "./utils";
// Shapes returned by mergefi-backend's TypeORM entities (see
// mergefi-backend/src/common/entities). These are intentionally loose since
// we only read the fields the UI needs.
interface RawRepository {
owner: string;
name: string;
}
interface RawUser {
username: string;
}
interface RawTeamSplit {
role: string | null;
percentage: string;
user?: RawUser | null;
}
interface RawIssue {
number: number;
title: string;
body: string | null;
labels: string[];
repository?: RawRepository;
// Nested here, not on RawBounty: mergefi-backend's Bounty entity has no
// milestoneId column at all — the milestone association lives on Issue
// (Issue.milestoneId / Issue.milestone), which Bounty only reaches via
// its one-to-one `issue` relation. See adaptBounty's doc comment (#86).
milestoneId?: string | null;
}
export interface RawBounty {
id: string;
amount: string;
asset: "USDC" | "XLM";
difficulty: Difficulty;
status: Bounty["status"];
deadline: string | null;
escrowId: string | null;
issue?: RawIssue;
claimedBy?: RawUser | null;
team?: { splits?: RawTeamSplit[] } | null;
}
/**
* Investigation notes for #86 (bounty-to-milestone cross-linking):
*
* - milestoneId was declared on the Bounty type but never set here — every
* Bounty this app constructed from live data had it permanently
* undefined. Fixed above by reading it from raw.issue.milestoneId, the
* shape confirmed against mergefi-backend's actual entities (there is no
* milestoneId on Bounty itself).
* - Full field-by-field audit of Bounty vs. this function's return object
* (see adapters.test.ts's "field coverage" test) found milestoneId was
* the *only* field with this silent-drop bug; every other field,
* including the structurally-similar escrowId, was already mapped.
* - Separately discovered while tracing this: mergefi-backend's
* `BountiesService.list()`/`findOne()` load bounties with no `relations`
* option at all, so `raw.issue`/`raw.claimedBy`/`raw.team` — and now
* `raw.issue.milestoneId` — are likely `undefined` on every live
* response today regardless of this fix, until that's corrected on the
* backend. Out of scope here (a different repo, and a much larger
* pre-existing gap than this issue's), but worth flagging since it
* affects whether this fix does anything observable yet in production.
* - UI usage recommendation (#86 asks this be investigated, not
* necessarily implemented): IssueDetailPage gets a small "Part of a
* milestone" indicator below, since it already has the single Bounty
* object this field now lives on — no new fetch needed. MilestonesPage
* cross-linking to member bounties is recommended *against* for this
* PR: Milestone only carries aggregate issueCount/completedCount
* numbers today, and listing member bounties would need either a new
* backend endpoint (bounties filtered by milestoneId) or fetching every
* bounty on a funding-overview page just to filter client-side —
* real new scope, not a natural extension of restoring this mapping.
*/
export function adaptBounty(raw: RawBounty): Bounty & { teamSplitsValid?: { valid: boolean; sum: number; message?: string } } {
const splits = raw.team?.splits?.map(
(split): TeamSplit => ({
role: split.role ?? "Contributor",
percentage: coercePercentage(split.percentage),
contributor: split.user?.username,
}),
);
return {
id: raw.id,
repo: raw.issue?.repository?.name ?? "unknown-repo",
org: raw.issue?.repository?.owner ?? "unknown-org",
issueNumber: raw.issue?.number ?? 0,
title: raw.issue?.title ?? "Untitled issue",
description: raw.issue?.body ?? "",
reward: coerceNonNegative(raw.amount),
asset: raw.asset,
difficulty: raw.difficulty,
status: raw.status,
deadline: raw.deadline ?? new Date().toISOString(),
labels: raw.issue?.labels ?? [],
claimedBy: raw.claimedBy?.username,
milestoneId: raw.issue?.milestoneId ?? undefined,
escrowId: raw.escrowId ?? undefined,
teamSplits: splits,
teamSplitsValid: splits ? validateTeamSplits(splits) : undefined,
};
}
export interface RawMilestone {
id: string;
title: string;
budget: string;
distributed: string;
asset: "USDC" | "XLM";
repository?: RawRepository;
issues?: { state: "open" | "closed" }[];
}
export function adaptMilestone(raw: RawMilestone): Milestone {
const issues = raw.issues ?? [];
return {
id: raw.id,
name: raw.title,
repo: raw.repository ? `${raw.repository.owner}/${raw.repository.name}` : "unassigned",
budget: coerceNonNegative(raw.budget),
distributed: coerceNonNegative(raw.distributed),
asset: raw.asset,
issueCount: issues.length,
completedCount: issues.filter((i) => i.state === "closed").length,
};
}
export interface RawMaintenancePool {
id: string;
monthlyDeposit: string;
balance: string;
asset: "USDC" | "XLM";
repository?: RawRepository | null;
}
export function adaptMaintenancePool(raw: RawMaintenancePool): MaintenancePool {
return {
id: raw.id,
repo: raw.repository ? `${raw.repository.owner}/${raw.repository.name}` : "platform-wide",
monthlyDeposit: coerceNonNegative(raw.monthlyDeposit),
balance: coerceNonNegative(raw.balance),
asset: raw.asset,
};
}
export interface RawReputationSnapshot {
totalEarnings: string;
mergedPrCount: number;
completionRate: string;
avgReviewTimeHours: string;
onTimeDeliveryPercentage: string;
languages: Record<string, number>;
orgsContributedTo: string[];
}
export interface RawUserProfile {
username: string;
avatarUrl: string | null;
}
export function adaptReputation(
user: RawUserProfile,
snapshot: RawReputationSnapshot | null,
): ReputationProfile {
return {
handle: user.username,
avatarUrl:
user.avatarUrl ??
`https://api.dicebear.com/9.x/identicon/svg?seed=${user.username}`,
lifetimeEarnings: snapshot ? coerceNonNegative(snapshot.totalEarnings) : 0,
mergedPRs: snapshot?.mergedPrCount ?? 0,
completionRate: snapshot ? coerceDecimal(snapshot.completionRate) / 100 : 0,
avgReviewTimeHours: snapshot ? coerceNonNegative(snapshot.avgReviewTimeHours) : 0,
onTimeDeliveryRate: snapshot ? coerceDecimal(snapshot.onTimeDeliveryPercentage) / 100 : 0,
languages: snapshot ? Object.keys(snapshot.languages) : [],
organizations: snapshot?.orgsContributedTo ?? [],
topClients: [],
};
}