出欠簿一括作成

This commit is contained in:
2025-02-12 15:47:26 +08:00
parent 4e39a1a680
commit 39bddd4b96
4 changed files with 97 additions and 44 deletions

View File

@@ -23,7 +23,6 @@ const env = {
},
"園児台帳": {
appId: 16,
uniqueKeyFieldCode: 'ユニークキー'
},
};

View File

@@ -18,9 +18,7 @@ const classItems = [
{ label: "ゆり", value: "ゆり" },
]
const errorEl = new Kuc.Notification({
type: 'danger',
});
let errorEl;
let loadingEl;
function getHeaderSpace(className, isDetailPage) {
@@ -123,7 +121,7 @@ function createBtnGroupArea(groupId, btnLabel, btnOnClick, { btnElId = false, ye
});
result[btnElId] = btnEl;
btnEl.addEventListener('click', (e) => {
errorEl.close();
showError(false);
const checkResult = checkInputData(result, { btnLabel, yearElId, monthElId, dateElId, classElId });
if (checkResult) {
btnOnClick(e, checkResult);
@@ -158,8 +156,7 @@ function checkInputData(map, { btnLabel, yearElId, monthElId, dateElId, classElI
errorMsgs.push(' · クラスを選択してください。');
}
if (errorMsgs.length > 0) {
errorEl.text = btnLabel + 'エラー\n' + errorMsgs.join('\n');
errorEl.open();
showError(true, btnLabel + 'エラー\n' + errorMsgs.join('\n'))
return;
}
@@ -250,3 +247,19 @@ function loading(show, text) {
loadingEl.open();
}
}
function showError(show, text) {
if (!errorEl) {
errorEl = new Kuc.Notification({
type: 'danger',
text
});
} else {
errorEl.close();
errorEl.text = text;
}
if (show) {
errorEl.open();
console.error(text);
}
}

View File

@@ -1,7 +1,7 @@
class BatchCreateHandler {
constructor(headerSpace) {
const elements = createBtnGroupArea('batch-action-area', '出欠簿一括作成', this.handleCreateDate, {
const elements = createBtnGroupArea('batch-action-area', '出欠簿一括作成', this.handleCreateData, {
btnElId: 'batch-btn',
})
if (!elements) {
@@ -10,47 +10,39 @@ class BatchCreateHandler {
headerSpace.appendChild(elements['batch-action-area']);
}
handleCreateDate = async (e) => {
handleCreateData = async (e) => {
loading(true, '出欠簿一括作成中...');
showError(false);
const api = new KintoneRestAPIClient();
let result = [];
try {
result = await api.record.getAllRecordsWithId({
app: env["園児台帳"].appId,
fields: ["$id", "ユニークキー", "和暦_退園年月日", "年_退園年月日", "月_退園年月日", "日_退園年月日"],
});
} catch (error) {
// TODO
loading(false);
// 園児台帳アプリからデータを読み取る
const result = await this.getMasterRecords(api);
if (!result) {
return;
}
const today = new Date();
const todayString = getFormatDateString(today)
const records = [];
for (const record of result) {
if (!this.needCreateData(record, today)) {
continue;
}
records.push(this.createRecord(record, todayString))
}
console.log(records);
const todayString = getFormatDateString(today);
const records = this.generateRecords(today, todayString, result);
// console.log(records);
try {
const addResult = await api.record.addAllRecords({
app: env["園児別出欠簿入力"].appId,
records
});
const createResult = await this.createData(api, records, todayString);
location.reload();
} catch (error) {
// TODO
} finally {
loading(false);
}
generateRecords = (today, todayString, result) => {
return result.reduce((acc, masterRecord) => {
if (this.needCreateData(masterRecord, today)) {
acc.push(this.createRecord(masterRecord, todayString));
}
return acc;
}, []);
}
needCreateData = (record, today) => {
// 当日の日付が、退園年月日「年_退園年月日」「月_退園年月日」「日_退園年月日」を参照より前の値
// ※同日の場合は取得条件に合致とする(取得する)、退園年月日に値が入っていない場合は無条件に取得
const era = record["和暦_退園年月日"].value;
const lastYear = record["年_退園年月日"].value;
const lastMonth = record["月_退園年月日"].value;
@@ -68,10 +60,60 @@ class BatchCreateHandler {
createRecord = (record, todayString) => {
return {
'登園日': { 'value': todayString },
'園児ユニークキー': { 'value': record["ユニークキー"].value },
// '学年': { 'value': record["学年"].value },
// 'クラス': { 'value': record["クラス"].value },
// '出席番号': { 'value': record["出席番号"].value },
'園児ユニークキー': { 'value': record['ユニークキー'].value },
}
}
getMasterRecords = async (api) => {
try {
return await api.record.getAllRecordsWithId({
app: env["園児台帳"].appId,
fields: ['ユニークキー', "和暦_退園年月日", "年_退園年月日", "月_退園年月日", "日_退園年月日"],
});
} catch (e) {
showError(true, '園児台帳アプリのデータ読み取りエラー\n - ' + e);
loading(false);
}
}
createData = async (api, records, todayString) => {
try {
const alreadyGeneratedKeys = await this.getAlreadyGeneratedKeys(api, records, todayString);
// console.log(alreadyGeneratedKeys);
if (!alreadyGeneratedKeys) {
// エラー
return;
}
const filteredRecords = records.filter((each) => !alreadyGeneratedKeys[each["園児ユニークキー"].value])
if (!filteredRecords.length) {
return;
}
const addResult = await api.record.addAllRecords({
app: env["園児別出欠簿入力"].appId,
records: filteredRecords
});
return addResult;
} catch (e) {
showError(true, '本アプリのデータ作成失敗\n - ' + e);
}
};
getAlreadyGeneratedKeys = async (api, records, todayString) => {
try {
const needCreateUniqueKeys = records.map((each) => `"${each["園児ユニークキー"].value}"`).join(',')
const alreadyGeneratedResult = await api.record.getAllRecordsWithId({
app: env["園児別出欠簿入力"].appId,
fields: ["園児ユニークキー"],
condition: `登園日 = "${todayString}" and 園児ユニークキー in (${needCreateUniqueKeys})`
})
return alreadyGeneratedResult.reduce((map, each) => {
map[each["園児ユニークキー"].value] = true;
return map;
}, {});
} catch (e) {
showError(true, '本アプリのデータ読み取りエラー\n - ' + e);
}
}

View File

@@ -1,8 +1,7 @@
(function () {
"use strict";
const APP_ENV = env["園児台帳"];
const FIELD_CODE = APP_ENV.uniqueKeyFieldCode;
const FIELD_CODE = "ユニークキー";
// ------------------- 詳細/印刷/編集画面表示時の処理 -------------------
kintone.events.on(['app.record.detail.show', 'app.record.print.show'], function (event) {