582 lines
14 KiB
JavaScript
582 lines
14 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 Subject_offeringsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const subject_offerings = await db.subject_offerings.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
learning_objectives_om: data.learning_objectives_om
|
|
||
|
|
null
|
|
,
|
|
|
|
learning_objectives_am: data.learning_objectives_am
|
|
||
|
|
null
|
|
,
|
|
|
|
learning_objectives_en: data.learning_objectives_en
|
|
||
|
|
null
|
|
,
|
|
|
|
topics_outline: data.topics_outline
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await subject_offerings.setSubject( data.subject || null, {
|
|
transaction,
|
|
});
|
|
|
|
await subject_offerings.setGrade( data.grade || null, {
|
|
transaction,
|
|
});
|
|
|
|
await subject_offerings.setStream( data.stream || null, {
|
|
transaction,
|
|
});
|
|
|
|
await subject_offerings.setResponsible_teacher( data.responsible_teacher || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return subject_offerings;
|
|
}
|
|
|
|
|
|
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 subject_offeringsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
learning_objectives_om: item.learning_objectives_om
|
|
||
|
|
null
|
|
,
|
|
|
|
learning_objectives_am: item.learning_objectives_am
|
|
||
|
|
null
|
|
,
|
|
|
|
learning_objectives_en: item.learning_objectives_en
|
|
||
|
|
null
|
|
,
|
|
|
|
topics_outline: item.topics_outline
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const subject_offerings = await db.subject_offerings.bulkCreate(subject_offeringsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return subject_offerings;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const subject_offerings = await db.subject_offerings.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.learning_objectives_om !== undefined) updatePayload.learning_objectives_om = data.learning_objectives_om;
|
|
|
|
|
|
if (data.learning_objectives_am !== undefined) updatePayload.learning_objectives_am = data.learning_objectives_am;
|
|
|
|
|
|
if (data.learning_objectives_en !== undefined) updatePayload.learning_objectives_en = data.learning_objectives_en;
|
|
|
|
|
|
if (data.topics_outline !== undefined) updatePayload.topics_outline = data.topics_outline;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await subject_offerings.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.subject !== undefined) {
|
|
await subject_offerings.setSubject(
|
|
|
|
data.subject,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.grade !== undefined) {
|
|
await subject_offerings.setGrade(
|
|
|
|
data.grade,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.stream !== undefined) {
|
|
await subject_offerings.setStream(
|
|
|
|
data.stream,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.responsible_teacher !== undefined) {
|
|
await subject_offerings.setResponsible_teacher(
|
|
|
|
data.responsible_teacher,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return subject_offerings;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const subject_offerings = await db.subject_offerings.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of subject_offerings) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of subject_offerings) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return subject_offerings;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const subject_offerings = await db.subject_offerings.findByPk(id, options);
|
|
|
|
await subject_offerings.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await subject_offerings.destroy({
|
|
transaction
|
|
});
|
|
|
|
return subject_offerings;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const subject_offerings = await db.subject_offerings.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!subject_offerings) {
|
|
return subject_offerings;
|
|
}
|
|
|
|
const output = subject_offerings.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.subject = await subject_offerings.getSubject({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.grade = await subject_offerings.getGrade({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.stream = await subject_offerings.getStream({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.responsible_teacher = await subject_offerings.getResponsible_teacher({
|
|
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.subjects,
|
|
as: 'subject',
|
|
|
|
where: filter.subject ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.subject.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name_en: {
|
|
[Op.or]: filter.subject.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.grades,
|
|
as: 'grade',
|
|
|
|
where: filter.grade ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.grade.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
label: {
|
|
[Op.or]: filter.grade.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.streams,
|
|
as: 'stream',
|
|
|
|
where: filter.stream ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.stream.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name_en: {
|
|
[Op.or]: filter.stream.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.staff_members,
|
|
as: 'responsible_teacher',
|
|
|
|
where: filter.responsible_teacher ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.responsible_teacher.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
full_name: {
|
|
[Op.or]: filter.responsible_teacher.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.learning_objectives_om) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'subject_offerings',
|
|
'learning_objectives_om',
|
|
filter.learning_objectives_om,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.learning_objectives_am) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'subject_offerings',
|
|
'learning_objectives_am',
|
|
filter.learning_objectives_am,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.learning_objectives_en) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'subject_offerings',
|
|
'learning_objectives_en',
|
|
filter.learning_objectives_en,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.topics_outline) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'subject_offerings',
|
|
'topics_outline',
|
|
filter.topics_outline,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.subject_offerings.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(
|
|
'subject_offerings',
|
|
'topics_outline',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.subject_offerings.findAll({
|
|
attributes: [ 'id', 'topics_outline' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['topics_outline', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.topics_outline,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|