From 57fc7a17b5e432cec762f52269deafc218f57346 Mon Sep 17 00:00:00 2001 From: "maruyama.t" Date: Wed, 20 Dec 2023 14:23:28 +0900 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E5=8B=95=E5=AE=9F=E8=A1=8C=E3=81=A7?= =?UTF-8?q?=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/functions/licenseAutoAllocation.ts | 83 ++++++++++++++++++- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/dictation_function/src/functions/licenseAutoAllocation.ts b/dictation_function/src/functions/licenseAutoAllocation.ts index 6bddc1a..a3721d1 100644 --- a/dictation_function/src/functions/licenseAutoAllocation.ts +++ b/dictation_function/src/functions/licenseAutoAllocation.ts @@ -1,4 +1,12 @@ -import { app, InvocationContext, Timer } from "@azure/functions"; +import { + app, + InvocationContext, + Timer, + HttpRequest, + HttpHandler, + HttpResponseInit, + HttpResponse, +} from "@azure/functions"; import { Between, DataSource, In, MoreThan, Repository } from "typeorm"; import { User } from "../entity/user.entity"; import { Account } from "../entity/account.entity"; @@ -20,14 +28,15 @@ import { export async function licenseAutoAllocationProcessing( context: InvocationContext, - datasource: DataSource + datasource: DataSource, + dateToTrigger?: Date ): Promise { try { context.log("[IN]licenseAutoAllocationProcessing"); // ライセンスの有効期間判定用 - const currentDateZeroTime = new DateWithZeroTime(); - const currentDateEndTime = new DateWithDayEndTime(); + const currentDateZeroTime = new DateWithZeroTime(dateToTrigger); + const currentDateEndTime = new DateWithDayEndTime(dateToTrigger); // 自動更新対象の候補となるアカウントを取得 const accountRepository = datasource.getRepository(Account); @@ -378,3 +387,69 @@ class autoAllocationList { accountId: number; userIds: number[]; } + +const httpTrigger: HttpHandler = async function ( + req: HttpRequest, + context: InvocationContext +): Promise { + try { + if (req.method === "GET") { + const queryParams = new URLSearchParams(req.url.split("?")[1]); // クエリパラメータを取得 + + const requestedDate = queryParams.get("date"); + + let dateToTrigger: Date; + if (requestedDate) { + dateToTrigger = new Date(requestedDate); + } else { + dateToTrigger = new Date(); + } + context.log("[IN]licenseAutoAllocation"); + dotenv.config({ path: ".env" }); + dotenv.config({ path: ".env.local", override: true }); + let datasource: DataSource; + try { + datasource = new DataSource({ + type: "mysql", + host: process.env.DB_HOST, + port: Number(process.env.DB_PORT), + username: process.env.DB_USERNAME, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, + entities: [User, Account, License, LicenseAllocationHistory], + }); + await datasource.initialize(); + } catch (e) { + context.log("database initialize failed."); + context.error(e); + throw e; + } + + await licenseAutoAllocationProcessing(context, datasource, dateToTrigger); + context.log("Automatic license allocation has been triggered."); + return { + status: 200, + body: "Automatic license allocation has been triggered.", + }; + } else { + context.log("Please use the GET method."); + return { + status: 400, + body: "Please use the GET method.", + }; + } + } catch (e) { + context.log("licenseAutoAllocation failed."); + context.error(e); + return { + status: 500, + body: "licenseAutoAllocation failed.", + }; + } finally { + context.log("[OUT]licenseAutoAllocation"); + } +}; + +export default httpTrigger; + +app.get("/licenseAutoAllocation", httpTrigger);