Skip to content

Commit 16f40d6

Browse files
authored
feat: add getAccountsOrgs and putAccountsOrgsDefault to api layer (#5197)
1 parent cc6775c commit 16f40d6

File tree

1 file changed

+69
-8
lines changed

1 file changed

+69
-8
lines changed

src/identity/apis/auth.ts

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// Functions calling API
22
import {
33
getAccount,
4+
getAccountsOrgs,
45
getIdentity,
56
getMe as getMeQuartz,
67
getOrg,
78
getOrgs,
89
putOrgsDefault,
910
putAccountsDefault,
11+
putAccountsOrgsDefault,
1012
Account,
1113
Identity,
1214
IdentityAccount,
@@ -31,12 +33,12 @@ import {CLOUD} from 'src/shared/constants'
3133
// Types
3234
import {RemoteDataState} from 'src/types'
3335

34-
// These are optional properties of the current account which are not retrieved from identity.
36+
// Additional properties of the current account, which are not retrieved from /quartz/identity.
3537
export interface CurrentAccount extends IdentityAccount {
3638
billingProvider?: 'zuora' | 'aws' | 'gcm' | 'azure'
3739
}
3840

39-
// Optional properties of the current org, which are not retrieved from identity.
41+
// Additional properties of the current org, which are not retrieved from /quartz/identity.
4042
export interface CurrentOrg {
4143
id: string
4244
clusterHost: string
@@ -70,6 +72,7 @@ export enum NetworkErrorTypes {
7072
UnauthorizedError = 'UnauthorizedError',
7173
NotFoundError = 'NotFoundError',
7274
ServerError = 'ServerError',
75+
UnprocessableEntity = 'UnprocessableEntity',
7376
GenericError = 'GenericError',
7477
}
7578

@@ -89,6 +92,14 @@ export class NotFoundError extends Error {
8992
}
9093
}
9194

95+
// 422 error
96+
export class UnprocessableEntityError extends Error {
97+
constructor(message) {
98+
super(message)
99+
this.name = 'UnprocessableEntityError'
100+
}
101+
}
102+
92103
// 500 error
93104
export class ServerError extends Error {
94105
constructor(message) {
@@ -153,7 +164,7 @@ export const fetchQuartzMe = async (): Promise<MeQuartz> => {
153164
return user
154165
}
155166

156-
// fetch user identity from /me (used in OSS and environments without Quartz)
167+
// fetch user identity from IDPE /me (used in OSS and environments without Quartz)
157168
export const fetchLegacyIdentity = async (): Promise<UserResponseIdpe> => {
158169
const response = await getMeIdpe({})
159170

@@ -170,7 +181,7 @@ export const fetchLegacyIdentity = async (): Promise<UserResponseIdpe> => {
170181
return user
171182
}
172183

173-
// fetch details about user's current account
184+
// fetch details about one of the user's accounts
174185
export const fetchAccountDetails = async (
175186
accountId: string | number
176187
): Promise<Account> => {
@@ -206,10 +217,10 @@ export const updateDefaultQuartzAccount = async (
206217
throw new ServerError(response.data.message)
207218
}
208219

209-
// success status code is 204; no data in response.body is expected.
220+
// success status code is 204; no response body expected.
210221
}
211222

212-
// fetch details about user's current organization
223+
// fetch details about one of the user's organizations
213224
export const fetchOrgDetails = async (orgId: string): Promise<Organization> => {
214225
const response = await getOrg({orgId})
215226

@@ -225,7 +236,7 @@ export const fetchOrgDetails = async (orgId: string): Promise<Organization> => {
225236
return orgDetails
226237
}
227238

228-
// fetch list of user's current organizations
239+
// fetch the list of organizations in the user's currently active account
229240
export const fetchQuartzOrgs = async (): Promise<OrganizationSummaries> => {
230241
const response = await getOrgs({})
231242

@@ -240,7 +251,7 @@ export const fetchQuartzOrgs = async (): Promise<OrganizationSummaries> => {
240251
return response.data
241252
}
242253

243-
// change default organization for a given account
254+
// change the default organization for the user's currently active account
244255
export const updateDefaultQuartzOrg = async (orgId: string) => {
245256
const response = await putOrgsDefault({
246257
data: {
@@ -255,3 +266,53 @@ export const updateDefaultQuartzOrg = async (orgId: string) => {
255266

256267
return response.data
257268
}
269+
270+
// fetch the list of organizations associated with a given account ID
271+
export const fetchOrgsByAccountID = async (
272+
accountNum: number
273+
): Promise<OrganizationSummaries> => {
274+
const accountId = accountNum.toString()
275+
276+
const response = await getAccountsOrgs({
277+
accountId,
278+
})
279+
280+
if (response.status === 401) {
281+
throw new UnauthorizedError(response.data.message)
282+
}
283+
284+
if (response.status === 500) {
285+
throw new ServerError(response.data.message)
286+
}
287+
288+
return response.data
289+
}
290+
291+
// update the default org for a given account
292+
export const updateDefaultOrgByAccountID = async ({
293+
accountNum,
294+
orgId,
295+
}): Promise<void> => {
296+
const accountId = accountNum.toString()
297+
298+
const response = await putAccountsOrgsDefault({
299+
accountId,
300+
data: {
301+
id: orgId,
302+
},
303+
})
304+
305+
if (response.status === 404) {
306+
throw new NotFoundError(response.data.message)
307+
}
308+
309+
if (response.status === 422) {
310+
throw new UnprocessableEntityError(response.data.message)
311+
}
312+
313+
if (response.status === 500) {
314+
throw new ServerError(response.data.message)
315+
}
316+
317+
// success status code is 204; no response body expected.
318+
}

0 commit comments

Comments
 (0)