2026-03-17 06:59:09 +00:00

673 lines
16 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 SiswaDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const siswa = await db.siswa.create(
{
id: data.id || undefined,
nisn: data.nisn
||
null
,
nis: data.nis
||
null
,
nama: data.nama
||
null
,
jenis_kelamin: data.jenis_kelamin
||
null
,
tempat_lahir: data.tempat_lahir
||
null
,
tanggal_lahir: data.tanggal_lahir
||
null
,
alamat: data.alamat
||
null
,
nama_ortu: data.nama_ortu
||
null
,
no_hp_ortu: data.no_hp_ortu
||
null
,
is_active: data.is_active
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await siswa.setUser( data.user || null, {
transaction,
});
await siswa.setKelas( data.kelas || null, {
transaction,
});
return siswa;
}
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 siswaData = data.map((item, index) => ({
id: item.id || undefined,
nisn: item.nisn
||
null
,
nis: item.nis
||
null
,
nama: item.nama
||
null
,
jenis_kelamin: item.jenis_kelamin
||
null
,
tempat_lahir: item.tempat_lahir
||
null
,
tanggal_lahir: item.tanggal_lahir
||
null
,
alamat: item.alamat
||
null
,
nama_ortu: item.nama_ortu
||
null
,
no_hp_ortu: item.no_hp_ortu
||
null
,
is_active: item.is_active
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const siswa = await db.siswa.bulkCreate(siswaData, { transaction });
// For each item created, replace relation files
return siswa;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const siswa = await db.siswa.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.nisn !== undefined) updatePayload.nisn = data.nisn;
if (data.nis !== undefined) updatePayload.nis = data.nis;
if (data.nama !== undefined) updatePayload.nama = data.nama;
if (data.jenis_kelamin !== undefined) updatePayload.jenis_kelamin = data.jenis_kelamin;
if (data.tempat_lahir !== undefined) updatePayload.tempat_lahir = data.tempat_lahir;
if (data.tanggal_lahir !== undefined) updatePayload.tanggal_lahir = data.tanggal_lahir;
if (data.alamat !== undefined) updatePayload.alamat = data.alamat;
if (data.nama_ortu !== undefined) updatePayload.nama_ortu = data.nama_ortu;
if (data.no_hp_ortu !== undefined) updatePayload.no_hp_ortu = data.no_hp_ortu;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
updatePayload.updatedById = currentUser.id;
await siswa.update(updatePayload, {transaction});
if (data.user !== undefined) {
await siswa.setUser(
data.user,
{ transaction }
);
}
if (data.kelas !== undefined) {
await siswa.setKelas(
data.kelas,
{ transaction }
);
}
return siswa;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const siswa = await db.siswa.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of siswa) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of siswa) {
await record.destroy({transaction});
}
});
return siswa;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const siswa = await db.siswa.findByPk(id, options);
await siswa.update({
deletedBy: currentUser.id
}, {
transaction,
});
await siswa.destroy({
transaction
});
return siswa;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const siswa = await db.siswa.findOne(
{ where },
{ transaction },
);
if (!siswa) {
return siswa;
}
const output = siswa.get({plain: true});
output.nilai_siswa = await siswa.getNilai_siswa({
transaction
});
output.sikap_siswa = await siswa.getSikap_siswa({
transaction
});
output.pengumpulan_tugas_siswa = await siswa.getPengumpulan_tugas_siswa({
transaction
});
output.absensi_siswa = await siswa.getAbsensi_siswa({
transaction
});
output.ai_insights_siswa = await siswa.getAi_insights_siswa({
transaction
});
output.raport_descriptions_siswa = await siswa.getRaport_descriptions_siswa({
transaction
});
output.user = await siswa.getUser({
transaction
});
output.kelas = await siswa.getKelas({
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.users,
as: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.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}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.nisn) {
where = {
...where,
[Op.and]: Utils.ilike(
'siswa',
'nisn',
filter.nisn,
),
};
}
if (filter.nis) {
where = {
...where,
[Op.and]: Utils.ilike(
'siswa',
'nis',
filter.nis,
),
};
}
if (filter.nama) {
where = {
...where,
[Op.and]: Utils.ilike(
'siswa',
'nama',
filter.nama,
),
};
}
if (filter.jenis_kelamin) {
where = {
...where,
[Op.and]: Utils.ilike(
'siswa',
'jenis_kelamin',
filter.jenis_kelamin,
),
};
}
if (filter.tempat_lahir) {
where = {
...where,
[Op.and]: Utils.ilike(
'siswa',
'tempat_lahir',
filter.tempat_lahir,
),
};
}
if (filter.alamat) {
where = {
...where,
[Op.and]: Utils.ilike(
'siswa',
'alamat',
filter.alamat,
),
};
}
if (filter.nama_ortu) {
where = {
...where,
[Op.and]: Utils.ilike(
'siswa',
'nama_ortu',
filter.nama_ortu,
),
};
}
if (filter.no_hp_ortu) {
where = {
...where,
[Op.and]: Utils.ilike(
'siswa',
'no_hp_ortu',
filter.no_hp_ortu,
),
};
}
if (filter.tanggal_lahirRange) {
const [start, end] = filter.tanggal_lahirRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
tanggal_lahir: {
...where.tanggal_lahir,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
tanggal_lahir: {
...where.tanggal_lahir,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.is_active) {
where = {
...where,
is_active: filter.is_active,
};
}
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.siswa.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(
'siswa',
'nama',
query,
),
],
};
}
const records = await db.siswa.findAll({
attributes: [ 'id', 'nama' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['nama', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.nama,
}));
}
};