@@ -509,6 +509,7 @@ function Journal() {
509509 let happiness = getDoFactionUpdate . happiness
510510 let doUpdate = getDoFactionUpdate . doUpdate
511511 let doUpdateTime = getDoFactionUpdate . doUpdateTime
512+ let lastHistoryRecord = getDoFactionUpdate . lastHistoryRecord
512513 let factionPresence = [ ]
513514 if ( doUpdate || doUpdateTime ) {
514515 // If doUpdateTime is set to false set the updated at time
@@ -573,6 +574,40 @@ function Journal() {
573574 }
574575 // Do the actual db operation
575576 await this . setFactionRecord ( factionObject . name_lower , factionObject )
577+ } else {
578+ if ( lastHistoryRecord . updated_at > factionPresenceUpdatedAt ) {
579+ let factionPresentSystemObject = { }
580+ factionPresence = factionObject . faction_presence
581+
582+ // Faction presence can be null when a basic record is created
583+ if ( ! factionPresence ) {
584+ factionPresence = [ ]
585+ }
586+
587+ factionPresence . forEach ( ( factionPresenceObject , index , factionPresenceArray ) => {
588+ if ( factionPresenceObject . system_id . equals ( lastHistoryRecord . system_id ) ) {
589+ // Iterates over all existing faction presences to create a new faction presence object for the current faction
590+ // This new object is then reapplied over the existing array element to update it
591+ factionPresentSystemObject = {
592+ system_name : lastHistoryRecord . system ,
593+ system_name_lower : lastHistoryRecord . system_lower ,
594+ system_id : lastHistoryRecord . system_id ,
595+ state : lastHistoryRecord . state ,
596+ influence : lastHistoryRecord . influence ,
597+ happiness : lastHistoryRecord . happiness ,
598+ active_states : lastHistoryRecord . active_states ,
599+ pending_states : lastHistoryRecord . pending_states ,
600+ recovering_states : lastHistoryRecord . recovering_states ,
601+ conflicts : lastHistoryRecord . conflicts ,
602+ updated_at : lastHistoryRecord . updated_at
603+ }
604+ factionPresenceArray [ index ] = factionPresentSystemObject
605+ }
606+ } )
607+
608+ factionObject . faction_presence = factionPresence
609+ await this . setFactionRecord ( factionObject . name_lower , factionObject )
610+ }
576611 }
577612 if ( doUpdate ) {
578613 // Create the faction history element for storing current systems
@@ -1055,6 +1090,7 @@ function Journal() {
10551090 } )
10561091 }
10571092 let factionName = dbFaction . name_lower
1093+ let lastHistoryRecord = { }
10581094 // Form the conflicts array
10591095 let conflicts = [ ]
10601096 if ( message . Conflicts ) {
@@ -1142,6 +1178,7 @@ function Journal() {
11421178 } )
11431179 . sort ( { updated_at : - 1 } )
11441180 . lean ( )
1181+ lastHistoryRecord = factionHistory . length > 0 ? factionHistory [ 0 ] : { }
11451182 // Check if the incoming details is the same as any record present in the last 2 days
11461183 // This prevents caching issues
11471184 if (
@@ -1162,7 +1199,16 @@ function Journal() {
11621199
11631200 // doUpdate indicate if the new record should be added into the history and the master record data updated
11641201 // doUpdateTime indicate if the master record's update time should be updated
1165- return { activeStates, pendingStates, recoveringStates, conflicts, happiness, doUpdate, doUpdateTime }
1202+ return {
1203+ activeStates,
1204+ pendingStates,
1205+ recoveringStates,
1206+ conflicts,
1207+ happiness,
1208+ doUpdate,
1209+ doUpdateTime,
1210+ lastHistoryRecord
1211+ }
11661212 }
11671213
11681214 // Used in V3 and V4
0 commit comments