File size: 7,350 Bytes
930e4a4
dcc1b84
 
 
930e4a4
 
 
 
 
9d45e6c
6624dea
 
 
 
930e4a4
 
 
 
 
 
 
 
 
 
 
0360fd2
930e4a4
 
 
 
0360fd2
 
 
 
 
 
 
 
 
 
 
930e4a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0360fd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
930e4a4
 
 
 
0360fd2
930e4a4
 
 
0360fd2
 
 
 
 
 
 
 
 
930e4a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d45e6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6624dea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcc1b84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import type {
  ConfidenceGroup,
  ConfidenceRatingCreate,
  ConfidenceRatingEntry,
  ExtractionRecordCreate,
  ExtractionRecordEntry,
  PrismaCounts,
  Protocol,
  ProtocolDraft,
  ReviewOptions,
  RobAssessmentCreate,
  RobAssessmentEntry,
  RobReference,
  RobSummaryItem,
  ScreeningRecordCreate,
  ScreeningRecordEntry,
  SearchRun,
  SynthesisResult,
} from "../types";

const BASE = "/api/v1/protocols";

async function handle<T>(resp: Response): Promise<T> {
  if (!resp.ok) {
    const body = await resp.json().catch(() => ({}));
    throw new Error(formatDetail(body.detail) || `Request failed (${resp.status})`);
  }
  return resp.json();
}

/** FastAPI returns validation errors as a list of objects — make them readable. */
function formatDetail(detail: unknown): string {
  if (typeof detail === "string") return detail;
  if (Array.isArray(detail)) {
    return detail
      .map((d) => (d && typeof d === "object" && "msg" in d ? String((d as { msg: unknown }).msg) : String(d)))
      .join("; ");
  }
  return "";
}

export async function listProtocols(): Promise<Protocol[]> {
  return handle(await fetch(BASE));
}

export async function getProtocol(id: string): Promise<Protocol> {
  return handle(await fetch(`${BASE}/${id}`));
}

export async function createProtocol(draft: ProtocolDraft): Promise<Protocol> {
  return handle(
    await fetch(BASE, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(draft),
    })
  );
}

export async function updateProtocol(
  id: string,
  updates: Partial<ProtocolDraft>
): Promise<Protocol> {
  return handle(
    await fetch(`${BASE}/${id}`, {
      method: "PATCH",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(updates),
    })
  );
}

export async function lockProtocol(id: string): Promise<Protocol> {
  return handle(await fetch(`${BASE}/${id}/lock`, { method: "POST" }));
}

/** Documented change to a locked protocol: only the changed fields plus a reason. */
export async function amendProtocol(
  id: string,
  changes: Partial<ProtocolDraft>,
  reason: string
): Promise<Protocol> {
  return handle(
    await fetch(`${BASE}/${id}/amend`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ ...changes, reason }),
    })
  );
}

export async function deleteProtocol(id: string): Promise<void> {
  const resp = await fetch(`${BASE}/${id}`, { method: "DELETE" });
  if (!resp.ok) {
    const body = await resp.json().catch(() => ({}));
    throw new Error(formatDetail(body.detail) || `Request failed (${resp.status})`);
  }
}

/** First run needs no reason; re-runs require one (kept in the search history). */
export async function runSearch(protocolId: string, reason?: string): Promise<SearchRun> {
  return handle(
    await fetch(`${BASE}/${protocolId}/search`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ reason: reason ?? null }),
    })
  );
}

export async function getSearchRun(protocolId: string): Promise<SearchRun | null> {
  const resp = await fetch(`${BASE}/${protocolId}/search`);
  if (resp.status === 404) return null;
  return handle(resp);
}

export async function recordScreeningDecision(
  protocolId: string,
  payload: ScreeningRecordCreate
): Promise<ScreeningRecordEntry> {
  return handle(
    await fetch(`${BASE}/${protocolId}/screening`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload),
    })
  );
}

export async function listScreeningRecords(
  protocolId: string
): Promise<ScreeningRecordEntry[]> {
  return handle(await fetch(`${BASE}/${protocolId}/screening`));
}

export async function recordExtraction(
  protocolId: string,
  payload: ExtractionRecordCreate
): Promise<ExtractionRecordEntry> {
  return handle(
    await fetch(`${BASE}/${protocolId}/extraction`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload),
    })
  );
}

export async function listExtractionRecords(
  protocolId: string
): Promise<ExtractionRecordEntry[]> {
  return handle(await fetch(`${BASE}/${protocolId}/extraction`));
}

export async function getPrismaCounts(protocolId: string): Promise<PrismaCounts | null> {
  const resp = await fetch(`${BASE}/${protocolId}/prisma`);
  if (resp.status === 404) return null;
  return handle(resp);
}

export async function generateSynthesis(protocolId: string): Promise<SynthesisResult> {
  return handle(await fetch(`${BASE}/${protocolId}/synthesis`, { method: "POST" }));
}

export async function getSynthesis(protocolId: string): Promise<SynthesisResult | null> {
  const resp = await fetch(`${BASE}/${protocolId}/synthesis`);
  if (resp.status === 404) return null;
  return handle(resp);
}
// --- Review type / domain dropdown options ---

let reviewOptionsCache: Promise<ReviewOptions> | null = null;

/** Dropdown options from the backend (fetched once per page load). */
export function getReviewOptions(): Promise<ReviewOptions> {
  if (!reviewOptionsCache) {
    reviewOptionsCache = fetch("/api/v1/review-options")
      .then((resp) => handle<ReviewOptions>(resp))
      .catch((err) => {
        reviewOptionsCache = null;
        throw err;
      });
  }
  return reviewOptionsCache;
}

// --- Risk of bias (OHAT) ---

let robReferenceCache: Promise<RobReference> | null = null;
let robReferenceValue: RobReference | null = null;

/** The OHAT questions, designs and ratings (fetched once per page load). */
export function getRobReference(): Promise<RobReference> {
  if (!robReferenceCache) {
    robReferenceCache = fetch("/api/v1/rob/reference")
      .then((resp) => handle<RobReference>(resp))
      .then((ref) => {
        robReferenceValue = ref;
        return ref;
      })
      .catch((err) => {
        robReferenceCache = null;
        throw err;
      });
  }
  return robReferenceCache;
}

/** The reference data if it has already loaded (lets components render immediately). */
export function peekRobReference(): RobReference | null {
  return robReferenceValue;
}

export async function listRobAssessments(protocolId: string): Promise<RobAssessmentEntry[]> {
  return handle(await fetch(`${BASE}/${protocolId}/rob`));
}

export async function getRobSummary(protocolId: string): Promise<RobSummaryItem[]> {
  return handle(await fetch(`${BASE}/${protocolId}/rob/summary`));
}

export async function recordRobAssessment(
  protocolId: string,
  payload: RobAssessmentCreate
): Promise<RobAssessmentEntry> {
  return handle(
    await fetch(`${BASE}/${protocolId}/rob`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload),
    })
  );
}

// --- Confidence in the body of evidence (OHAT) ---

export async function listConfidenceGroups(protocolId: string): Promise<ConfidenceGroup[]> {
  return handle(await fetch(`${BASE}/${protocolId}/confidence`));
}

export async function rateConfidence(
  protocolId: string,
  payload: ConfidenceRatingCreate
): Promise<ConfidenceRatingEntry> {
  return handle(
    await fetch(`${BASE}/${protocolId}/confidence`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload),
    })
  );
}