817 lines
21 KiB
JavaScript
817 lines
21 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 Massage_sessionsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const massage_sessions = await db.massage_sessions.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
session_name: data.session_name
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at: data.started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
ended_at: data.ended_at
|
|
||
|
|
null
|
|
,
|
|
|
|
state: data.state
|
|
||
|
|
null
|
|
,
|
|
|
|
stop_reason: data.stop_reason
|
|
||
|
|
null
|
|
,
|
|
|
|
planned_duration_seconds: data.planned_duration_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_duration_seconds: data.actual_duration_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
avg_left_intensity: data.avg_left_intensity
|
|
||
|
|
null
|
|
,
|
|
|
|
avg_right_intensity: data.avg_right_intensity
|
|
||
|
|
null
|
|
,
|
|
|
|
safety_cutoff_triggered: data.safety_cutoff_triggered
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await massage_sessions.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await massage_sessions.setProgram( data.program || null, {
|
|
transaction,
|
|
});
|
|
|
|
await massage_sessions.setDevice_profile( data.device_profile || null, {
|
|
transaction,
|
|
});
|
|
|
|
await massage_sessions.setGamepad( data.gamepad || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return massage_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 massage_sessionsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
session_name: item.session_name
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at: item.started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
ended_at: item.ended_at
|
|
||
|
|
null
|
|
,
|
|
|
|
state: item.state
|
|
||
|
|
null
|
|
,
|
|
|
|
stop_reason: item.stop_reason
|
|
||
|
|
null
|
|
,
|
|
|
|
planned_duration_seconds: item.planned_duration_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_duration_seconds: item.actual_duration_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
avg_left_intensity: item.avg_left_intensity
|
|
||
|
|
null
|
|
,
|
|
|
|
avg_right_intensity: item.avg_right_intensity
|
|
||
|
|
null
|
|
,
|
|
|
|
safety_cutoff_triggered: item.safety_cutoff_triggered
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const massage_sessions = await db.massage_sessions.bulkCreate(massage_sessionsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return massage_sessions;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const massage_sessions = await db.massage_sessions.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.session_name !== undefined) updatePayload.session_name = data.session_name;
|
|
|
|
|
|
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
|
|
|
|
|
|
if (data.ended_at !== undefined) updatePayload.ended_at = data.ended_at;
|
|
|
|
|
|
if (data.state !== undefined) updatePayload.state = data.state;
|
|
|
|
|
|
if (data.stop_reason !== undefined) updatePayload.stop_reason = data.stop_reason;
|
|
|
|
|
|
if (data.planned_duration_seconds !== undefined) updatePayload.planned_duration_seconds = data.planned_duration_seconds;
|
|
|
|
|
|
if (data.actual_duration_seconds !== undefined) updatePayload.actual_duration_seconds = data.actual_duration_seconds;
|
|
|
|
|
|
if (data.avg_left_intensity !== undefined) updatePayload.avg_left_intensity = data.avg_left_intensity;
|
|
|
|
|
|
if (data.avg_right_intensity !== undefined) updatePayload.avg_right_intensity = data.avg_right_intensity;
|
|
|
|
|
|
if (data.safety_cutoff_triggered !== undefined) updatePayload.safety_cutoff_triggered = data.safety_cutoff_triggered;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await massage_sessions.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.user !== undefined) {
|
|
await massage_sessions.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.program !== undefined) {
|
|
await massage_sessions.setProgram(
|
|
|
|
data.program,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.device_profile !== undefined) {
|
|
await massage_sessions.setDevice_profile(
|
|
|
|
data.device_profile,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.gamepad !== undefined) {
|
|
await massage_sessions.setGamepad(
|
|
|
|
data.gamepad,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return massage_sessions;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const massage_sessions = await db.massage_sessions.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of massage_sessions) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of massage_sessions) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return massage_sessions;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const massage_sessions = await db.massage_sessions.findByPk(id, options);
|
|
|
|
await massage_sessions.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await massage_sessions.destroy({
|
|
transaction
|
|
});
|
|
|
|
return massage_sessions;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const massage_sessions = await db.massage_sessions.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!massage_sessions) {
|
|
return massage_sessions;
|
|
}
|
|
|
|
const output = massage_sessions.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.safety_events_session = await massage_sessions.getSafety_events_session({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
output.user = await massage_sessions.getUser({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.program = await massage_sessions.getProgram({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.device_profile = await massage_sessions.getDevice_profile({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.gamepad = await massage_sessions.getGamepad({
|
|
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: 'user',
|
|
|
|
where: filter.user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.massage_programs,
|
|
as: 'program',
|
|
|
|
where: filter.program ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.program.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.program.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.device_profiles,
|
|
as: 'device_profile',
|
|
|
|
where: filter.device_profile ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.device_profile.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.device_profile.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.gamepads,
|
|
as: 'gamepad',
|
|
|
|
where: filter.gamepad ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.gamepad.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
display_name: {
|
|
[Op.or]: filter.gamepad.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.session_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'massage_sessions',
|
|
'session_name',
|
|
filter.session_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'massage_sessions',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
started_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
ended_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
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.planned_duration_secondsRange) {
|
|
const [start, end] = filter.planned_duration_secondsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
planned_duration_seconds: {
|
|
...where.planned_duration_seconds,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
planned_duration_seconds: {
|
|
...where.planned_duration_seconds,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.actual_duration_secondsRange) {
|
|
const [start, end] = filter.actual_duration_secondsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
actual_duration_seconds: {
|
|
...where.actual_duration_seconds,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
actual_duration_seconds: {
|
|
...where.actual_duration_seconds,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.avg_left_intensityRange) {
|
|
const [start, end] = filter.avg_left_intensityRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
avg_left_intensity: {
|
|
...where.avg_left_intensity,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
avg_left_intensity: {
|
|
...where.avg_left_intensity,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.avg_right_intensityRange) {
|
|
const [start, end] = filter.avg_right_intensityRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
avg_right_intensity: {
|
|
...where.avg_right_intensity,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
avg_right_intensity: {
|
|
...where.avg_right_intensity,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.state) {
|
|
where = {
|
|
...where,
|
|
state: filter.state,
|
|
};
|
|
}
|
|
|
|
if (filter.stop_reason) {
|
|
where = {
|
|
...where,
|
|
stop_reason: filter.stop_reason,
|
|
};
|
|
}
|
|
|
|
if (filter.safety_cutoff_triggered) {
|
|
where = {
|
|
...where,
|
|
safety_cutoff_triggered: filter.safety_cutoff_triggered,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.massage_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(
|
|
'massage_sessions',
|
|
'session_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.massage_sessions.findAll({
|
|
attributes: [ 'id', 'session_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['session_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.session_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|