Skip to content

Commit

Permalink
Remove count_other from staff_attendance
Browse files Browse the repository at this point in the history
  • Loading branch information
akheron committed Dec 30, 2024
1 parent bce24f8 commit 6c69f07
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export default React.memo(function StaffAttendance({
body: {
groupId,
date,
count,
countOther: null
count
}
}),
[groupId]
Expand Down
11 changes: 1 addition & 10 deletions frontend/src/employee-mobile-frontend/units/UnitList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default React.memo(function UnitList() {
presentChildren,
totalChildren,
presentStaff,
presentStaffOther,
totalStaff,
utilization
}) => (
Expand All @@ -72,11 +71,7 @@ export default React.memo(function UnitList() {
</div>
<div>
<Stat data-qa="staff-count">
{presentStaff}
{presentStaffOther ? (
<OtherStaff>+{presentStaffOther}</OtherStaff>
) : null}
/{totalStaff}
{presentStaff}/{totalStaff}
</Stat>
<StatDesc>{i18n.units.staff}</StatDesc>
</div>
Expand Down Expand Up @@ -128,10 +123,6 @@ const Stat = styled.div`
color: ${colors.grayscale.g100};
`

const OtherStaff = styled.span`
color: ${colors.grayscale.g70};
`

const StatDesc = styled.div`
color: ${colors.grayscale.g70};
font-weight: ${fontWeights.semibold};
Expand Down
1 change: 0 additions & 1 deletion frontend/src/lib-common/generated/api-types/attendance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ export interface UnitStats {
name: string
presentChildren: number
presentStaff: number
presentStaffOther: number
totalChildren: number
totalStaff: number
utilization: number
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/lib-common/generated/api-types/daycare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ export interface GroupOccupancies {
*/
export interface GroupStaffAttendance {
count: number
countOther: number
date: LocalDate
groupId: GroupId
updated: HelsinkiDateTime
Expand Down Expand Up @@ -475,7 +474,6 @@ export interface StaffAttendanceForDates {
*/
export interface StaffAttendanceUpdate {
count: number | null
countOther: number | null
date: LocalDate
groupId: GroupId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class StaffAttendanceServiceIntegrationTest : PureJdbiTest(resetDbBeforeEach = t
val attendanceDate = groupStartDate
staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId, attendanceDate, 1.0, 0.5),
StaffAttendanceUpdate(groupId, attendanceDate, 1.0),
)

val result =
Expand All @@ -91,69 +91,14 @@ class StaffAttendanceServiceIntegrationTest : PureJdbiTest(resetDbBeforeEach = t

assertEquals(1, result.attendances.size)
assertEquals(1.0, result.attendances[attendanceDate]?.count)
assertEquals(0.5, result.attendances[attendanceDate]?.countOther)
}

@Test
fun `creating an attendance with null countOther sets countOther to 0`() {
val attendanceDate = groupStartDate
staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId, attendanceDate, 1.0, null),
)

val result =
staffAttendanceService.getGroupAttendancesByMonth(
db,
attendanceDate.year,
attendanceDate.monthValue,
groupId,
)

assertEquals(1, result.attendances.size)
assertEquals(1.0, result.attendances[attendanceDate]?.count)
assertEquals(0.0, result.attendances[attendanceDate]?.countOther)
}

@Test
fun `modify attendance`() {
val attendanceDate = groupStartDate
staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId, attendanceDate, 1.0, 0.5),
)

var result =
staffAttendanceService.getGroupAttendancesByMonth(
db,
attendanceDate.year,
attendanceDate.monthValue,
groupId,
)
assertEquals(1.0, result.attendances[attendanceDate]?.count)
assertEquals(0.5, result.attendances[attendanceDate]?.countOther)

staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId, attendanceDate, 2.5, 0.0),
)
result =
staffAttendanceService.getGroupAttendancesByMonth(
db,
attendanceDate.year,
attendanceDate.monthValue,
groupId,
)
assertEquals(2.5, result.attendances[attendanceDate]?.count)
assertEquals(0.0, result.attendances[attendanceDate]?.countOther)
}

@Test
fun `modifying attendance with null countOther doesn't change countOther`() {
val attendanceDate = groupStartDate
staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId, attendanceDate, 1.0, 0.5),
StaffAttendanceUpdate(groupId, attendanceDate, 1.0),
)

var result =
Expand All @@ -164,11 +109,10 @@ class StaffAttendanceServiceIntegrationTest : PureJdbiTest(resetDbBeforeEach = t
groupId,
)
assertEquals(1.0, result.attendances[attendanceDate]?.count)
assertEquals(0.5, result.attendances[attendanceDate]?.countOther)

staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId, attendanceDate, 2.5, null),
StaffAttendanceUpdate(groupId, attendanceDate, 2.5),
)
result =
staffAttendanceService.getGroupAttendancesByMonth(
Expand All @@ -178,7 +122,6 @@ class StaffAttendanceServiceIntegrationTest : PureJdbiTest(resetDbBeforeEach = t
groupId,
)
assertEquals(2.5, result.attendances[attendanceDate]?.count)
assertEquals(0.5, result.attendances[attendanceDate]?.countOther)
}

@Test
Expand All @@ -187,7 +130,7 @@ class StaffAttendanceServiceIntegrationTest : PureJdbiTest(resetDbBeforeEach = t
assertThrows<BadRequest> {
staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId, attendanceDate, 1.0, 0.5),
StaffAttendanceUpdate(groupId, attendanceDate, 1.0),
)
}

Expand All @@ -206,23 +149,22 @@ class StaffAttendanceServiceIntegrationTest : PureJdbiTest(resetDbBeforeEach = t
val firstDay = groupStartDate
staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId, firstDay, 1.0, 0.5),
StaffAttendanceUpdate(groupId, firstDay, 1.0),
)
staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId2, firstDay, 2.0, 1.5),
StaffAttendanceUpdate(groupId2, firstDay, 2.0),
)

val secondDay = groupStartDate.plusDays(5)
staffAttendanceService.upsertStaffAttendance(
db,
StaffAttendanceUpdate(groupId, secondDay, 6.5, null),
StaffAttendanceUpdate(groupId, secondDay, 6.5),
)

val unitResult = staffAttendanceService.getUnitAttendancesForDate(db, daycareId, firstDay)
assertEquals(firstDay, unitResult.date)
assertEquals(3.0, unitResult.count)
assertEquals(2.0, unitResult.countOther)
assertEquals(2, unitResult.groups.size)
assertEquals(
db.read {
Expand All @@ -238,14 +180,11 @@ class StaffAttendanceServiceIntegrationTest : PureJdbiTest(resetDbBeforeEach = t

val groupResult = unitResult.groups.find { it.groupId == groupId }!!
assertEquals(1.0, groupResult.count)
assertEquals(0.5, groupResult.countOther)
val group2Result = unitResult.groups.find { it.groupId == groupId2 }!!
assertEquals(2.0, group2Result.count)
assertEquals(1.5, group2Result.countOther)

val unitResult2 = staffAttendanceService.getUnitAttendancesForDate(db, daycareId, secondDay)
assertEquals(secondDay, unitResult2.date)
assertEquals(6.5, unitResult2.count)
assertEquals(0.0, unitResult2.countOther)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ data class UnitStats(
val presentChildren: Int,
val totalChildren: Int,
val presentStaff: Double,
val presentStaffOther: Double,
val totalStaff: Double,
val utilization: Double,
)
Expand Down Expand Up @@ -314,23 +313,23 @@ WITH present_children AS (
WHERE unit_id = ANY(${bind(unitIds)})
GROUP BY unit_id
), present_staff AS (
SELECT g.daycare_id AS unit_id, sum(sa.capacity) AS capacity, sum(sa.count) AS count, sum(sa.count_other) AS count_other
SELECT g.daycare_id AS unit_id, sum(sa.capacity) AS capacity, sum(sa.count) AS count
FROM daycare_group g
JOIN daycare ON g.daycare_id = daycare.id
JOIN (
SELECT sa.group_id, 7 * sa.count as capacity, sa.count AS count, sa.count_other, FALSE AS realtime
SELECT sa.group_id, 7 * sa.count as capacity, sa.count AS count, FALSE AS realtime
FROM staff_attendance sa
WHERE sa.date = ${bind(date)}
UNION ALL
SELECT sa.group_id, sa.occupancy_coefficient as capacity, 1 AS count, 0 AS count_other, TRUE AS realtime
SELECT sa.group_id, sa.occupancy_coefficient as capacity, 1 AS count, TRUE AS realtime
FROM staff_attendance_realtime sa
WHERE sa.departed IS NULL AND sa.occupancy_coefficient > 0
UNION ALL
SELECT sa.group_id, sa.occupancy_coefficient as capacity, 1 AS count, 0 AS count_other, TRUE AS realtime
SELECT sa.group_id, sa.occupancy_coefficient as capacity, 1 AS count, TRUE AS realtime
FROM staff_attendance_external sa
WHERE sa.departed IS NULL AND sa.occupancy_coefficient > 0
) sa ON sa.group_id = g.id AND realtime = ('REALTIME_STAFF_ATTENDANCE' = ANY(daycare.enabled_pilot_features))
Expand All @@ -348,7 +347,6 @@ SELECT
coalesce(pc.count, 0) AS present_children,
coalesce(tc.count, 0) AS total_children,
coalesce(ps.count, 0) AS present_staff,
coalesce(ps.count_other, 0) AS present_staff_other,
coalesce(ts.count, 0) AS total_staff,
CASE
WHEN pc.capacity IS NULL OR pc.capacity = 0 THEN 0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,20 @@ class StaffAttendanceService {
}

fun clearStaffAttendance(db: Database.Connection, groupId: GroupId, date: LocalDate) {
db.transaction { tx ->
val countOther = tx.getCountOtherForDate(groupId, date)
if (countOther > 0) {
tx.upsertStaffAttendance(
StaffAttendanceUpdate(groupId, date, count = 0.0, countOther)
)
} else {
tx.deleteStaffAttendance(groupId, date)
}
}
db.transaction { tx -> tx.deleteStaffAttendance(groupId, date) }
}
}

data class GroupStaffAttendance(
val groupId: GroupId,
val date: LocalDate,
val count: Double,
val countOther: Double,
val updated: HelsinkiDateTime,
)

data class UnitStaffAttendance(
val date: LocalDate,
val count: Double,
val countOther: Double,
val updated: HelsinkiDateTime?,
val groups: List<GroupStaffAttendance>,
)
Expand All @@ -106,12 +95,7 @@ data class GroupInfo(
val endDate: LocalDate?,
)

data class StaffAttendanceUpdate(
val groupId: GroupId,
val date: LocalDate,
val count: Double?,
val countOther: Double?,
)
data class StaffAttendanceUpdate(val groupId: GroupId, val date: LocalDate, val count: Double?)

fun Database.Read.getGroupInfo(groupId: GroupId): GroupInfo? =
createQuery {
Expand Down Expand Up @@ -142,26 +126,14 @@ SELECT EXISTS (

fun Database.Transaction.upsertStaffAttendance(staffAttendance: StaffAttendanceUpdate) {
execute {
if (staffAttendance.countOther != null) {
sql(
"""
INSERT INTO staff_attendance (group_id, date, count, count_other)
VALUES (${bind(staffAttendance.groupId)}, ${bind(staffAttendance.date)}, ${bind(staffAttendance.count)}, ${bind(staffAttendance.countOther)})
ON CONFLICT (group_id, date) DO UPDATE SET
count = ${bind(staffAttendance.count)},
count_other = ${bind(staffAttendance.countOther)}
"""
)
} else {
sql(
"""
INSERT INTO staff_attendance (group_id, date, count, count_other)
VALUES (${bind(staffAttendance.groupId)}, ${bind(staffAttendance.date)}, ${bind(staffAttendance.count)}, DEFAULT)
sql(
"""
INSERT INTO staff_attendance (group_id, date, count)
VALUES (${bind(staffAttendance.groupId)}, ${bind(staffAttendance.date)}, ${bind(staffAttendance.count)})
ON CONFLICT (group_id, date) DO UPDATE SET
count = ${bind(staffAttendance.count)}
"""
)
}
)
}
}

Expand All @@ -172,7 +144,7 @@ fun Database.Read.getStaffAttendanceByRange(
createQuery {
sql(
"""
SELECT group_id, date, count, count_other, updated
SELECT group_id, date, count, updated
FROM staff_attendance
WHERE group_id = ${bind(groupId)}
AND between_start_and_end(${bind(range)}, date)
Expand All @@ -189,7 +161,7 @@ fun Database.Read.getUnitStaffAttendanceForDate(
createQuery {
sql(
"""
SELECT group_id, date, count, count_other, updated
SELECT group_id, date, count, updated
FROM staff_attendance sa
JOIN daycare_group dg on sa.group_id = dg.id
WHERE dg.daycare_id = ${bind(unitId)}
Expand All @@ -200,31 +172,16 @@ WHERE dg.daycare_id = ${bind(unitId)}
.toList<GroupStaffAttendance>()

val count = groupAttendances.sumOf { it.count }
val countOther = groupAttendances.sumOf { it.countOther }
val updated = groupAttendances.maxOfOrNull { it.updated }

return UnitStaffAttendance(
date = date,
count = count,
countOther = countOther,
groups = groupAttendances,
updated = updated,
)
}

fun Database.Read.getCountOtherForDate(groupId: GroupId, date: LocalDate): Double =
createQuery {
sql(
"""
SELECT count_other
FROM staff_attendance
WHERE group_id = ${bind(groupId)}
AND date = ${bind(date)}
"""
)
}
.exactlyOneOrNull() ?: 0.0

fun Database.Transaction.deleteStaffAttendance(groupId: GroupId, date: LocalDate) {
createUpdate {
sql(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE staff_attendance DROP COLUMN count_other;
1 change: 1 addition & 0 deletions service/src/main/resources/migrations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,4 @@ V475__application_modified_metadata.sql
V476__finance_metadata_process.sql
V477__titania_errors_id.sql
V478__delete_disallowed_personal_mobile_pairings.sql
V479__drop_staff_attendance_count_other.sql

0 comments on commit 6c69f07

Please sign in to comment.