Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 2x 2x 1x 1x 1x 1x 3x 3x 3x 3x 3x 2x 2x 3x 7x 3x 1x 1x 1x 1x | import { inject, Injectable, signal } from "@angular/core";
import { CreateProjectFormValidationService } from "./project.create.validation";
import { NewProjectDraft, ProjectRequest } from "./project.interfaces";
import { catchError, map, Observable, throwError } from "rxjs";
import { UserRole } from "../users/user.interfaces";
import { HttpClient, HttpErrorResponse } from "@angular/common/http";
import { AuthService } from "../auth/auth.service";
import { environment } from "../../../environments/environment";
import { ProjectService } from "./project.service";
import { ProjectTeamTableService } from "./project.team.table.service";
@Injectable({ providedIn: 'root' })
export class ProjectCreateService {
readonly validateFormService = inject(CreateProjectFormValidationService);
private readonly authService = inject(AuthService);
private readonly projectService = inject(ProjectService);
private readonly projectTeamTableService = inject(ProjectTeamTableService)
private readonly isCreatePanelOpenSignal = signal<boolean>(false);
private readonly isLoadingSignal = signal<boolean>(false);
private readonly apiBase = environment.authApiBaseUrl;
readonly isCreatePanelOpen = this.isCreatePanelOpenSignal.asReadonly();
readonly isLoading = this.isLoadingSignal.asReadonly();
constructor(private http: HttpClient) {}
setLoading(loading: boolean) {
this.isLoadingSignal.set(loading);
}
openCreateProjectPanel(): void {
this.isCreatePanelOpenSignal.set(true);
this.projectTeamTableService.setSelectedMembers([]);
this.projectTeamTableService.setIsEditMode(true);
this.projectTeamTableService.cleanInputMember();
}
closeCreateProjectPanel(): void {
this.isCreatePanelOpenSignal.set(false);
this.projectTeamTableService.setSelectedMembers([]);
this.projectTeamTableService.setIsEditMode(false);
this.projectTeamTableService.cleanInputMember();
}
createProject(draft: NewProjectDraft, teamMembers: UserRole[]): Observable<string> {
const tags = this.splitCommaSeparatedValues(draft.tags);
const newProject: ProjectRequest = {
name: draft.name.trim(),
client: draft.client.trim(),
summary: draft.summary.trim(),
priority: draft.priority,
health: 'Descubrimiento',
progress: 0,
methodology: draft.methodology,
createdDate: new Date().toISOString(),
startDate: new Date().toISOString(),
dueDate: draft.dueDate,
tags: tags.length ? tags : ['Nuevo proyecto'],
userCreated: { userId: this.getUserId(), role: draft.role.trim() },
teamMembers: teamMembers.map(member => ({ userId: member.id, role: member.role.trim() }))
};
return this.CreateProjectRequest(newProject);
}
private CreateProjectRequest(projectData: ProjectRequest): Observable<string> {
return this.http.post<ProjectRequest>(this.apiBase + '/api/v1/project/', projectData, {
withCredentials: true
})
.pipe(
map((res) => {
this.projectService.getProjectsFromApi();
return "Project created successfully";
}),
catchError(this.handleError)
);
}
private splitCommaSeparatedValues(value: string): string[] {
return value
.split(',')
.map((entry) => entry.trim())
.filter(Boolean);
}
private getUserId(): string {
return this.authService.authUser()?.id ?? '';
}
private handleError(error: HttpErrorResponse) {
let errorMessage = 'Error desconocido';
Iif (error.error instanceof ErrorEvent) {
// Error del lado del cliente
errorMessage = `Error: ${error.error.message}`;
} else {
// Error del lado del servidor
errorMessage = `Código: ${error.status} - Mensaje: ${error.message}`;
}
return throwError(() => new Error(errorMessage));
}
} |