Skip to content

Commit c258cfb

Browse files
committed
refactor: Improve conditional rendering for organization scope in ExtractSeats.ts;only get the seat informaiton when the scope is organization.
1 parent b100f96 commit c258cfb

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

src/api/ExtractSeats.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,17 @@ export const getSeatsApi = async (): Promise<Seat[]> => {
1111
let seatsData: Seat[] = [];
1212

1313
let response;
14-
if (process.env.VUE_APP_MOCKED_DATA === "true") {
15-
if (process.env.VUE_APP_SCOPE === "organization") {
14+
if (process.env.VUE_APP_SCOPE !== "organization") {
15+
// when the scope is not organization, return seatsData,by default it will return empty array
16+
return seatsData;
17+
}
18+
else{
19+
if (process.env.VUE_APP_MOCKED_DATA === "true") {
1620
response = organizationMockedResponse_seats;
17-
} else if (process.env.VUE_APP_SCOPE === "enterprise") {
18-
response = enterpriseMockedResponse_seats;
19-
} else {
20-
throw new Error(`Invalid VUE_APP_SCOPE value: ${process.env.VUE_APP_SCOPE}. Expected "organization" or "enterprise".`);
21-
}
22-
seatsData = response.seats.map((item: any) => new Seat(item));
23-
} else {
24-
// Fetch the first page
25-
response = await axios.get(`https://api.github.com/orgs/${process.env.VUE_APP_GITHUB_ORG}/copilot/billing/seats`, {
26-
headers: {
27-
Accept: "application/vnd.github+json",
28-
Authorization: `Bearer ${process.env.VUE_APP_GITHUB_TOKEN}`,
29-
"X-GitHub-Api-Version": "2022-11-28",
30-
},
31-
params: {
32-
per_page: perPage,
33-
page: page
34-
}
35-
});
36-
37-
seatsData = seatsData.concat(response.data.seats.map((item: any) => new Seat(item)));
38-
39-
// Calculate the total pages
40-
const totalSeats = response.data.total_seats;
41-
const totalPages = Math.ceil(totalSeats / perPage);
42-
43-
// Fetch the remaining pages
44-
for (page = 2; page <= totalPages; page++) {
21+
seatsData = seatsData.concat(response.seats.map((item: any) => new Seat(item)));
22+
}
23+
else if (process.env.VUE_APP_MOCKED_DATA === "false") {
24+
// Fetch the first page to get the total number of seats
4525
response = await axios.get(`https://api.github.com/orgs/${process.env.VUE_APP_GITHUB_ORG}/copilot/billing/seats`, {
4626
headers: {
4727
Accept: "application/vnd.github+json",
@@ -55,8 +35,27 @@ export const getSeatsApi = async (): Promise<Seat[]> => {
5535
});
5636

5737
seatsData = seatsData.concat(response.data.seats.map((item: any) => new Seat(item)));
58-
}
59-
}
60-
38+
// Calculate the total pages
39+
const totalSeats = response.data.total_seats;
40+
const totalPages = Math.ceil(totalSeats / perPage);
41+
42+
// Fetch the remaining pages
43+
for (page = 2; page <= totalPages; page++) {
44+
response = await axios.get(`https://api.github.com/orgs/${process.env.VUE_APP_GITHUB_ORG}/copilot/billing/seats`, {
45+
headers: {
46+
Accept: "application/vnd.github+json",
47+
Authorization: `Bearer ${process.env.VUE_APP_GITHUB_TOKEN}`,
48+
"X-GitHub-Api-Version": "2022-11-28",
49+
},
50+
params: {
51+
per_page: perPage,
52+
page: page
53+
}
54+
});
55+
56+
seatsData = seatsData.concat(response.data.seats.map((item: any) => new Seat(item)));
57+
} //end of else if (process.env.VUE_APP_MOCKED_DATA === "false")
58+
} //end of else if (process.env.VUE_APP_SCOPE !== "organization")
6159
return seatsData;
60+
}
6261
}

0 commit comments

Comments
 (0)