38330-vm/backend/src/db/api/daily_wellbeing_entries.js
2026-02-09 20:55:09 +00:00

623 lines
16 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 Daily_wellbeing_entriesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const daily_wellbeing_entries = await db.daily_wellbeing_entries.create(
{
id: data.id || undefined,
entry_date: data.entry_date
||
null
,
energy_level: data.energy_level
||
null
,
stress_level: data.stress_level
||
null
,
stress_signals: data.stress_signals
||
null
,
joy_triggers: data.joy_triggers
||
null
,
rest_quality: data.rest_quality
||
null
,
rest_minutes: data.rest_minutes
||
null
,
behavior_notes: data.behavior_notes
||
null
,
gentle_observations: data.gentle_observations
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await daily_wellbeing_entries.setDog( data.dog || null, {
transaction,
});
await daily_wellbeing_entries.setLogged_by( data.logged_by || null, {
transaction,
});
return daily_wellbeing_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 daily_wellbeing_entriesData = data.map((item, index) => ({
id: item.id || undefined,
entry_date: item.entry_date
||
null
,
energy_level: item.energy_level
||
null
,
stress_level: item.stress_level
||
null
,
stress_signals: item.stress_signals
||
null
,
joy_triggers: item.joy_triggers
||
null
,
rest_quality: item.rest_quality
||
null
,
rest_minutes: item.rest_minutes
||
null
,
behavior_notes: item.behavior_notes
||
null
,
gentle_observations: item.gentle_observations
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const daily_wellbeing_entries = await db.daily_wellbeing_entries.bulkCreate(daily_wellbeing_entriesData, { transaction });
// For each item created, replace relation files
return daily_wellbeing_entries;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const daily_wellbeing_entries = await db.daily_wellbeing_entries.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.entry_date !== undefined) updatePayload.entry_date = data.entry_date;
if (data.energy_level !== undefined) updatePayload.energy_level = data.energy_level;
if (data.stress_level !== undefined) updatePayload.stress_level = data.stress_level;
if (data.stress_signals !== undefined) updatePayload.stress_signals = data.stress_signals;
if (data.joy_triggers !== undefined) updatePayload.joy_triggers = data.joy_triggers;
if (data.rest_quality !== undefined) updatePayload.rest_quality = data.rest_quality;
if (data.rest_minutes !== undefined) updatePayload.rest_minutes = data.rest_minutes;
if (data.behavior_notes !== undefined) updatePayload.behavior_notes = data.behavior_notes;
if (data.gentle_observations !== undefined) updatePayload.gentle_observations = data.gentle_observations;
updatePayload.updatedById = currentUser.id;
await daily_wellbeing_entries.update(updatePayload, {transaction});
if (data.dog !== undefined) {
await daily_wellbeing_entries.setDog(
data.dog,
{ transaction }
);
}
if (data.logged_by !== undefined) {
await daily_wellbeing_entries.setLogged_by(
data.logged_by,
{ transaction }
);
}
return daily_wellbeing_entries;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const daily_wellbeing_entries = await db.daily_wellbeing_entries.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of daily_wellbeing_entries) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of daily_wellbeing_entries) {
await record.destroy({transaction});
}
});
return daily_wellbeing_entries;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const daily_wellbeing_entries = await db.daily_wellbeing_entries.findByPk(id, options);
await daily_wellbeing_entries.update({
deletedBy: currentUser.id
}, {
transaction,
});
await daily_wellbeing_entries.destroy({
transaction
});
return daily_wellbeing_entries;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const daily_wellbeing_entries = await db.daily_wellbeing_entries.findOne(
{ where },
{ transaction },
);
if (!daily_wellbeing_entries) {
return daily_wellbeing_entries;
}
const output = daily_wellbeing_entries.get({plain: true});
output.dog = await daily_wellbeing_entries.getDog({
transaction
});
output.logged_by = await daily_wellbeing_entries.getLogged_by({
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.dogs,
as: 'dog',
where: filter.dog ? {
[Op.or]: [
{ id: { [Op.in]: filter.dog.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.dog.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'logged_by',
where: filter.logged_by ? {
[Op.or]: [
{ id: { [Op.in]: filter.logged_by.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.logged_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.stress_signals) {
where = {
...where,
[Op.and]: Utils.ilike(
'daily_wellbeing_entries',
'stress_signals',
filter.stress_signals,
),
};
}
if (filter.joy_triggers) {
where = {
...where,
[Op.and]: Utils.ilike(
'daily_wellbeing_entries',
'joy_triggers',
filter.joy_triggers,
),
};
}
if (filter.behavior_notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'daily_wellbeing_entries',
'behavior_notes',
filter.behavior_notes,
),
};
}
if (filter.gentle_observations) {
where = {
...where,
[Op.and]: Utils.ilike(
'daily_wellbeing_entries',
'gentle_observations',
filter.gentle_observations,
),
};
}
if (filter.entry_dateRange) {
const [start, end] = filter.entry_dateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
entry_date: {
...where.entry_date,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
entry_date: {
...where.entry_date,
[Op.lte]: end,
},
};
}
}
if (filter.rest_minutesRange) {
const [start, end] = filter.rest_minutesRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
rest_minutes: {
...where.rest_minutes,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
rest_minutes: {
...where.rest_minutes,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.energy_level) {
where = {
...where,
energy_level: filter.energy_level,
};
}
if (filter.stress_level) {
where = {
...where,
stress_level: filter.stress_level,
};
}
if (filter.rest_quality) {
where = {
...where,
rest_quality: filter.rest_quality,
};
}
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.daily_wellbeing_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(
'daily_wellbeing_entries',
'behavior_notes',
query,
),
],
};
}
const records = await db.daily_wellbeing_entries.findAll({
attributes: [ 'id', 'behavior_notes' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['behavior_notes', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.behavior_notes,
}));
}
};