diff --git a/data_migration_tools/server/src/features/transfer/transfer.service.ts b/data_migration_tools/server/src/features/transfer/transfer.service.ts index c1b0757..cf8dd53 100644 --- a/data_migration_tools/server/src/features/transfer/transfer.service.ts +++ b/data_migration_tools/server/src/features/transfer/transfer.service.ts @@ -50,7 +50,7 @@ export class TransferService { let usersFileLines: UsersFile[] = []; let licensesFileLines: LicensesFile[] = []; let worktypesFileLines: WorktypesFile[] = []; - + let errorArray: string[] = []; let userIdIndex = 0; // authorIdとuserIdの対応関係を保持するMapを定義 const authorIdToUserIdMap: Map = new Map(); @@ -75,9 +75,11 @@ export class TransferService { if (line.parent_id) { parentAccountId = accountIdMap.get(line.parent_id); } - // 万が一parent_idが入力されているのに存在しなかった場合は、nullを設定する。 + // 万が一parent_idが入力されているのに存在しなかった場合は、エラー配列に追加する if (parentAccountId === undefined) { - parentAccountId = null; + errorArray.push( + `parent_id is invalid. parent_id=${line.parent_id}` + ); } // userIdIndexをインクリメントする @@ -126,8 +128,8 @@ export class TransferService { } // ライセンスのデータの作成を行う - // line.expired_dateが9999/12/31 23:59:59.997のデータの場合はデモライセンスなので登録しない - if (line.expired_date !== "9999/12/31 23:59:59.997") { + // line.expired_dateが"9999/12/31 23:59:59"のデータの場合はデモライセンスなので登録しない + if (line.expired_date !== "9999/12/31 23:59:59") { // authorIdが設定されてる場合、statusは"allocated"、allocated_user_idは対象のユーザID // されていない場合、statusは"reusable"、allocated_user_idはnull let status: string; @@ -176,6 +178,15 @@ export class TransferService { } } }); + // エラー配列に値が存在する場合はエラーファイルを出力する + if (errorArray.length > 0) { + const errorFileJson = JSON.stringify(errorArray); + fs.writeFileSync(`error.json`, errorFileJson); + throw new HttpException( + `errorArray is invalid. errorArray=${errorArray}`, + HttpStatus.BAD_REQUEST + ); + } return { accountsFileTypeLines, usersFileLines, @@ -211,24 +222,23 @@ export class TransferService { try { const relocatedAccounts: AccountsFile[] = []; - const countryRecords: Map = new Map(); + const dealerRecords: Map = new Map(); // accountsFileTypeをループ accountsFileType.forEach((account) => { - // Countryの場合はDistributorのアカウントIDと新たな親アカウントID(BC)の組み合わせをMapに登録 - if (account.type === MIGRATION_TYPE.COUNTRY) { - // 配下のDistributorアカウントを取得 - const distributor = accountsFileType.find( - (distributor) => - distributor.dealerAccountId === account.accountId && - distributor.type === MIGRATION_TYPE.DISTRIBUTOR + // Distributorの場合はdealerを検索し、COUNTRYかチェックする + if (account.type === MIGRATION_TYPE.DISTRIBUTOR) { + const distributorParent = accountsFileType.find( + (a) => a.accountId === account.dealerAccountId ); - if (distributor) { - countryRecords.set(distributor.accountId, account.dealerAccountId); + if (distributorParent.type === MIGRATION_TYPE.COUNTRY) { + dealerRecords.set( + account.accountId, + distributorParent.dealerAccountId // Countryの親、BCのIDを設定 + ); } } else { - // Country以外のアカウントの場合は、そのまま登録 - countryRecords.set(account.accountId, account.dealerAccountId); + dealerRecords.set(account.accountId, account.dealerAccountId); } }); @@ -237,7 +247,7 @@ export class TransferService { // Countryのレコードは除外する if (account.type !== MIGRATION_TYPE.COUNTRY) { const dealerAccountId = - countryRecords.get(account.dealerAccountId) ?? + dealerRecords.get(account.dealerAccountId) ?? account.dealerAccountId; const type = this.getAccountType(account.type); const newAccount: AccountsFile = {