-
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
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import { openDB } from "idb";
import { createRandomBytes, packDate } from "./helpers";
import { CONTEXT } from "./symbols";
import { type Context, type RPCMessage } from "./types";
import { service } from "./yamori/service";
class IDBBackend {
[CONTEXT]: Context;
constructor(ctx: Context) {
this[CONTEXT] = ctx;
}
handle(request: RPCMessage): Promise<Uint8Array> {
return service(request, this[CONTEXT]);
}
}
export async function idbBackend(): Promise<IDBBackend> {
return new IDBBackend({
db: await openDB("yamori", 9, {
// TODO: マイグレーションを個別のファイルに切り分ける
async upgrade(db, oldVersion, _newVersion, transaction, _event) {
// v1
if (oldVersion < 1) {
const workspaces = db.createObjectStore("workspaces", {
keyPath: "id",
});
workspaces.createIndex("updatedAt", "updatedAt", {
unique: false,
});
}
// v2
if (oldVersion < 2) {
const store = transaction.objectStore("workspaces");
for await (const cursor of store.iterate()) {
const deletionKey = new Uint8Array(16);
self.crypto.getRandomValues(deletionKey);
const updateKey = new Uint8Array(16);
self.crypto.getRandomValues(updateKey);
const workerAddKey = new Uint8Array(16);
self.crypto.getRandomValues(workerAddKey);
await cursor.update({
...cursor.value,
capabilities: {
...cursor.value.capabilities,
deletionKey,
updateKey,
workerAddKey,
},
});
}
}
// v3
if (oldVersion < 3) {
const workers = db.createObjectStore("workers", {
keyPath: "id",
});
workers.createIndex("workspaceId", "workspaceId", {
unique: false,
});
workers.createIndex("updatedAt", "updatedAt", {
unique: false,
});
}
// v4
if (oldVersion < 4) {
const store = transaction.objectStore("workspaces");
for await (const cursor of store.iterate()) {
const createLeaveDefinitionKey = new Uint8Array(16);
self.crypto.getRandomValues(createLeaveDefinitionKey);
await cursor.update({
...cursor.value,
capabilities: {
...cursor.value.capabilities,
createLeaveDefinitionKey,
},
});
}
}
// v5
if (oldVersion < 5) {
// v6 でカバーされるためスキップ
}
// v6
if (oldVersion < 6) {
const store = transaction.objectStore("workspaces");
for await (const cursor of store.iterate()) {
const defs = cursor.value.leaveDefinitions;
await cursor.update({
...cursor.value,
// 労働基準法 第三十九条 第十項 で定義されている「出勤したものとみなす」休業に
// ついてはどんな会社でも必ず必須となるため一律で登録する。
leaveDefinitions: [
{
id:
defs.find(
(def) =>
def.createdBy === "system" && def.displayName === "育児休業",
)?.id || `lv-${self.crypto.randomUUID()}`,
displayName: "育児休業",
updateKey: createRandomBytes(),
createdBy: "system",
revisions: [
{
id: `lr-${self.crypto.randomUUID()}`,
// 法律第七十九号
startAtPackedDate: packDate({
year: 1994,
month: 4,
day: 1,
}),
snapshot: {
isWorkerDeemedToBeWorked: true,
},
},
],
},
{
id:
defs.find(
(def) =>
def.createdBy === "system" && def.displayName === "介護休業",
)?.id || `lv-${self.crypto.randomUUID()}`,
displayName: "介護休業",
updateKey: createRandomBytes(),
createdBy: "system",
revisions: [
{
id: `lr-${self.crypto.randomUUID()}`,
// 法律第百七号
startAtPackedDate: packDate({
year: 1995,
month: 10,
day: 1,
}),
snapshot: {
isWorkerDeemedToBeWorked: true,
},
},
],
},
{
id:
defs.find(
(def) =>
def.createdBy === "system" && def.displayName === "産前産後休業",
)?.id || `lv-${self.crypto.randomUUID()}`,
displayName: "産前産後休業",
updateKey: createRandomBytes(),
createdBy: "system",
revisions: [
{
id: `lr-${self.crypto.randomUUID()}`,
// 最初からある (法律第四十九号)
// 三十九条の施行年月日は調べてもわからなかったので早い方 (遅い方は11/1)
// ただぶっちゃけ昔のことだからシステム運用的にはどっちでもいい。
// こんな昔のデータを入れるとしたら付与日数なんかも過去の計算式を用意
// する必要があるため、あくまでも記録・教育的な側面のデータ。
startAtPackedDate: packDate({
year: 1947,
month: 9,
day: 1,
}),
snapshot: {
isWorkerDeemedToBeWorked: true,
},
},
],
},
...defs.filter((def) => def.createdBy !== "system"),
],
});
}
}
// v7
if (oldVersion < 7) {
const store = transaction.objectStore("workspaces");
for await (const cursor of store.iterate()) {
await cursor.update({
...cursor.value,
leaveDefinitions: cursor.value.leaveDefinitions.map((def) => {
return {
...def,
deletionKey:
def.createdBy === "user" ? createRandomBytes(16) : undefined,
};
}),
});
}
}
// v8
if (oldVersion < 8) {
const store = transaction.objectStore("workers");
for await (const cursor of store.iterate()) {
await cursor.update({
...cursor.value,
writeWorkRecordKey:
cursor.value.writeWorkRecordKey || createRandomBytes(16),
});
}
const workRecords = db.createObjectStore("workRecords", {
keyPath: "recordId",
});
workRecords.createIndex(
"workspaceId/workerId/datePacked",
["workspaceId", "workerId", "datePacked"],
{
unique: true,
},
);
}
// v9
if (oldVersion < 9) {
const store = transaction.objectStore("workers");
for await (const cursor of store.iterate()) {
await cursor.update({
...cursor.value,
providePaidLeaveKey:
cursor.value.providePaidLeaveKey || createRandomBytes(16),
});
}
const paidLeaveProvision = db.createObjectStore("paidLeaveProvision", {
keyPath: "id",
});
paidLeaveProvision.createIndex(
"workspaceId,workerId",
["workspaceId", "workerId"],
{ unique: false },
);
paidLeaveProvision.createIndex(
"workspaceId,workerId,providedAtPacked",
["workspaceId", "workerId", "providedAtPacked"],
{ unique: true },
);
}
},
}),
});
}