-
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
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import * as proto from "@bufbuild/protobuf";
import { NotFoundSchema } from "@yamori/proto/yamori/error/v1/not_found_pb.js";
import { DateSchema } from "@yamori/proto/yamori/type/v1/date_pb.js";
import { type LeaveSchema } from "@yamori/proto/yamori/work_record/v1/leave_pb.js";
import { type WorkerSchema } from "@yamori/proto/yamori/worker/v1/worker_pb.js";
import { type WorkspaceSchema } from "@yamori/proto/yamori/workspace/v1/workspace_pb.js";
import { WriteWorkRecordRequestSchema } from "@yamori/proto/yamori/worker/v1/write_work_record_request_pb.js";
import { WriteWorkRecordResponseSchema } from "@yamori/proto/yamori/worker/v1/write_work_record_response_pb.js";
import { isBefore } from "date-fns";
import { useMethodMutation } from "../../../contexts/Service.tsx";
import { useToast } from "../../../contexts/Toast.tsx";
import { IllegalMessageError } from "../../../errors/IllegalMessageError.ts";
import { fromProtoDate, toProtoDate } from "../../../helpers.ts";
import type * as Spreadsheet from "./Spreadsheet.ts";
export interface Actions {
isPending: boolean;
/**
* 選択されたセルを出勤として設定する。
*/
markAsWorked(): Promise<void>;
/**
* 選択されたセルを欠勤として設定する。
*/
markAsAbsent(): Promise<void>;
/**
* 選択されたセルを休日として設定する。
*/
markAsDayOff(): Promise<void>;
/**
* 選択されたセルを法定・特別休暇として設定する。
*/
markAsWorkspaceDefinedLeave(
leave: proto.MessageShape<typeof LeaveSchema>,
): Promise<void>;
/**
* 選択されたセルを年次有給休暇として設定する。
*/
markAsPaidLeave(): Promise<void>;
}
export interface UseActionsInput {
workspace: proto.MessageShape<typeof WorkspaceSchema>;
selection: Spreadsheet.Selection;
workers: readonly proto.MessageShape<typeof WorkerSchema>[];
dates: readonly Date[];
onWorkRecordUpdated?(): void;
}
export function useActions({
workspace,
selection,
workers,
dates,
onWorkRecordUpdated,
}: UseActionsInput): Actions {
const toast = useToast();
const mutation = useMethodMutation({
service: "yamori.worker.v1.WorkerService",
method: "WriteWorkRecord",
request: {
schema: WriteWorkRecordRequestSchema,
},
response: {
schema: WriteWorkRecordResponseSchema,
},
mapResponse(resp) {
if (resp.result.case === "ok") {
return resp.result.value.workRecords;
}
if (typeof resp.result.case === "undefined") {
throw new IllegalMessageError(resp);
}
throw resp.result.value;
},
options: {
onError(error) {
if (
proto.isMessage(error, NotFoundSchema) &&
error.typeName === "yamori.worker.v1.PaidLeaveProvision"
) {
toast.open({
severity: "warn",
title: "年次有給休暇の残り日数がありません",
});
return;
}
toast.open({
severity: "danger",
title: "勤怠記録の更新に失敗しました",
});
},
},
});
const runForWorkers = async (
callback: (
worker: proto.MessageShape<typeof WorkerSchema>,
dates: proto.MessageShape<typeof DateSchema>[],
) => Promise<void>,
) => {
try {
for (let i = 0, l = workers.length; i < l; i++) {
const cells = selection.cells
.filter((cell) => cell.row === i)
.map((cell) => {
const date = dates[cell.column];
if (!date) {
return null;
}
return toProtoDate(date);
})
.filter((d): d is proto.MessageShape<typeof DateSchema> => !!d);
const worker = workers[i];
if (!worker || !cells.length) {
continue;
}
if (!worker.writeWorkRecordKey) {
toast.open({
severity: "warn",
title: "書き込み権限がありません",
description: `労働者「${worker.displayName}」への書き込み権限がありません。`,
});
continue;
}
await callback(worker, cells);
}
} finally {
onWorkRecordUpdated?.();
}
};
return {
isPending: mutation.isPending,
markAsDayOff() {
return runForWorkers(async (worker, dates) => {
await mutation.mutateAsync({
workspaceId: workspace.id,
workerId: worker.id,
writeWorkRecordKey: worker.writeWorkRecordKey,
workRecord: {
dates,
record: {
case: "dayOff",
value: {},
},
},
});
});
},
markAsWorked() {
return runForWorkers(async (worker, dates) => {
await mutation.mutateAsync({
workspaceId: workspace.id,
workerId: worker.id,
writeWorkRecordKey: worker.writeWorkRecordKey,
workRecord: {
dates,
kind: {
case: "dayWhole",
value: {
kind: {
case: "worked",
value: {},
},
},
},
},
});
});
},
markAsAbsent() {
return runForWorkers(async (worker, dates) => {
await mutation.mutateAsync({
workspaceId: workspace.id,
workerId: worker.id,
writeWorkRecordKey: worker.writeWorkRecordKey,
workRecord: {
dates,
kind: {
case: "dayWhole",
value: {
kind: {
case: "skipped",
value: {},
},
},
},
},
});
});
},
markAsPaidLeave() {
return runForWorkers(async (worker, dates) => {
await mutation.mutateAsync({
workspaceId: workspace.id,
workerId: worker.id,
writeWorkRecordKey: worker.writeWorkRecordKey,
workRecord: {
dates,
kind: {
case: "dayWhole",
value: {
kind: {
case: "paidLeave",
value: {},
},
},
},
},
});
});
},
markAsWorkspaceDefinedLeave(leave) {
return runForWorkers(async (worker, dates) => {
const firstAvailableAt =
leave.revisions[0]?.startAt && fromProtoDate(leave.revisions[0].startAt);
const datesTheLeaveNotAvailableFor = firstAvailableAt
? dates.filter((date) => isBefore(fromProtoDate(date), firstAvailableAt))
: [];
if (firstAvailableAt && datesTheLeaveNotAvailableFor.length > 0) {
toast.open({
severity: "warn",
title: `${leave.displayName}は利用できません`,
description: `対象範囲に${leave.displayName}の運用開始日以前の日付が含まれているため${worker.displayName}への書き込みをスキップします。`,
});
return;
}
await mutation.mutateAsync({
workspaceId: workspace.id,
workerId: worker.id,
writeWorkRecordKey: worker.writeWorkRecordKey,
workRecord: {
dates,
kind: {
case: "dayWhole",
value: {
kind: {
case: "workspaceDefinedLeaveId",
value: leave.id!,
},
},
},
},
});
});
},
};
}