fix 園児台帳
This commit is contained in:
104
src/園児台帳/main.js
104
src/園児台帳/main.js
@@ -3,29 +3,28 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const FIELD_CODE = "ユニークキー";
|
const FIELD_CODE = "ユニークキー";
|
||||||
|
|
||||||
// ------------------- 詳細/印刷/編集画面表示時の処理 -------------------
|
// ------------------- 画面表示時の処理 -------------------
|
||||||
|
// 詳細画面/印刷画面表示時の処理
|
||||||
kintone.events.on(['app.record.detail.show', 'app.record.print.show'], function (event) {
|
kintone.events.on(['app.record.detail.show', 'app.record.print.show'], function (event) {
|
||||||
hideField(FIELD_CODE);
|
hideField(FIELD_CODE);
|
||||||
return event;
|
return event;
|
||||||
});
|
});
|
||||||
|
|
||||||
kintone.events.on(['app.record.index.edit.show'], (event) => {
|
// 編集画面/新規作成画面表示時の処理
|
||||||
disableField(event.record, FIELD_CODE);
|
|
||||||
return event
|
|
||||||
})
|
|
||||||
|
|
||||||
// ------------------- 編集画面表示時の処理 -------------------
|
|
||||||
kintone.events.on(['app.record.edit.show', 'app.record.create.show'], function (event) {
|
kintone.events.on(['app.record.edit.show', 'app.record.create.show'], function (event) {
|
||||||
const targetFieldEl = kintone.app.record.getSpaceElement("before-unique-key").parentElement.nextSibling;
|
|
||||||
targetFieldEl.querySelector('.input-constraints-cybozu').style.display = 'none';
|
|
||||||
|
|
||||||
if (event.type === "app.record.create.show") {
|
if (event.type === "app.record.create.show") {
|
||||||
|
// レコード複製作成時には既存のユニークキーを削除する
|
||||||
event.record[FIELD_CODE].value = "";
|
event.record[FIELD_CODE].value = "";
|
||||||
}
|
}
|
||||||
hideField(FIELD_CODE);
|
hideField(FIELD_CODE);
|
||||||
return event;
|
return event;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 一覧画面での編集モード表示時の処理
|
||||||
|
kintone.events.on(['app.record.index.edit.show'], (event) => {
|
||||||
|
disableField(event.record, FIELD_CODE);
|
||||||
|
return event
|
||||||
|
})
|
||||||
|
|
||||||
function hideField(fieldCode) {
|
function hideField(fieldCode) {
|
||||||
kintone.app.record.setFieldShown(fieldCode, false);
|
kintone.app.record.setFieldShown(fieldCode, false);
|
||||||
@@ -37,7 +36,24 @@
|
|||||||
|
|
||||||
// ------------------- 新規保存時の処理 -------------------
|
// ------------------- 新規保存時の処理 -------------------
|
||||||
kintone.events.on(['app.record.create.submit.success'], async function (event) {
|
kintone.events.on(['app.record.create.submit.success'], async function (event) {
|
||||||
|
// 新規作成したレコードのIDをユニークキーとして設定
|
||||||
const api = new KintoneRestAPIClient();
|
const api = new KintoneRestAPIClient();
|
||||||
|
await updateUniqueKey(api, event);
|
||||||
|
return event;
|
||||||
|
});
|
||||||
|
|
||||||
|
// ------------------- 編集保存時の処理 -------------------
|
||||||
|
// 関連アプリ情報の定義
|
||||||
|
// uniqueKeyField 園児台帳を参照するフィールドコード
|
||||||
|
// dateField 日付範囲指定用フィールド
|
||||||
|
const LOOKUP_APPS_INFO = {
|
||||||
|
"園児別出欠簿入力": {
|
||||||
|
uniqueKeyField: "園児ユニークキー",
|
||||||
|
dateField: "登園日"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateUniqueKey(api, event) {
|
||||||
await api.record.updateRecord({
|
await api.record.updateRecord({
|
||||||
app: env["園児台帳"].appId,
|
app: env["園児台帳"].appId,
|
||||||
id: event.recordId,
|
id: event.recordId,
|
||||||
@@ -46,62 +62,76 @@
|
|||||||
value: event.recordId
|
value: event.recordId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
return event;
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// ------------------- 編集画面保存時の処理 -------------------
|
// 保存成功後イベント処理(編集/一覧編集)
|
||||||
// 保存成功後イベント
|
|
||||||
kintone.events.on([
|
kintone.events.on([
|
||||||
'app.record.edit.submit.success',
|
'app.record.edit.submit.success',
|
||||||
'app.record.index.edit.submit.success'
|
'app.record.index.edit.submit.success'
|
||||||
], async (event) => {
|
], async (event) => {
|
||||||
loading(true, 'データを反映中...');
|
loading(true, 'データを反映中...');
|
||||||
|
|
||||||
const api = new KintoneRestAPIClient();
|
const api = new KintoneRestAPIClient();
|
||||||
const key = event.record[FIELD_CODE]['value'];
|
const uniqueKeyValue = event.record[FIELD_CODE]['value'];
|
||||||
const yearRange = getYearRange()
|
const yearRange = getYearRange()
|
||||||
const appName = "園児別出欠簿入力";
|
|
||||||
|
|
||||||
const records = (await getAllRecords(api, appName, "園児ユニークキー", key, "登園日", yearRange))
|
// レガシーキー判別(旧方式: 出席番号+学年+クラス+名前)
|
||||||
if (!records || records.length === 0) {
|
const isLegacyUniqueKey = (event.recordId + "") !== uniqueKeyValue
|
||||||
loading(false)
|
let newKeyGetter;
|
||||||
return event;
|
|
||||||
|
if (isLegacyUniqueKey) {
|
||||||
|
// 旧キー方式から移行する処理:レコードIDを使用するように変更し、関連する全ての参照箇所を更新する必要があります
|
||||||
|
await updateUniqueKey(api, event);
|
||||||
|
newKeyGetter = () => event.recordId
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
for (const appName of Object.keys(LOOKUP_APPS_INFO)) {
|
||||||
const updateResult = await api.record.updateAllRecords({
|
// 対象年度の関連レコードを取得
|
||||||
app: env[appName].appId,
|
const records = await getAllUpdatedRecords(api, appName, uniqueKeyValue, yearRange, newKeyGetter)
|
||||||
records
|
if (!records || records.length === 0) {
|
||||||
});
|
continue;
|
||||||
updateResult.yearRange = yearRange;
|
}
|
||||||
showSuccessDialog(updateResult)
|
|
||||||
} catch (e) {
|
try {
|
||||||
showError(true, `アプリ「${appName}」のデータ上書で失敗エラー\n - ` + e);
|
const updateResult = await api.record.updateAllRecords({
|
||||||
|
app: env[appName].appId,
|
||||||
|
records
|
||||||
|
});
|
||||||
|
|
||||||
|
updateResult.yearRange = yearRange;
|
||||||
|
showSuccessDialog(updateResult)
|
||||||
|
} catch (e) {
|
||||||
|
showError(true, `アプリ「${appName}」のデータ上書処理中にエラーが発生しました\n - ` + e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loading(false)
|
loading(false)
|
||||||
return event;
|
return event;
|
||||||
});
|
});
|
||||||
|
|
||||||
async function getAllRecords(api, appName, keyName, key, dateKey, yearRange) {
|
async function getAllUpdatedRecords(api, appName, uniqueKeyValue, yearRange, newKeyGetter) {
|
||||||
|
const uniqueKeyField = LOOKUP_APPS_INFO[appName].uniqueKeyField
|
||||||
|
const dateField = LOOKUP_APPS_INFO[appName].dateField
|
||||||
|
newKeyGetter = newKeyGetter || (record => record[uniqueKeyField].value)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const records = await api.record.getAllRecordsWithId({
|
const records = await api.record.getAllRecordsWithId({
|
||||||
app: env[appName].appId,
|
app: env[appName].appId,
|
||||||
condition: `${keyName} = "${key}" and ${dateKey} >= "${yearRange.start}" and ${dateKey} <= "${yearRange.end}"`
|
condition: `${uniqueKeyField} = "${uniqueKeyValue}" and ${dateField} >= "${yearRange.start}" and ${dateField} <= "${yearRange.end}"`
|
||||||
});
|
});
|
||||||
// await new Promise(resolve => setTimeout(resolve, 3000));
|
|
||||||
|
|
||||||
return records.map((record) => {
|
return records.map((record) => {
|
||||||
return {
|
return {
|
||||||
id: record.$id.value,
|
id: record.$id.value,
|
||||||
record: {
|
record: {
|
||||||
[keyName]: {
|
[uniqueKeyField]: {
|
||||||
value: record[keyName].value
|
value: newKeyGetter(record)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
showError(true, `アプリ「${appName}」のデータ読み取りエラー\n - ` + e);
|
showError(true, `アプリ「${appName}」のデータ上書のためにデータを取得中にエラーが発生しました\n - ` + e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user