666 lines
17 KiB
JavaScript
666 lines
17 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 Workout_exercisesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const workout_exercises = await db.workout_exercises.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
order_index: data.order_index
|
|
||
|
|
null
|
|
,
|
|
|
|
prescription_type: data.prescription_type
|
|
||
|
|
null
|
|
,
|
|
|
|
sets: data.sets
|
|
||
|
|
null
|
|
,
|
|
|
|
reps: data.reps
|
|
||
|
|
null
|
|
,
|
|
|
|
time_seconds: data.time_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
rest_seconds: data.rest_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
load_kg: data.load_kg
|
|
||
|
|
null
|
|
,
|
|
|
|
intensity: data.intensity
|
|
||
|
|
null
|
|
,
|
|
|
|
coaching_notes: data.coaching_notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await workout_exercises.setProgram_workout( data.program_workout || null, {
|
|
transaction,
|
|
});
|
|
|
|
await workout_exercises.setExercise( data.exercise || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return workout_exercises;
|
|
}
|
|
|
|
|
|
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 workout_exercisesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
order_index: item.order_index
|
|
||
|
|
null
|
|
,
|
|
|
|
prescription_type: item.prescription_type
|
|
||
|
|
null
|
|
,
|
|
|
|
sets: item.sets
|
|
||
|
|
null
|
|
,
|
|
|
|
reps: item.reps
|
|
||
|
|
null
|
|
,
|
|
|
|
time_seconds: item.time_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
rest_seconds: item.rest_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
load_kg: item.load_kg
|
|
||
|
|
null
|
|
,
|
|
|
|
intensity: item.intensity
|
|
||
|
|
null
|
|
,
|
|
|
|
coaching_notes: item.coaching_notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const workout_exercises = await db.workout_exercises.bulkCreate(workout_exercisesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return workout_exercises;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const workout_exercises = await db.workout_exercises.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.order_index !== undefined) updatePayload.order_index = data.order_index;
|
|
|
|
|
|
if (data.prescription_type !== undefined) updatePayload.prescription_type = data.prescription_type;
|
|
|
|
|
|
if (data.sets !== undefined) updatePayload.sets = data.sets;
|
|
|
|
|
|
if (data.reps !== undefined) updatePayload.reps = data.reps;
|
|
|
|
|
|
if (data.time_seconds !== undefined) updatePayload.time_seconds = data.time_seconds;
|
|
|
|
|
|
if (data.rest_seconds !== undefined) updatePayload.rest_seconds = data.rest_seconds;
|
|
|
|
|
|
if (data.load_kg !== undefined) updatePayload.load_kg = data.load_kg;
|
|
|
|
|
|
if (data.intensity !== undefined) updatePayload.intensity = data.intensity;
|
|
|
|
|
|
if (data.coaching_notes !== undefined) updatePayload.coaching_notes = data.coaching_notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await workout_exercises.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.program_workout !== undefined) {
|
|
await workout_exercises.setProgram_workout(
|
|
|
|
data.program_workout,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.exercise !== undefined) {
|
|
await workout_exercises.setExercise(
|
|
|
|
data.exercise,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return workout_exercises;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const workout_exercises = await db.workout_exercises.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of workout_exercises) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of workout_exercises) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return workout_exercises;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const workout_exercises = await db.workout_exercises.findByPk(id, options);
|
|
|
|
await workout_exercises.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await workout_exercises.destroy({
|
|
transaction
|
|
});
|
|
|
|
return workout_exercises;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const workout_exercises = await db.workout_exercises.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!workout_exercises) {
|
|
return workout_exercises;
|
|
}
|
|
|
|
const output = workout_exercises.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.program_workout = await workout_exercises.getProgram_workout({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.exercise = await workout_exercises.getExercise({
|
|
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.program_workouts,
|
|
as: 'program_workout',
|
|
|
|
where: filter.program_workout ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.program_workout.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.program_workout.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.exercises,
|
|
as: 'exercise',
|
|
|
|
where: filter.exercise ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.exercise.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.exercise.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.reps) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'workout_exercises',
|
|
'reps',
|
|
filter.reps,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.coaching_notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'workout_exercises',
|
|
'coaching_notes',
|
|
filter.coaching_notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.order_indexRange) {
|
|
const [start, end] = filter.order_indexRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
order_index: {
|
|
...where.order_index,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
order_index: {
|
|
...where.order_index,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.setsRange) {
|
|
const [start, end] = filter.setsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
sets: {
|
|
...where.sets,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
sets: {
|
|
...where.sets,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.time_secondsRange) {
|
|
const [start, end] = filter.time_secondsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
time_seconds: {
|
|
...where.time_seconds,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
time_seconds: {
|
|
...where.time_seconds,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.rest_secondsRange) {
|
|
const [start, end] = filter.rest_secondsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
rest_seconds: {
|
|
...where.rest_seconds,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
rest_seconds: {
|
|
...where.rest_seconds,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.load_kgRange) {
|
|
const [start, end] = filter.load_kgRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
load_kg: {
|
|
...where.load_kg,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
load_kg: {
|
|
...where.load_kg,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.prescription_type) {
|
|
where = {
|
|
...where,
|
|
prescription_type: filter.prescription_type,
|
|
};
|
|
}
|
|
|
|
if (filter.intensity) {
|
|
where = {
|
|
...where,
|
|
intensity: filter.intensity,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.workout_exercises.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(
|
|
'workout_exercises',
|
|
'reps',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.workout_exercises.findAll({
|
|
attributes: [ 'id', 'reps' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['reps', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.reps,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|