All files / app/core/project project.create.validation.ts

30.95% Statements 13/42
54.54% Branches 6/11
16.66% Functions 1/6
25.64% Lines 10/39

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          7x   7x                     7x   7x 7x 7x 7x 7x 7x 7x                                                                                                  
import { Injectable, signal } from '@angular/core';
import { form, required, validate } from '@angular/forms/signals';
import { NewProjectDraft } from './project.interfaces';
 
@Injectable({ providedIn: 'root' })
export class CreateProjectFormValidationService {
 
  private readonly initialValues: NewProjectDraft = {
    name: '',
    client: '',
    role: '',
    summary: '',
    priority: 'Media',
    methodology: 'Kanban',
    dueDate: '',
    tags: ''
  };
 
  private readonly projectModel = signal<NewProjectDraft>(this.initialValues);
 
  private readonly projectForm = form(this.projectModel, (schemaPath) => {
    required(schemaPath.name, {message: 'El nombre de proyecto es requerido'});
    required(schemaPath.client, {message: 'El cliente es requerido'});
    required(schemaPath.role, {message: 'Asignarse un rol es requerido'});
    required(schemaPath.summary, {message: 'Resumen del proyecto es requerdio'});
    required(schemaPath.dueDate, {message: 'Un fecha de entrega es requerida'});
    validate(schemaPath.dueDate, (ctx) => {
      const value = ctx.value();
      if (!value) {
        return [];
      }
      const today = new Date();
      today.setHours(0, 0, 0, 0);
      const selectedDate = new Date(value);
      selectedDate.setHours(0, 0, 0, 0);
      if (selectedDate < today) {
        return [{
          kind: 'datePast',
          message: 'La fecha no puede ser anterior al día de hoy'
        }];
      }
      return [];
    });
  });
 
  resetForm() {
    this.projectModel.set({ ...this.initialValues });
    this.projectForm.name().reset();
    this.projectForm.client().reset();
    this.projectForm.role().reset();
    this.projectForm.summary().reset();
    this.projectForm.priority().reset();
    this.projectForm.methodology().reset();
    this.projectForm.dueDate().reset();
    this.projectForm.tags().reset();
  }
 
  markAllFieldsAsTouched() {
    this.projectForm.name().markAsTouched();
    this.projectForm.client().markAsTouched();
    this.projectForm.role().markAsTouched();
    this.projectForm.summary().markAsTouched();
    this.projectForm.priority().markAsTouched();
    this.projectForm.methodology().markAsTouched();
    this.projectForm.dueDate().markAsTouched();
    this.projectForm.tags().markAsTouched();
  }
 
  getProjectModel() {
    return this.projectModel;
  }
  getProjectForm() {
    return this.projectForm;
  }
 
}