2歳以上日誌データ連携

This commit is contained in:
2025-02-12 23:56:37 +08:00
parent 9837a495ab
commit 3031c07b9c
4 changed files with 128 additions and 24 deletions

View File

@@ -18,7 +18,7 @@ const classItems = [
{ label: "ゆり", value: "ゆり" }, { label: "ゆり", value: "ゆり" },
] ]
let errorEl; let notificationEl;
let loadingEl; let loadingEl;
function getHeaderSpace(className, isDetailPage) { function getHeaderSpace(className, isDetailPage) {
@@ -135,7 +135,7 @@ function createBtnGroupArea(groupId, btnLabel, btnOnClick, { btnElId = false, ye
function checkInputData(map, { btnLabel, yearElId, monthElId, dateElId, classElId }) { function checkInputData(map, { btnLabel, yearElId, monthElId, dateElId, classElId }) {
const year = yearElId && map[yearElId].value; const year = yearElId && map[yearElId].value;
const month = monthElId && map[monthElId].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 className = classElId && map[classElId].value;
const errorMsgs = []; 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) { function createBtn(id, label, onClick) {
const btnEl = new Kuc.Button({ const btnEl = new Kuc.Button({
text: label, text: label,
@@ -225,12 +229,18 @@ function convertToWesternYear(year, era) {
return warekiStartYear[era] + year - 1; return warekiStartYear[era] + year - 1;
} }
function getFormatDateString(date) { function getFormatDateString(dateObjOrYear, month, date) {
const year = date.getFullYear(); let year = dateObjOrYear;
const month = String(date.getMonth() + 1).padStart(2, '0'); if (typeof dateObjOrYear === "object") {
const day = String(date.getDate()).padStart(2, '0'); 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) { function loading(show, text) {
@@ -249,17 +259,33 @@ function loading(show, text) {
} }
function showError(show, text) { function showError(show, text) {
if (!errorEl) { if (show) {
errorEl = new Kuc.Notification({ buildNotification('danger', text);
type: 'danger', 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 text
}); });
} else { } else {
errorEl.close(); notificationEl.close();
errorEl.text = text; notificationEl.type = type;
} notificationEl.text = text;
if (show) {
errorEl.open();
console.error(text);
} }
} }

View File

@@ -16,14 +16,16 @@ class BatchCreateHandler {
const api = new KintoneRestAPIClient(); const api = new KintoneRestAPIClient();
// 園児台帳アプリからデータを読み取る // 園児台帳アプリからデータを読み取る
const result = await this.getMasterRecords(api); const masterRecords = await this.getMasterRecords(api);
if (!result) { if (!masterRecords) {
// エラー
loading(false);
return; return;
} }
const today = new Date(); const today = new Date();
const todayString = getFormatDateString(today); const todayString = getFormatDateString(today);
const records = this.generateRecords(today, todayString, result); const records = this.generateRecords(today, todayString, masterRecords);
// console.log(records); // console.log(records);
const createResult = await this.createData(api, records, todayString); const createResult = await this.createData(api, records, todayString);
@@ -31,8 +33,8 @@ class BatchCreateHandler {
loading(false); loading(false);
} }
generateRecords = (today, todayString, result) => { generateRecords = (today, todayString, masterRecords) => {
return result.reduce((acc, masterRecord) => { return masterRecords.reduce((acc, masterRecord) => {
if (this.needCreateData(masterRecord, today)) { if (this.needCreateData(masterRecord, today)) {
acc.push(this.createRecord(masterRecord, todayString)); acc.push(this.createRecord(masterRecord, todayString));
} }
@@ -72,7 +74,6 @@ class BatchCreateHandler {
}); });
} catch (e) { } catch (e) {
showError(true, '園児台帳アプリのデータ読み取りエラー\n - ' + e); showError(true, '園児台帳アプリのデータ読み取りエラー\n - ' + e);
loading(false);
} }
} }

View File

@@ -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) { static getInstance(headerSpace) {
if (!Link2Handler.instance) { if (!Link2Handler.instance) {
Link2Handler.instance = new Link2Handler(headerSpace); Link2Handler.instance = new Link2Handler(headerSpace);

View File

@@ -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); event.record[FIELD_CODE]['value'] = getUniqueKey(event.record);
return event; return event;
}); });