手動実行できるように変更

This commit is contained in:
maruyama.t 2023-12-20 14:23:28 +09:00
parent 32a452bdb2
commit 57fc7a17b5

View File

@ -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<void> {
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<HttpResponseInit | HttpResponse> {
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);