38421-vm/backend/src/db/api/feedback_entries.js
2026-02-14 09:02:11 +00:00

595 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 Feedback_entriesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const feedback_entries = await db.feedback_entries.create(
{
id: data.id || undefined,
feedback_type: data.feedback_type
||
null
,
comment: data.comment
||
null
,
rating: data.rating
||
null
,
given_at: data.given_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await feedback_entries.setFrom_user( data.from_user || null, {
transaction,
});
await feedback_entries.setTo_user( data.to_user || null, {
transaction,
});
await feedback_entries.setMatch( data.match || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.feedback_entries.getTableName(),
belongsToColumn: 'attachments',
belongsToId: feedback_entries.id,
},
data.attachments,
options,
);
return feedback_entries;
}
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 feedback_entriesData = data.map((item, index) => ({
id: item.id || undefined,
feedback_type: item.feedback_type
||
null
,
comment: item.comment
||
null
,
rating: item.rating
||
null
,
given_at: item.given_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const feedback_entries = await db.feedback_entries.bulkCreate(feedback_entriesData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < feedback_entries.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.feedback_entries.getTableName(),
belongsToColumn: 'attachments',
belongsToId: feedback_entries[i].id,
},
data[i].attachments,
options,
);
}
return feedback_entries;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const feedback_entries = await db.feedback_entries.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.feedback_type !== undefined) updatePayload.feedback_type = data.feedback_type;
if (data.comment !== undefined) updatePayload.comment = data.comment;
if (data.rating !== undefined) updatePayload.rating = data.rating;
if (data.given_at !== undefined) updatePayload.given_at = data.given_at;
updatePayload.updatedById = currentUser.id;
await feedback_entries.update(updatePayload, {transaction});
if (data.from_user !== undefined) {
await feedback_entries.setFrom_user(
data.from_user,
{ transaction }
);
}
if (data.to_user !== undefined) {
await feedback_entries.setTo_user(
data.to_user,
{ transaction }
);
}
if (data.match !== undefined) {
await feedback_entries.setMatch(
data.match,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.feedback_entries.getTableName(),
belongsToColumn: 'attachments',
belongsToId: feedback_entries.id,
},
data.attachments,
options,
);
return feedback_entries;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const feedback_entries = await db.feedback_entries.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of feedback_entries) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of feedback_entries) {
await record.destroy({transaction});
}
});
return feedback_entries;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const feedback_entries = await db.feedback_entries.findByPk(id, options);
await feedback_entries.update({
deletedBy: currentUser.id
}, {
transaction,
});
await feedback_entries.destroy({
transaction
});
return feedback_entries;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const feedback_entries = await db.feedback_entries.findOne(
{ where },
{ transaction },
);
if (!feedback_entries) {
return feedback_entries;
}
const output = feedback_entries.get({plain: true});
output.from_user = await feedback_entries.getFrom_user({
transaction
});
output.to_user = await feedback_entries.getTo_user({
transaction
});
output.match = await feedback_entries.getMatch({
transaction
});
output.attachments = await feedback_entries.getAttachments({
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: 'from_user',
where: filter.from_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.from_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.from_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'to_user',
where: filter.to_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.to_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.to_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.matches,
as: 'match',
where: filter.match ? {
[Op.or]: [
{ id: { [Op.in]: filter.match.split('|').map(term => Utils.uuid(term)) } },
{
match_title: {
[Op.or]: filter.match.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'attachments',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.comment) {
where = {
...where,
[Op.and]: Utils.ilike(
'feedback_entries',
'comment',
filter.comment,
),
};
}
if (filter.ratingRange) {
const [start, end] = filter.ratingRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
rating: {
...where.rating,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
rating: {
...where.rating,
[Op.lte]: end,
},
};
}
}
if (filter.given_atRange) {
const [start, end] = filter.given_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
given_at: {
...where.given_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
given_at: {
...where.given_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.feedback_type) {
where = {
...where,
feedback_type: filter.feedback_type,
};
}
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.feedback_entries.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(
'feedback_entries',
'comment',
query,
),
],
};
}
const records = await db.feedback_entries.findAll({
attributes: [ 'id', 'comment' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['comment', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.comment,
}));
}
};