Skip to content

Commit d02b723

Browse files
author
Gabriele Panico
committed
added: ProjectUser interface and refactored project retrieval logic
1 parent 61f11ba commit d02b723

File tree

2 files changed

+84
-41
lines changed

2 files changed

+84
-41
lines changed

src/app/components/project-item/project-item.component.ts

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { AppConfigProvider } from 'src/app/services/app-config';
1515
import { ConvertRequestToConversation } from 'src/chat21-core/utils/convertRequestToConversation';
1616
import { compareValues } from 'src/chat21-core/utils/utils';
1717
import { ProjectService } from 'src/app/services/projects/project.service';
18+
import { ProjectUser } from 'src/chat21-core/models/project_user';
19+
import { Project } from 'src/chat21-core/models/projects';
1820

1921
@Component({
2022
selector: 'app-project-item',
@@ -164,63 +166,68 @@ export class ProjectItemComponent implements OnInit {
164166
}
165167
}
166168

167-
getLastProjectStoredAndSubscToWSAvailabilityAndConversations() {
168-
let stored_project = ''
169+
getStoredProject(): ProjectUser | null {
169170
try {
170-
stored_project = localStorage.getItem('last_project')
171-
this.logger.log('PROJECT-ITEM - THERE IS A STORED PROJECT ', stored_project)
171+
const raw = localStorage.getItem('last_project');
172+
173+
if (!raw) {
174+
return null;
175+
}
176+
177+
const parsed = JSON.parse(raw);
178+
179+
if (this.isValidStoredProject(parsed)) {
180+
return parsed;
181+
}
182+
183+
// modello sbagliato → pulizia
184+
this.logger.warn('[PROJECT-ITEM] Invalid stored project schema, clearing storage');
185+
localStorage.removeItem('last_project');
186+
return null;
187+
172188
} catch (err) {
173-
this.logger.error('Get local storage LAST PROJECT ', err)
189+
this.logger.error('[PROJECT-ITEM] Error parsing stored project', err);
190+
localStorage.removeItem('last_project');
191+
return null;
174192
}
193+
}
194+
195+
getLastProjectStoredAndSubscToWSAvailabilityAndConversations() {
175196

197+
let stored_project = this.getStoredProject();
176198

177-
if (!stored_project || stored_project === 'undefined') {
178-
this.logger.log('PROJECT-ITEM - THERE IS NOT STORED LAST PROJECT OR IS UNDEFINED ', stored_project)
199+
if (!stored_project) {
200+
this.logger.log('[PROJECT-ITEM] No valid stored project, fetching remote');
179201
this.projectService.getProjects().subscribe(projects => {
180-
this.logger.log('[PROJECT-ITEM - GET PROJECTS - RES', projects);
181-
182-
this.logger.log('[INFO-CONTENT-COMP] - GET PROJECTS - RES this.project', this.project);
183-
184-
if(this.projectID){
185-
const project = projects.find(prjct => prjct.id_project._id === this.projectID)
186-
if(project){
187-
this.project = project
188-
this.logger.log('[PROJECT-ITEM] - GET PROJECTS - project found with this.projectID', this.project);
189-
localStorage.setItem('last_project', JSON.stringify(this.project))
190-
this.doProjectSubscriptions(this.project)
191-
return
192-
}else{
193-
this.logger.log('[PROJECT-ITEM] - GET PROJECTS - project NOT found with this.projectID', this.projectID);
194-
}
202+
let project: Project | undefined;
203+
204+
if (this.projectID) {
205+
project = projects.find( p => p.id_project?._id === this.projectID );
195206
}
196207

197-
if (projects[0]) {
198-
localStorage.setItem('last_project', JSON.stringify(projects[0]))
199-
this.project = projects[0];
200-
this.doProjectSubscriptions(this.project)
208+
if (!project) {
209+
project = projects[0];
201210
}
202211

203-
}, (error) => {
204-
this.logger.error('[PROJECT-ITEM] - GET PROJECTS - ERROR ', error);
212+
if (!project) {
213+
this.logger.warn('[PROJECT-ITEM] No projects returned from API');
214+
return;
215+
}
205216

206-
}, () => {
207-
this.logger.log('[INFO-CONTENT-COMP] - GET PROJECTS * COMPLETE *');
217+
this.project = project;
218+
localStorage.setItem('last_project', JSON.stringify(project));
219+
this.doProjectSubscriptions(project);
208220

221+
}, error => {
222+
this.logger.error('[PROJECT-ITEM] GET PROJECTS ERROR', error);
209223
});
210-
}
211-
212224

213-
if (stored_project && stored_project !== 'undefined') {
214-
this.logger.log('PROJECT-ITEM - THERE IS STORED LAST PROJECT ', stored_project)
215-
if (stored_project) {
216-
this.project = JSON.parse(stored_project)
217-
}
218-
this.doProjectSubscriptions(this.project)
219-
this.logger.log('[PROJECT-ITEM] - LAST PROJECT PARSED ', this.project)
225+
return;
220226
}
221227

222-
223-
228+
// ✅ stored project valido
229+
this.project = stored_project;
230+
this.doProjectSubscriptions(stored_project);
224231

225232
}
226233

@@ -345,6 +352,17 @@ export class ProjectItemComponent implements OnInit {
345352
}
346353
}
347354

355+
isValidStoredProject(obj: any): obj is ProjectUser {
356+
return (
357+
obj &&
358+
typeof obj === 'object' &&
359+
obj.id_project &&
360+
typeof obj.id_project === 'object' &&
361+
typeof obj.id_project._id === 'string' &&
362+
typeof obj._id === 'string' &&
363+
typeof obj.role === 'string'
364+
);
365+
}
348366

349367

350368

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export interface ProjectUser {
2+
attributes?: any;
3+
createdAt?: string;
4+
createdBy?: string;
5+
id?:string;
6+
id_project?: string;
7+
id_user?: any;
8+
isAuthenticated?: boolean;
9+
isBusy?: boolean;
10+
last_login_at?: string;
11+
number_assigned_requests?: number;
12+
permissions?:any;
13+
presence?:any;
14+
profileStatus?: string;
15+
role?: string;
16+
roleType?: number;
17+
status?: string;
18+
tags?:any;
19+
trashed?: boolean;
20+
updatedAt?: string;
21+
user_available?: boolean;
22+
is_group_member?: boolean;
23+
__v?: any;
24+
_id?: string;
25+
}

0 commit comments

Comments
 (0)