From 28ec1c3d675a606f653c055821715314dfcf2f4a Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Tue, 14 Apr 2026 19:05:10 +0000 Subject: [PATCH] Get latest syncs from the API --- platform/type.go | 27 --------------------- platform/types/address.go | 4 ++-- resource/session.go | 2 ++ ts/store/sync.ts | 50 +++++++++++++++++++++++++++++++++++++++ ts/type/api.ts | 29 +++++++++++++++++++++++ ts/view/Home.vue | 7 +++++- 6 files changed, 89 insertions(+), 30 deletions(-) delete mode 100644 platform/type.go create mode 100644 ts/store/sync.ts diff --git a/platform/type.go b/platform/type.go deleted file mode 100644 index 1a9495ad..00000000 --- a/platform/type.go +++ /dev/null @@ -1,27 +0,0 @@ -package platform - -import ( - "time" - - "github.com/Gleipnir-Technology/nidus-sync/db/models" - "github.com/Gleipnir-Technology/nidus-sync/platform/types" -) - -type Location = types.Location - -type ClientSync struct { - Fieldseeker FieldseekerRecordsSync - Since time.Time -} - -type FieldseekerRecordsSync struct { - MosquitoSources []MosquitoSource - ServiceRequests models.FieldseekerServicerequestSlice - TrapData models.FieldseekerTraplocationSlice -} - -type MosquitoSource struct { - PointLocation models.FieldseekerPointlocation - Inspections models.FieldseekerMosquitoinspectionSlice - Treatments models.FieldseekerTreatmentSlice -} diff --git a/platform/types/address.go b/platform/types/address.go index 682b6e9a..1d0d7354 100644 --- a/platform/types/address.go +++ b/platform/types/address.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/Gleipnir-Technology/nidus-sync/db/models" - "github.com/rs/zerolog/log" + //"github.com/rs/zerolog/log" ) type Address struct { @@ -25,7 +25,7 @@ func (a Address) String() string { return fmt.Sprintf("%s %s, %s, %s, %s, %s", a.Number, a.Street, a.Locality, a.Region, a.PostalCode, a.Country) } func AddressFromModel(m *models.Address) Address { - log.Debug().Int32("id", m.ID).Float64("lat", m.LocationLatitude.GetOr(0.0)).Float64("lng", m.LocationLongitude.GetOr(0.0)).Msg("converting address") + //log.Debug().Int32("id", m.ID).Float64("lat", m.LocationLatitude.GetOr(0.0)).Float64("lng", m.LocationLongitude.GetOr(0.0)).Msg("converting address") return Address{ Country: m.Country, GID: m.Gid, diff --git a/resource/session.go b/resource/session.go index ee978a92..25597fc8 100644 --- a/resource/session.go +++ b/resource/session.go @@ -52,6 +52,7 @@ type sessionURLAPI struct { PublicreportMessage string `json:"publicreport_message"` ReviewTask string `json:"review_task"` Signal string `json:"signal"` + Sync string `json:"sync"` Upload string `json:"upload"` User string `json:"user"` } @@ -96,6 +97,7 @@ func (res *sessionR) Get(ctx context.Context, r *http.Request, user platform.Use PublicreportMessage: urls.API.Publicreport.Message, ReviewTask: config.MakeURLNidus("/api/review-task"), Signal: config.MakeURLNidus("/api/signal"), + Sync: config.MakeURLNidus("/api/sync"), Upload: config.MakeURLNidus("/api/upload"), User: config.MakeURLNidus("/api/user"), }, diff --git a/ts/store/sync.ts b/ts/store/sync.ts new file mode 100644 index 00000000..12c26a81 --- /dev/null +++ b/ts/store/sync.ts @@ -0,0 +1,50 @@ +import { defineStore } from "pinia"; +import { ref } from "vue"; +import { Sync } from "@/type/api"; +import { SSEManager, SSEMessage } from "@/SSEManager"; +import { useSessionStore } from "@/store/session"; + +export const useStoreSync = defineStore("sync", () => { + // State + const all = ref(null); + const loading = ref(false); + const error = ref(null); + + // Subscription + SSEManager.subscribe((msg: SSEMessage) => { + if (msg.resource.startsWith("sync:sync")) { + fetchAll(); + } + }); + // Actions + async function fetchAll(): Promise { + const session = useSessionStore(); + if (session.urls == null) { + throw new Error("can't fetch without user URL data"); + } + + loading.value = true; + error.value = null; + try { + const params = new URLSearchParams(); + + const response = await fetch(`${session.urls.api.sync}?${params}`); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = (await response.json()) as Sync[]; + all.value = data; + return data; + } catch (err) { + console.error("Error loading communications:", err); + throw err; + } + } + return { + // State + all, + // Actions + fetchAll, + }; +}); diff --git a/ts/type/api.ts b/ts/type/api.ts index 2928018c..959a1739 100644 --- a/ts/type/api.ts +++ b/ts/type/api.ts @@ -637,6 +637,34 @@ export interface Session { self: User; urls: URLs; } +export interface SyncDTO { + created: Date; + id: string; + organization: string; + records_created: number; + records_unchanged: number; + records_updated: number; +} +export class Sync { + constructor( + public created: Date, + public id: string, + public organization: string, + public records_created: number, + public records_unchanged: number, + public records_updated: number, + ) {} + static fromJSON(json: SyncDTO): Sync { + return new Sync( + new Date(json.created), + json.id, + json.organization, + json.records_created, + json.records_unchanged, + json.records_updated, + ); + } +} export interface URLs { api: URLsAPI; tegola: string; @@ -650,6 +678,7 @@ interface URLsAPI { publicreport_message: string; review_task: string; signal: string; + sync: string; upload: string; user: string; } diff --git a/ts/view/Home.vue b/ts/view/Home.vue index 7e9e419b..4126df6a 100644 --- a/ts/view/Home.vue +++ b/ts/view/Home.vue @@ -159,6 +159,7 @@ import { onMounted, reactive } from "vue"; import MapAggregate from "@/components/MapAggregate.vue"; import { formatBigNumber, formatTimeRelative } from "@/format"; import { useSessionStore } from "@/store/session"; +import { useStoreSync } from "@/store/sync"; import type { Bounds } from "@/type/api"; const dashboard = reactive({ @@ -180,8 +181,12 @@ const dashboard = reactive({ }, recentRequests: [], }); +const storeSync = useStoreSync(); const session = useSessionStore(); -onMounted(async () => {}); +onMounted(async () => { + const syncs = await storeSync.fetchAll(); + console.log("syncs", syncs); +}); function mapBounds(): Bounds | undefined { if (session.organization?.service_area) { return session.organization?.service_area;