763 lines
19 KiB
JavaScript
763 lines
19 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 ExercisesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const exercises = await db.exercises.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name
|
|
||
|
|
null
|
|
,
|
|
|
|
slug: data.slug
|
|
||
|
|
null
|
|
,
|
|
|
|
difficulty: data.difficulty
|
|
||
|
|
null
|
|
,
|
|
|
|
exercise_type: data.exercise_type
|
|
||
|
|
null
|
|
,
|
|
|
|
mechanics: data.mechanics
|
|
||
|
|
null
|
|
,
|
|
|
|
description: data.description
|
|
||
|
|
null
|
|
,
|
|
|
|
setup_instructions: data.setup_instructions
|
|
||
|
|
null
|
|
,
|
|
|
|
execution_cues: data.execution_cues
|
|
||
|
|
null
|
|
,
|
|
|
|
common_mistakes: data.common_mistakes
|
|
||
|
|
null
|
|
,
|
|
|
|
is_active: data.is_active
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
|
|
|
|
await exercises.setPrimary_muscle_groups(data.primary_muscle_groups || [], {
|
|
transaction,
|
|
});
|
|
|
|
await exercises.setSecondary_muscle_groups(data.secondary_muscle_groups || [], {
|
|
transaction,
|
|
});
|
|
|
|
await exercises.setEquipment(data.equipment || [], {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.exercises.getTableName(),
|
|
belongsToColumn: 'video',
|
|
belongsToId: exercises.id,
|
|
},
|
|
data.video,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.exercises.getTableName(),
|
|
belongsToColumn: 'images',
|
|
belongsToId: exercises.id,
|
|
},
|
|
data.images,
|
|
options,
|
|
);
|
|
|
|
|
|
return 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 exercisesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name
|
|
||
|
|
null
|
|
,
|
|
|
|
slug: item.slug
|
|
||
|
|
null
|
|
,
|
|
|
|
difficulty: item.difficulty
|
|
||
|
|
null
|
|
,
|
|
|
|
exercise_type: item.exercise_type
|
|
||
|
|
null
|
|
,
|
|
|
|
mechanics: item.mechanics
|
|
||
|
|
null
|
|
,
|
|
|
|
description: item.description
|
|
||
|
|
null
|
|
,
|
|
|
|
setup_instructions: item.setup_instructions
|
|
||
|
|
null
|
|
,
|
|
|
|
execution_cues: item.execution_cues
|
|
||
|
|
null
|
|
,
|
|
|
|
common_mistakes: item.common_mistakes
|
|
||
|
|
null
|
|
,
|
|
|
|
is_active: item.is_active
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const exercises = await db.exercises.bulkCreate(exercisesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < exercises.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.exercises.getTableName(),
|
|
belongsToColumn: 'video',
|
|
belongsToId: exercises[i].id,
|
|
},
|
|
data[i].video,
|
|
options,
|
|
);
|
|
}
|
|
|
|
for (let i = 0; i < exercises.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.exercises.getTableName(),
|
|
belongsToColumn: 'images',
|
|
belongsToId: exercises[i].id,
|
|
},
|
|
data[i].images,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return exercises;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const exercises = await db.exercises.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
|
|
if (data.slug !== undefined) updatePayload.slug = data.slug;
|
|
|
|
|
|
if (data.difficulty !== undefined) updatePayload.difficulty = data.difficulty;
|
|
|
|
|
|
if (data.exercise_type !== undefined) updatePayload.exercise_type = data.exercise_type;
|
|
|
|
|
|
if (data.mechanics !== undefined) updatePayload.mechanics = data.mechanics;
|
|
|
|
|
|
if (data.description !== undefined) updatePayload.description = data.description;
|
|
|
|
|
|
if (data.setup_instructions !== undefined) updatePayload.setup_instructions = data.setup_instructions;
|
|
|
|
|
|
if (data.execution_cues !== undefined) updatePayload.execution_cues = data.execution_cues;
|
|
|
|
|
|
if (data.common_mistakes !== undefined) updatePayload.common_mistakes = data.common_mistakes;
|
|
|
|
|
|
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await exercises.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (data.primary_muscle_groups !== undefined) {
|
|
await exercises.setPrimary_muscle_groups(data.primary_muscle_groups, { transaction });
|
|
}
|
|
|
|
if (data.secondary_muscle_groups !== undefined) {
|
|
await exercises.setSecondary_muscle_groups(data.secondary_muscle_groups, { transaction });
|
|
}
|
|
|
|
if (data.equipment !== undefined) {
|
|
await exercises.setEquipment(data.equipment, { transaction });
|
|
}
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.exercises.getTableName(),
|
|
belongsToColumn: 'video',
|
|
belongsToId: exercises.id,
|
|
},
|
|
data.video,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.exercises.getTableName(),
|
|
belongsToColumn: 'images',
|
|
belongsToId: exercises.id,
|
|
},
|
|
data.images,
|
|
options,
|
|
);
|
|
|
|
|
|
return exercises;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const exercises = await db.exercises.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of exercises) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of exercises) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return exercises;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const exercises = await db.exercises.findByPk(id, options);
|
|
|
|
await exercises.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await exercises.destroy({
|
|
transaction
|
|
});
|
|
|
|
return exercises;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const exercises = await db.exercises.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!exercises) {
|
|
return exercises;
|
|
}
|
|
|
|
const output = exercises.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.workout_exercises_exercise = await exercises.getWorkout_exercises_exercise({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
output.exercise_logs_exercise = await exercises.getExercise_logs_exercise({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
output.video = await exercises.getVideo({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.images = await exercises.getImages({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.primary_muscle_groups = await exercises.getPrimary_muscle_groups({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.secondary_muscle_groups = await exercises.getSecondary_muscle_groups({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.equipment = await exercises.getEquipment({
|
|
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.muscle_groups,
|
|
as: 'primary_muscle_groups',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.muscle_groups,
|
|
as: 'secondary_muscle_groups',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.equipment_items,
|
|
as: 'equipment',
|
|
required: false,
|
|
},
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'video',
|
|
},
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'images',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'exercises',
|
|
'name',
|
|
filter.name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.slug) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'exercises',
|
|
'slug',
|
|
filter.slug,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.description) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'exercises',
|
|
'description',
|
|
filter.description,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.setup_instructions) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'exercises',
|
|
'setup_instructions',
|
|
filter.setup_instructions,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.execution_cues) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'exercises',
|
|
'execution_cues',
|
|
filter.execution_cues,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.common_mistakes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'exercises',
|
|
'common_mistakes',
|
|
filter.common_mistakes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.difficulty) {
|
|
where = {
|
|
...where,
|
|
difficulty: filter.difficulty,
|
|
};
|
|
}
|
|
|
|
if (filter.exercise_type) {
|
|
where = {
|
|
...where,
|
|
exercise_type: filter.exercise_type,
|
|
};
|
|
}
|
|
|
|
if (filter.mechanics) {
|
|
where = {
|
|
...where,
|
|
mechanics: filter.mechanics,
|
|
};
|
|
}
|
|
|
|
if (filter.is_active) {
|
|
where = {
|
|
...where,
|
|
is_active: filter.is_active,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.primary_muscle_groups) {
|
|
const searchTerms = filter.primary_muscle_groups.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.muscle_groups,
|
|
as: 'primary_muscle_groups_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
if (filter.secondary_muscle_groups) {
|
|
const searchTerms = filter.secondary_muscle_groups.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.muscle_groups,
|
|
as: 'secondary_muscle_groups_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
if (filter.equipment) {
|
|
const searchTerms = filter.equipment.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.equipment_items,
|
|
as: 'equipment_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
|
|
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.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(
|
|
'exercises',
|
|
'name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.exercises.findAll({
|
|
attributes: [ 'id', 'name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|