497 lines
12 KiB
JavaScript
497 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 Grading_keywordsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const grading_keywords = await db.grading_keywords.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
keyword: data.keyword
|
|
||
|
|
null
|
|
,
|
|
|
|
points_delta: data.points_delta
|
|
||
|
|
null
|
|
,
|
|
|
|
is_required: data.is_required
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await grading_keywords.setGrading_rule( data.grading_rule || null, {
|
|
transaction,
|
|
});
|
|
|
|
await grading_keywords.setSchools( data.schools || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return grading_keywords;
|
|
}
|
|
|
|
|
|
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 grading_keywordsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
keyword: item.keyword
|
|
||
|
|
null
|
|
,
|
|
|
|
points_delta: item.points_delta
|
|
||
|
|
null
|
|
,
|
|
|
|
is_required: item.is_required
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const grading_keywords = await db.grading_keywords.bulkCreate(grading_keywordsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return grading_keywords;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const grading_keywords = await db.grading_keywords.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.keyword !== undefined) updatePayload.keyword = data.keyword;
|
|
|
|
|
|
if (data.points_delta !== undefined) updatePayload.points_delta = data.points_delta;
|
|
|
|
|
|
if (data.is_required !== undefined) updatePayload.is_required = data.is_required;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await grading_keywords.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.grading_rule !== undefined) {
|
|
await grading_keywords.setGrading_rule(
|
|
|
|
data.grading_rule,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.schools !== undefined) {
|
|
await grading_keywords.setSchools(
|
|
|
|
data.schools,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return grading_keywords;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const grading_keywords = await db.grading_keywords.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of grading_keywords) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of grading_keywords) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return grading_keywords;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const grading_keywords = await db.grading_keywords.findByPk(id, options);
|
|
|
|
await grading_keywords.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await grading_keywords.destroy({
|
|
transaction
|
|
});
|
|
|
|
return grading_keywords;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const grading_keywords = await db.grading_keywords.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!grading_keywords) {
|
|
return grading_keywords;
|
|
}
|
|
|
|
const output = grading_keywords.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.grading_rule = await grading_keywords.getGrading_rule({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.schools = await grading_keywords.getSchools({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
globalAccess, options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userSchools = (user && user.schools?.id) || null;
|
|
|
|
|
|
|
|
if (userSchools) {
|
|
if (options?.currentUser?.schoolsId) {
|
|
where.schoolsId = options.currentUser.schoolsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.grading_rules,
|
|
as: 'grading_rule',
|
|
|
|
where: filter.grading_rule ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.grading_rule.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
rule_name: {
|
|
[Op.or]: filter.grading_rule.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.schools,
|
|
as: 'schools',
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.keyword) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'grading_keywords',
|
|
'keyword',
|
|
filter.keyword,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.points_deltaRange) {
|
|
const [start, end] = filter.points_deltaRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
points_delta: {
|
|
...where.points_delta,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
points_delta: {
|
|
...where.points_delta,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.is_required) {
|
|
where = {
|
|
...where,
|
|
is_required: filter.is_required,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.schools) {
|
|
const listItems = filter.schools.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
schoolsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (globalAccess) {
|
|
delete where.schoolsId;
|
|
}
|
|
|
|
|
|
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.grading_keywords.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, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'grading_keywords',
|
|
'keyword',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.grading_keywords.findAll({
|
|
attributes: [ 'id', 'keyword' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['keyword', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.keyword,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|