39259-vm/backend/src/db/api/call_sessions.js
2026-03-21 16:03:41 +00:00

589 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 Call_sessionsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const call_sessions = await db.call_sessions.create(
{
id: data.id || undefined,
call_type: data.call_type
||
null
,
direction: data.direction
||
null
,
started_at: data.started_at
||
null
,
ended_at: data.ended_at
||
null
,
duration_seconds: data.duration_seconds
||
null
,
status: data.status
||
null
,
provider_reference: data.provider_reference
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await call_sessions.setConversation( data.conversation || null, {
transaction,
});
await call_sessions.setInitiator( data.initiator || null, {
transaction,
});
return call_sessions;
}
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 call_sessionsData = data.map((item, index) => ({
id: item.id || undefined,
call_type: item.call_type
||
null
,
direction: item.direction
||
null
,
started_at: item.started_at
||
null
,
ended_at: item.ended_at
||
null
,
duration_seconds: item.duration_seconds
||
null
,
status: item.status
||
null
,
provider_reference: item.provider_reference
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const call_sessions = await db.call_sessions.bulkCreate(call_sessionsData, { transaction });
// For each item created, replace relation files
return call_sessions;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const call_sessions = await db.call_sessions.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.call_type !== undefined) updatePayload.call_type = data.call_type;
if (data.direction !== undefined) updatePayload.direction = data.direction;
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
if (data.ended_at !== undefined) updatePayload.ended_at = data.ended_at;
if (data.duration_seconds !== undefined) updatePayload.duration_seconds = data.duration_seconds;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.provider_reference !== undefined) updatePayload.provider_reference = data.provider_reference;
updatePayload.updatedById = currentUser.id;
await call_sessions.update(updatePayload, {transaction});
if (data.conversation !== undefined) {
await call_sessions.setConversation(
data.conversation,
{ transaction }
);
}
if (data.initiator !== undefined) {
await call_sessions.setInitiator(
data.initiator,
{ transaction }
);
}
return call_sessions;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const call_sessions = await db.call_sessions.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of call_sessions) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of call_sessions) {
await record.destroy({transaction});
}
});
return call_sessions;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const call_sessions = await db.call_sessions.findByPk(id, options);
await call_sessions.update({
deletedBy: currentUser.id
}, {
transaction,
});
await call_sessions.destroy({
transaction
});
return call_sessions;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const call_sessions = await db.call_sessions.findOne(
{ where },
{ transaction },
);
if (!call_sessions) {
return call_sessions;
}
const output = call_sessions.get({plain: true});
output.conversation = await call_sessions.getConversation({
transaction
});
output.initiator = await call_sessions.getInitiator({
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.conversations,
as: 'conversation',
where: filter.conversation ? {
[Op.or]: [
{ id: { [Op.in]: filter.conversation.split('|').map(term => Utils.uuid(term)) } },
{
subject: {
[Op.or]: filter.conversation.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'initiator',
where: filter.initiator ? {
[Op.or]: [
{ id: { [Op.in]: filter.initiator.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.initiator.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.provider_reference) {
where = {
...where,
[Op.and]: Utils.ilike(
'call_sessions',
'provider_reference',
filter.provider_reference,
),
};
}
if (filter.started_atRange) {
const [start, end] = filter.started_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.lte]: end,
},
};
}
}
if (filter.ended_atRange) {
const [start, end] = filter.ended_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ended_at: {
...where.ended_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ended_at: {
...where.ended_at,
[Op.lte]: end,
},
};
}
}
if (filter.duration_secondsRange) {
const [start, end] = filter.duration_secondsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
duration_seconds: {
...where.duration_seconds,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
duration_seconds: {
...where.duration_seconds,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.call_type) {
where = {
...where,
call_type: filter.call_type,
};
}
if (filter.direction) {
where = {
...where,
direction: filter.direction,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.call_sessions.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(
'call_sessions',
'provider_reference',
query,
),
],
};
}
const records = await db.call_sessions.findAll({
attributes: [ 'id', 'provider_reference' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['provider_reference', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.provider_reference,
}));
}
};