Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/screens/worker/WorkerMonthlyCalendarScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import useCorrectionRequest from "../../hooks/worker/useCorrectionRequest";
import { useLogoutHandler } from "../../hooks/common/useLogoutHandler";
import { WorkerStackParamList } from "../../navigation/WorkerStack";
import { colors } from "../../constants/colors";
import { calculateWorkSummary } from "../../utils/workSummary";

const WorkerMonthlyCalendarScreen: React.FC = ({ navigation }: any) => {
const [isDrawerVisible, setIsDrawerVisible] = useState(false);
Expand Down Expand Up @@ -114,9 +115,10 @@ const WorkerMonthlyCalendarScreen: React.FC = ({ navigation }: any) => {

// 월간 요약 계산 (근무 총 시간/급여)
const monthLabel = `${month + 1}월`;
const totalMinutes = works.reduce((sum, w) => sum + w.totalWorkMinutes, 0);
const totalHours = Math.round((totalMinutes / 60) * 10) / 10;
const estimatedPay = works.reduce((sum, w) => sum + (w.totalSalary ?? 0), 0);
const { totalHours, estimatedPay } = useMemo(
() => calculateWorkSummary(works),
[works]
);

return (
<SafeAreaView style={styles.container}>
Expand Down
12 changes: 4 additions & 8 deletions src/screens/worker/WorkerWeeklyCalendarScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
getWeekLabel,
getWeekRange,
} from "../../utils/date";
import { calculateWorkSummary } from "../../utils/workSummary";

type Props = NativeStackScreenProps<WorkerStackParamList, "WorkerHomeMain">;

Expand Down Expand Up @@ -128,14 +129,9 @@ const WorkerWeeklyCalendarScreen: React.FC<Props> = ({ navigation }) => {
} = useCorrectionRequest();

// 주간 요약 계산
const totalMinutes = works.reduce(
(sum, w) => sum + w.totalWorkMinutes,
0
);
const totalHours = Math.round((totalMinutes / 60) * 10) / 10;
const estimatedPay = works.reduce(
(sum, w) => sum + (w.totalSalary ?? 0),
0
const { totalHours, estimatedPay } = useMemo(
() => calculateWorkSummary(works),
[works]
);

return (
Expand Down
30 changes: 30 additions & 0 deletions src/utils/workSummary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { WorkItem } from "../types/worker.types";

export interface WorkSummary {
totalHours: number;
estimatedPay: number;
}

/**
* 근무 목록에서 요약(총 근무시간, 예상 근무비) 계산. DELETED 근무는 두 값 모두 제외.
* - 총 근무시간: COMPLETED 근무의 totalWorkMinutes 합산
* - 예상 근무비: COMPLETED + SCHEDULED의 totalSalary 합산
* (백엔드가 SCHEDULED도 totalSalary를 계산해 반환 — PayCheck-backend#177)
*/
export const calculateWorkSummary = (works: WorkItem[]): WorkSummary => {
let completedMinutes = 0;
let estimatedPay = 0;

for (const work of works) {
if (work.status === "DELETED") continue;

if (work.status === "COMPLETED") {
completedMinutes += work.totalWorkMinutes;
}
estimatedPay += work.totalSalary ?? 0;
}

const totalHours = Math.round((completedMinutes / 60) * 10) / 10;

return { totalHours, estimatedPay };
};