756 lines
19 KiB
JavaScript
756 lines
19 KiB
JavaScript
|
|
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class Raport_descriptionsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const raport_descriptions = await db.raport_descriptions.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
tahun_ajaran: data.tahun_ajaran
|
|
||
|
|
null
|
|
,
|
|
|
|
semester: data.semester
|
|
||
|
|
null
|
|
,
|
|
|
|
nilai_akhir: data.nilai_akhir
|
|
||
|
|
null
|
|
,
|
|
|
|
predikat: data.predikat
|
|
||
|
|
null
|
|
,
|
|
|
|
deskripsi_kktp: data.deskripsi_kktp
|
|
||
|
|
null
|
|
,
|
|
|
|
ranking_kelas: data.ranking_kelas
|
|
||
|
|
null
|
|
,
|
|
|
|
rata_rata_kelas: data.rata_rata_kelas
|
|
||
|
|
null
|
|
,
|
|
|
|
ai_prompt: data.ai_prompt
|
|
||
|
|
null
|
|
,
|
|
|
|
ai_output: data.ai_output
|
|
||
|
|
null
|
|
,
|
|
|
|
generated_at: data.generated_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await raport_descriptions.setSiswa( data.siswa || null, {
|
|
transaction,
|
|
});
|
|
|
|
await raport_descriptions.setKelas( data.kelas || null, {
|
|
transaction,
|
|
});
|
|
|
|
await raport_descriptions.setMata_pelajaran( data.mata_pelajaran || null, {
|
|
transaction,
|
|
});
|
|
|
|
await raport_descriptions.setGenerated_by( data.generated_by || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return raport_descriptions;
|
|
}
|
|
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const raport_descriptionsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
tahun_ajaran: item.tahun_ajaran
|
|
||
|
|
null
|
|
,
|
|
|
|
semester: item.semester
|
|
||
|
|
null
|
|
,
|
|
|
|
nilai_akhir: item.nilai_akhir
|
|
||
|
|
null
|
|
,
|
|
|
|
predikat: item.predikat
|
|
||
|
|
null
|
|
,
|
|
|
|
deskripsi_kktp: item.deskripsi_kktp
|
|
||
|
|
null
|
|
,
|
|
|
|
ranking_kelas: item.ranking_kelas
|
|
||
|
|
null
|
|
,
|
|
|
|
rata_rata_kelas: item.rata_rata_kelas
|
|
||
|
|
null
|
|
,
|
|
|
|
ai_prompt: item.ai_prompt
|
|
||
|
|
null
|
|
,
|
|
|
|
ai_output: item.ai_output
|
|
||
|
|
null
|
|
,
|
|
|
|
generated_at: item.generated_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const raport_descriptions = await db.raport_descriptions.bulkCreate(raport_descriptionsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return raport_descriptions;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const raport_descriptions = await db.raport_descriptions.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.tahun_ajaran !== undefined) updatePayload.tahun_ajaran = data.tahun_ajaran;
|
|
|
|
|
|
if (data.semester !== undefined) updatePayload.semester = data.semester;
|
|
|
|
|
|
if (data.nilai_akhir !== undefined) updatePayload.nilai_akhir = data.nilai_akhir;
|
|
|
|
|
|
if (data.predikat !== undefined) updatePayload.predikat = data.predikat;
|
|
|
|
|
|
if (data.deskripsi_kktp !== undefined) updatePayload.deskripsi_kktp = data.deskripsi_kktp;
|
|
|
|
|
|
if (data.ranking_kelas !== undefined) updatePayload.ranking_kelas = data.ranking_kelas;
|
|
|
|
|
|
if (data.rata_rata_kelas !== undefined) updatePayload.rata_rata_kelas = data.rata_rata_kelas;
|
|
|
|
|
|
if (data.ai_prompt !== undefined) updatePayload.ai_prompt = data.ai_prompt;
|
|
|
|
|
|
if (data.ai_output !== undefined) updatePayload.ai_output = data.ai_output;
|
|
|
|
|
|
if (data.generated_at !== undefined) updatePayload.generated_at = data.generated_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await raport_descriptions.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.siswa !== undefined) {
|
|
await raport_descriptions.setSiswa(
|
|
|
|
data.siswa,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.kelas !== undefined) {
|
|
await raport_descriptions.setKelas(
|
|
|
|
data.kelas,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.mata_pelajaran !== undefined) {
|
|
await raport_descriptions.setMata_pelajaran(
|
|
|
|
data.mata_pelajaran,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.generated_by !== undefined) {
|
|
await raport_descriptions.setGenerated_by(
|
|
|
|
data.generated_by,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return raport_descriptions;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const raport_descriptions = await db.raport_descriptions.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of raport_descriptions) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of raport_descriptions) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return raport_descriptions;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const raport_descriptions = await db.raport_descriptions.findByPk(id, options);
|
|
|
|
await raport_descriptions.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await raport_descriptions.destroy({
|
|
transaction
|
|
});
|
|
|
|
return raport_descriptions;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const raport_descriptions = await db.raport_descriptions.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!raport_descriptions) {
|
|
return raport_descriptions;
|
|
}
|
|
|
|
const output = raport_descriptions.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.siswa = await raport_descriptions.getSiswa({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.kelas = await raport_descriptions.getKelas({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.mata_pelajaran = await raport_descriptions.getMata_pelajaran({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.generated_by = await raport_descriptions.getGenerated_by({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.siswa,
|
|
as: 'siswa',
|
|
|
|
where: filter.siswa ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.siswa.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
nama: {
|
|
[Op.or]: filter.siswa.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.kelas,
|
|
as: 'kelas',
|
|
|
|
where: filter.kelas ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.kelas.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
nama_kelas: {
|
|
[Op.or]: filter.kelas.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.mata_pelajaran,
|
|
as: 'mata_pelajaran',
|
|
|
|
where: filter.mata_pelajaran ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.mata_pelajaran.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
nama_mapel: {
|
|
[Op.or]: filter.mata_pelajaran.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'generated_by',
|
|
|
|
where: filter.generated_by ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.generated_by.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.generated_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.tahun_ajaran) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'raport_descriptions',
|
|
'tahun_ajaran',
|
|
filter.tahun_ajaran,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.deskripsi_kktp) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'raport_descriptions',
|
|
'deskripsi_kktp',
|
|
filter.deskripsi_kktp,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.ai_prompt) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'raport_descriptions',
|
|
'ai_prompt',
|
|
filter.ai_prompt,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.ai_output) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'raport_descriptions',
|
|
'ai_output',
|
|
filter.ai_output,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.nilai_akhirRange) {
|
|
const [start, end] = filter.nilai_akhirRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
nilai_akhir: {
|
|
...where.nilai_akhir,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
nilai_akhir: {
|
|
...where.nilai_akhir,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.ranking_kelasRange) {
|
|
const [start, end] = filter.ranking_kelasRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
ranking_kelas: {
|
|
...where.ranking_kelas,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
ranking_kelas: {
|
|
...where.ranking_kelas,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.rata_rata_kelasRange) {
|
|
const [start, end] = filter.rata_rata_kelasRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
rata_rata_kelas: {
|
|
...where.rata_rata_kelas,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
rata_rata_kelas: {
|
|
...where.rata_rata_kelas,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.generated_atRange) {
|
|
const [start, end] = filter.generated_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
generated_at: {
|
|
...where.generated_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
generated_at: {
|
|
...where.generated_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.semester) {
|
|
where = {
|
|
...where,
|
|
semester: filter.semester,
|
|
};
|
|
}
|
|
|
|
if (filter.predikat) {
|
|
where = {
|
|
...where,
|
|
predikat: filter.predikat,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order: filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.raport_descriptions.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'raport_descriptions',
|
|
'ai_output',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.raport_descriptions.findAll({
|
|
attributes: [ 'id', 'ai_output' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['ai_output', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.ai_output,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|