@shisyamo4131/air-guard-v2-schemas 2.3.7-dev.20 → 2.3.7-dev.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Employee.js +64 -32
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shisyamo4131/air-guard-v2-schemas",
3
- "version": "2.3.7-dev.20",
3
+ "version": "2.3.7-dev.21",
4
4
  "description": "Schemas for AirGuard V2",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/Employee.js CHANGED
@@ -105,12 +105,16 @@ const classProps = {
105
105
  * @prop {string} fullNameKana - Full name in Kana combining last and first names (read-only)
106
106
  * @prop {string} fullAddress - Full address combining prefecture, city, and address (read-only)
107
107
  * @prop {string} prefecture - Prefecture name derived from `prefCode` (read-only)
108
+ *
109
+ * @getter
108
110
  * @prop {number} age - Age calculated from `dateOfBirth` (read-only)
109
111
  * @prop {number} yearsOfService - Years of service calculated from `dateOfHire` (read-only)
110
112
  *
111
113
  * @static
112
114
  * @prop {string} STATUS_ACTIVE - constant for active employment status
113
115
  * @prop {string} STATUS_TERMINATED - constant for terminated employment status
116
+ *
117
+ * @function toTerminated - Change the current employee instance to terminated status.
114
118
  *****************************************************************************/
115
119
  export default class Employee extends FireModel {
116
120
  static className = "従業員";
@@ -136,6 +140,8 @@ export default class Employee extends FireModel {
136
140
  static STATUS_ACTIVE = VALUES.ACTIVE.value;
137
141
  static STATUS_TERMINATED = VALUES.TERMINATED.value;
138
142
 
143
+ _skipToTerminatedCheck = false;
144
+
139
145
  afterInitialize(item = {}) {
140
146
  super.afterInitialize(item);
141
147
  Object.defineProperties(this, {
@@ -143,37 +149,37 @@ export default class Employee extends FireModel {
143
149
  fullNameKana: defAccessor("fullNameKana"),
144
150
  fullAddress: defAccessor("fullAddress"),
145
151
  prefecture: defAccessor("prefecture"),
146
- age: {
147
- enumerable: true,
148
- configurable: true,
149
- get() {
150
- if (!this.dateOfBirth) return null;
151
- const today = new Date();
152
- let age = today.getUTCFullYear() - this.dateOfBirth.getUTCFullYear();
153
- const m = today.getUTCMonth() - this.dateOfBirth.getUTCMonth();
154
- const d = today.getUTCDate() - this.dateOfBirth.getUTCDate();
155
- if (m < 0 || (m === 0 && d < 0)) age--;
156
- return age;
157
- },
158
- set() {},
159
- },
160
- yearsOfService: {
161
- enumerable: true,
162
- configurable: true,
163
- get() {
164
- if (!this.dateOfHire) return null;
165
- const today = new Date();
166
- let years = today.getUTCFullYear() - this.dateOfHire.getUTCFullYear();
167
- const m = today.getUTCMonth() - this.dateOfHire.getUTCMonth();
168
- const d = today.getUTCDate() - this.dateOfHire.getUTCDate();
169
- if (m < 0 || (m === 0 && d < 0)) years--;
170
- return years;
171
- },
172
- set() {},
173
- },
174
152
  });
175
153
  }
176
154
 
155
+ /**
156
+ * 生年月日から年齢を計算します。
157
+ * @returns {number|null} 年齢。dateOfBirthが設定されていない場合はnull。
158
+ */
159
+ get age() {
160
+ if (!this.dateOfBirth) return null;
161
+ const today = new Date();
162
+ let age = today.getUTCFullYear() - this.dateOfBirth.getUTCFullYear();
163
+ const m = today.getUTCMonth() - this.dateOfBirth.getUTCMonth();
164
+ const d = today.getUTCDate() - this.dateOfBirth.getUTCDate();
165
+ if (m < 0 || (m === 0 && d < 0)) age--;
166
+ return age;
167
+ }
168
+
169
+ /**
170
+ * 入社日からの勤続年数を計算します。
171
+ * @returns {number|null} 勤続年数。dateOfHireが設定されていない場合はnull。
172
+ */
173
+ get yearsOfService() {
174
+ if (!this.dateOfHire) return null;
175
+ const today = new Date();
176
+ let years = today.getUTCFullYear() - this.dateOfHire.getUTCFullYear();
177
+ const m = today.getUTCMonth() - this.dateOfHire.getUTCMonth();
178
+ const d = today.getUTCDate() - this.dateOfHire.getUTCDate();
179
+ if (m < 0 || (m === 0 && d < 0)) years--;
180
+ return years;
181
+ }
182
+
177
183
  /**
178
184
  * 外国籍の場合の必須フィールドを検証します。
179
185
  * - エラーがある場合は例外をスローします。
@@ -253,16 +259,36 @@ export default class Employee extends FireModel {
253
259
  */
254
260
  async beforeUpdate() {
255
261
  await super.beforeUpdate();
256
- if (this.employmentStatus !== this._beforeData.employmentStatus) {
262
+
263
+ // `employmentStatus` の `terminated` への直接変更の禁止
264
+ // - 従業員を退職させる場合、様々なチェックが必要になることが想定されるため、専用メソッドとして `toTerminated` を使用する。
265
+ // - `employmentStatus` を `terminated` に変更する場合は、必ず `toTerminated` メソッドを使用すること。
266
+ // - 一度退職処理した従業員の復帰処理は現状想定していないが、将来的に必要になった場合は `toActive` メソッド等を追加実装すること。
267
+ if (
268
+ !this._skipToTerminatedCheck &&
269
+ this.employmentStatus === Employee.STATUS_TERMINATED &&
270
+ this._beforeData.employmentStatus === Employee.STATUS_ACTIVE
271
+ ) {
257
272
  throw new Error(
258
- "[Employee.js] employmentStatus cannot be changed via update. Use toTerminated() method to change status to terminated."
273
+ "[Employee.js] Direct changes to employmentStatus to 'terminated' are not allowed. Use toTerminated() method instead."
259
274
  );
260
275
  }
276
+
261
277
  this._validateForeignerRequiredFields();
262
278
  this._validateTerminatedRequiredFields();
263
279
  }
264
280
 
265
- async toTerminated(dateOfTermination) {
281
+ /**
282
+ * 現在インスタンスに読み込まれている従業員を退職状態に変更します。
283
+ * @param {Date} dateOfTermination - 退職日(Dateオブジェクト)
284
+ * @param {Object} options - パラメータオブジェクト
285
+ * @param {Function|null} [options.transaction=null] - Firestore トランザクション関数
286
+ * @param {Function|null} [options.callBack=null] - カスタム処理用コールバック
287
+ * @param {string|null} [options.prefix=null] - パスのプレフィックス
288
+ * @returns {Promise<DocumentReference>} 更新されたドキュメントの参照
289
+ * @throws {Error} docIdが存在しない場合、または有効なdateOfTerminationが提供されていない場合。
290
+ */
291
+ async toTerminated(dateOfTermination, options = {}) {
266
292
  if (!this.docId) {
267
293
  throw new Error(
268
294
  "[Employee.js] docId is required to terminate an employee."
@@ -277,6 +303,12 @@ export default class Employee extends FireModel {
277
303
  this.employmentStatus = Employee.STATUS_TERMINATED;
278
304
  this.dateOfTermination = dateOfTermination;
279
305
 
280
- await FireModel.prototype.update.call(this);
306
+ this._skipToTerminatedCheck = true;
307
+
308
+ try {
309
+ return await this.update(options);
310
+ } finally {
311
+ this._skipToTerminatedCheck = false;
312
+ }
281
313
  }
282
314
  }