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
159 lines (145 loc) · 4.26 KB
/
Copy pathadapters.ts
File metadata and controls
159 lines (145 loc) · 4.26 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
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;
}
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;
}
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,
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: [],
};
}