595 lines
15 KiB
JavaScript
595 lines
15 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 Survey_answersDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const survey_answers = await db.survey_answers.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
rating_value: data.rating_value
|
|
||
|
|
null
|
|
,
|
|
|
|
choice_value: data.choice_value
|
|
||
|
|
null
|
|
,
|
|
|
|
text_value: data.text_value
|
|
||
|
|
null
|
|
,
|
|
|
|
emoji_value: data.emoji_value
|
|
||
|
|
null
|
|
,
|
|
|
|
sentiment_score: data.sentiment_score
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await survey_answers.setResponse( data.response || null, {
|
|
transaction,
|
|
});
|
|
|
|
await survey_answers.setQuestion( data.question || null, {
|
|
transaction,
|
|
});
|
|
|
|
await survey_answers.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return survey_answers;
|
|
}
|
|
|
|
|
|
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 survey_answersData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
rating_value: item.rating_value
|
|
||
|
|
null
|
|
,
|
|
|
|
choice_value: item.choice_value
|
|
||
|
|
null
|
|
,
|
|
|
|
text_value: item.text_value
|
|
||
|
|
null
|
|
,
|
|
|
|
emoji_value: item.emoji_value
|
|
||
|
|
null
|
|
,
|
|
|
|
sentiment_score: item.sentiment_score
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const survey_answers = await db.survey_answers.bulkCreate(survey_answersData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return survey_answers;
|
|
}
|
|
|
|
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 survey_answers = await db.survey_answers.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.rating_value !== undefined) updatePayload.rating_value = data.rating_value;
|
|
|
|
|
|
if (data.choice_value !== undefined) updatePayload.choice_value = data.choice_value;
|
|
|
|
|
|
if (data.text_value !== undefined) updatePayload.text_value = data.text_value;
|
|
|
|
|
|
if (data.emoji_value !== undefined) updatePayload.emoji_value = data.emoji_value;
|
|
|
|
|
|
if (data.sentiment_score !== undefined) updatePayload.sentiment_score = data.sentiment_score;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await survey_answers.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.response !== undefined) {
|
|
await survey_answers.setResponse(
|
|
|
|
data.response,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.question !== undefined) {
|
|
await survey_answers.setQuestion(
|
|
|
|
data.question,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await survey_answers.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return survey_answers;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const survey_answers = await db.survey_answers.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of survey_answers) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of survey_answers) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return survey_answers;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const survey_answers = await db.survey_answers.findByPk(id, options);
|
|
|
|
await survey_answers.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await survey_answers.destroy({
|
|
transaction
|
|
});
|
|
|
|
return survey_answers;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const survey_answers = await db.survey_answers.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!survey_answers) {
|
|
return survey_answers;
|
|
}
|
|
|
|
const output = survey_answers.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.response = await survey_answers.getResponse({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.question = await survey_answers.getQuestion({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await survey_answers.getOrganizations({
|
|
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 userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.survey_responses,
|
|
as: 'response',
|
|
|
|
where: filter.response ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.response.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
respondent_hash: {
|
|
[Op.or]: filter.response.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.survey_questions,
|
|
as: 'question',
|
|
|
|
where: filter.question ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.question.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
prompt: {
|
|
[Op.or]: filter.question.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.choice_value) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'survey_answers',
|
|
'choice_value',
|
|
filter.choice_value,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.text_value) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'survey_answers',
|
|
'text_value',
|
|
filter.text_value,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.rating_valueRange) {
|
|
const [start, end] = filter.rating_valueRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
rating_value: {
|
|
...where.rating_value,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
rating_value: {
|
|
...where.rating_value,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.sentiment_scoreRange) {
|
|
const [start, end] = filter.sentiment_scoreRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
sentiment_score: {
|
|
...where.sentiment_score,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
sentiment_score: {
|
|
...where.sentiment_score,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.emoji_value) {
|
|
where = {
|
|
...where,
|
|
emoji_value: filter.emoji_value,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: {[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.organizationsId;
|
|
}
|
|
|
|
|
|
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.survey_answers.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(
|
|
'survey_answers',
|
|
'choice_value',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.survey_answers.findAll({
|
|
attributes: [ 'id', 'choice_value' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['choice_value', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.choice_value,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|