All files / services customerProjectService.js

0% Statements 0/59
0% Branches 0/17
0% Functions 0/6
0% Lines 0/59

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214                                                                                                                                                                                                                                                                                                                                                                                                                                           
const logger = require('../utils/logger');
 
class CustomerProjectService {
  constructor(databaseService) {
    this.db = databaseService;
  }
 
  // Opret nyt kunde projekt
  async createProject(projectData) {
    try {
      const {
        projectName,
        customerName,
        customerEmail,
        customerPhone,
        customerAddress,
        projectDescription
      } = projectData;
 
      const query = `
        INSERT INTO customer_projects (
          project_name, customer_name, customer_email, 
          customer_phone, customer_address, project_description
        ) VALUES (?, ?, ?, ?, ?, ?)
      `;
 
      const [result] = await this.db.pool.execute(query, [
        projectName,
        customerName,
        customerEmail,
        customerPhone,
        customerAddress,
        projectDescription
      ]);
 
      logger.info('Customer project created', { 
        projectId: result.insertId, 
        projectName, 
        customerName 
      });
 
      return {
        id: result.insertId,
        projectName,
        customerName,
        status: 'draft',
        created_at: new Date()
      };
    } catch (error) {
      logger.error('Error creating customer project:', error);
      throw error;
    }
  }
 
  // Hent projekt med alle relaterede data
  async getProjectWithDetails(projectId) {
    try {
      // Hent grundlæggende projekt info
      const [projectRows] = await this.db.pool.execute(
        'SELECT * FROM customer_projects WHERE id = ?',
        [projectId]
      );
 
      if (projectRows.length === 0) {
        return null;
      }
 
      const project = projectRows[0];
 
      // Hent geometri data
      const [geometryRows] = await this.db.pool.execute(
        'SELECT * FROM roof_geometry WHERE project_id = ?',
        [projectId]
      );
 
      // Hent arbejdstimer data
      const [laborRows] = await this.db.pool.execute(
        'SELECT * FROM project_labor WHERE project_id = ?',
        [projectId]
      );
 
      // Hent materialer
      const [materialRows] = await this.db.pool.execute(
        'SELECT * FROM project_materials WHERE project_id = ? ORDER BY material_category, material_name',
        [projectId]
      );
 
      // Hent beregninger
      const [calculationRows] = await this.db.pool.execute(
        'SELECT * FROM project_calculations WHERE project_id = ? ORDER BY created_at DESC LIMIT 1',
        [projectId]
      );
 
      // Hent genererede tilbud
      const [quoteRows] = await this.db.pool.execute(
        'SELECT * FROM generated_quotes WHERE project_id = ? ORDER BY created_at DESC',
        [projectId]
      );
 
      return {
        project,
        geometry: geometryRows[0] || null,
        labor: laborRows[0] || null,
        materials: materialRows,
        calculation: calculationRows[0] || null,
        quotes: quoteRows
      };
    } catch (error) {
      logger.error('Error getting project details:', error);
      throw error;
    }
  }
 
  // Opdater projekt status
  async updateProjectStatus(projectId, status) {
    try {
      const query = `
        UPDATE customer_projects 
        SET project_status = ?, updated_at = CURRENT_TIMESTAMP 
        WHERE id = ?
      `;
 
      await this.db.pool.execute(query, [status, projectId]);
      
      logger.info('Project status updated', { projectId, status });
      return true;
    } catch (error) {
      logger.error('Error updating project status:', error);
      throw error;
    }
  }
 
  // Hent alle projekter med pagination
  async getProjects(page = 1, limit = 20, status = null) {
    try {
      const offset = (page - 1) * limit;
      
      let query = `
        SELECT cp.*, 
               rg.total_area,
               pl.total_labor_cost,
               pc.total_incl_vat,
               COUNT(pm.id) as material_count
        FROM customer_projects cp
        LEFT JOIN roof_geometry rg ON cp.id = rg.project_id
        LEFT JOIN project_labor pl ON cp.id = pl.project_id
        LEFT JOIN project_calculations pc ON cp.id = pc.project_id
        LEFT JOIN project_materials pm ON cp.id = pm.project_id
      `;
      
      let params = [];
      
      if (status) {
        query += ' WHERE cp.project_status = ?';
        params.push(status);
      }
      
      query += `
        GROUP BY cp.id
        ORDER BY cp.updated_at DESC 
        LIMIT ? OFFSET ?
      `;
      
      params.push(limit, offset);
 
      const [rows] = await this.db.pool.execute(query, params);
 
      // Hent total count for pagination
      let countQuery = 'SELECT COUNT(*) as total FROM customer_projects';
      let countParams = [];
      
      if (status) {
        countQuery += ' WHERE project_status = ?';
        countParams.push(status);
      }
 
      const [countRows] = await this.db.pool.execute(countQuery, countParams);
 
      return {
        projects: rows,
        pagination: {
          currentPage: page,
          totalPages: Math.ceil(countRows[0].total / limit),
          totalProjects: countRows[0].total,
          projectsPerPage: limit
        }
      };
    } catch (error) {
      logger.error('Error getting projects:', error);
      throw error;
    }
  }
 
  // Slet projekt og alle relaterede data
  async deleteProject(projectId) {
    try {
      const query = 'DELETE FROM customer_projects WHERE id = ?';
      const [result] = await this.db.pool.execute(query, [projectId]);
      
      if (result.affectedRows === 0) {
        throw new Error('Project not found');
      }
      
      logger.info('Project deleted', { projectId });
      return true;
    } catch (error) {
      logger.error('Error deleting project:', error);
      throw error;
    }
  }
}
 
module.exports = CustomerProjectService;