@shisyamo4131/air-guard-v2-schemas 2.4.2-dev.98 → 2.4.2-dev.99

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 +47 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shisyamo4131/air-guard-v2-schemas",
3
- "version": "2.4.2-dev.98",
3
+ "version": "2.4.2-dev.99",
4
4
  "description": "Schemas for AirGuard V2",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/Employee.js CHANGED
@@ -11,6 +11,7 @@ import Certification from "./Certification.js";
11
11
  import { GeocodableMixin } from "./mixins/GeocodableMixin.js";
12
12
  import { VALIDATION_ERRORS } from "./errorDefinitions.js";
13
13
  import Insurance from "./Insurance.js";
14
+ import User from "./User.js";
14
15
 
15
16
  /*****************************************************************************
16
17
  * @class Employee
@@ -798,16 +799,29 @@ export default class Employee extends GeocodableMixin(FireModel) {
798
799
 
799
800
  /**
800
801
  * 現在インスタンスに読み込まれている従業員を退職状態に変更します。
802
+ * - この処理は従業員ドキュメントの更新と、紐づいているユーザーアカウントの削除をトランザクション内で実行します。
803
+ * - サーバーサイド(ServerAdapter)からの呼び出しはサポートしていません。
804
+ * - Firestoreトランザクションの制約上、ユーザー検索はトランザクション外で実行されます。
805
+ *
801
806
  * @param {Date} dateOfTermination - 退職日(Dateオブジェクト)
802
807
  * @param {string} reasonOfTermination - 退職理由
803
- * @param {Object} options - パラメータオブジェクト
804
- * @param {Function|null} [options.transaction=null] - Firestore トランザクション関数
805
- * @param {Function|null} [options.callBack=null] - カスタム処理用コールバック
806
- * @param {string|null} [options.prefix=null] - パスのプレフィックス
807
- * @returns {Promise<DocumentReference>} 更新されたドキュメントの参照
808
- * @throws {Error} docIdが存在しない場合、または有効なdateOfTerminationが提供されていない場合。
808
+ * @returns {Promise<void>} 処理完了時に解決されるPromise
809
+ * @throws {Error} サーバーサイドから呼び出された場合
810
+ * @throws {Error} docIdが存在しない場合
811
+ * @throws {Error} 有効なdateOfTerminationが提供されていない場合
812
+ * @throws {Error} dateOfTerminationがdateOfHireより前の日付の場合
813
+ * @throws {Error} 有効なreasonOfTerminationが提供されていない場合
814
+ * @throws {Error} 従業員が管理者アカウントと紐づいている場合
809
815
  */
810
- async toTerminated(dateOfTermination, reasonOfTermination, options = {}) {
816
+ async toTerminated(dateOfTermination, reasonOfTermination) {
817
+ // サーバーサイドからの呼び出しチェック
818
+ if (Employee.type === "SERVER") {
819
+ throw new Error(
820
+ "[Employee.js] toTerminated cannot be called from server-side adapter. Use separate Employee.update() and User.delete() operations with explicit prefix instead.",
821
+ );
822
+ }
823
+
824
+ // バリデーション
811
825
  if (!this.docId) {
812
826
  throw new Error(
813
827
  "[Employee.js] docId is required to terminate an employee.",
@@ -823,21 +837,45 @@ export default class Employee extends GeocodableMixin(FireModel) {
823
837
  "[Employee.js] dateOfTermination cannot be earlier than dateOfHire.",
824
838
  );
825
839
  }
826
-
827
840
  if (!reasonOfTermination || typeof reasonOfTermination !== "string") {
828
841
  throw new Error(
829
842
  "[Employee.js] A valid reasonOfTermination is required to terminate an employee.",
830
843
  );
831
844
  }
832
845
 
846
+ // User検索(トランザクション外で実行)
847
+ const userInstance = new User();
848
+ const users = await userInstance.fetchDocs({
849
+ constraints: [["where", "employeeId", "==", this.docId]],
850
+ });
851
+
852
+ const user = users.length > 0 ? users[0] : null;
853
+
854
+ // 管理者アカウントチェック
855
+ if (user && user.isAdmin) {
856
+ throw new Error(
857
+ "[Employee.js] Cannot terminate employee with administrator user account. Remove admin privileges first.",
858
+ );
859
+ }
860
+
861
+ // 退職情報をインスタンスにセット
833
862
  this.employmentStatus = Employee.STATUS_RESIGNED;
834
863
  this.dateOfTermination = dateOfTermination;
835
864
  this.reasonOfTermination = reasonOfTermination;
836
865
 
866
+ // update メソッドで弾かれないようにフラグをセット
837
867
  this._skipToTerminatedCheck = true;
838
868
 
839
869
  try {
840
- return await this.update(options);
870
+ // トランザクション内でWrite操作のみ実行
871
+ await Employee.runTransaction(async (transaction) => {
872
+ await this.update({ transaction });
873
+
874
+ // 紐づいているユーザーアカウントを削除
875
+ if (user) {
876
+ await user.delete({ transaction });
877
+ }
878
+ });
841
879
  } catch (error) {
842
880
  this.rollback();
843
881
  throw error;