diff --git a/client/src/entities/teacher-reported-today.jsx b/client/src/entities/teacher-reported-today.jsx index 19c7bebb..a8750b2c 100644 --- a/client/src/entities/teacher-reported-today.jsx +++ b/client/src/entities/teacher-reported-today.jsx @@ -29,12 +29,12 @@ const formatHour = (value) => (value ? new Date(value).toLocaleTimeString('he-IL const NO_KLASS_KEY = 'none'; -// The view has one row per teacher+lesson+date+klass; group rows sharing the same -// teacher+date+klass into a single card, collecting their lesson rows. +// The view has one row per teacher+lesson+date+klass; each lesson gets its own card, +// so a teacher who taught two lessons shows as two cards. function groupByTeacherAndDate(rows) { const groups = new Map(); rows.forEach((row) => { - const key = `${row.userId}_${row.teacherReferenceId}_${row.reportDate}_${row.klassReferenceId ?? NO_KLASS_KEY}`; + const key = `${row.userId}_${row.teacherReferenceId}_${row.reportDate}_${row.klassReferenceId ?? NO_KLASS_KEY}_${row.lessonReferenceId ?? 'none'}`; if (!groups.has(key)) { groups.set(key, { ...row, lessonRows: [] }); } @@ -66,8 +66,8 @@ function groupByKlass(groups) { // count when it has no per-lesson breakdown (the "ללא שיוך שיעור" case). function getTotalMissingGirls(group) { return group.lessonRows.length > 0 - ? group.lessonRows.reduce((sum, row) => sum + (row.missingGirlsCount || 0), 0) - : group.missingGirlsCount || 0; + ? group.lessonRows.reduce((sum, row) => sum + (Number(row.missingGirlsCount) || 0), 0) + : Number(group.missingGirlsCount) || 0; } const TeacherReportCards = ({ isAdmin }) => { @@ -116,7 +116,7 @@ const TeacherReportCards = ({ isAdmin }) => { {group.lessonRows.length > 0 ? ( group.lessonRows.map((row) => ( - + diff --git a/server/src/db/view-entities/TeacherReportedToday.entity.ts b/server/src/db/view-entities/TeacherReportedToday.entity.ts index 5f0fc2c1..0881d836 100644 --- a/server/src/db/view-entities/TeacherReportedToday.entity.ts +++ b/server/src/db/view-entities/TeacherReportedToday.entity.ts @@ -16,7 +16,7 @@ import { AttReport } from '../entities/AttReport.entity'; .addSelect('att_report.lessonReferenceId', 'lessonReferenceId') .addSelect('att_report.klassReferenceId', 'klassReferenceId') .addSelect( - 'MIN(MIN(att_report.createdAt)) OVER (PARTITION BY att_report.userId, att_report.teacherReferenceId, att_report.reportDate, att_report.klassReferenceId)', + 'MIN(att_report.createdAt)', 'reportHour', ) .addSelect( diff --git a/server/src/migrations/1791305020926-ReportHourPerLessonInTeacherReportedTodayView.ts b/server/src/migrations/1791305020926-ReportHourPerLessonInTeacherReportedTodayView.ts new file mode 100644 index 00000000..98a12032 --- /dev/null +++ b/server/src/migrations/1791305020926-ReportHourPerLessonInTeacherReportedTodayView.ts @@ -0,0 +1,22 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class ReportHourPerLessonInTeacherReportedTodayView1791305020926 implements MigrationInterface { + name = 'ReportHourPerLessonInTeacherReportedTodayView1791305020926' + + public async up(queryRunner: QueryRunner): Promise { + const dbName = queryRunner.connection.options.database; + await queryRunner.query(`DELETE FROM \`${dbName}\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","teacher_reported_today",dbName]); + await queryRunner.query(`DROP VIEW IF EXISTS \`teacher_reported_today\``); + await queryRunner.query(`CREATE OR REPLACE VIEW \`teacher_reported_today\` AS SELECT \`att_report\`.\`user_id\` AS \`userId\`, \`att_report\`.\`teacherReferenceId\` AS \`teacherReferenceId\`, \`att_report\`.\`klassReferenceId\` AS \`klassReferenceId\`, \`att_report\`.\`lessonReferenceId\` AS \`lessonReferenceId\`, \`att_report\`.\`report_date\` AS \`reportDate\`, CONCAT(\`att_report\`.\`user_id\`, '_', \`att_report\`.\`teacherReferenceId\`, '_', \`att_report\`.\`report_date\`, '_', COALESCE(\`att_report\`.\`lessonReferenceId\`, 'null'), '_', COALESCE(\`att_report\`.\`klassReferenceId\`, 'null')) AS \`id\`, MIN(\`att_report\`.\`created_at\`) AS \`reportHour\`, COUNT(DISTINCT CASE WHEN \`att_report\`.\`abs_count\` > 0 THEN \`att_report\`.\`studentReferenceId\` END) AS \`missingGirlsCount\` FROM \`att_reports\` \`att_report\` WHERE \`att_report\`.\`teacherReferenceId\` IS NOT NULL GROUP BY \`att_report\`.\`user_id\`, \`att_report\`.\`teacherReferenceId\`, \`att_report\`.\`report_date\`, \`att_report\`.\`lessonReferenceId\`, \`att_report\`.\`klassReferenceId\``); + await queryRunner.query(`INSERT INTO \`${dbName}\`.\`typeorm_metadata\`(\`database\`, \`schema\`, \`table\`, \`type\`, \`name\`, \`value\`) VALUES (DEFAULT, ?, DEFAULT, ?, ?, ?)`, [dbName,"VIEW","teacher_reported_today","SELECT `att_report`.`user_id` AS `userId`, `att_report`.`teacherReferenceId` AS `teacherReferenceId`, `att_report`.`klassReferenceId` AS `klassReferenceId`, `att_report`.`lessonReferenceId` AS `lessonReferenceId`, `att_report`.`report_date` AS `reportDate`, CONCAT(`att_report`.`user_id`, '_', `att_report`.`teacherReferenceId`, '_', `att_report`.`report_date`, '_', COALESCE(`att_report`.`lessonReferenceId`, 'null'), '_', COALESCE(`att_report`.`klassReferenceId`, 'null')) AS `id`, MIN(`att_report`.`created_at`) AS `reportHour`, COUNT(DISTINCT CASE WHEN `att_report`.`abs_count` > 0 THEN `att_report`.`studentReferenceId` END) AS `missingGirlsCount` FROM `att_reports` `att_report` WHERE `att_report`.`teacherReferenceId` IS NOT NULL GROUP BY `att_report`.`user_id`, `att_report`.`teacherReferenceId`, `att_report`.`report_date`, `att_report`.`lessonReferenceId`, `att_report`.`klassReferenceId`"]); + } + + public async down(queryRunner: QueryRunner): Promise { + const dbName = queryRunner.connection.options.database; + await queryRunner.query(`DELETE FROM \`${dbName}\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","teacher_reported_today",dbName]); + await queryRunner.query(`DROP VIEW IF EXISTS \`teacher_reported_today\``); + await queryRunner.query(`CREATE OR REPLACE VIEW \`teacher_reported_today\` AS SELECT \`att_report\`.\`user_id\` AS \`userId\`, \`att_report\`.\`teacherReferenceId\` AS \`teacherReferenceId\`, \`att_report\`.\`lessonReferenceId\` AS \`lessonReferenceId\`, \`att_report\`.\`klassReferenceId\` AS \`klassReferenceId\`, \`att_report\`.\`report_date\` AS \`reportDate\`, CONCAT(\`att_report\`.\`user_id\`, '_', \`att_report\`.\`teacherReferenceId\`, '_', \`att_report\`.\`report_date\`, '_', COALESCE(\`att_report\`.\`lessonReferenceId\`, 'null'), '_', COALESCE(\`att_report\`.\`klassReferenceId\`, 'null')) AS \`id\`, MIN(MIN(\`att_report\`.\`created_at\`)) OVER (PARTITION BY \`att_report\`.\`user_id\`, \`att_report\`.\`teacherReferenceId\`, \`att_report\`.\`report_date\`, \`att_report\`.\`klassReferenceId\`) AS \`reportHour\`, COUNT(DISTINCT CASE WHEN \`att_report\`.\`abs_count\` > 0 THEN \`att_report\`.\`studentReferenceId\` END) AS \`missingGirlsCount\` FROM \`att_reports\` \`att_report\` WHERE \`att_report\`.\`teacherReferenceId\` IS NOT NULL GROUP BY \`att_report\`.\`user_id\`, \`att_report\`.\`teacherReferenceId\`, \`att_report\`.\`report_date\`, \`att_report\`.\`lessonReferenceId\`, \`att_report\`.\`klassReferenceId\``); + await queryRunner.query(`INSERT INTO \`${dbName}\`.\`typeorm_metadata\`(\`database\`, \`schema\`, \`table\`, \`type\`, \`name\`, \`value\`) VALUES (DEFAULT, ?, DEFAULT, ?, ?, ?)`, [dbName,"VIEW","teacher_reported_today","SELECT `att_report`.`user_id` AS `userId`, `att_report`.`teacherReferenceId` AS `teacherReferenceId`, `att_report`.`lessonReferenceId` AS `lessonReferenceId`, `att_report`.`klassReferenceId` AS `klassReferenceId`, `att_report`.`report_date` AS `reportDate`, CONCAT(`att_report`.`user_id`, '_', `att_report`.`teacherReferenceId`, '_', `att_report`.`report_date`, '_', COALESCE(`att_report`.`lessonReferenceId`, 'null'), '_', COALESCE(`att_report`.`klassReferenceId`, 'null')) AS `id`, MIN(MIN(`att_report`.`created_at`)) OVER (PARTITION BY `att_report`.`user_id`, `att_report`.`teacherReferenceId`, `att_report`.`report_date`, `att_report`.`klassReferenceId`) AS `reportHour`, COUNT(DISTINCT CASE WHEN `att_report`.`abs_count` > 0 THEN `att_report`.`studentReferenceId` END) AS `missingGirlsCount` FROM `att_reports` `att_report` WHERE `att_report`.`teacherReferenceId` IS NOT NULL GROUP BY `att_report`.`user_id`, `att_report`.`teacherReferenceId`, `att_report`.`report_date`, `att_report`.`lessonReferenceId`, `att_report`.`klassReferenceId`"]); + } + +}