523 lines
12 KiB
JavaScript
523 lines
12 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 SikapDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const sikap = await db.sikap.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
semester: data.semester
|
|
||
|
|
null
|
|
,
|
|
|
|
tahun_ajaran: data.tahun_ajaran
|
|
||
|
|
null
|
|
,
|
|
|
|
predikat_sikap: data.predikat_sikap
|
|
||
|
|
null
|
|
,
|
|
|
|
catatan_wali_kelas: data.catatan_wali_kelas
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await sikap.setSiswa( data.siswa || null, {
|
|
transaction,
|
|
});
|
|
|
|
await sikap.setKelas( data.kelas || null, {
|
|
transaction,
|
|
});
|
|
|
|
await sikap.setWali_kelas( data.wali_kelas || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sikap;
|
|
}
|
|
|
|
|
|
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 sikapData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
semester: item.semester
|
|
||
|
|
null
|
|
,
|
|
|
|
tahun_ajaran: item.tahun_ajaran
|
|
||
|
|
null
|
|
,
|
|
|
|
predikat_sikap: item.predikat_sikap
|
|
||
|
|
null
|
|
,
|
|
|
|
catatan_wali_kelas: item.catatan_wali_kelas
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const sikap = await db.sikap.bulkCreate(sikapData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return sikap;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const sikap = await db.sikap.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.semester !== undefined) updatePayload.semester = data.semester;
|
|
|
|
|
|
if (data.tahun_ajaran !== undefined) updatePayload.tahun_ajaran = data.tahun_ajaran;
|
|
|
|
|
|
if (data.predikat_sikap !== undefined) updatePayload.predikat_sikap = data.predikat_sikap;
|
|
|
|
|
|
if (data.catatan_wali_kelas !== undefined) updatePayload.catatan_wali_kelas = data.catatan_wali_kelas;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await sikap.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.siswa !== undefined) {
|
|
await sikap.setSiswa(
|
|
|
|
data.siswa,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.kelas !== undefined) {
|
|
await sikap.setKelas(
|
|
|
|
data.kelas,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.wali_kelas !== undefined) {
|
|
await sikap.setWali_kelas(
|
|
|
|
data.wali_kelas,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sikap;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const sikap = await db.sikap.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of sikap) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of sikap) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return sikap;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const sikap = await db.sikap.findByPk(id, options);
|
|
|
|
await sikap.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await sikap.destroy({
|
|
transaction
|
|
});
|
|
|
|
return sikap;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const sikap = await db.sikap.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!sikap) {
|
|
return sikap;
|
|
}
|
|
|
|
const output = sikap.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.siswa = await sikap.getSiswa({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.kelas = await sikap.getKelas({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.wali_kelas = await sikap.getWali_kelas({
|
|
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.users,
|
|
as: 'wali_kelas',
|
|
|
|
where: filter.wali_kelas ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.wali_kelas.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.wali_kelas.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(
|
|
'sikap',
|
|
'tahun_ajaran',
|
|
filter.tahun_ajaran,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.catatan_wali_kelas) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'sikap',
|
|
'catatan_wali_kelas',
|
|
filter.catatan_wali_kelas,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.semester) {
|
|
where = {
|
|
...where,
|
|
semester: filter.semester,
|
|
};
|
|
}
|
|
|
|
if (filter.predikat_sikap) {
|
|
where = {
|
|
...where,
|
|
predikat_sikap: filter.predikat_sikap,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.sikap.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(
|
|
'sikap',
|
|
'catatan_wali_kelas',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.sikap.findAll({
|
|
attributes: [ 'id', 'catatan_wali_kelas' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['catatan_wali_kelas', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.catatan_wali_kelas,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|