From 415fd2eb58bd42999d2bb00c222e72e346a842f4 Mon Sep 17 00:00:00 2001 From: "maruyama.t" Date: Tue, 12 Mar 2024 05:43:49 +0000 Subject: [PATCH] =?UTF-8?q?Merged=20PR=20825:=20AzureFunctions=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=EF=BC=92=EF=BC=88=E5=8F=96=E5=BE=97=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=83=87=E3=83=BC=E3=82=BF=E3=82=92CSV=E7=94=A8=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=8F=9B=E3=81=99=E3=82=8B=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 概要 [Task3844: AzureFunctions実装2(取得したデータをCSV用に変換する)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/3844) transferData()の実装 ccbの最新よりマージした状態での引数に変更 各ライセンス数をCSV配列に出力できるところまでを実装 (実際にローカルにCSV出力して中身を確認済み) ![image.png](https://dev.azure.com/ODMSCloud/6023ff7b-d41c-4fa7-9c6f-f576ba48c07c/_apis/git/repositories/302da463-a2d7-40f9-b2bb-6e8edf324fa9/pullRequests/825/attachments/image.png) ※UIの変更ではない為、ここにそのまま張り付けさせていただきます。 ## レビューポイント - 関数化の範囲は適切か。 テストコードは最低限の記述になるが、問題ないか (33行*7項目の突合せをコード上に実装するのは時間的余裕がないためやってない) 詳細な動作確認は、別タスク https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/OMDSDictation/_sprints/taskboard/OMDSDictation%20%E3%83%81%E3%83%BC%E3%83%A0/OMDSDictation/%E3%82%B9%E3%83%97%E3%83%AA%E3%83%B3%E3%83%88%2029-2?workitem=3861 で行います。 ## 補足 - 相談、参考資料などがあれば --- dictation_function/src/constants/index.ts | 53 + .../src/functions/analysisLicenses.ts | 1410 ++++++++++++++++- .../src/test/analysisLicenses.spec.ts | 545 ++++++- dictation_function/src/test/common/utility.ts | 8 +- 4 files changed, 1981 insertions(+), 35 deletions(-) diff --git a/dictation_function/src/constants/index.ts b/dictation_function/src/constants/index.ts index fe21f13..0c31242 100644 --- a/dictation_function/src/constants/index.ts +++ b/dictation_function/src/constants/index.ts @@ -337,3 +337,56 @@ export const RoleNumberMap: Record = { export const SYSTEM_IMPORT_USERS = "import-users"; export const ROW_START_INDEX = 2; + +/** + * ライセンス数推移出力機能のCSVヘッダ + * @const {string[]} + */ +export const LICENSE_COUNT_ANALYSIS_HEADER = { + ACCOUNT: "アカウント", + TARGET_YEAE_AND_MONTH: "対象年月", + CATEGORY_1: "カテゴリー1", + CATEGORY_2: "カテゴリー2", + LICENSE_TYPE: "ライセンス種別", + ROLE: "役割", + COUNT: "数量", +}; +/** + * ライセンス数推移出力機能のCSV項目で使用する日本語(カテゴリー1) + * @const {string[]} + */ +export const LICENSE_COUNT_ANALYSIS_CATEGORY_1 = { + VALID_LICENSES: "有効ライセンス数", + NEW_ISSUE_LICENSES: "新規発行ライセンス数", + INVALID_LICENSES: "失効ライセンス数", + SWICHED_LICENSES: "有効ライセンス切り替え", +}; +/** + * ライセンス数推移出力機能のCSV項目で使用する日本語(カテゴリー2) + * @const {string[]} + */ +export const LICENSE_COUNT_ANALYSIS_CATEGORY_2 = { + OWNER_LICENSES: "所有ライセンス数", + IN_USE_LICENSES: "使用中ライセンス数", +}; +/** + * ライセンス数推移出力機能のCSV項目で使用する日本語(ライセンス種別) + * @const {string[]} + */ +export const LICENSE_COUNT_ANALYSIS_LICENSE_TYPE = { + TRIAL: "Trial", + STANDARD: "Standard", + CARD: "Card", + SWITCH_FROM_TRIAL: "トライアルから切り替え", + SWITCH_FROM_CARD: "カードから切り替え", +}; +/** + * ライセンス数推移出力機能のCSV項目で使用する日本語(役割) + * @const {string[]} + */ +export const LICENSE_COUNT_ANALYSIS_ROLE = { + AUTHOR: "Author", + TYPIST: "Typist", + NONE: "None", + UNALLOCATED: "Unallocated", +}; diff --git a/dictation_function/src/functions/analysisLicenses.ts b/dictation_function/src/functions/analysisLicenses.ts index e3d0c83..515b3aa 100644 --- a/dictation_function/src/functions/analysisLicenses.ts +++ b/dictation_function/src/functions/analysisLicenses.ts @@ -13,7 +13,19 @@ import { LICENSE_ALLOCATED_STATUS, TIERS, SWITCH_FROM_TYPE, + LICENSE_COUNT_ANALYSIS_CATEGORY_1, + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE, + LICENSE_COUNT_ANALYSIS_ROLE, + LICENSE_COUNT_ANALYSIS_CATEGORY_2, + BLOB_STORAGE_REGION_AU, + USER_ROLES, + BLOB_STORAGE_REGION_US, + BLOB_STORAGE_REGION_EU, + LICENSE_TYPE, + LICENSE_COUNT_ANALYSIS_HEADER, } from "../constants"; +import * as fs from "fs"; +import * as path from "path"; import { DateWithDayEndTime } from "../common/types/types"; import { initializeDataSource } from "../database/initializeDataSource"; @@ -37,8 +49,13 @@ export async function analysisLicensesProcessing( targetMonthYYYYMM, datasource ); + const outputCsvData = await transferData( + context, + baseData, + baseDataFromDeletedAccounts, + targetMonthYYYYMM + ); // TODO: 後続処理の呼び出しイメージ(別タスクで追加) - // const outputCsvData = await transferData(context, baseData, baseDataFromDeletedAccounts); // await outputData(context, blobstorageService, outputCsvData); } catch (e) { context.log("analysisLicensesProcessing failed."); @@ -367,3 +384,1394 @@ type BaseDataFromDeletedAccounts = { deletedLicensesExpiredInTargetMonth: LicenseArchive[]; deletedSwitchedlicensesInTargetMonth: LicenseAllocationHistoryArchive[]; }; + +type outputDataAnalysisLicensesCSV = { + outputDataUS: string[]; + outputDataEU: string[]; + outputDataAU: string[]; +}; + +/** + * アカウントと紐づくユーザー、ライセンスからCSV出力用の配列を作成する + * @param context + * @param baseData + * @param baseDataFromDeletedAccounts + * @param targetMonthYYYYMM + * @returns outputDataAnalysisLicensesCSV + */ +export async function transferData( + context: InvocationContext, + baseData: BaseData, + baseDataFromDeletedAccounts: BaseDataFromDeletedAccounts, + targetMonthYYYYMM: string +): Promise { + context.log("[IN]transferData"); + class userIdAndRoles { + id: number; + role: string; + } + const accountsAndUsersFromTier5 = baseData.accountsAndUsersFromTier5; + const validLicenses = baseData.avairableLicenses; + const currentMonthIssuedLicenses = baseData.licensesIssuedInTargetMonth; + const invalidLicenses = baseData.licensesExpiredInTargetMonth; + const switchedLicenses = baseData.switchedlicensesInTargetMonth; + const deletedAccountsAndUsersFromTier5 = + baseDataFromDeletedAccounts.deletedAccountsAndUsersFromTier5; + const deletedValidLicenses = + baseDataFromDeletedAccounts.deletedAvairableLicenses; + const deletedCurrentMonthIssuedLicenses = + baseDataFromDeletedAccounts.deletedLicensesIssuedInTargetMonth; + const deletedInvalidLicenses = + baseDataFromDeletedAccounts.deletedLicensesExpiredInTargetMonth; + const deletedSwitchedLicenses = + baseDataFromDeletedAccounts.deletedSwitchedlicensesInTargetMonth; + + // 出力データ格納配列 + let outputDataUS: string[] = []; + let outputDataEU: string[] = []; + let outputDataAU: string[] = []; + // 出力データのヘッダーを作成 + const header = [ + '"' + LICENSE_COUNT_ANALYSIS_HEADER.ACCOUNT + '",', + '"' + LICENSE_COUNT_ANALYSIS_HEADER.TARGET_YEAE_AND_MONTH + '",', + '"' + LICENSE_COUNT_ANALYSIS_HEADER.CATEGORY_1 + '",', + '"' + LICENSE_COUNT_ANALYSIS_HEADER.CATEGORY_2 + '",', + '"' + LICENSE_COUNT_ANALYSIS_HEADER.LICENSE_TYPE + '",', + '"' + LICENSE_COUNT_ANALYSIS_HEADER.ROLE + '",', + '"' + LICENSE_COUNT_ANALYSIS_HEADER.COUNT + '"\r\n', + ] as string[]; + // ヘッダーを出力データに追加 + outputDataUS.push(...header); + outputDataEU.push(...header); + outputDataAU.push(...header); + + // ユーザーIDとロールを格納する配列(型が違う為新たに作成する) + let tier5userIdAndRoles: userIdAndRoles[] = []; + try { + // 第五階層のアカウントごとにループ + for (const account of accountsAndUsersFromTier5) { + // account.userとaccount.userArchiveが存在しない場合次のアカウントに進む + if (!account.user && !account.userArchive) { + console.log( + "account.user and account.userArchive is not exist.accountId:" + + account.id + ); + continue; + } + + // ユーザーとユーザーアーカイブからユーザーIDとロールを取得する + if (account.user) { + tier5userIdAndRoles = account.user.map((user) => { + return { id: user.id, role: user.role }; + }); + } + if (account.userArchive) { + tier5userIdAndRoles = tier5userIdAndRoles.concat( + account.userArchive.map((userArchive) => { + return { id: userArchive.id, role: userArchive.role }; + }) + ); + } + // アカウントに紐づくライセンスを取得 + const accountLicenses = validLicenses.filter( + (license) => license.account_id === account.id + ); + // 抽出したライセンスを種別ごとに分ける。(typeカラムで判別)(トライアル・通常・カード) + const trialLicenses = accountLicenses.filter( + (license) => license.type === LICENSE_TYPE.TRIAL + ); + const normalLicenses = accountLicenses.filter( + (license) => license.type === LICENSE_TYPE.NORMAL + ); + const cardLicenses = accountLicenses.filter( + (license) => license.type === LICENSE_TYPE.CARD + ); + // 種別ごとのライセンスから使用中のライセンスを抽出statusカラムがAllocated + const usedTrialLicenses = trialLicenses.filter( + (license) => license.status === LICENSE_ALLOCATED_STATUS.ALLOCATED + ); + const usedNormalLicenses = normalLicenses.filter( + (license) => license.status === LICENSE_ALLOCATED_STATUS.ALLOCATED + ); + const usedCardLicenses = cardLicenses.filter( + (license) => license.status === LICENSE_ALLOCATED_STATUS.ALLOCATED + ); + // どのロールのユーザーが使用しているライセンスかを判別し、ロールごとに分ける。 + // (allcated_user_idからユーザーを特定) + // (Author・Typist・None) + const usedTrialLicensesAuthor = usedTrialLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.AUTHOR + ) + ); + const usedTrialLicensesTypist = usedTrialLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.TYPIST + ) + ); + const usedTrialLicensesNone = usedTrialLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.NONE + ) + ); + const usedNormalLicensesAuthor = usedNormalLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.AUTHOR + ) + ); + const usedNormalLicensesTypist = usedNormalLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.TYPIST + ) + ); + const usedNormalLicensesNone = usedNormalLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.NONE + ) + ); + const usedCardLicensesAuthor = usedCardLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.AUTHOR + ) + ); + const usedCardLicensesTypist = usedCardLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.TYPIST + ) + ); + const usedCardLicensesNone = usedCardLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.NONE + ) + ); + // 使用中のライセンスの数をカウント + const trialLicensesCount = trialLicenses.length; + const normalLicensesCount = normalLicenses.length; + const cardLicensesCount = cardLicenses.length; + const usedTrialLicensesAuthorCount = usedTrialLicensesAuthor.length; + const usedTrialLicensesTypistCount = usedTrialLicensesTypist.length; + const usedTrialLicensesNoneCount = usedTrialLicensesNone.length; + const usedNormalLicensesAuthorCount = usedNormalLicensesAuthor.length; + const usedNormalLicensesTypistCount = usedNormalLicensesTypist.length; + const usedNormalLicensesNoneCount = usedNormalLicensesNone.length; + const usedCardLicensesAuthorCount = usedCardLicensesAuthor.length; + const usedCardLicensesTypistCount = usedCardLicensesTypist.length; + const usedCardLicensesNoneCount = usedCardLicensesNone.length; + + // アカウントに紐づく当月発行ライセンスを取得 + const accountCurrentMonthIssuedLicenses = + currentMonthIssuedLicenses.filter( + (license) => license.account_id === account.id + ); + // 当月発行ライセンスを種別ごとに分ける。(typeカラムで判別)(トライアル・通常・カード) + const currentMonthIssuedTrialLicenses = + accountCurrentMonthIssuedLicenses.filter( + (license) => license.type === LICENSE_TYPE.TRIAL + ); + const currentMonthIssuedNormalLicenses = + accountCurrentMonthIssuedLicenses.filter( + (license) => license.type === LICENSE_TYPE.NORMAL + ); + const currentMonthIssuedCardLicenses = + accountCurrentMonthIssuedLicenses.filter( + (license) => license.type === LICENSE_TYPE.CARD + ); + // 当月発行ライセンスの数をカウント + const currentMonthIssuedTrialLicensesCount = + currentMonthIssuedTrialLicenses.length; + const currentMonthIssuedNormalLicensesCount = + currentMonthIssuedNormalLicenses.length; + const currentMonthIssuedCardLicensesCount = + currentMonthIssuedCardLicenses.length; + + // アカウントに紐づく失効ライセンスを取得 + const accountInvalidLicenses = invalidLicenses.filter( + (license) => license.account_id === account.id + ); + // 失効ライセンスを種別ごとに分ける。(typeカラムで判別)(トライアル・通常・カード) + const invalidTrialLicenses = accountInvalidLicenses.filter( + (license) => license.type === LICENSE_TYPE.TRIAL + ); + const invalidNormalLicenses = accountInvalidLicenses.filter( + (license) => license.type === LICENSE_TYPE.NORMAL + ); + const invalidCardLicenses = accountInvalidLicenses.filter( + (license) => license.type === LICENSE_TYPE.CARD + ); + // どのロールのユーザーに割り当てたまま失効したライセンスかを判別し、ロールごとに分ける。 + //(allcated_user_idからユーザーを特定、値がない場合は未割当) + // (Author・Typist・None・未割当) + const invalidTrialLicensesAuthor = invalidTrialLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.AUTHOR + ) + ); + const invalidTrialLicensesTypist = invalidTrialLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.TYPIST + ) + ); + const invalidTrialLicensesNone = invalidTrialLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.NONE + ) + ); + const invalidNormalLicensesAuthor = invalidNormalLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.AUTHOR + ) + ); + const invalidNormalLicensesTypist = invalidNormalLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.TYPIST + ) + ); + const invalidNormalLicensesNone = invalidNormalLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.NONE + ) + ); + const invalidCardLicensesAuthor = invalidCardLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.AUTHOR + ) + ); + const invalidCardLicensesTypist = invalidCardLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.TYPIST + ) + ); + const invalidCardLicensesNone = invalidCardLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.allocated_user_id && + user.role === USER_ROLES.NONE + ) + ); + const invalidTrialLicensesUnallocated = invalidTrialLicenses.filter( + (license) => !license.allocated_user_id + ); + const invalidNormalLicensesUnallocated = invalidNormalLicenses.filter( + (license) => !license.allocated_user_id + ); + const invalidCardLicensesUnallocated = invalidCardLicenses.filter( + (license) => !license.allocated_user_id + ); + // 失効ライセンスの数をカウント + const invalidTrialLicensesAuthorCount = invalidTrialLicensesAuthor.length; + const invalidTrialLicensesTypistCount = invalidTrialLicensesTypist.length; + const invalidTrialLicensesNoneCount = invalidTrialLicensesNone.length; + const invalidTrialLicensesUnallocatedCount = + invalidTrialLicensesUnallocated.length; + const invalidNormalLicensesAuthorCount = + invalidNormalLicensesAuthor.length; + const invalidNormalLicensesTypistCount = + invalidNormalLicensesTypist.length; + const invalidNormalLicensesNoneCount = invalidNormalLicensesNone.length; + const invalidNormalLicensesUnallocatedCount = + invalidNormalLicensesUnallocated.length; + const invalidCardLicensesAuthorCount = invalidCardLicensesAuthor.length; + const invalidCardLicensesTypistCount = invalidCardLicensesTypist.length; + const invalidCardLicensesNoneCount = invalidCardLicensesNone.length; + const invalidCardLicensesUnallocatedCount = + invalidCardLicensesUnallocated.length; + + // アカウントに紐づく切り替えライセンスを取得 + const accountSwitchedLicenses = switchedLicenses.filter( + (license) => license.account_id === account.id + ); + // どの種別のライセンスから切り替えられたかで分ける(switch_from_typeカラムで判別)(トライアル・カード) + const switchedTrialLicenses = accountSwitchedLicenses.filter( + (license) => license.switch_from_type === SWITCH_FROM_TYPE.TRIAL + ); + const switchedCardLicenses = accountSwitchedLicenses.filter( + (license) => license.switch_from_type === SWITCH_FROM_TYPE.CARD + ); + // どのロールのユーザーに対して切り替えが行われたかで分ける。 + //(user_idからユーザーを特定) + //(Typist・Author・None) + const switchedTypistLicensesTypist = switchedTrialLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.user_id && user.role === USER_ROLES.TYPIST + ) + ); + const switchedTypistLicensesAuthor = switchedTrialLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.user_id && user.role === USER_ROLES.AUTHOR + ) + ); + const switchedTypistLicensesNone = switchedTrialLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.user_id && user.role === USER_ROLES.NONE + ) + ); + const switchedCardLicensesTypist = switchedCardLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.user_id && user.role === USER_ROLES.TYPIST + ) + ); + const switchedCardLicensesAuthor = switchedCardLicenses.filter( + (license) => + tier5userIdAndRoles.find( + (user) => + user.id === license.user_id && user.role === USER_ROLES.AUTHOR + ) + ); + const switchedCardLicensesNone = switchedCardLicenses.filter((license) => + tier5userIdAndRoles.find( + (user) => user.id === license.user_id && user.role === USER_ROLES.NONE + ) + ); + // 切り替えライセンスの数をカウント + const switchedTypistLicensesTypistCount = + switchedTypistLicensesTypist.length; + const switchedTypistLicensesAuthorCount = + switchedTypistLicensesAuthor.length; + const switchedTypistLicensesNoneCount = switchedTypistLicensesNone.length; + const switchedCardLicensesTypistCount = switchedCardLicensesTypist.length; + const switchedCardLicensesAuthorCount = switchedCardLicensesAuthor.length; + const switchedCardLicensesNoneCount = switchedCardLicensesNone.length; + + // 国に対応したリージョンに応じた配列に格納する + if (BLOB_STORAGE_REGION_US.includes(account.country)) { + outputDataUS = outputDataUS.concat( + await createOutputData( + context, + account.company_name, + targetMonthYYYYMM, + trialLicensesCount, + normalLicensesCount, + cardLicensesCount, + usedTrialLicensesAuthorCount, + usedTrialLicensesTypistCount, + usedTrialLicensesNoneCount, + usedNormalLicensesAuthorCount, + usedNormalLicensesTypistCount, + usedNormalLicensesNoneCount, + usedCardLicensesAuthorCount, + usedCardLicensesTypistCount, + usedCardLicensesNoneCount, + currentMonthIssuedTrialLicensesCount, + currentMonthIssuedNormalLicensesCount, + currentMonthIssuedCardLicensesCount, + invalidTrialLicensesAuthorCount, + invalidTrialLicensesTypistCount, + invalidTrialLicensesNoneCount, + invalidTrialLicensesUnallocatedCount, + invalidNormalLicensesAuthorCount, + invalidNormalLicensesTypistCount, + invalidNormalLicensesNoneCount, + invalidNormalLicensesUnallocatedCount, + invalidCardLicensesAuthorCount, + invalidCardLicensesTypistCount, + invalidCardLicensesNoneCount, + invalidCardLicensesUnallocatedCount, + switchedTypistLicensesAuthorCount, + switchedTypistLicensesTypistCount, + switchedTypistLicensesNoneCount, + switchedCardLicensesAuthorCount, + switchedCardLicensesTypistCount, + switchedCardLicensesNoneCount + ) + ); + } else if (BLOB_STORAGE_REGION_EU.includes(account.country)) { + outputDataEU = outputDataEU.concat( + await createOutputData( + context, + account.company_name, + targetMonthYYYYMM, + trialLicensesCount, + normalLicensesCount, + cardLicensesCount, + usedTrialLicensesAuthorCount, + usedTrialLicensesTypistCount, + usedTrialLicensesNoneCount, + usedNormalLicensesAuthorCount, + usedNormalLicensesTypistCount, + usedNormalLicensesNoneCount, + usedCardLicensesAuthorCount, + usedCardLicensesTypistCount, + usedCardLicensesNoneCount, + currentMonthIssuedTrialLicensesCount, + currentMonthIssuedNormalLicensesCount, + currentMonthIssuedCardLicensesCount, + invalidTrialLicensesAuthorCount, + invalidTrialLicensesTypistCount, + invalidTrialLicensesNoneCount, + invalidTrialLicensesUnallocatedCount, + invalidNormalLicensesAuthorCount, + invalidNormalLicensesTypistCount, + invalidNormalLicensesNoneCount, + invalidNormalLicensesUnallocatedCount, + invalidCardLicensesAuthorCount, + invalidCardLicensesTypistCount, + invalidCardLicensesNoneCount, + invalidCardLicensesUnallocatedCount, + switchedTypistLicensesAuthorCount, + switchedTypistLicensesTypistCount, + switchedTypistLicensesNoneCount, + switchedCardLicensesAuthorCount, + switchedCardLicensesTypistCount, + switchedCardLicensesNoneCount + ) + ); + } else if (BLOB_STORAGE_REGION_AU.includes(account.country)) { + outputDataAU = outputDataAU.concat( + await createOutputData( + context, + account.company_name, + targetMonthYYYYMM, + trialLicensesCount, + normalLicensesCount, + cardLicensesCount, + usedTrialLicensesAuthorCount, + usedTrialLicensesTypistCount, + usedTrialLicensesNoneCount, + usedNormalLicensesAuthorCount, + usedNormalLicensesTypistCount, + usedNormalLicensesNoneCount, + usedCardLicensesAuthorCount, + usedCardLicensesTypistCount, + usedCardLicensesNoneCount, + currentMonthIssuedTrialLicensesCount, + currentMonthIssuedNormalLicensesCount, + currentMonthIssuedCardLicensesCount, + invalidTrialLicensesAuthorCount, + invalidTrialLicensesTypistCount, + invalidTrialLicensesNoneCount, + invalidTrialLicensesUnallocatedCount, + invalidNormalLicensesAuthorCount, + invalidNormalLicensesTypistCount, + invalidNormalLicensesNoneCount, + invalidNormalLicensesUnallocatedCount, + invalidCardLicensesAuthorCount, + invalidCardLicensesTypistCount, + invalidCardLicensesNoneCount, + invalidCardLicensesUnallocatedCount, + switchedTypistLicensesAuthorCount, + switchedTypistLicensesTypistCount, + switchedTypistLicensesNoneCount, + switchedCardLicensesAuthorCount, + switchedCardLicensesTypistCount, + switchedCardLicensesNoneCount + ) + ); + } else { + throw new Error("invalid country"); + } + } + // 削除版 + tier5userIdAndRoles = []; + // 第五階層のアカウントごとにループ + for (const account of deletedAccountsAndUsersFromTier5) { + // account.userArchiveが存在しない場合次のアカウントに進む + if (!account.userArchive) { + continue; + } + // アカウントに紐づくユーザーを取得 + if (account.userArchive) { + tier5userIdAndRoles = account.userArchive.map((userArchive) => { + return { id: userArchive.id, role: userArchive.role }; + }); + } + // アカウントに紐づくライセンスを取得 + const accountLicenses = deletedValidLicenses.filter( + (license) => license.account_id === account.id + ); + // 抽出したライセンスを種別ごとに分ける。(typeカラムで判別)(トライアル・通常・カード) + const trialLicenses = accountLicenses.filter( + (license) => license.type === LICENSE_TYPE.TRIAL + ); + const normalLicenses = accountLicenses.filter( + (license) => license.type === LICENSE_TYPE.NORMAL + ); + const cardLicenses = accountLicenses.filter( + (license) => license.type === LICENSE_TYPE.CARD + ); + // 種別ごとのライセンスから使用中のライセンスを抽出statusカラムがAllocated + const usedTrialLicenses = trialLicenses.filter( + (license) => license.status === LICENSE_ALLOCATED_STATUS.ALLOCATED + ); + const usedNormalLicenses = normalLicenses.filter( + (license) => license.status === LICENSE_ALLOCATED_STATUS.ALLOCATED + ); + const usedCardLicenses = cardLicenses.filter( + (license) => license.status === LICENSE_ALLOCATED_STATUS.ALLOCATED + ); + // どのロールのユーザーが使用しているライセンスかを判別し、ロールごとに分ける。 + // (allcated_user_idからユーザーを特定) + // (Author・Typist・None) + const usedTrialLicensesAuthor = usedTrialLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.AUTHOR + ); + const usedTrialLicensesTypist = usedTrialLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.TYPIST + ); + const usedTrialLicensesNone = usedTrialLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.NONE + ); + const usedNormalLicensesAuthor = usedNormalLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.AUTHOR + ); + const usedNormalLicensesTypist = usedNormalLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.TYPIST + ); + const usedNormalLicensesNone = usedNormalLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.NONE + ); + const usedCardLicensesAuthor = usedCardLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.AUTHOR + ); + const usedCardLicensesTypist = usedCardLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.TYPIST + ); + const usedCardLicensesNone = usedCardLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.NONE + ); + // 使用中のライセンスの数をカウント + const trialLicensesCount = trialLicenses.length; + const normalLicensesCount = normalLicenses.length; + const cardLicensesCount = cardLicenses.length; + const usedTrialLicensesAuthorCount = usedTrialLicensesAuthor.length; + const usedTrialLicensesTypistCount = usedTrialLicensesTypist.length; + const usedTrialLicensesNoneCount = usedTrialLicensesNone.length; + const usedNormalLicensesAuthorCount = usedNormalLicensesAuthor.length; + const usedNormalLicensesTypistCount = usedNormalLicensesTypist.length; + const usedNormalLicensesNoneCount = usedNormalLicensesNone.length; + const usedCardLicensesAuthorCount = usedCardLicensesAuthor.length; + const usedCardLicensesTypistCount = usedCardLicensesTypist.length; + const usedCardLicensesNoneCount = usedCardLicensesNone.length; + + // アカウントに紐づく当月発行ライセンスを取得 + const accountCurrentMonthIssuedLicenses = + deletedCurrentMonthIssuedLicenses.filter( + (license) => license.account_id === account.id + ); + // 当月発行ライセンスを種別ごとに分ける。(typeカラムで判別)(トライアル・通常・カード) + const currentMonthIssuedTrialLicenses = + accountCurrentMonthIssuedLicenses.filter( + (license) => license.type === LICENSE_TYPE.TRIAL + ); + const currentMonthIssuedNormalLicenses = + accountCurrentMonthIssuedLicenses.filter( + (license) => license.type === LICENSE_TYPE.NORMAL + ); + const currentMonthIssuedCardLicenses = + accountCurrentMonthIssuedLicenses.filter( + (license) => license.type === LICENSE_TYPE.CARD + ); + // 当月発行ライセンスの数をカウント + const currentMonthIssuedTrialLicensesCount = + currentMonthIssuedTrialLicenses.length; + const currentMonthIssuedNormalLicensesCount = + currentMonthIssuedNormalLicenses.length; + const currentMonthIssuedCardLicensesCount = + currentMonthIssuedCardLicenses.length; + + // アカウントに紐づく失効ライセンスを取得 + const accountInvalidLicenses = deletedInvalidLicenses.filter( + (license) => license.account_id === account.id + ); + // 失効ライセンスを種別ごとに分ける。(typeカラムで判別)(トライアル・通常・カード) + const invalidTrialLicenses = accountInvalidLicenses.filter( + (license) => license.type === LICENSE_TYPE.TRIAL + ); + const invalidNormalLicenses = accountInvalidLicenses.filter( + (license) => license.type === LICENSE_TYPE.NORMAL + ); + const invalidCardLicenses = accountInvalidLicenses.filter( + (license) => license.type === LICENSE_TYPE.CARD + ); + // どのロールのユーザーに割り当てたまま失効したライセンスかを判別し、ロールごとに分ける。 + //(allcated_user_idからユーザーを特定、値がない場合は未割当) + // (Author・Typist・None・未割当) + const invalidTrialLicensesAuthor = invalidTrialLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.AUTHOR + ); + const invalidTrialLicensesTypist = invalidTrialLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.TYPIST + ); + const invalidTrialLicensesNone = invalidTrialLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.NONE + ); + const invalidNormalLicensesAuthor = invalidNormalLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.AUTHOR + ); + const invalidNormalLicensesTypist = invalidNormalLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.TYPIST + ); + const invalidNormalLicensesNone = invalidNormalLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.NONE + ); + const invalidCardLicensesAuthor = invalidCardLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.AUTHOR + ); + const invalidCardLicensesTypist = invalidCardLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.TYPIST + ); + const invalidCardLicensesNone = invalidCardLicenses.filter( + (license) => + tier5userIdAndRoles?.find( + (user) => user.id === license.allocated_user_id + )?.role === USER_ROLES.NONE + ); + const invalidTrialLicensesUnallocated = invalidTrialLicenses.filter( + (license) => !license.allocated_user_id + ); + const invalidNormalLicensesUnallocated = invalidNormalLicenses.filter( + (license) => !license.allocated_user_id + ); + const invalidCardLicensesUnallocated = invalidCardLicenses.filter( + (license) => !license.allocated_user_id + ); + // 失効ライセンスの数をカウント + const invalidTrialLicensesAuthorCount = invalidTrialLicensesAuthor.length; + const invalidTrialLicensesTypistCount = invalidTrialLicensesTypist.length; + const invalidTrialLicensesNoneCount = invalidTrialLicensesNone.length; + const invalidTrialLicensesUnallocatedCount = + invalidTrialLicensesUnallocated.length; + const invalidNormalLicensesAuthorCount = + invalidNormalLicensesAuthor.length; + const invalidNormalLicensesTypistCount = + invalidNormalLicensesTypist.length; + const invalidNormalLicensesNoneCount = invalidNormalLicensesNone.length; + const invalidNormalLicensesUnallocatedCount = + invalidNormalLicensesUnallocated.length; + const invalidCardLicensesAuthorCount = invalidCardLicensesAuthor.length; + const invalidCardLicensesTypistCount = invalidCardLicensesTypist.length; + const invalidCardLicensesNoneCount = invalidCardLicensesNone.length; + const invalidCardLicensesUnallocatedCount = + invalidCardLicensesUnallocated.length; + + // アカウントに紐づく切り替えライセンスを取得 + const accountSwitchedLicenses = deletedSwitchedLicenses.filter( + (license) => license.account_id === account.id + ); + // どの種別のライセンスから切り替えられたかで分ける(switch_from_typeカラムで判別)(トライアル・カード) + const switchedTrialLicenses = accountSwitchedLicenses.filter( + (license) => license.switch_from_type === SWITCH_FROM_TYPE.TRIAL + ); + const switchedCardLicenses = accountSwitchedLicenses.filter( + (license) => license.switch_from_type === SWITCH_FROM_TYPE.CARD + ); + // どのロールのユーザーに対して切り替えが行われたかで分ける。 + //(user_idからユーザーを特定) + //(Typist・Author・None) + const switchedTypistLicensesTypist = switchedTrialLicenses.filter( + (license) => + tier5userIdAndRoles?.find((user) => user.id === license.user_id) + ?.role === USER_ROLES.TYPIST + ); + const switchedTypistLicensesAuthor = switchedTrialLicenses.filter( + (license) => + tier5userIdAndRoles?.find((user) => user.id === license.user_id) + ?.role === USER_ROLES.AUTHOR + ); + const switchedTypistLicensesNone = switchedTrialLicenses.filter( + (license) => + tier5userIdAndRoles?.find((user) => user.id === license.user_id) + ?.role === USER_ROLES.NONE + ); + const switchedCardLicensesTypist = switchedCardLicenses.filter( + (license) => + tier5userIdAndRoles?.find((user) => user.id === license.user_id) + ?.role === USER_ROLES.TYPIST + ); + const switchedCardLicensesAuthor = switchedCardLicenses.filter( + (license) => + tier5userIdAndRoles?.find((user) => user.id === license.user_id) + ?.role === USER_ROLES.AUTHOR + ); + const switchedCardLicensesNone = switchedCardLicenses.filter( + (license) => + tier5userIdAndRoles?.find((user) => user.id === license.user_id) + ?.role === USER_ROLES.NONE + ); + // 切り替えライセンスの数をカウント + const switchedTypistLicensesTypistCount = + switchedTypistLicensesTypist.length; + const switchedTypistLicensesAuthorCount = + switchedTypistLicensesAuthor.length; + const switchedTypistLicensesNoneCount = switchedTypistLicensesNone.length; + const switchedCardLicensesTypistCount = switchedCardLicensesTypist.length; + const switchedCardLicensesAuthorCount = switchedCardLicensesAuthor.length; + const switchedCardLicensesNoneCount = switchedCardLicensesNone.length; + + // 国に対応したリージョンに応じた配列に格納する + if (BLOB_STORAGE_REGION_US.includes(account.country)) { + outputDataUS = outputDataUS.concat( + await createOutputData( + context, + account.id.toString(), + targetMonthYYYYMM, + trialLicensesCount, + normalLicensesCount, + cardLicensesCount, + usedTrialLicensesAuthorCount, + usedTrialLicensesTypistCount, + usedTrialLicensesNoneCount, + usedNormalLicensesAuthorCount, + usedNormalLicensesTypistCount, + usedNormalLicensesNoneCount, + usedCardLicensesAuthorCount, + usedCardLicensesTypistCount, + usedCardLicensesNoneCount, + currentMonthIssuedTrialLicensesCount, + currentMonthIssuedNormalLicensesCount, + currentMonthIssuedCardLicensesCount, + invalidTrialLicensesAuthorCount, + invalidTrialLicensesTypistCount, + invalidTrialLicensesNoneCount, + invalidTrialLicensesUnallocatedCount, + invalidNormalLicensesAuthorCount, + invalidNormalLicensesTypistCount, + invalidNormalLicensesNoneCount, + invalidNormalLicensesUnallocatedCount, + invalidCardLicensesAuthorCount, + invalidCardLicensesTypistCount, + invalidCardLicensesNoneCount, + invalidCardLicensesUnallocatedCount, + switchedTypistLicensesAuthorCount, + switchedTypistLicensesTypistCount, + switchedTypistLicensesNoneCount, + switchedCardLicensesAuthorCount, + switchedCardLicensesTypistCount, + switchedCardLicensesNoneCount + ) + ); + } else if (BLOB_STORAGE_REGION_EU.includes(account.country)) { + outputDataEU = outputDataEU.concat( + await createOutputData( + context, + account.id.toString(), + targetMonthYYYYMM, + trialLicensesCount, + normalLicensesCount, + cardLicensesCount, + usedTrialLicensesAuthorCount, + usedTrialLicensesTypistCount, + usedTrialLicensesNoneCount, + usedNormalLicensesAuthorCount, + usedNormalLicensesTypistCount, + usedNormalLicensesNoneCount, + usedCardLicensesAuthorCount, + usedCardLicensesTypistCount, + usedCardLicensesNoneCount, + currentMonthIssuedTrialLicensesCount, + currentMonthIssuedNormalLicensesCount, + currentMonthIssuedCardLicensesCount, + invalidTrialLicensesAuthorCount, + invalidTrialLicensesTypistCount, + invalidTrialLicensesNoneCount, + invalidTrialLicensesUnallocatedCount, + invalidNormalLicensesAuthorCount, + invalidNormalLicensesTypistCount, + invalidNormalLicensesNoneCount, + invalidNormalLicensesUnallocatedCount, + invalidCardLicensesAuthorCount, + invalidCardLicensesTypistCount, + invalidCardLicensesNoneCount, + invalidCardLicensesUnallocatedCount, + switchedTypistLicensesAuthorCount, + switchedTypistLicensesTypistCount, + switchedTypistLicensesNoneCount, + switchedCardLicensesAuthorCount, + switchedCardLicensesTypistCount, + switchedCardLicensesNoneCount + ) + ); + } else if (BLOB_STORAGE_REGION_AU.includes(account.country)) { + outputDataAU = outputDataAU.concat( + await createOutputData( + context, + account.id.toString(), + targetMonthYYYYMM, + trialLicensesCount, + normalLicensesCount, + cardLicensesCount, + usedTrialLicensesAuthorCount, + usedTrialLicensesTypistCount, + usedTrialLicensesNoneCount, + usedNormalLicensesAuthorCount, + usedNormalLicensesTypistCount, + usedNormalLicensesNoneCount, + usedCardLicensesAuthorCount, + usedCardLicensesTypistCount, + usedCardLicensesNoneCount, + currentMonthIssuedTrialLicensesCount, + currentMonthIssuedNormalLicensesCount, + currentMonthIssuedCardLicensesCount, + invalidTrialLicensesAuthorCount, + invalidTrialLicensesTypistCount, + invalidTrialLicensesNoneCount, + invalidTrialLicensesUnallocatedCount, + invalidNormalLicensesAuthorCount, + invalidNormalLicensesTypistCount, + invalidNormalLicensesNoneCount, + invalidNormalLicensesUnallocatedCount, + invalidCardLicensesAuthorCount, + invalidCardLicensesTypistCount, + invalidCardLicensesNoneCount, + invalidCardLicensesUnallocatedCount, + switchedTypistLicensesAuthorCount, + switchedTypistLicensesTypistCount, + switchedTypistLicensesNoneCount, + switchedCardLicensesAuthorCount, + switchedCardLicensesTypistCount, + switchedCardLicensesNoneCount + ) + ); + } + } + // outputDataUSをローカルにCSV出力(テスト用、別タスクで消す) + // outputDataUSの配列をCSV形式に変換 + /*let csvContentUS = ""; + for (let i = 0; i < outputDataUS.length; i++) { + //カンマ区切りの文字列を作成 + + csvContentUS += outputDataUS[i]; + } + // CSVファイルを出力 + const filePathUS = path.join(__dirname, "outputDataUS.csv"); + fs.writeFileSync(filePathUS, csvContentUS); + */ + return { + outputDataUS: outputDataUS, + outputDataEU: outputDataEU, + outputDataAU: outputDataAU, + }; + } catch (e) { + context.log("transferData failed."); + context.error(e); + throw e; + } finally { + context.log("[OUT]transferData"); + } +} + +/** + * 出力データ作成 + * @param context + * @param company_name + * @param targetMonthYYYYMM + * @param trialLicensesCount + * @param normalLicensesCount + * @param cardLicensesCount + * @param usedTrialLicensesAuthorCount + * @param usedTrialLicensesTypistCount + * @param usedTrialLicensesNoneCount + * @param usedNormalLicensesAuthorCount + * @param usedNormalLicensesTypistCount + * @param usedNormalLicensesNoneCount + * @param usedCardLicensesAuthorCount + * @param usedCardLicensesTypistCount + * @param usedCardLicensesNoneCount + * @param currentMonthIssuedTrialLicensesCount + * @param currentMonthIssuedNormalLicensesCount + * @param currentMonthIssuedCardLicensesCount + * @param invalidTrialLicensesAuthorCount + * @param invalidTrialLicensesTypistCount + * @param invalidTrialLicensesNoneCount + * @param invalidTrialLicensesUnallocatedCount + * @param invalidNormalLicensesAuthorCount + * @param invalidNormalLicensesTypistCount + * @param invalidNormalLicensesNoneCount + * @param invalidNormalLicensesUnallocatedCount + * @param invalidCardLicensesAuthorCount + * @param invalidCardLicensesTypistCount + * @param invalidCardLicensesNoneCount + * @param invalidCardLicensesUnallocatedCount + * @param switchedTypistLicensesAuthorCount + * @param switchedTypistLicensesTypistCount + * @param switchedTypistLicensesNoneCount + * @param switchedCardLicensesAuthorCount + * @param switchedCardLicensesTypistCount + * @param switchedCardLicensesNoneCount + * @returns string[] + */ +async function createOutputData( + context: InvocationContext, + company_name: string, + targetMonthYYYYMM: string, + trialLicensesCount: number, + normalLicensesCount: number, + cardLicensesCount: number, + usedTrialLicensesAuthorCount: number, + usedTrialLicensesTypistCount: number, + usedTrialLicensesNoneCount: number, + usedNormalLicensesAuthorCount: number, + usedNormalLicensesTypistCount: number, + usedNormalLicensesNoneCount: number, + usedCardLicensesAuthorCount: number, + usedCardLicensesTypistCount: number, + usedCardLicensesNoneCount: number, + currentMonthIssuedTrialLicensesCount: number, + currentMonthIssuedNormalLicensesCount: number, + currentMonthIssuedCardLicensesCount: number, + invalidTrialLicensesAuthorCount: number, + invalidTrialLicensesTypistCount: number, + invalidTrialLicensesNoneCount: number, + invalidTrialLicensesUnallocatedCount: number, + invalidNormalLicensesAuthorCount: number, + invalidNormalLicensesTypistCount: number, + invalidNormalLicensesNoneCount: number, + invalidNormalLicensesUnallocatedCount: number, + invalidCardLicensesAuthorCount: number, + invalidCardLicensesTypistCount: number, + invalidCardLicensesNoneCount: number, + invalidCardLicensesUnallocatedCount: number, + switchedTypistLicensesAuthorCount: number, + switchedTypistLicensesTypistCount: number, + switchedTypistLicensesNoneCount: number, + switchedCardLicensesAuthorCount: number, + switchedCardLicensesTypistCount: number, + switchedCardLicensesNoneCount: number +): Promise { + context.log("[IN]createOutputData"); + const resultOutputData: string[] = []; + try { + // アカウントが保持するトライアルライセンス[] + resultOutputData.push( + // 会社名(ダブルクォーテーションで囲む) + '"' + company_name + '",', + // 対象年月(先月)YYYYMM + // 2024年3月に実行した場合:202402 + '"' + targetMonthYYYYMM + '",', + // カテゴリー1 + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + // カテゴリー2 + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.OWNER_LICENSES + '",', + // ライセンス種別 + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL + '",', + // 役割 + '"' + "" + '",', + // 数量 + '"' + trialLicensesCount.toString() + '"\r\n' + ); + // アカウントが保持する通常ライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.OWNER_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.STANDARD + '",', + '"' + "" + '",', + '"' + normalLicensesCount.toString() + '"\r\n' + ); + // アカウントが保持するカードライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.OWNER_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.CARD + '",', + '"' + "" + '",', + '"' + cardLicensesCount.toString() + '"\r\n' + ); + // Authorが使用中のトライアルライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.IN_USE_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.AUTHOR + '",', + '"' + usedTrialLicensesAuthorCount.toString() + '"\r\n' + ); + // Typistが使用中のトライアルライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.IN_USE_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.TYPIST + '",', + '"' + usedTrialLicensesTypistCount.toString() + '"\r\n' + ); + // Noneが使用中のトライアルライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.IN_USE_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.NONE + '",', + '"' + usedTrialLicensesNoneCount.toString() + '"\r\n' + ); + // Authorが使用中の通常ライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.IN_USE_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.STANDARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.AUTHOR + '",', + '"' + usedNormalLicensesAuthorCount.toString() + '"\r\n' + ); + // Typistが使用中の通常ライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.IN_USE_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.STANDARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.TYPIST + '",', + '"' + usedNormalLicensesTypistCount.toString() + '"\r\n' + ); + // Noneが使用中の通常ライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.IN_USE_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.STANDARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.NONE + '",', + '"' + usedNormalLicensesNoneCount.toString() + '"\r\n' + ); + // Authorが使用中のカードライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.IN_USE_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.AUTHOR + '",', + '"' + usedCardLicensesAuthorCount.toString() + '"\r\n' + ); + // Typistが使用中のカードライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.IN_USE_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.TYPIST + '",', + '"' + usedCardLicensesTypistCount.toString() + '"\r\n' + ); + // Noneが使用中のカードライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_2.IN_USE_LICENSES + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.NONE + '",', + '"' + usedCardLicensesNoneCount.toString() + '"\r\n' + ); + // アカウントが保持する当月発行トライアルライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.NEW_ISSUE_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL + '",', + '"' + "" + '",', + '"' + currentMonthIssuedTrialLicensesCount.toString() + '"\r\n' + ); + // アカウントが保持する当月発行通常ライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.NEW_ISSUE_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.STANDARD + '",', + '"' + "" + '",', + '"' + currentMonthIssuedNormalLicensesCount.toString() + '"\r\n' + ); + // アカウントが保持する当月発行カードライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.NEW_ISSUE_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.CARD + '",', + '"' + "" + '",', + '"' + currentMonthIssuedCardLicensesCount.toString() + '"\r\n' + ); + // Authorに割り当てられたままの失効トライアルライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.AUTHOR + '",', + '"' + invalidTrialLicensesAuthorCount.toString() + '"\r\n' + ); + // Typistに割り当てられたままの失効トライアルライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.TYPIST + '",', + '"' + invalidTrialLicensesTypistCount.toString() + '"\r\n' + ); + // Noneに割り当てられたままの失効トライアルライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.NONE + '",', + '"' + invalidTrialLicensesNoneCount.toString() + '"\r\n' + ); + // 未割当の失効トライアルライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.UNALLOCATED + '",', + '"' + invalidTrialLicensesUnallocatedCount.toString() + '"\r\n' + ); + // Authorに割り当てられたままの失効通常ライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.STANDARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.AUTHOR + '",', + '"' + invalidNormalLicensesAuthorCount.toString() + '"\r\n' + ); + // Typistに割り当てられたままの失効通常ライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.STANDARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.TYPIST + '",', + '"' + invalidNormalLicensesTypistCount.toString() + '"\r\n' + ); + // Noneに割り当てられたままの失効通常ライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.STANDARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.NONE + '",', + '"' + invalidNormalLicensesNoneCount.toString() + '"\r\n' + ); + // 未割当の失効通常ライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.STANDARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.UNALLOCATED + '",', + '"' + invalidNormalLicensesUnallocatedCount.toString() + '"\r\n' + ); + // Authorに割り当てられたままの失効カードライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.AUTHOR + '",', + '"' + invalidCardLicensesAuthorCount.toString() + '"\r\n' + ); + // Typistに割り当てられたままの失効カードライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.TYPIST + '",', + '"' + invalidCardLicensesTypistCount.toString() + '"\r\n' + ); + // Noneに割り当てられたままの失効カードライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.NONE + '",', + '"' + invalidCardLicensesNoneCount.toString() + '"\r\n' + ); + // 未割当の失効カードライセンス[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.INVALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.UNALLOCATED + '",', + '"' + invalidCardLicensesUnallocatedCount.toString() + '"\r\n' + ); + // Authorにトライアルライセンスからの切り替え[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.SWITCH_FROM_TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.AUTHOR + '",', + '"' + switchedTypistLicensesAuthorCount.toString() + '"\r\n' + ); + // Typistにトライアルライセンスからの切り替え[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.SWITCH_FROM_TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.TYPIST + '",', + '"' + switchedTypistLicensesTypistCount.toString() + '"\r\n' + ); + // Noneにトライアルライセンスからの切り替え[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.SWITCH_FROM_TRIAL + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.NONE + '",', + '"' + switchedTypistLicensesNoneCount.toString() + '"\r\n' + ); + // Authorにカードライセンスからの切り替え[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.SWITCH_FROM_CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.AUTHOR + '",', + '"' + switchedCardLicensesAuthorCount.toString() + '"\r\n' + ); + // Typistにカードライセンスからの切り替え[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.SWITCH_FROM_CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.TYPIST + '",', + '"' + switchedCardLicensesTypistCount.toString() + '"\r\n' + ); + // Noneにカードライセンスからの切り替え[] + resultOutputData.push( + '"' + company_name + '",', + '"' + targetMonthYYYYMM + '",', + '"' + LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES + '",', + '"' + "" + '",', + '"' + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.SWITCH_FROM_CARD + '",', + '"' + LICENSE_COUNT_ANALYSIS_ROLE.NONE + '",', + '"' + switchedCardLicensesNoneCount.toString() + '"\r\n' + ); + return resultOutputData; + } catch (e) { + context.log("createOutputData failed."); + context.error(e); + throw e; + } finally { + context.log("[OUT]createOutputData"); + } +} diff --git a/dictation_function/src/test/analysisLicenses.spec.ts b/dictation_function/src/test/analysisLicenses.spec.ts index cd26bdb..4545ca7 100644 --- a/dictation_function/src/test/analysisLicenses.spec.ts +++ b/dictation_function/src/test/analysisLicenses.spec.ts @@ -2,6 +2,7 @@ import { DataSource } from "typeorm"; import { getBaseData, getBaseDataFromDeletedAccounts, + transferData, } from "../functions/analysisLicenses"; import { makeTestAccount, @@ -11,6 +12,7 @@ import { makeTestAccountArchive, createLicenseArchive, createLicenseAllocationHistoryArchive, + makeTestUser, } from "./common/utility"; import * as dotenv from "dotenv"; import { @@ -20,6 +22,9 @@ import { import { InvocationContext } from "@azure/functions"; import { LICENSE_ALLOCATED_STATUS, + LICENSE_COUNT_ANALYSIS_CATEGORY_1, + LICENSE_COUNT_ANALYSIS_CATEGORY_2, + LICENSE_COUNT_ANALYSIS_LICENSE_TYPE, SWITCH_FROM_TYPE, } from "../constants"; describe("analysisLicenses", () => { @@ -112,7 +117,7 @@ describe("analysisLicenses", () => { 1, null, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.ALLOCATED, null, null, @@ -125,7 +130,7 @@ describe("analysisLicenses", () => { 2, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, @@ -138,7 +143,7 @@ describe("analysisLicenses", () => { 3, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, @@ -152,7 +157,7 @@ describe("analysisLicenses", () => { 4, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.DELETED, null, null, @@ -173,7 +178,7 @@ describe("analysisLicenses", () => { 11, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.ALLOCATED, null, null, @@ -186,7 +191,7 @@ describe("analysisLicenses", () => { 12, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, @@ -199,7 +204,7 @@ describe("analysisLicenses", () => { 13, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, @@ -213,7 +218,7 @@ describe("analysisLicenses", () => { 14, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.DELETED, null, null, @@ -233,7 +238,7 @@ describe("analysisLicenses", () => { 21, lastMonth, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.ALLOCATED, null, null, @@ -246,7 +251,7 @@ describe("analysisLicenses", () => { 22, lastMonth, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, @@ -259,7 +264,7 @@ describe("analysisLicenses", () => { 23, lastMonth, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, @@ -273,7 +278,7 @@ describe("analysisLicenses", () => { 24, lastMonth, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.DELETED, null, null, @@ -287,7 +292,7 @@ describe("analysisLicenses", () => { 25, last2Month, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, @@ -391,10 +396,8 @@ describe("analysisLicenses", () => { expect(result.switchedlicensesInTargetMonth).toHaveLength(2); expect(result.switchedlicensesInTargetMonth[0].id).toBe(1); expect(result.switchedlicensesInTargetMonth[1].id).toBe(2); - }); - it("getBaseDataFromDeletedAccounts取得情報の確認", async () => { if (!source) fail(); const context = new InvocationContext(); @@ -456,7 +459,7 @@ describe("analysisLicenses", () => { 1, null, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.ALLOCATED, null, null, @@ -469,7 +472,7 @@ describe("analysisLicenses", () => { 2, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, @@ -482,7 +485,7 @@ describe("analysisLicenses", () => { 3, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, @@ -496,7 +499,7 @@ describe("analysisLicenses", () => { 4, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.DELETED, null, null, @@ -517,7 +520,7 @@ describe("analysisLicenses", () => { 11, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.ALLOCATED, null, null, @@ -530,7 +533,7 @@ describe("analysisLicenses", () => { 12, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, @@ -543,7 +546,7 @@ describe("analysisLicenses", () => { 13, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, @@ -557,7 +560,7 @@ describe("analysisLicenses", () => { 14, expiringSoonDate, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.DELETED, null, null, @@ -577,7 +580,7 @@ describe("analysisLicenses", () => { 21, lastMonth, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.ALLOCATED, null, null, @@ -590,7 +593,7 @@ describe("analysisLicenses", () => { 22, lastMonth, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, @@ -603,7 +606,7 @@ describe("analysisLicenses", () => { 23, lastMonth, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, @@ -617,7 +620,7 @@ describe("analysisLicenses", () => { 24, lastMonth, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.DELETED, null, null, @@ -631,7 +634,7 @@ describe("analysisLicenses", () => { 25, last2Month, account5_1.id, - "STANDARD", + "NORMAL", LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, @@ -688,7 +691,11 @@ describe("analysisLicenses", () => { SWITCH_FROM_TYPE.TRIAL ); - const result = await getBaseDataFromDeletedAccounts(context, lastMonthYYYYMM, source); + const result = await getBaseDataFromDeletedAccounts( + context, + lastMonthYYYYMM, + source + ); expect(result.deletedAccountsAndUsersFromTier5).toHaveLength(2); expect(result.deletedAccountsAndUsersFromTier5[0].id).toBe(account5_1.id); expect(result.deletedAccountsAndUsersFromTier5[1].id).toBe(account5_2.id); @@ -734,4 +741,484 @@ describe("analysisLicenses", () => { expect(result.deletedSwitchedlicensesInTargetMonth[1].id).toBe(2); }); + it("transferDataの確認", async () => { + // getBaseData取得情報の確認とgetBaseDataFromDeletedAccounts取得情報の確認 + if (!source) fail(); + const context = new InvocationContext(); + + const currentDate = new DateWithZeroTime(); + const expiringSoonDate = new ExpirationThresholdDate(currentDate.getTime()); + + // 現在の日付を取得 + const nowDate = new Date(); + + // 先月の日付を取得 + const lastMonth = new Date(nowDate); + lastMonth.setMonth(nowDate.getMonth() - 1); + const lastMonthYYYYMM = `${lastMonth.getFullYear()}${( + lastMonth.getMonth() + 1 + ) + .toString() + .padStart(2, "0")}`; + + // 先々月の日付を取得 + const last2Month = new Date(nowDate); + last2Month.setMonth(nowDate.getMonth() - 2); + const last2MonthYYYYMM = `${last2Month.getFullYear()}${( + last2Month.getMonth() + 1 + ) + .toString() + .padStart(2, "0")}`; + + // tier4とtier5のアカウント+管理者を作る + const { account: account4, admin: admin4 } = await makeTestAccount( + source, + { tier: 4 }, + { external_id: "external_id_tier4admin" } + ); + const { account: account5_1, admin: admin5_1_1 } = await makeTestAccount( + source, + { + tier: 5, + parent_account_id: account4.id, + }, + { + external_id: "external_id_tier5admin1", + role: "author", + } + ); + // 第五アカウントに紐づくユーザーを作成する + const user5_1_2 = await makeTestUser(source, { + account_id: account5_1.id, + role: "typist", + }); + const user5_1_3 = await makeTestUser(source, { + account_id: account5_1.id, + role: "typist", + }); + const user5_1_4 = await makeTestUser(source, { + account_id: account5_1.id, + role: "typist", + }); + const user5_1_5 = await makeTestUser(source, { + account_id: account5_1.id, + role: "none", + }); + const user5_1_6 = await makeTestUser(source, { + account_id: account5_1.id, + role: "author", + }); + const user5_1_7 = await makeTestUser(source, { + account_id: account5_1.id, + role: "typist", + }); + const user5_1_8 = await makeTestUser(source, { + account_id: account5_1.id, + role: "none", + }); + const user5_1_9 = await makeTestUser(source, { + account_id: account5_1.id, + role: "none", + }); + const user5_1_10 = await makeTestUser(source, { + account_id: account5_1.id, + role: "typist", + }); + + // 削除ユーザを作成する + const userArchive5_1_4 = await makeTestUserArchive(source, { + account_id: account5_1.id, + }); + // 第五階層以外だとヒットしないことの確認 + const userArchive4 = await makeTestUserArchive(source, { + account_id: account4.id, + }); + + // 所有ライセンス + // trialLicensesCount 3件 + // normalLicensesCount 5件 + // cardLicensesCount 4件 + // usedTrialLicensesAuthorCount 1件 + await createLicense( + source, + 1, + null, + account5_1.id, + "TRIAL", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + admin5_1_1.id, + null, + null, + null, + last2Month + ); + // usedTrialLicensesTypistCount 2件 + await createLicense( + source, + 2, + expiringSoonDate, + account5_1.id, + "TRIAL", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + user5_1_2.id, + null, + null, + null, + last2Month + ); + await createLicense( + source, + 3, + expiringSoonDate, + account5_1.id, + "TRIAL", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + user5_1_3.id, + null, + null, + null, + last2Month + ); + // usedTrialLicensesNoneCount 0件 + // usedNormalLicensesAuthorCount 0件 + // usedNormalLicensesTypistCount 1件 + await createLicense( + source, + 4, + expiringSoonDate, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + user5_1_4.id, + null, + null, + null, + last2Month + ); + // usedNormalLicensesNoneCount 1件 + await createLicense( + source, + 5, + expiringSoonDate, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + user5_1_5.id, + null, + null, + null, + last2Month + ); + // usedCardLicensesAuthorCount 1件 + await createLicense( + source, + 6, + expiringSoonDate, + account5_1.id, + "CARD", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + user5_1_6.id, + null, + null, + null, + last2Month + ); + // usedCardLicensesTypistCount 1件 + await createLicense( + source, + 7, + expiringSoonDate, + account5_1.id, + "CARD", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + user5_1_7.id, + null, + null, + null, + last2Month + ); + // usedCardLicensesNoneCount 1件 + await createLicense( + source, + 8, + expiringSoonDate, + account5_1.id, + "CARD", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + user5_1_8.id, + null, + null, + null, + last2Month + ); + // deleteはヒットしないことの確認 + await createLicense( + source, + 100, + expiringSoonDate, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.DELETED, + null, + null, + null, + null, + last2Month + ); + // その月に発行したライセンスを作成 + // 条件: + // ・第五アカウント + // ・ステータス:ALLOCATED/REUSABLE/UNALLOCATED + // ・作成日:今日から1か月前 + // ・期限:14日後 + // currentMonthIssuedTrialLicensesCount 0件 + // currentMonthIssuedNormalLicensesCount 3件 + await createLicense( + source, + 11, + expiringSoonDate, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.UNALLOCATED, + null, + null, + null, + null, + lastMonth + ); + await createLicense( + source, + 12, + expiringSoonDate, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.UNALLOCATED, + null, + null, + null, + null, + lastMonth + ); + await createLicense( + source, + 13, + expiringSoonDate, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.UNALLOCATED, + null, + null, + null, + null, + lastMonth + ); + // currentMonthIssuedCardLicensesCount 1件 + await createLicense( + source, + 14, + expiringSoonDate, + account5_1.id, + "CARD", + LICENSE_ALLOCATED_STATUS.UNALLOCATED, + null, + null, + null, + null, + lastMonth + ); + // deleteはヒットしないことの確認 + await createLicense( + source, + 101, + expiringSoonDate, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.DELETED, + null, + null, + null, + null, + lastMonth + ); + // その月に失効したライセンスを作成 + // 条件: + // ・第五アカウント + // ・ステータス:ALLOCATED/REUSABLE/UNALLOCATED + // ・作成日:今日から2か月前 + // ・期限:先月 + // invalidTrialLicensesAuthorCount 0件 + // invalidTrialLicensesTypistCount 0件 + // invalidTrialLicensesNoneCount 1件 + await createLicense( + source, + 22, + lastMonth, + account5_1.id, + "TRIAL", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + user5_1_9.id, + null, + null, + null, + last2Month + ); + // invalidTrialLicensesUnallocatedCount 0件 + // invalidNormalLicensesAuthorCount 0件 + // invalidNormalLicensesTypistCount 1件 + await createLicense( + source, + 23, + lastMonth, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.ALLOCATED, + user5_1_10.id, + null, + null, + null, + last2Month + ); + // invalidNormalLicensesNoneCount 0件 + // invalidNormalLicensesUnallocatedCount 1件 + await createLicense( + source, + 24, + lastMonth, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.UNALLOCATED, + null, + null, + null, + null, + last2Month + ); + + // invalidCardLicensesAuthorCount 0件 + // invalidCardLicensesTypistCount 0件 + // invalidCardLicensesNoneCount 0件 + // invalidCardLicensesUnallocatedCount 0件 + // deleteはヒットしないことの確認 + await createLicense( + source, + 102, + lastMonth, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.DELETED, + null, + null, + null, + null, + last2Month + ); + // 先々月はヒットしないことの確認 + await createLicense( + source, + 103, + last2Month, + account5_1.id, + "NORMAL", + LICENSE_ALLOCATED_STATUS.UNALLOCATED, + null, + null, + null, + null, + last2Month + ); + + // 第五階層がその月におこなったライセンス切り替え情報を作成 + // 条件: + // ・第五アカウント + // ・実行日時:先月 + // ・切り替えタイプ:CARD/TRIAL + // switchedTypistLicensesAuthorCount 1件 + await createLicenseAllocationHistory( + source, + 1, + admin5_1_1.id, + 1, + true, + account5_1.id, + lastMonth, + SWITCH_FROM_TYPE.TRIAL + ); + // switchedTypistLicensesTypistCount 0件 + // switchedTypistLicensesNoneCount 0件 + // switchedCardLicensesAuthorCount 1件 + await createLicenseAllocationHistory( + source, + 2, + admin5_1_1.id, + 1, + true, + account5_1.id, + lastMonth, + SWITCH_FROM_TYPE.CARD + ); + // switchedCardLicensesTypistCount 0件 + // switchedCardLicensesNoneCount 0件 + // SWITCH_FROM_TYPE.NONEではヒットしないことの確認 + await createLicenseAllocationHistory( + source, + 3, + admin5_1_1.id, + 1, + true, + account5_1.id, + lastMonth, + SWITCH_FROM_TYPE.NONE + ); + // 先々月の登録ではヒットしないことの確認 + await createLicenseAllocationHistory( + source, + 4, + admin5_1_1.id, + 1, + true, + account5_1.id, + last2Month, + SWITCH_FROM_TYPE.TRIAL + ); + + const result = await getBaseData(context, lastMonthYYYYMM, source); + const result_D = await getBaseDataFromDeletedAccounts( + context, + lastMonthYYYYMM, + source + ); + const transferDataResult = await transferData( + context, + result, + result_D, + lastMonthYYYYMM + ); + // ヘッダー行 + // "アカウント", "対象年月", "カテゴリー1", "カテゴリー2", "ライセンス種別", "役割", "数量" + expect(transferDataResult.outputDataUS[0]).toEqual('"アカウント",'); + expect(transferDataResult.outputDataUS[1]).toEqual('"対象年月",'); + expect(transferDataResult.outputDataUS[2]).toEqual(`"カテゴリー1",`); + expect(transferDataResult.outputDataUS[3]).toEqual(`"カテゴリー2",`); + expect(transferDataResult.outputDataUS[4]).toEqual(`"ライセンス種別",`); + expect(transferDataResult.outputDataUS[5]).toEqual(`"役割",`); + expect(transferDataResult.outputDataUS[6]).toEqual(`"数量"` + "\r\n"); + // データ行 + // "アカウント", "対象年月", "カテゴリー1", "カテゴリー2", "ライセンス種別", "役割", "数量" + // 1行目だけ確認する + expect(transferDataResult.outputDataUS[7]).toEqual( + `"${account5_1.company_name}",` + ); + expect(transferDataResult.outputDataUS[8]).toEqual(`"${lastMonthYYYYMM}",`); + expect(transferDataResult.outputDataUS[9]).toEqual( + `"${LICENSE_COUNT_ANALYSIS_CATEGORY_1.VALID_LICENSES}",` + ); + expect(transferDataResult.outputDataUS[10]).toEqual( + `"${LICENSE_COUNT_ANALYSIS_CATEGORY_2.OWNER_LICENSES}",` + ); + expect(transferDataResult.outputDataUS[11]).toEqual( + `"${LICENSE_COUNT_ANALYSIS_LICENSE_TYPE.TRIAL}",` + ); + expect(transferDataResult.outputDataUS[12]).toEqual(`"",`); + expect(transferDataResult.outputDataUS[13]).toEqual(`"3"` + "\r\n"); + }); }); diff --git a/dictation_function/src/test/common/utility.ts b/dictation_function/src/test/common/utility.ts index a22eed7..c167750 100644 --- a/dictation_function/src/test/common/utility.ts +++ b/dictation_function/src/test/common/utility.ts @@ -7,7 +7,7 @@ import { License, LicenseAllocationHistory, LicenseArchive, - LicenseAllocationHistoryArchive + LicenseAllocationHistoryArchive, } from "../../entity/license.entity"; type InitialTestDBState = { @@ -131,7 +131,7 @@ export const makeTestAccount = async ( const { identifiers } = await datasource.getRepository(User).insert({ external_id: d?.external_id ?? uuidv4(), account_id: accountId, - role: d?.role ?? "admin none", + role: d?.role ?? "none", author_id: d?.author_id ?? undefined, accepted_eula_version: d?.accepted_eula_version ?? "1.0", accepted_dpa_version: d?.accepted_dpa_version ?? "1.0", @@ -224,7 +224,7 @@ export const createLicenseAllocationHistory = async ( is_allocated: boolean, account_id: number, executed_at: Date, - switch_from_type: string, + switch_from_type: string ): Promise => { const { identifiers } = await datasource .getRepository(LicenseAllocationHistory) @@ -316,8 +316,6 @@ export const makeTestUserArchive = async ( return userArchive; }; - - /** * テスト ユーティリティ: 指定したプロパティを上書きしたアカウントとその管理者ユーザーを作成する * @param dataSource データソース