147 lines
5.8 KiB
JavaScript
147 lines
5.8 KiB
JavaScript
/**
|
||
* apply-jisseki-label-fix-496626.js
|
||
* ------------------------------------------------------------
|
||
* site-496626のColumns定義のうち、Date111〜Date148(実績日、37項目)の
|
||
* LabelTextが「実行」のままになっているものだけを「実績」に書き換える専用スクリプト。
|
||
*
|
||
* 現在のサイト全体(getsite)を取得し、対象37項目のLabelTextだけを書き換えて
|
||
* updatesite(全体更新)で送信する。Columns内の対象37項目以外のフィールド
|
||
* (ColumnName/GridLabelText/Type/各種設定等)、GridColumns、他のSiteSettings・
|
||
* Permissions・Title等は一切変更しない。
|
||
*
|
||
* 使い方:
|
||
* node apply-jisseki-label-fix-496626.js … 差分表示のみ(送信なし)
|
||
* node apply-jisseki-label-fix-496626.js --execute … 上記に加えて実際に送信する
|
||
* ------------------------------------------------------------
|
||
*/
|
||
|
||
const fs = require("fs");
|
||
const path = require("path");
|
||
const { findSiteDir, newModifyDir, latestModifyDir } = require("./site-paths");
|
||
|
||
const SITE_ID = 496626;
|
||
const baseDir = path.join(__dirname, "..", "..");
|
||
const configsDir = path.join(baseDir, "configs");
|
||
const execute = process.argv.includes("--execute");
|
||
|
||
// 対象: Date111〜Date148
|
||
const TARGET_COLUMNS = new Set(
|
||
Array.from({ length: 38 }, (_, i) => i + 111).map((n) => `Date${n}`)
|
||
);
|
||
const OLD_LABEL = "実行";
|
||
const NEW_LABEL = "実績";
|
||
|
||
function loadJson(p) {
|
||
return JSON.parse(fs.readFileSync(p, "utf-8"));
|
||
}
|
||
|
||
function maskApiKey(key) {
|
||
if (!key) return key;
|
||
return key.length > 8 ? key.slice(0, 4) + "****" + key.slice(-4) : "****";
|
||
}
|
||
|
||
function timestamp() {
|
||
return new Date().toISOString().replace(/[:.]/g, "-");
|
||
}
|
||
|
||
const config = loadJson(path.join(baseDir, "config.json"));
|
||
const siteDir = findSiteDir(configsDir, SITE_ID);
|
||
|
||
(async () => {
|
||
console.log("[INFO] 現在のサイト情報を取得しています...");
|
||
const getUrl = `${config.BaseUrl.replace(/\/+$/, "")}/api/items/${SITE_ID}/getsite`;
|
||
const getRes = await fetch(getUrl, {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ ApiVersion: config.ApiVersion || "1.1", ApiKey: config.ApiKey }),
|
||
});
|
||
const getJson = await getRes.json();
|
||
if (getJson.StatusCode !== 200) {
|
||
console.error("[エラー] getsiteに失敗しました:", JSON.stringify(getJson).slice(0, 500));
|
||
process.exit(1);
|
||
}
|
||
const data = getJson.Response.Data;
|
||
console.log(`[OK] 取得しました(現在のVer: ${data.Ver}, UpdatedTime: ${data.UpdatedTime})`);
|
||
|
||
const siteSettings = JSON.parse(JSON.stringify(data.SiteSettings));
|
||
|
||
console.log("========================================");
|
||
console.log(` Columns LabelText「実行」→「実績」書き換え(updatesite全体更新)${execute ? "(実行)" : "(プレビューのみ)"}`);
|
||
console.log("========================================");
|
||
|
||
const changed = [];
|
||
const alreadyOk = [];
|
||
const missing = [];
|
||
|
||
TARGET_COLUMNS.forEach((columnName) => {
|
||
const column = siteSettings.Columns.find((c) => c.ColumnName === columnName);
|
||
if (!column) {
|
||
missing.push(columnName);
|
||
return;
|
||
}
|
||
if (column.LabelText === OLD_LABEL) {
|
||
column.LabelText = NEW_LABEL;
|
||
changed.push(`${columnName} (${column.GridLabelText || ""})`);
|
||
} else if (column.LabelText === NEW_LABEL) {
|
||
alreadyOk.push(columnName);
|
||
} else {
|
||
console.log(` [注意] ${columnName} のLabelTextは想定外の値です: "${column.LabelText}"(変更しません)`);
|
||
}
|
||
});
|
||
|
||
console.log(`変更対象: ${changed.length}件`);
|
||
changed.forEach((c) => console.log(` ${c}: 実行 -> 実績`));
|
||
if (alreadyOk.length > 0) {
|
||
console.log(`既に「実績」: ${alreadyOk.length}件`);
|
||
}
|
||
if (missing.length > 0) {
|
||
console.log(`[警告] Columnsに見つからなかった項目: ${missing.join(", ")}`);
|
||
}
|
||
console.log("他のSiteSettings(Columns内の対象外フィールド・GridColumns・Scripts/Styles/ServerScripts等)・Permissions・Title等は一切変更しません。");
|
||
|
||
if (changed.length === 0) {
|
||
console.log("\n変更対象がないため、送信を行いません。");
|
||
process.exit(0);
|
||
}
|
||
|
||
const body = {
|
||
ApiVersion: config.ApiVersion || "1.1",
|
||
ApiKey: config.ApiKey,
|
||
SiteId: SITE_ID,
|
||
Title: data.Title,
|
||
ReferenceType: data.ReferenceType,
|
||
ParentId: data.ParentId,
|
||
InheritPermission: data.InheritPermission,
|
||
Permissions: data.Permissions,
|
||
SiteSettings: siteSettings,
|
||
};
|
||
|
||
const modifyDir = latestModifyDir(siteDir) || newModifyDir(siteDir, "jisseki-labeltext-fix");
|
||
const ts = timestamp();
|
||
const maskedBody = { ...body, ApiKey: maskApiKey(body.ApiKey) };
|
||
const previewPath = path.join(modifyDir, `site-${SITE_ID}_labeltext_update_preview_${ts}.json`);
|
||
fs.writeFileSync(previewPath, JSON.stringify(maskedBody, null, 2), "utf-8");
|
||
console.log(`\n[OK] 送信予定内容を保存しました: ${previewPath}`);
|
||
|
||
if (!execute) {
|
||
console.log("\n[注意] --execute が指定されていないため、送信は行っていません。");
|
||
console.log("内容を確認の上、問題なければ次を実行してください: node apply-jisseki-label-fix-496626.js --execute");
|
||
process.exit(0);
|
||
}
|
||
|
||
console.log("\n実際に送信します(updatesite)...");
|
||
const url = `${config.BaseUrl.replace(/\/+$/, "")}/api/items/${SITE_ID}/updatesite`;
|
||
const res = await fetch(url, {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify(body),
|
||
});
|
||
const text = await res.text();
|
||
const resultOutPath = path.join(modifyDir, `site-${SITE_ID}_labeltext_update_result_${ts}.json`);
|
||
fs.writeFileSync(resultOutPath, text, "utf-8");
|
||
|
||
console.log(`HTTP ${res.status} ${res.statusText}`);
|
||
console.log(`[OK] 応答を保存しました: ${resultOutPath}`);
|
||
console.log(text);
|
||
})();
|