@@ -7,68 +7,107 @@ import {
77 updateCampaign as updateCampaignService ,
88 deleteCampaign as deleteCampaignService
99} from '@/services/campaignService' ;
10+ import { getMap } from '@/services/mapService' ;
11+ import { getSteading } from '@/services/steadingService' ;
12+ import { getFront } from '@/services/frontService' ;
1013
1114export const useCampaignStore = defineStore ( 'campaigns' , {
1215 state : ( ) => ( {
1316 campaigns : [ ] as Campaign [ ] ,
1417 selectedCampaign : null as Campaign | null ,
18+ linkedSteadings : [ ] as any [ ] ,
19+ linkedFronts : [ ] as any [ ] ,
1520 } ) ,
1621 actions : {
17- async fetchCampaigns ( userId : string ) {
18- try {
19- this . campaigns = await getCampaigns ( userId ) ;
20- } catch ( error ) {
21- console . error ( 'Failed to fetch campaigns' , error ) ;
22+ async fetchCampaigns ( userId : string ) : Promise < true | string > {
23+ const result = await getCampaigns ( userId ) ;
24+ if ( typeof result === 'string' ) {
25+ console . error ( 'Failed to fetch campaigns' , result ) ;
26+ return result ;
2227 }
28+ this . campaigns = result ;
29+ return true ;
2330 } ,
2431
25- async createCampaign ( campaignData : Partial < Campaign > , userId : string ) {
26- try {
27- const created = await createCampaignService ( {
28- ...campaignData ,
29- userId
30- } as Omit < Campaign , 'id' > ) ;
32+ async createCampaign ( campaignData : Partial < Campaign > , userId : string ) : Promise < true | string > {
33+ const result = await createCampaignService ( {
34+ ...campaignData ,
35+ userId
36+ } as Omit < Campaign , 'id' > ) ;
3137
32- if ( created ) {
33- this . campaigns . push ( created ) ;
34- }
35- } catch ( error ) {
36- console . error ( 'Failed to create campaign' , error ) ;
38+ if ( typeof result === 'string' ) {
39+ console . error ( 'Failed to create campaign' , result ) ;
40+ return result ;
3741 }
42+
43+ this . campaigns . push ( result ) ;
44+ return true ;
3845 } ,
3946
40- async updateCampaign ( campaignId : string , updates : Partial < Campaign > ) {
41- try {
42- const success = await updateCampaignService ( campaignId , updates ) ;
43- if ( success ) {
44- await this . fetchCampaigns ( updates . userId ! ) ; // assumes userId exists in updates
45- }
46- } catch ( error ) {
47- console . error ( 'Failed to update campaign' , error ) ;
47+ async updateCampaign ( campaignId : string , updates : Partial < Campaign > ) : Promise < true | string > {
48+ const result = await updateCampaignService ( campaignId , updates ) ;
49+
50+ if ( typeof result === 'string' ) {
51+ console . error ( 'Failed to update campaign' , result ) ;
52+ return result ;
4853 }
54+
55+ if ( updates . userId ) {
56+ await this . fetchCampaigns ( updates . userId ) ;
57+ }
58+ return true ;
4959 } ,
5060
51- async deleteCampaign ( campaignId : string ) {
52- try {
53- const success = await deleteCampaignService ( campaignId ) ;
54- if ( success ) {
55- this . campaigns = this . campaigns . filter ( c => c . id !== campaignId ) ;
56- }
57- } catch ( error ) {
58- console . error ( 'Failed to delete campaign' , error ) ;
61+ async deleteCampaign ( campaignId : string ) : Promise < true | string > {
62+ const result = await deleteCampaignService ( campaignId ) ;
63+ if ( typeof result === 'string' ) {
64+ console . error ( 'Failed to delete campaign' , result ) ;
65+ return result ;
5966 }
67+ this . campaigns = this . campaigns . filter ( c => c . id !== campaignId ) ;
68+ return true ;
6069 } ,
6170
62- async fetchCampaignById ( id : string ) {
63- try {
64- this . selectedCampaign = await getCampaign ( id ) ;
65- } catch ( error ) {
66- console . error ( 'Failed to fetch campaign by ID' , error ) ;
71+ async fetchCampaignById ( id : string ) : Promise < true | string > {
72+ const result = await getCampaign ( id ) ;
73+ if ( typeof result === 'string' ) {
74+ console . error ( 'Failed to fetch campaign by ID' , result ) ;
75+ return result ;
6776 }
77+ this . selectedCampaign = result ;
78+ await this . loadLinkedEntities ( ) ;
79+ return true ;
80+ } ,
81+
82+ async loadLinkedEntities ( ) {
83+ if ( ! this . selectedCampaign ) return ;
84+ const mapIds = this . selectedCampaign . mapIds || [ ] ;
85+
86+ const loadedMaps = await Promise . all ( mapIds . map ( id => getMap ( id ) ) ) ;
87+
88+ const frontIds = new Set < string > ( ) ;
89+ const steadingIds = new Set < string > ( ) ;
90+
91+ for ( const map of loadedMaps . filter ( Boolean ) ) {
92+ const locations = typeof map ?. locations === 'string' ? JSON . parse ( map . locations ) : map ?. locations ;
93+
94+ for ( const loc of locations || [ ] ) {
95+ if ( loc . steading_id ) steadingIds . add ( loc . steading_id ) ;
96+ if ( Array . isArray ( loc . fronts ) ) loc . fronts . forEach ( ( fid : string ) => frontIds . add ( fid ) ) ;
97+ }
98+ }
99+
100+ const [ steadings , fronts ] = await Promise . all ( [
101+ Promise . all ( Array . from ( steadingIds ) . map ( id => getSteading ( id ) ) ) ,
102+ Promise . all ( Array . from ( frontIds ) . map ( id => getFront ( id ) ) )
103+ ] ) ;
104+
105+ this . linkedSteadings = steadings . filter ( Boolean ) ;
106+ this . linkedFronts = fronts . filter ( Boolean ) ;
68107 } ,
69108
70109 selectCampaign ( id : string ) {
71110 this . selectedCampaign = this . campaigns . find ( c => c . id === id ) || null ;
72111 } ,
73112 } ,
74- } ) ;
113+ } ) ;
0 commit comments