diff --git a/src/utils.js b/src/utils.js index a94f2b9..50f1c00 100644 --- a/src/utils.js +++ b/src/utils.js @@ -18,7 +18,7 @@ const classItems = [ { label: "ゆり", value: "ゆり" }, ] -let errorEl; +let notificationEl; let loadingEl; function getHeaderSpace(className, isDetailPage) { @@ -135,7 +135,7 @@ function createBtnGroupArea(groupId, btnLabel, btnOnClick, { btnElId = false, ye function checkInputData(map, { btnLabel, yearElId, monthElId, dateElId, classElId }) { const year = yearElId && map[yearElId].value; const month = monthElId && map[monthElId].value; - const date = dateElId && map[dateElId].value; + const date = dateElId && (map[dateElId].value === 'end' ? getLastDate(year, month) : map[dateElId].value); const className = classElId && map[classElId].value; const errorMsgs = []; @@ -168,6 +168,10 @@ function checkInputData(map, { btnLabel, yearElId, monthElId, dateElId, classElI } } +function getLastDate(year, month) { + return new Date(year, month, 0).getDate(); +} + function createBtn(id, label, onClick) { const btnEl = new Kuc.Button({ text: label, @@ -225,12 +229,18 @@ function convertToWesternYear(year, era) { return warekiStartYear[era] + year - 1; } -function getFormatDateString(date) { - const year = date.getFullYear(); - const month = String(date.getMonth() + 1).padStart(2, '0'); - const day = String(date.getDate()).padStart(2, '0'); +function getFormatDateString(dateObjOrYear, month, date) { + let year = dateObjOrYear; + if (typeof dateObjOrYear === "object") { + year = dateObjOrYear.getFullYear(); + month = dateObjOrYear.getMonth() + 1; + date = dateObjOrYear.getDate(); + } + const formatY = year; + const formatM = String(month).padStart(2, '0'); + const formatD = String(date).padStart(2, '0'); - return `${year}-${month}-${day}`; + return `${formatY}-${formatM}-${formatD}`; } function loading(show, text) { @@ -249,17 +259,33 @@ function loading(show, text) { } function showError(show, text) { - if (!errorEl) { - errorEl = new Kuc.Notification({ - type: 'danger', + if (show) { + buildNotification('danger', text); + notificationEl.open(); + console.error(text); + } else { + notificationEl && notificationEl.close(); + } +} + +function showSuccess(show, text) { + if (show) { + buildNotification('success', text); + notificationEl.open(); + } else { + notificationEl && notificationEl.close(); + } +} + +function buildNotification(type, text) { + if (!notificationEl) { + notificationEl = new Kuc.Notification({ + type, text }); } else { - errorEl.close(); - errorEl.text = text; - } - if (show) { - errorEl.open(); - console.error(text); + notificationEl.close(); + notificationEl.type = type; + notificationEl.text = text; } } \ No newline at end of file diff --git a/src/園児別出欠簿入力/BatchCreateHandler.js b/src/園児別出欠簿入力/BatchCreateHandler.js index f58878c..020853b 100644 --- a/src/園児別出欠簿入力/BatchCreateHandler.js +++ b/src/園児別出欠簿入力/BatchCreateHandler.js @@ -16,14 +16,16 @@ class BatchCreateHandler { const api = new KintoneRestAPIClient(); // 園児台帳アプリからデータを読み取る - const result = await this.getMasterRecords(api); - if (!result) { + const masterRecords = await this.getMasterRecords(api); + if (!masterRecords) { + // エラー + loading(false); return; } const today = new Date(); const todayString = getFormatDateString(today); - const records = this.generateRecords(today, todayString, result); + const records = this.generateRecords(today, todayString, masterRecords); // console.log(records); const createResult = await this.createData(api, records, todayString); @@ -31,8 +33,8 @@ class BatchCreateHandler { loading(false); } - generateRecords = (today, todayString, result) => { - return result.reduce((acc, masterRecord) => { + generateRecords = (today, todayString, masterRecords) => { + return masterRecords.reduce((acc, masterRecord) => { if (this.needCreateData(masterRecord, today)) { acc.push(this.createRecord(masterRecord, todayString)); } @@ -72,7 +74,6 @@ class BatchCreateHandler { }); } catch (e) { showError(true, '園児台帳アプリのデータ読み取りエラー\n - ' + e); - loading(false); } } diff --git a/src/園児別出欠簿入力/Link2Handler.js b/src/園児別出欠簿入力/Link2Handler.js index 766b87b..a136dc0 100644 --- a/src/園児別出欠簿入力/Link2Handler.js +++ b/src/園児別出欠簿入力/Link2Handler.js @@ -15,9 +15,86 @@ class Link2Handler { } - handleLink(e, { year, month, date }) { + handleLink = async (e, { year, month, date }) => { + loading(true, '日誌データ連携中...'); + showError(false); + const api = new KintoneRestAPIClient(); + + const dateString = getFormatDateString(year, month, date) + // 本アプリからデータを読み取る + const currentAppRecords = await this.getRecords(api, dateString); + if (!currentAppRecords) { + // エラー + loading(false); + return; + } + + const record = this.generateRecord(dateString, currentAppRecords); + console.log(record); + + const result = await this.insertOrUpdateData(api, record, dateString); + if (result) { + showSuccess(true, "日誌データ連携作成完了"); + } + loading(false); } + getRecords = async (api, dateString) => { + try { + return await api.record.getAllRecordsWithId({ + app: env["園児別出欠簿入力"].appId, + fields: ['クラス', "園児名", "園での様子_伝達事項", "評価反省"], + condition: `学年 not in ("0歳児", "1歳児") and 登園日 = "${dateString}"` + }); + } catch (e) { + showError(true, '園児台帳アプリのデータ読み取りエラー\n - ' + e); + loading(false); + } + } + + generateRecord = (todayString, currentAppRecords) => { + return { + '登園日': { 'value': todayString }, + '園児別テーブル': { + 'value': currentAppRecords.map((record) => { + return { + 'value': { + 'クラス名': { 'value': record['クラス'].value }, + '名前': { 'value': record['園児名'].value }, + '子どもの様子': { 'value': record['園での様子_伝達事項'].value }, + '反省評価_園児別テーブル': { 'value': record['評価反省'].value }, + } + } + }) + } + }; + } + + insertOrUpdateData = async (api, record, dateString) => { + try { + const generatedRecords = await api.record.getAllRecordsWithId({ + app: env["2歳以上日誌出力用"].appId, + fields: ["$id", "登園日"], + condition: `登園日 = "${dateString}"` + }) + const generatedRecordId = generatedRecords[0]?.$id.value; + const param = { + app: env["2歳以上日誌出力用"].appId, + record + } + let addResult; + if (generatedRecordId) { + param.id = generatedRecordId; + addResult = await api.record.updateRecord(param); + } else { + addResult = await api.record.addRecord(param); + } + return addResult; + } catch (e) { + showError(true, '日誌データ連携作成失敗\n - ' + e); + } + }; + static getInstance(headerSpace) { if (!Link2Handler.instance) { Link2Handler.instance = new Link2Handler(headerSpace); diff --git a/src/園児台帳/main.js b/src/園児台帳/main.js index d624010..a31775c 100644 --- a/src/園児台帳/main.js +++ b/src/園児台帳/main.js @@ -29,7 +29,7 @@ } // ------------------- 編集画面保存時の処理 ------------------- - kintone.events.on(['app.record.create.submit', 'app.record.edit.submit'], function (event) { + kintone.events.on(['app.record.create.submit', 'app.record.edit.submit', 'app.record.index.edit.submit'], function (event) { event.record[FIELD_CODE]['value'] = getUniqueKey(event.record); return event; });