出欠簿一括作成

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, appId: 16,
uniqueKeyFieldCode: 'ユニークキー'
}, },
}; };

View File

@@ -18,9 +18,7 @@ const classItems = [
{ label: "ゆり", value: "ゆり" }, { label: "ゆり", value: "ゆり" },
] ]
const errorEl = new Kuc.Notification({ let errorEl;
type: 'danger',
});
let loadingEl; let loadingEl;
function getHeaderSpace(className, isDetailPage) { function getHeaderSpace(className, isDetailPage) {
@@ -123,7 +121,7 @@ function createBtnGroupArea(groupId, btnLabel, btnOnClick, { btnElId = false, ye
}); });
result[btnElId] = btnEl; result[btnElId] = btnEl;
btnEl.addEventListener('click', (e) => { btnEl.addEventListener('click', (e) => {
errorEl.close(); showError(false);
const checkResult = checkInputData(result, { btnLabel, yearElId, monthElId, dateElId, classElId }); const checkResult = checkInputData(result, { btnLabel, yearElId, monthElId, dateElId, classElId });
if (checkResult) { if (checkResult) {
btnOnClick(e, checkResult); btnOnClick(e, checkResult);
@@ -158,8 +156,7 @@ function checkInputData(map, { btnLabel, yearElId, monthElId, dateElId, classElI
errorMsgs.push(' · クラスを選択してください。'); errorMsgs.push(' · クラスを選択してください。');
} }
if (errorMsgs.length > 0) { if (errorMsgs.length > 0) {
errorEl.text = btnLabel + 'エラー\n' + errorMsgs.join('\n'); showError(true, btnLabel + 'エラー\n' + errorMsgs.join('\n'))
errorEl.open();
return; return;
} }
@@ -249,4 +246,20 @@ function loading(show, text) {
if (show) { if (show) {
loadingEl.open(); 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 { class BatchCreateHandler {
constructor(headerSpace) { constructor(headerSpace) {
const elements = createBtnGroupArea('batch-action-area', '出欠簿一括作成', this.handleCreateDate, { const elements = createBtnGroupArea('batch-action-area', '出欠簿一括作成', this.handleCreateData, {
btnElId: 'batch-btn', btnElId: 'batch-btn',
}) })
if (!elements) { if (!elements) {
@@ -10,47 +10,39 @@ class BatchCreateHandler {
headerSpace.appendChild(elements['batch-action-area']); headerSpace.appendChild(elements['batch-action-area']);
} }
handleCreateDate = async (e) => { handleCreateData = async (e) => {
loading(true, '出欠簿一括作成中...'); loading(true, '出欠簿一括作成中...');
showError(false);
const api = new KintoneRestAPIClient(); const api = new KintoneRestAPIClient();
let result = [];
try { // 園児台帳アプリからデータを読み取る
result = await api.record.getAllRecordsWithId({ const result = await this.getMasterRecords(api);
app: env["園児台帳"].appId, if (!result) {
fields: ["$id", "ユニークキー", "和暦_退園年月日", "年_退園年月日", "月_退園年月日", "日_退園年月日"],
});
} catch (error) {
// TODO
loading(false);
return; return;
} }
const today = new Date(); const today = new Date();
const todayString = getFormatDateString(today) const todayString = getFormatDateString(today);
const records = []; const records = this.generateRecords(today, todayString, result);
for (const record of result) { // console.log(records);
if (!this.needCreateData(record, today)) {
continue;
}
records.push(this.createRecord(record, todayString))
}
console.log(records);
try { const createResult = await this.createData(api, records, todayString);
const addResult = await api.record.addAllRecords({ location.reload();
app: env["園児別出欠簿入力"].appId, loading(false);
records }
});
location.reload(); generateRecords = (today, todayString, result) => {
} catch (error) { return result.reduce((acc, masterRecord) => {
// TODO if (this.needCreateData(masterRecord, today)) {
} finally { acc.push(this.createRecord(masterRecord, todayString));
loading(false); }
} return acc;
}, []);
} }
needCreateData = (record, today) => { needCreateData = (record, today) => {
// 当日の日付が、退園年月日「年_退園年月日」「月_退園年月日」「日_退園年月日」を参照より前の値
// ※同日の場合は取得条件に合致とする(取得する)、退園年月日に値が入っていない場合は無条件に取得
const era = record["和暦_退園年月日"].value; const era = record["和暦_退園年月日"].value;
const lastYear = record["年_退園年月日"].value; const lastYear = record["年_退園年月日"].value;
const lastMonth = record["月_退園年月日"].value; const lastMonth = record["月_退園年月日"].value;
@@ -68,10 +60,60 @@ class BatchCreateHandler {
createRecord = (record, todayString) => { createRecord = (record, todayString) => {
return { return {
'登園日': { 'value': todayString }, '登園日': { '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 () { (function () {
"use strict"; "use strict";
const APP_ENV = env["園児台帳"]; const FIELD_CODE = "ユニークキー";
const FIELD_CODE = APP_ENV.uniqueKeyFieldCode;
// ------------------- 詳細/印刷/編集画面表示時の処理 ------------------- // ------------------- 詳細/印刷/編集画面表示時の処理 -------------------
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) {