add grade grouping
This commit is contained in:
@@ -29,10 +29,14 @@ class Link2Handler {
|
||||
return;
|
||||
}
|
||||
|
||||
const record = this.generateRecord(dateString, currentAppRecords);
|
||||
// console.log(record);
|
||||
const groupedRecords = this.groupingAndSort(currentAppRecords);
|
||||
|
||||
const result = await this.insertOrUpdateData(api, record, dateString);
|
||||
const records = [];
|
||||
for (const item of groupedRecords) {
|
||||
records.push(this.generateRecord(dateString, item));
|
||||
}
|
||||
|
||||
const result = await this.insertOrUpdateData(api, records, dateString);
|
||||
if (result) {
|
||||
// showSuccess(true, "日誌データ連携作成完了");
|
||||
this.showSuccessDialog(result, year, month, date);
|
||||
@@ -41,15 +45,17 @@ class Link2Handler {
|
||||
}
|
||||
|
||||
showSuccessDialog(result, year, month, date) {
|
||||
const dateString = year + '年' + month + '月' + date + '日';
|
||||
const dateString = year + '年' + month + '月' + date + '日';
|
||||
const contentEl = document.createElement('div');
|
||||
contentEl.style.fontSize = '16px';
|
||||
if (!result.size) {
|
||||
contentEl.innerHTML = `${dateString}の園児別テーブルに追加するデータはありませんでした。`;
|
||||
} else if (result.type === 'update') {
|
||||
} else if (!result.insert) {
|
||||
contentEl.innerHTML = `${dateString}の園児別テーブルに${result.size}件のデータを再作成しました。`;
|
||||
} else {
|
||||
} else if (!result.update) {
|
||||
contentEl.innerHTML = `${dateString}の日誌は生成しましたが、園児別テーブルに${result.size}件のデータを追加しました。`;
|
||||
} else {
|
||||
contentEl.innerHTML = `${dateString}の${result.update}人の園児の日誌を更新し、${result.insert}人の園児の日誌を新規作成しました。`
|
||||
}
|
||||
showDialog({
|
||||
title: '日誌データ連携作成完了',
|
||||
@@ -57,7 +63,7 @@ class Link2Handler {
|
||||
ok: 'アプリへ行く',
|
||||
cancel: '閉じる',
|
||||
dataHolder: result,
|
||||
onOk: (dataHolder) => { window.open(`${location.origin}/k/${env["2歳以上日誌出力用"].appId}/show#record=` + dataHolder.id) },
|
||||
onOk: () => { window.open(`${location.origin}/k/${env["2歳以上日誌出力用"].appId}/`) },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -65,7 +71,7 @@ class Link2Handler {
|
||||
try {
|
||||
return await api.record.getAllRecordsWithId({
|
||||
app: env["園児別出欠簿入力"].appId,
|
||||
fields: ['クラス', "園児名", "園での様子_伝達事項", "評価反省"],
|
||||
fields: ['クラス', '学年', "園児名", "園での様子_伝達事項", "評価反省"],
|
||||
condition: `学年 not in ("", "0歳児", "1歳児") and 登園日 = "${dateString}"`
|
||||
});
|
||||
} catch (e) {
|
||||
@@ -74,11 +80,49 @@ class Link2Handler {
|
||||
}
|
||||
}
|
||||
|
||||
generateRecord = (todayString, currentAppRecords) => {
|
||||
groupingAndSort = (data) => {
|
||||
const groupedByGrade = {};
|
||||
// 1. group by 学年
|
||||
data.forEach((record) => {
|
||||
const grade = record['学年'].value;
|
||||
if (!groupedByGrade[grade]) {
|
||||
groupedByGrade[grade] = [];
|
||||
}
|
||||
groupedByGrade[grade].push(record);
|
||||
});
|
||||
|
||||
// 2. sort by クラス
|
||||
const classOrder = {};
|
||||
classItems.forEach((item, index) => {
|
||||
classOrder[item.value] = index;
|
||||
});
|
||||
for (const grade in groupedByGrade) {
|
||||
groupedByGrade[grade].sort((a, b) => {
|
||||
return classOrder[a["クラス"].value] - classOrder[b["クラス"].value];
|
||||
});
|
||||
}
|
||||
|
||||
// 3. return a list
|
||||
return termItems.reduce((acc, term) => {
|
||||
const grade = term.value;
|
||||
// Skip 0歳児 and 1歳児
|
||||
if (grade === "0歳児" || grade === "1歳児") {
|
||||
return acc;
|
||||
}
|
||||
acc.push({
|
||||
grade: grade,
|
||||
items: groupedByGrade[grade] || []
|
||||
});
|
||||
return acc;
|
||||
}, []);
|
||||
}
|
||||
|
||||
generateRecord = (todayString, groupedRecords) => {
|
||||
return {
|
||||
'登園日': { 'value': todayString },
|
||||
'学年': { 'value': groupedRecords.grade },
|
||||
'園児別テーブル': {
|
||||
'value': currentAppRecords.map((record) => {
|
||||
'value': groupedRecords.items.map((record) => {
|
||||
return {
|
||||
'value': {
|
||||
'クラス名': { 'value': record['クラス'].value },
|
||||
@@ -92,39 +136,66 @@ class Link2Handler {
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
insertOrUpdateData = async (api, records, dateString) => {
|
||||
const results = [];
|
||||
const generatedRecords = await api.record.getAllRecordsWithId({
|
||||
app: env["2歳以上日誌出力用"].appId,
|
||||
fields: ["$id", "学年"],
|
||||
condition: `登園日 = "${dateString}"`
|
||||
})
|
||||
const existIdMap = generatedRecords.reduce((map, item) => {
|
||||
const grade = item['学年'].value;
|
||||
const id = item['$id'].value;
|
||||
map[grade] = id;
|
||||
return map;
|
||||
}, {});
|
||||
|
||||
const param = {
|
||||
app: env["2歳以上日誌出力用"].appId,
|
||||
record
|
||||
}
|
||||
const result = {
|
||||
id: generatedRecordId,
|
||||
type: generatedRecordId ? 'update' : 'insert',
|
||||
size: record["園児別テーブル"].value.length,
|
||||
};
|
||||
for (const record of records) {
|
||||
try {
|
||||
const generatedRecordId = existIdMap[record["学年"].value];
|
||||
const param = {
|
||||
app: env["2歳以上日誌出力用"].appId,
|
||||
record
|
||||
}
|
||||
const result = {
|
||||
id: generatedRecordId,
|
||||
type: generatedRecordId ? 'update' : 'insert',
|
||||
size: record["園児別テーブル"].value.length,
|
||||
};
|
||||
|
||||
let awaitResult;
|
||||
if (result.type === 'update') {
|
||||
param.id = generatedRecordId;
|
||||
// 本アプリの当日データでテーブルを上書きし、対応する園児データがない行を【削除】します
|
||||
awaitResult = await api.record.updateRecord(param);
|
||||
} else {
|
||||
awaitResult = await api.record.addRecord(param);
|
||||
let awaitResult;
|
||||
|
||||
if (result.type === 'update') {
|
||||
param.id = generatedRecordId;
|
||||
// 本アプリの当日データでテーブルを上書きし、対応する園児データがない行を【削除】します
|
||||
awaitResult = await api.record.updateRecord(param);
|
||||
} else {
|
||||
awaitResult = await api.record.addRecord(param);
|
||||
}
|
||||
Object.assign(result, awaitResult);
|
||||
results.push(result);
|
||||
} catch (e) {
|
||||
showError(true, '日誌データ連携作成失敗\n - ' + e);
|
||||
}
|
||||
}
|
||||
|
||||
return results.reduce((result, item) => {
|
||||
// Sum all sizes
|
||||
result.size += item.size;
|
||||
|
||||
// Sum based on type
|
||||
if (item.type === 'insert') {
|
||||
result.insert += item.size;
|
||||
} else if (item.type === 'update') {
|
||||
result.update += item.size;
|
||||
}
|
||||
Object.assign(result, awaitResult);
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
showError(true, '日誌データ連携作成失敗\n - ' + e);
|
||||
}
|
||||
}, {
|
||||
size: 0, // Total sum of all sizes
|
||||
insert: 0, // Sum of insert sizes
|
||||
update: 0 // Sum of update sizes
|
||||
});;
|
||||
};
|
||||
|
||||
static getInstance(headerSpace) {
|
||||
|
||||
Reference in New Issue
Block a user