@@ -13,6 +13,12 @@ export interface CachedCaseResponse {
1313 results : CachedCase [ ] ;
1414}
1515
16+ export interface WorksiteChange {
17+ worksite_id : number ;
18+ incident_id : number ;
19+ invalidated_at : string | null ;
20+ }
21+
1622const debug = createDebug ( '@ccu:utils:worksite' ) ;
1723
1824const loadCases = async ( query : Record < string , unknown > ) => {
@@ -57,6 +63,18 @@ const loadCasesPaginated = async (query: Record<string, unknown>) => {
5763 } ;
5864} ;
5965
66+ const loadWorksiteChanges = async (
67+ since : string ,
68+ ) : Promise < WorksiteChange [ ] > => {
69+ const response = await axios . get < WorksiteChange [ ] > (
70+ `${ import . meta. env . VITE_APP_API_BASE_URL } /worksites_changes` ,
71+ {
72+ params : { since } ,
73+ } ,
74+ ) ;
75+ return response . data ;
76+ } ;
77+
6078const loadUserLocations = async ( query : Record < string , unknown > ) => {
6179 const response = await axios . get (
6280 `${ import . meta. env . VITE_APP_API_BASE_URL } /user_geo_locations/latest_locations` ,
@@ -89,26 +107,28 @@ const loadCasesCached = async (query: Record<string, unknown>) => {
89107 const casesReconciledAt = ( ( await DbService . getItem ( cacheKeys . RECONCILED ) ) ||
90108 moment ( ) . toISOString ( ) ) as string ; // ISO date string
91109 if ( cachedCases ) {
92- const [ response , reconciliationResponse ] = await Promise . all ( [
110+ const [ response , worksiteChanges ] = await Promise . all ( [
93111 loadCases ( {
94112 ...query ,
95113 updated_at__gt : casesUpdatedAt ,
96114 } ) ,
97- loadCases ( {
98- updated_at__gt : casesReconciledAt ,
99- fields : 'id,incident' ,
100- } ) ,
115+ loadWorksiteChanges ( casesReconciledAt ) ,
101116 ] ) ;
102117
103- for ( const element of reconciliationResponse . results ) {
118+ for ( const change of worksiteChanges ) {
104119 const itemIndex = cachedCases . results . findIndex (
105- ( o ) => o . id === element . id ,
120+ ( o ) => o . id === change . worksite_id ,
106121 ) ;
107- if (
108- itemIndex > - 1 &&
109- element . incident !== cachedCases . results [ itemIndex ] . incident
110- ) {
111- cachedCases . results . splice ( itemIndex , 1 ) ;
122+ if ( itemIndex > - 1 ) {
123+ if ( change . invalidated_at !== null ) {
124+ // Remove invalidated worksites
125+ cachedCases . results . splice ( itemIndex , 1 ) ;
126+ } else if (
127+ change . incident_id !== cachedCases . results [ itemIndex ] . incident
128+ ) {
129+ // Remove worksites moved to different incidents
130+ cachedCases . results . splice ( itemIndex , 1 ) ;
131+ }
112132 }
113133 }
114134
@@ -172,27 +192,29 @@ const loadCasesCachedWithPagination = async (
172192 moment ( ) . toISOString ( ) ) as string ; // ISO date string
173193
174194 if ( cachedCases ) {
175- const [ response , reconciliationResponse ] = await Promise . all ( [
195+ const [ response , worksiteChanges ] = await Promise . all ( [
176196 loadCasesPaginated ( {
177197 ...query ,
178198 updated_at__gt : casesUpdatedAt ,
179199 } ) ,
180- loadCasesPaginated ( {
181- updated_at__gt : casesReconciledAt ,
182- fields : 'id,incident' ,
183- } ) ,
200+ loadWorksiteChanges ( casesReconciledAt ) ,
184201 ] ) ;
185202
186203 // Reconcile data
187- for ( const element of reconciliationResponse . results ) {
204+ for ( const change of worksiteChanges ) {
188205 const itemIndex = cachedCases . results . findIndex (
189- ( o ) => o . id === element . id ,
206+ ( o ) => o . id === change . worksite_id ,
190207 ) ;
191- if (
192- itemIndex > - 1 &&
193- element . incident !== cachedCases . results [ itemIndex ] . incident
194- ) {
195- cachedCases . results . splice ( itemIndex , 1 ) ;
208+ if ( itemIndex > - 1 ) {
209+ if ( change . invalidated_at !== null ) {
210+ // Remove invalidated worksites
211+ cachedCases . results . splice ( itemIndex , 1 ) ;
212+ } else if (
213+ change . incident_id !== cachedCases . results [ itemIndex ] . incident
214+ ) {
215+ // Remove worksites moved to different incidents
216+ cachedCases . results . splice ( itemIndex , 1 ) ;
217+ }
196218 }
197219 }
198220
@@ -445,4 +467,5 @@ export {
445467 loadCases ,
446468 loadCaseImagesCached ,
447469 loadUserLocations ,
470+ loadWorksiteChanges ,
448471} ;
0 commit comments