From 39bddd4b969259bd337b4a38f7b3c2d2c6579e25 Mon Sep 17 00:00:00 2001 From: xue jiahao Date: Wed, 12 Feb 2025 15:47:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=BA=E6=AC=A0=E7=B0=BF=E4=B8=80=E6=8B=AC?= =?UTF-8?q?=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/env.js | 1 - src/utils.js | 25 +++-- src/園児別出欠簿入力/BatchCreateHandler.js | 112 ++++++++++++++------- src/園児台帳/main.js | 3 +- 4 files changed, 97 insertions(+), 44 deletions(-) diff --git a/src/env.js b/src/env.js index d746ee1..d7094b1 100644 --- a/src/env.js +++ b/src/env.js @@ -23,7 +23,6 @@ const env = { }, "園児台帳": { appId: 16, - uniqueKeyFieldCode: 'ユニークキー' }, }; diff --git a/src/utils.js b/src/utils.js index 1297238..a94f2b9 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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; } @@ -249,4 +246,20 @@ function loading(show, text) { if (show) { 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); + } } \ No newline at end of file diff --git a/src/園児別出欠簿入力/BatchCreateHandler.js b/src/園児別出欠簿入力/BatchCreateHandler.js index 12df43f..f58878c 100644 --- a/src/園児別出欠簿入力/BatchCreateHandler.js +++ b/src/園児別出欠簿入力/BatchCreateHandler.js @@ -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 - }); - location.reload(); - } catch (error) { - // TODO - } finally { - loading(false); - } + const createResult = await this.createData(api, records, todayString); + location.reload(); + 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); } } diff --git a/src/園児台帳/main.js b/src/園児台帳/main.js index 15a4afd..d624010 100644 --- a/src/園児台帳/main.js +++ b/src/園児台帳/main.js @@ -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) {