11import { Dispatch } from 'react'
22
33// API
4- import { fetchQuartzOrgs , updateDefaultQuartzOrg } from 'src/identity/apis/auth'
4+ import {
5+ fetchOrgsByAccountID ,
6+ updateDefaultOrgByAccountID ,
7+ } from 'src/identity/apis/auth'
58
69// Actions
710import {
@@ -13,73 +16,92 @@ import {
1316import { PublishNotificationAction } from 'src/shared/actions/notifications'
1417
1518// Types
16- import { GetState , RemoteDataState } from 'src/types'
19+ import { AppThunk , GetState , RemoteDataState } from 'src/types'
1720import { OrganizationSummaries } from 'src/client/unityRoutes'
18-
1921type Actions = QuartzOrganizationActions | PublishNotificationAction
2022type DefaultOrg = OrganizationSummaries [ number ]
2123
22- class OrgNotFoundError extends Error {
24+ interface UpdateOrgParams {
25+ accountId : number
26+ newDefaultOrg : DefaultOrg
27+ }
28+
29+ // Utils
30+ import { reportErrorThroughHoneyBadger } from 'src/shared/utils/errors'
31+
32+ export enum OrganizationThunkErrors {
33+ DefaultOrgStateError = 'DefaultOrgStateError' ,
34+ DefaultOrgNetworkError = 'DefaultOrgNetworkError' ,
35+ }
36+
37+ export class DefaultOrgStateError extends Error {
2338 constructor ( message ) {
2439 super ( message )
25- this . name = 'DefaultOrgNotFoundError'
40+ this . name = OrganizationThunkErrors . DefaultOrgStateError
2641 }
2742}
2843
29- // Error Reporting
30- import { reportErrorThroughHoneyBadger } from 'src/shared/utils/errors'
44+ export class DefaultOrgNetworkError extends Error {
45+ constructor ( message ) {
46+ super ( message )
47+ this . name = OrganizationThunkErrors . DefaultOrgNetworkError
48+ }
49+ }
3150
32- export const getQuartzOrganizationsThunk = ( ) => async (
51+ // Thunks
52+ export const getQuartzOrganizationsThunk = ( accountId : number ) => async (
3353 dispatch : Dispatch < Actions > ,
3454 getState : GetState
3555) => {
3656 try {
3757 dispatch ( setQuartzOrganizationsStatus ( RemoteDataState . Loading ) )
38- const quartzOrganizations = await fetchQuartzOrgs ( )
58+
59+ const quartzOrganizations = await fetchOrgsByAccountID ( accountId )
3960
4061 dispatch ( setQuartzOrganizations ( quartzOrganizations ) )
4162 } catch ( err ) {
42- dispatch ( setQuartzOrganizationsStatus ( RemoteDataState . Error ) )
43-
4463 reportErrorThroughHoneyBadger ( err , {
4564 name : 'Failed to fetch /quartz/orgs/' ,
4665 context : { state : getState ( ) } ,
4766 } )
4867 }
4968}
5069
51- export const updateDefaultOrgThunk = ( newDefaultOrg : DefaultOrg ) => async (
70+ export const updateDefaultOrgThunk = ( {
71+ accountId,
72+ newDefaultOrg,
73+ } : UpdateOrgParams ) : AppThunk < Promise < void > > => async (
5274 dispatch : Dispatch < Actions > ,
5375 getState : GetState
54- ) => {
76+ ) : Promise < void > => {
5577 try {
5678 dispatch ( setQuartzOrganizationsStatus ( RemoteDataState . Loading ) )
5779
58- await updateDefaultQuartzOrg ( newDefaultOrg . id )
80+ await updateDefaultOrgByAccountID ( {
81+ accountNum : accountId ,
82+ orgId : newDefaultOrg . id ,
83+ } )
5984
6085 dispatch ( setQuartzDefaultOrg ( newDefaultOrg . id ) )
6186
6287 const state = getState ( )
6388 const orgStatus = state . identity . currentIdentity . status
6489
6590 if ( orgStatus === RemoteDataState . Error ) {
66- const defaultOrgErrMsg =
67- 'quartzOrganizations state does not contain requested default organization'
68-
69- reportErrorThroughHoneyBadger ( new OrgNotFoundError ( defaultOrgErrMsg ) , {
70- name : defaultOrgErrMsg ,
71- context : {
72- org : newDefaultOrg ,
73- state : getState ( ) ,
74- } ,
75- } )
91+ throw new DefaultOrgStateError (
92+ OrganizationThunkErrors . DefaultOrgStateError
93+ )
7694 }
7795 } catch ( err ) {
78- dispatch ( setQuartzOrganizationsStatus ( RemoteDataState . Error ) )
79-
8096 reportErrorThroughHoneyBadger ( err , {
81- name : 'Failed to update /quartz/orgs/default' ,
82- context : { state : getState ( ) } ,
97+ name : err . name ,
98+ context : {
99+ message : err . message ,
100+ org : newDefaultOrg ,
101+ state : getState ( ) ,
102+ } ,
83103 } )
104+
105+ throw Error ( err )
84106 }
85107}
0 commit comments